Commit 8d60e099 authored by Arnaud FREISMUTH's avatar Arnaud FREISMUTH

Diamond kata TS

parent 180971b6
......@@ -38,3 +38,4 @@ include:
- local: "/java-puzzles/.gitlab-ci.yml"
- local: "/java-monades/.gitlab-ci.yml"
- local: "/java-diamond/.gitlab-ci.yml"
- local: "/diamond-ts/.gitlab-ci.yml"
module.exports = {
root: true,
env: {
node: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/typescript',
],
plugins: ['prettier', '@typescript-eslint'],
rules: {
quotes: ['error', 'single', { avoidEscape: true }],
'comma-dangle': [
'error',
{
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'never',
},
],
'import/order': [
'error',
{
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
'newlines-between': 'always',
},
],
'prettier/prettier': [
'error',
{
singleQuote: true,
trailingComma: 'es5',
printWidth: 140,
},
],
},
settings: {
'import/resolver': {
typescript: {
alwaysTryTypes: true,
},
},
'import/core-modules': ['sinon'],
},
parserOptions: {
parser: '@typescript-eslint/parser',
},
overrides: [
{
files: ['**/__tests__/*.{j,t}s?(x)', '**/*.spec.{j,t}s?(x)'],
env: {
jest: true,
},
},
{
files: ['**/*.ts', '**/*.tsx'],
rules: {
'no-unused-vars': ['off'],
'no-undef': ['off'],
},
},
],
};
diamond-ts:
variables:
PROJECT_FOLDER: "diamond-ts"
extends: .node14
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- ".gitlab-common-node-ci.yml"
- "diamond-ts/**/*"
# Diamond
Résolution en TDD (enfin on a au moins essayé ...) et en TypeScript du kata [Diamond](https://codingdojo.org/kata/Diamond/).
- **Auteurs** : Simon Gayet et Arnaud Freismuth
- **Date** : 17/05/2021
- **Langage** : TypeScript
- **Niveau** : Moyen
- **Replay** : [Twitch](https://www.twitch.tv/videos/1028951757)
module.exports = {
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testEnvironment: 'node',
};
This diff is collapsed.
{
"name": "diamond",
"version": "1.0.0",
"description": "Diamond kata",
"main": "src/index.ts",
"dependencies": {
"@types/jest": "^26.0.22",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"eslint": "^7.23.0",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"ts-jest": "^26.5.4",
"typescript": "^4.2.4"
},
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:watch:all": "jest --watchAll",
"eslint:ci": "eslint './**/*.{ts,js}'",
"eslint": "eslint './**/*.{ts,js}' --fix"
},
"author": "Arnaud Freismuth",
"license": "MIT"
}
const letters = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
];
const getDiamond = (letter: string) => {
if (letter === 'A') {
return getPyramid(letter);
}
const topPyramid = getPyramid(letter);
const bottomPyramid = topPyramid.slice(0, -1).reverse();
return [...topPyramid, ...bottomPyramid];
};
const getSpace = (numberOfSpace: number) => ' '.repeat(numberOfSpace);
const getLine = (index: number, letterIndex: number) => {
const currentLetter = letters[index];
const aroundSpace = letterIndex - index;
if (currentLetter === 'A') {
return getSpace(aroundSpace) + currentLetter + getSpace(aroundSpace);
}
const insideSpace = index * 2 - 1;
return getSpace(aroundSpace) + currentLetter + getSpace(insideSpace) + currentLetter + getSpace(aroundSpace);
};
const getPyramid = (givenLetter: string) => {
const letterIndex = letters.indexOf(givenLetter);
const pyramid = [];
for (let index = 0; index <= letterIndex; index++) {
const line = getLine(index, letterIndex);
pyramid.push(line);
}
return pyramid;
};
describe('Diamond', () => {
it('Should be A for A', () => {
expect(getDiamond('A')).toStrictEqual(['A']);
});
it('Should get B diamond', () => {
expect(getDiamond('B')).toStrictEqual([' A ', 'B B', ' A ']);
});
it('Should get C diamond', () => {
expect(getDiamond('C')).toStrictEqual([' A ', ' B B ', 'C C', ' B B ', ' A ']);
});
it('Should get A pyramid', () => {
expect(getPyramid('A')).toStrictEqual(['A']);
});
it('Should get C pyramid', () => {
expect(getPyramid('C')).toStrictEqual([' A ', ' B B ', 'C C']);
});
});
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "dist/main.js",
"sourceMap": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
......@@ -61,6 +61,7 @@ Un kata de code est un petit exercice pensé pour s'entrainer jusqu'à maitriser
- [Roman calculator](/roman-calculator)
- [H2G2](/h2g2)
- [Mastermind](/mastermind)
- [Diamond](/diamond-ts)
## Front
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment