@ssa-ui-kit/utils 0.0.1-alpha → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/LICENSE +21 -21
  2. package/dist/index.js.map +1 -1
  3. package/dist/utils/objects/path.d.ts +1 -1
  4. package/dist/utils/objects/pathOr.d.ts +1 -1
  5. package/package.json +1 -1
  6. package/src/index.ts +8 -8
  7. package/src/utils/CallAll.ts +5 -5
  8. package/src/utils/dates/dateFormatters.ts +61 -61
  9. package/src/utils/dates/dates.spec.tsx +127 -127
  10. package/src/utils/dates/index.ts +1 -1
  11. package/src/utils/debounce/debounce.spec.ts +85 -85
  12. package/src/utils/debounce/debounce.ts +24 -24
  13. package/src/utils/debounce/index.ts +1 -1
  14. package/src/utils/objects/assocPath.spec.tsx +53 -53
  15. package/src/utils/objects/assocPath.ts +11 -11
  16. package/src/utils/objects/dissocPath.spec.tsx +49 -49
  17. package/src/utils/objects/dissocPath.ts +14 -14
  18. package/src/utils/objects/index.ts +7 -7
  19. package/src/utils/objects/mapObjIndexed.spec.tsx +33 -33
  20. package/src/utils/objects/mapObjIndexed.ts +15 -15
  21. package/src/utils/objects/path.spec.tsx +24 -24
  22. package/src/utils/objects/path.ts +4 -4
  23. package/src/utils/objects/pathOr.spec.tsx +23 -23
  24. package/src/utils/objects/pathOr.ts +11 -11
  25. package/src/utils/objects/prop.spec.tsx +17 -17
  26. package/src/utils/objects/prop.ts +4 -4
  27. package/src/utils/objects/propOr.spec.tsx +17 -17
  28. package/src/utils/objects/propOr.ts +11 -11
  29. package/src/utils/pagination/generateRange.spec.ts +100 -100
  30. package/src/utils/pagination/generateRange.ts +91 -91
  31. package/src/utils/pagination/index.ts +1 -1
  32. package/src/utils/pagination/types.ts +4 -4
  33. package/src/utils/throttle/index.ts +1 -1
  34. package/src/utils/throttle/throttle.spec.ts +56 -56
  35. package/src/utils/throttle/throttle.ts +43 -43
  36. package/src/utils/types.ts +4 -4
  37. package/tsbuildcache +1 -1
  38. package/tsconfig.build.json +35 -35
  39. package/tsconfig.json +17 -17
  40. package/webpack.config.js +16 -16
@@ -1,23 +1,23 @@
1
- import { pathOr } from '.';
2
-
3
- describe('utils => objects => pathOr', () => {
4
- const data = {
5
- a: {
6
- name: 'a',
7
- value: 1,
8
- },
9
- b: {
10
- name: 'b',
11
- value: 2,
12
- },
13
- };
14
-
15
- it('should work with correct path', () => {
16
- const result = pathOr('Default value', ['a', 'name'])(data);
17
- expect(result).toEqual('a');
18
- });
19
- it('should work with incorrect path', () => {
20
- const result = pathOr('Default value', ['a', 'name', 'value'])(data);
21
- expect(result).toEqual('Default value');
22
- });
23
- });
1
+ import { pathOr } from '.';
2
+
3
+ describe('utils => objects => pathOr', () => {
4
+ const data = {
5
+ a: {
6
+ name: 'a',
7
+ value: 1,
8
+ },
9
+ b: {
10
+ name: 'b',
11
+ value: 2,
12
+ },
13
+ };
14
+
15
+ it('should work with correct path', () => {
16
+ const result = pathOr('Default value', ['a', 'name'])(data);
17
+ expect(result).toEqual('a');
18
+ });
19
+ it('should work with incorrect path', () => {
20
+ const result = pathOr('Default value', ['a', 'name', 'value'])(data);
21
+ expect(result).toEqual('Default value');
22
+ });
23
+ });
@@ -1,11 +1,11 @@
1
- import { path as originalPath } from './path';
2
-
3
- export const pathOr =
4
- <T extends Record<string | number, any>, R>(
5
- defaultValue: any,
6
- path: string[],
7
- ) =>
8
- (obj: T): R => {
9
- const result = originalPath(path)(obj);
10
- return result === null || result === undefined ? defaultValue : result;
11
- };
1
+ import { path as originalPath } from './path';
2
+
3
+ export const pathOr =
4
+ <T extends Record<string | number, any>, R>(
5
+ defaultValue: any,
6
+ path: Array<string | number>,
7
+ ) =>
8
+ (obj: T): R => {
9
+ const result = originalPath(path)(obj);
10
+ return result === null || result === undefined ? defaultValue : result;
11
+ };
@@ -1,17 +1,17 @@
1
- import { prop } from '.';
2
-
3
- describe('utils => objects => prop', () => {
4
- const data = {
5
- name: 'a',
6
- value: 1,
7
- };
8
-
9
- it('should work with correct prop', () => {
10
- const result = prop('name')(data);
11
- expect(result).toEqual('a');
12
- });
13
- it('should work with incorrect prop', () => {
14
- const result = prop('incorrect_prop')(data);
15
- expect(result).toEqual(undefined);
16
- });
17
- });
1
+ import { prop } from '.';
2
+
3
+ describe('utils => objects => prop', () => {
4
+ const data = {
5
+ name: 'a',
6
+ value: 1,
7
+ };
8
+
9
+ it('should work with correct prop', () => {
10
+ const result = prop('name')(data);
11
+ expect(result).toEqual('a');
12
+ });
13
+ it('should work with incorrect prop', () => {
14
+ const result = prop('incorrect_prop')(data);
15
+ expect(result).toEqual(undefined);
16
+ });
17
+ });
@@ -1,4 +1,4 @@
1
- export const prop =
2
- <T extends Record<string | number, any>, R = any>(propName: string) =>
3
- (obj: T): R =>
4
- obj?.[propName];
1
+ export const prop =
2
+ <T extends Record<string | number, any>, R = any>(propName: string) =>
3
+ (obj: T): R =>
4
+ obj?.[propName];
@@ -1,17 +1,17 @@
1
- import { propOr } from '.';
2
-
3
- describe('utils => objects => propOr', () => {
4
- const data = {
5
- name: 'a',
6
- value: 1,
7
- };
8
-
9
- it('should work with correct prop', () => {
10
- const result = propOr('Default value', 'name')(data);
11
- expect(result).toEqual('a');
12
- });
13
- it('should work with incorrect prop', () => {
14
- const result = propOr('Default value', 'incorrect_prop')(data);
15
- expect(result).toEqual('Default value');
16
- });
17
- });
1
+ import { propOr } from '.';
2
+
3
+ describe('utils => objects => propOr', () => {
4
+ const data = {
5
+ name: 'a',
6
+ value: 1,
7
+ };
8
+
9
+ it('should work with correct prop', () => {
10
+ const result = propOr('Default value', 'name')(data);
11
+ expect(result).toEqual('a');
12
+ });
13
+ it('should work with incorrect prop', () => {
14
+ const result = propOr('Default value', 'incorrect_prop')(data);
15
+ expect(result).toEqual('Default value');
16
+ });
17
+ });
@@ -1,11 +1,11 @@
1
- import { prop } from './prop';
2
-
3
- export const propOr =
4
- <T extends Record<string | number, any>, R = any>(
5
- defaultValue: any,
6
- propName: string,
7
- ) =>
8
- (obj: T): R => {
9
- const result = prop(propName)(obj);
10
- return result === null || result === undefined ? defaultValue : result;
11
- };
1
+ import { prop } from './prop';
2
+
3
+ export const propOr =
4
+ <T extends Record<string | number, any>, R = any>(
5
+ defaultValue: any,
6
+ propName: string,
7
+ ) =>
8
+ (obj: T): R => {
9
+ const result = prop(propName)(obj);
10
+ return result === null || result === undefined ? defaultValue : result;
11
+ };
@@ -1,100 +1,100 @@
1
- import generateRange from './generateRange';
2
-
3
- describe('generateRange', () => {
4
- it('returns an empty array when 0 or negative pages count passed', () => {
5
- let range = generateRange(0);
6
- expect(range).toEqual([]);
7
-
8
- range = generateRange(-1);
9
- expect(range).toEqual([]);
10
- });
11
- describe('No page selected', () => {
12
- const noSelectedItemTestCases = [
13
- { pages: 1, expected: [1] },
14
- { pages: 2, expected: [1, 2] },
15
- { pages: 3, expected: [1, 2, 3] },
16
- { pages: 4, expected: [1, 2, 3, 4] },
17
- { pages: 5, expected: [1, 2, 3, 4, 5] },
18
- { pages: 6, expected: [1, 2, 3, -1, 6] },
19
- { pages: 7, expected: [1, 2, 3, -1, 7] },
20
- { pages: 8, expected: [1, 2, 3, -1, 8] },
21
- { pages: 9, expected: [1, 2, 3, -1, 9] },
22
- { pages: 10, expected: [1, 2, 3, -1, 10] },
23
- ];
24
-
25
- it.each(noSelectedItemTestCases)(
26
- 'returns the range for $pages pages',
27
- ({ pages, expected }) => {
28
- const range = generateRange(pages);
29
- expect(range).toEqual(expected);
30
- },
31
- );
32
- });
33
-
34
- describe('With a selected page', () => {
35
- const selectedItemTestCases = [
36
- { pages: 10, selected: 1, expected: [1, 2, 3, -1, 10] },
37
- { pages: 10, selected: 2, expected: [1, 2, 3, -1, 10] },
38
- { pages: 10, selected: 3, expected: [1, 2, 3, 4, -1, 10] },
39
- { pages: 10, selected: 4, expected: [1, 2, 3, 4, 5, -1, 10] },
40
- { pages: 10, selected: 5, expected: [1, -1, 4, 5, 6, -1, 10] },
41
- { pages: 10, selected: 6, expected: [1, -1, 5, 6, 7, -1, 10] },
42
- { pages: 10, selected: 7, expected: [1, -1, 6, 7, 8, 9, 10] },
43
- { pages: 10, selected: 8, expected: [1, -1, 7, 8, 9, 10] },
44
- { pages: 10, selected: 9, expected: [1, -1, 8, 9, 10] },
45
- { pages: 10, selected: 10, expected: [1, -1, 9, 10] },
46
- ];
47
-
48
- it.each(selectedItemTestCases)(
49
- 'returns the range for $pages pages when selected item $selected',
50
- ({ pages, selected, expected }) => {
51
- const range = generateRange(pages, selected);
52
- expect(range).toEqual(expected);
53
- },
54
- );
55
- });
56
-
57
- it('throws an error if the selected page is out of range', () => {
58
- expect(() => generateRange(10, 0)).toThrow(
59
- new Error('Selected page 0 is out of range'),
60
- );
61
-
62
- expect(() => generateRange(10, 11)).toThrow(
63
- new Error('Selected page 11 is out of range'),
64
- );
65
- });
66
-
67
- it('throws an error when the 1st argument is not an integer', () => {
68
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
69
- // @ts-ignore
70
- expect(() => generateRange('1')).toThrow(
71
- new Error('Pages count should be an integer'),
72
- );
73
-
74
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
75
- // @ts-ignore
76
- expect(() => generateRange(null)).toThrow(
77
- new Error('Pages count should be an integer'),
78
- );
79
-
80
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
81
- // @ts-ignore
82
- expect(() => generateRange(0.1)).toThrow(
83
- new Error('Pages count should be an integer'),
84
- );
85
- });
86
-
87
- it('throws an error when the 2nd argument is not an integer', () => {
88
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
89
- // @ts-ignore
90
- expect(() => generateRange(10, '1')).toThrow(
91
- new Error('Selected page should be an integer'),
92
- );
93
-
94
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
95
- // @ts-ignore
96
- expect(() => generateRange(10, 0.1)).toThrow(
97
- new Error('Selected page should be an integer'),
98
- );
99
- });
100
- });
1
+ import generateRange from './generateRange';
2
+
3
+ describe('generateRange', () => {
4
+ it('returns an empty array when 0 or negative pages count passed', () => {
5
+ let range = generateRange(0);
6
+ expect(range).toEqual([]);
7
+
8
+ range = generateRange(-1);
9
+ expect(range).toEqual([]);
10
+ });
11
+ describe('No page selected', () => {
12
+ const noSelectedItemTestCases = [
13
+ { pages: 1, expected: [1] },
14
+ { pages: 2, expected: [1, 2] },
15
+ { pages: 3, expected: [1, 2, 3] },
16
+ { pages: 4, expected: [1, 2, 3, 4] },
17
+ { pages: 5, expected: [1, 2, 3, 4, 5] },
18
+ { pages: 6, expected: [1, 2, 3, -1, 6] },
19
+ { pages: 7, expected: [1, 2, 3, -1, 7] },
20
+ { pages: 8, expected: [1, 2, 3, -1, 8] },
21
+ { pages: 9, expected: [1, 2, 3, -1, 9] },
22
+ { pages: 10, expected: [1, 2, 3, -1, 10] },
23
+ ];
24
+
25
+ it.each(noSelectedItemTestCases)(
26
+ 'returns the range for $pages pages',
27
+ ({ pages, expected }) => {
28
+ const range = generateRange(pages);
29
+ expect(range).toEqual(expected);
30
+ },
31
+ );
32
+ });
33
+
34
+ describe('With a selected page', () => {
35
+ const selectedItemTestCases = [
36
+ { pages: 10, selected: 1, expected: [1, 2, 3, -1, 10] },
37
+ { pages: 10, selected: 2, expected: [1, 2, 3, -1, 10] },
38
+ { pages: 10, selected: 3, expected: [1, 2, 3, 4, -1, 10] },
39
+ { pages: 10, selected: 4, expected: [1, 2, 3, 4, 5, -1, 10] },
40
+ { pages: 10, selected: 5, expected: [1, -1, 4, 5, 6, -1, 10] },
41
+ { pages: 10, selected: 6, expected: [1, -1, 5, 6, 7, -1, 10] },
42
+ { pages: 10, selected: 7, expected: [1, -1, 6, 7, 8, 9, 10] },
43
+ { pages: 10, selected: 8, expected: [1, -1, 7, 8, 9, 10] },
44
+ { pages: 10, selected: 9, expected: [1, -1, 8, 9, 10] },
45
+ { pages: 10, selected: 10, expected: [1, -1, 9, 10] },
46
+ ];
47
+
48
+ it.each(selectedItemTestCases)(
49
+ 'returns the range for $pages pages when selected item $selected',
50
+ ({ pages, selected, expected }) => {
51
+ const range = generateRange(pages, selected);
52
+ expect(range).toEqual(expected);
53
+ },
54
+ );
55
+ });
56
+
57
+ it('throws an error if the selected page is out of range', () => {
58
+ expect(() => generateRange(10, 0)).toThrow(
59
+ new Error('Selected page 0 is out of range'),
60
+ );
61
+
62
+ expect(() => generateRange(10, 11)).toThrow(
63
+ new Error('Selected page 11 is out of range'),
64
+ );
65
+ });
66
+
67
+ it('throws an error when the 1st argument is not an integer', () => {
68
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
69
+ // @ts-ignore
70
+ expect(() => generateRange('1')).toThrow(
71
+ new Error('Pages count should be an integer'),
72
+ );
73
+
74
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
75
+ // @ts-ignore
76
+ expect(() => generateRange(null)).toThrow(
77
+ new Error('Pages count should be an integer'),
78
+ );
79
+
80
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
81
+ // @ts-ignore
82
+ expect(() => generateRange(0.1)).toThrow(
83
+ new Error('Pages count should be an integer'),
84
+ );
85
+ });
86
+
87
+ it('throws an error when the 2nd argument is not an integer', () => {
88
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
89
+ // @ts-ignore
90
+ expect(() => generateRange(10, '1')).toThrow(
91
+ new Error('Selected page should be an integer'),
92
+ );
93
+
94
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
95
+ // @ts-ignore
96
+ expect(() => generateRange(10, 0.1)).toThrow(
97
+ new Error('Selected page should be an integer'),
98
+ );
99
+ });
100
+ });
@@ -1,91 +1,91 @@
1
- import { GenerateRangeFn } from './types';
2
-
3
- /**
4
- * The function that returns an array of page numbers to show in the pagination
5
- * component.
6
- *
7
- * Rules:
8
- * - To always show the 1st and the last page.
9
- * - To show one item before and one item after the selected page.
10
- * - To return "-1" for the skipped items. This is to be able to display "..." in
11
- * the pagination component.
12
- * */
13
- const SKIPPED_ITEMS_DELTA = 2;
14
-
15
- const getSelectedRange = (pagesCount: number, selectedPage: number) => {
16
- const range: number[] = [];
17
-
18
- if (selectedPage !== pagesCount) {
19
- range.push(selectedPage);
20
- }
21
-
22
- if (selectedPage > 1) {
23
- range.unshift(selectedPage - 1);
24
- }
25
-
26
- if (selectedPage + 1 < pagesCount) {
27
- range.push(selectedPage + 1);
28
- }
29
-
30
- return range;
31
- };
32
-
33
- const fill = (range: number[], minValue: number, maxValue: number) => {
34
- for (let i = minValue; i < maxValue; ++i) {
35
- range.push(i);
36
- }
37
- };
38
-
39
- const generateRange: GenerateRangeFn = (pagesCount, selectedPage) => {
40
- if (pagesCount == null || !Number.isInteger(pagesCount)) {
41
- throw new Error('Pages count should be an integer');
42
- }
43
-
44
- if (pagesCount <= 0) {
45
- return [];
46
- }
47
-
48
- let range = [1];
49
-
50
- if (pagesCount === 1) {
51
- return range;
52
- }
53
-
54
- if (selectedPage != null && !Number.isInteger(selectedPage)) {
55
- throw new Error('Selected page should be an integer');
56
- }
57
-
58
- if (selectedPage != null && (selectedPage < 1 || selectedPage > pagesCount)) {
59
- throw new Error(`Selected page ${selectedPage} is out of range`);
60
- }
61
-
62
- if (selectedPage && selectedPage > 2) {
63
- const selectedRange = getSelectedRange(pagesCount, selectedPage);
64
-
65
- const [minSelectedRange, , maxSelectedRange] = selectedRange;
66
-
67
- if (minSelectedRange - SKIPPED_ITEMS_DELTA > 1) {
68
- range.push(-1);
69
- } else {
70
- fill(range, 2, minSelectedRange);
71
- }
72
-
73
- range = range.concat(selectedRange);
74
-
75
- if (pagesCount - maxSelectedRange > SKIPPED_ITEMS_DELTA) {
76
- range.push(-1);
77
- } else {
78
- fill(range, maxSelectedRange + 1, pagesCount);
79
- }
80
- } else if (pagesCount <= 5) {
81
- fill(range, 2, pagesCount);
82
- } else {
83
- range.push(2, 3, -1);
84
- }
85
-
86
- range.push(pagesCount);
87
-
88
- return range;
89
- };
90
-
91
- export default generateRange;
1
+ import { GenerateRangeFn } from './types';
2
+
3
+ /**
4
+ * The function that returns an array of page numbers to show in the pagination
5
+ * component.
6
+ *
7
+ * Rules:
8
+ * - To always show the 1st and the last page.
9
+ * - To show one item before and one item after the selected page.
10
+ * - To return "-1" for the skipped items. This is to be able to display "..." in
11
+ * the pagination component.
12
+ * */
13
+ const SKIPPED_ITEMS_DELTA = 2;
14
+
15
+ const getSelectedRange = (pagesCount: number, selectedPage: number) => {
16
+ const range: number[] = [];
17
+
18
+ if (selectedPage !== pagesCount) {
19
+ range.push(selectedPage);
20
+ }
21
+
22
+ if (selectedPage > 1) {
23
+ range.unshift(selectedPage - 1);
24
+ }
25
+
26
+ if (selectedPage + 1 < pagesCount) {
27
+ range.push(selectedPage + 1);
28
+ }
29
+
30
+ return range;
31
+ };
32
+
33
+ const fill = (range: number[], minValue: number, maxValue: number) => {
34
+ for (let i = minValue; i < maxValue; ++i) {
35
+ range.push(i);
36
+ }
37
+ };
38
+
39
+ const generateRange: GenerateRangeFn = (pagesCount, selectedPage) => {
40
+ if (pagesCount == null || !Number.isInteger(pagesCount)) {
41
+ throw new Error('Pages count should be an integer');
42
+ }
43
+
44
+ if (pagesCount <= 0) {
45
+ return [];
46
+ }
47
+
48
+ let range = [1];
49
+
50
+ if (pagesCount === 1) {
51
+ return range;
52
+ }
53
+
54
+ if (selectedPage != null && !Number.isInteger(selectedPage)) {
55
+ throw new Error('Selected page should be an integer');
56
+ }
57
+
58
+ if (selectedPage != null && (selectedPage < 1 || selectedPage > pagesCount)) {
59
+ throw new Error(`Selected page ${selectedPage} is out of range`);
60
+ }
61
+
62
+ if (selectedPage && selectedPage > 2) {
63
+ const selectedRange = getSelectedRange(pagesCount, selectedPage);
64
+
65
+ const [minSelectedRange, , maxSelectedRange] = selectedRange;
66
+
67
+ if (minSelectedRange - SKIPPED_ITEMS_DELTA > 1) {
68
+ range.push(-1);
69
+ } else {
70
+ fill(range, 2, minSelectedRange);
71
+ }
72
+
73
+ range = range.concat(selectedRange);
74
+
75
+ if (pagesCount - maxSelectedRange > SKIPPED_ITEMS_DELTA) {
76
+ range.push(-1);
77
+ } else {
78
+ fill(range, maxSelectedRange + 1, pagesCount);
79
+ }
80
+ } else if (pagesCount <= 5) {
81
+ fill(range, 2, pagesCount);
82
+ } else {
83
+ range.push(2, 3, -1);
84
+ }
85
+
86
+ range.push(pagesCount);
87
+
88
+ return range;
89
+ };
90
+
91
+ export default generateRange;
@@ -1 +1 @@
1
- export { default as generateRange } from './generateRange';
1
+ export { default as generateRange } from './generateRange';
@@ -1,4 +1,4 @@
1
- export type GenerateRangeFn = (
2
- pagesCount: number,
3
- selectedPage?: number,
4
- ) => Array<number>;
1
+ export type GenerateRangeFn = (
2
+ pagesCount: number,
3
+ selectedPage?: number,
4
+ ) => Array<number>;
@@ -1 +1 @@
1
- export { throttle } from './throttle';
1
+ export { throttle } from './throttle';