Skip to content
Snippets Groups Projects
Commit 657b2f63 authored by Anthony REY's avatar Anthony REY
Browse files

Add pagination seven

parent fc1e5343
No related branches found
No related tags found
1 merge request!13Resolve "Pagination Seven"
...@@ -19,3 +19,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré ...@@ -19,3 +19,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
2. [RomanNumerals](/roman-numerals) 2. [RomanNumerals](/roman-numerals)
3. [StringCalculator](/string-calculator) 3. [StringCalculator](/string-calculator)
4. [C'est une bonne situation ça techlead ? (débat)](https://www.youtube.com/watch?v=9tOoXfOE12o) 4. [C'est une bonne situation ça techlead ? (débat)](https://www.youtube.com/watch?v=9tOoXfOE12o)
5. [Pagination Seven](/pagination-seven)
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'],
},
},
],
};
/.idea
# Pagination Seven
Résolution en TDD et avec explications du kata [Pagination Seven](https://gitlab.com/codingdojo-org/codingdojo.org/-/blob/master/content/kata/PaginationSeven.fr.md) ([english version](https://gitlab.com/codingdojo-org/codingdojo.org/-/blob/master/content/kata/PaginationSeven.md)).
- **Auteurs** : Julie CONTE et Anthony REY
- **Date** : 04/08/2020
- **Langage** : TypeScript
- **Niveau** : Moyen
- **Replay** : [Pagination Seven par Julie et Anthony](https://www.youtube.com/watch?v=JoPDq27d7QY)
module.exports = {
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
Source diff could not be displayed: it is too large. Options to address this: view the blob.
{
"name": "template-ts-kata",
"version": "1.0.0",
"description": "Template TS kata",
"main": "src/index.ts",
"dependencies": {
"@types/jest": "^26.0.8",
"@typescript-eslint/eslint-plugin": "^3.7.1",
"@typescript-eslint/parser": "^3.7.1",
"eslint": "^7.6.0",
"eslint-import-resolver-typescript": "^2.0.0",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-vue": "^6.1.2",
"jest": "^26.2.2",
"prettier": "^2.0.5",
"ts-jest": "^26.1.4",
"typescript": "^3.7.5"
},
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:watch:all": "jest --watchAll",
"eslint:ci": "eslint './**/*.{ts,js}'",
"eslint": "eslint './**/*.{ts,js}' --fix"
},
"author": "Anthony REY",
"license": "MIT"
}
const FIRST_THRESHOLD = 4;
const LAST_THRESHOLD = 3;
const FIRST_PAGE = 1;
const SEVEN = 7;
const ELLIPSIS = '';
const pageRepresentation = (currentPage: number) => (page: number): string => {
if (page === currentPage) {
return `(${page})`;
}
return page.toString();
};
const paginationBetween = (currentPage: number, from: number, to: number): string => {
const pages: number = to - from + 1;
return Array(pages)
.fill(0)
.map((_, index) => index + from)
.map(pageRepresentation(currentPage))
.join(' ');
};
const paginationUntilSeven = (currentPage: number, pages: number): string => paginationBetween(currentPage, FIRST_PAGE, pages);
const firstPartPagination = (currentPage: number, pages: number) =>
`${paginationBetween(currentPage, FIRST_PAGE, FIRST_THRESHOLD + 1)} ${ELLIPSIS} ${pages}`;
const lastPartPagination = (currentPage: number, pages: number) =>
`${FIRST_PAGE} ${ELLIPSIS} ${paginationBetween(currentPage, pages - LAST_THRESHOLD - 1, pages)}`;
const paginationOverSeven = (currentPage: number, pages: number): string => {
if (currentPage <= FIRST_THRESHOLD) {
return firstPartPagination(currentPage, pages);
}
if (currentPage >= pages - LAST_THRESHOLD) {
return lastPartPagination(currentPage, pages);
}
return `${FIRST_PAGE} ${ELLIPSIS} ${currentPage - 1} (${currentPage}) ${currentPage + 1} ${ELLIPSIS} ${pages}`;
};
const paginationSeven = (currentPage: number, pages: number): string => {
if (pages <= SEVEN) {
return paginationUntilSeven(currentPage, pages);
}
return paginationOverSeven(currentPage, pages);
};
describe('Pagination seven', () => {
it('Should show (1) when current page is 1 on 1 page', () => {
expect(paginationSeven(1, 1)).toBe('(1)');
});
it('Should show "1 2 (3) 4 5 6 7" when current page is 3 on 7 pages', () => {
expect(paginationSeven(3, 7)).toBe('1 2 (3) 4 5 6 7');
});
it('Should show "1 … 4 (5) 6 … 9" when current page is 5 on 9 pages', () => {
expect(paginationSeven(5, 9)).toBe('1 … 4 (5) 6 … 9');
});
it('Should show "1 2 3 (4) 5 … 9" when current page is 4 on 9 pages', () => {
expect(paginationSeven(4, 9)).toBe('1 2 3 (4) 5 … 9');
});
it('Should show "1 … 5 (6) 7 8 9" when current page is 6 on 9 pages', () => {
expect(paginationSeven(6, 9)).toBe('1 … 5 (6) 7 8 9');
});
it('Should show "1 … 41 (42) 43 … 100" when current page is 42 on 100 pages', () => {
expect(paginationSeven(42, 100)).toBe('1 … 41 (42) 43 … 100');
});
});
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "dist/main.js",
"sourceMap": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment