@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,85 +1,85 @@
1
- import { debounce } from '.';
2
-
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- const callNTimes = (fn: any, n: number) => {
5
- for (let i = 0; i < n; i++) {
6
- fn();
7
- }
8
- };
9
-
10
- describe('debounce', () => {
11
- beforeEach(() => {
12
- jest.useFakeTimers();
13
- jest.spyOn(global, 'setTimeout');
14
- jest.spyOn(global, 'clearTimeout');
15
- });
16
-
17
- afterAll(() => {
18
- jest.useRealTimers();
19
- jest.restoreAllMocks();
20
- });
21
-
22
- it('should cancel immediately without executing', () => {
23
- const fn = jest.fn();
24
- const [debouncedFn, cancel] = debounce(fn, 100);
25
-
26
- debouncedFn();
27
- // Cancel should not execute any pending call
28
- cancel();
29
- jest.advanceTimersByTime(150);
30
- expect(fn).toHaveBeenCalledTimes(0);
31
- });
32
-
33
- it('should execute deferred function on the second time, after cancelling for the first time', () => {
34
- const fn = jest.fn();
35
- const [debouncedFn, cancel] = debounce(fn, 100);
36
-
37
- debouncedFn();
38
- // Cancel should not execute any pending call
39
- cancel();
40
- jest.advanceTimersByTime(150);
41
- expect(fn).toHaveBeenCalledTimes(0);
42
-
43
- debouncedFn();
44
- jest.advanceTimersByTime(150);
45
- expect(fn).toHaveBeenCalledTimes(1);
46
- });
47
-
48
- it('delays a function call', () => {
49
- const fn = jest.fn();
50
- const [debouncedFn, cancel] = debounce(fn, 100);
51
-
52
- // Subsequent calls within the throttle window should be throttled
53
- callNTimes(debouncedFn, 3);
54
- jest.advanceTimersByTime(50);
55
- expect(fn).toHaveBeenCalledTimes(0);
56
- jest.advanceTimersByTime(500);
57
- expect(fn).toHaveBeenCalledTimes(1);
58
-
59
- // After the throttle window, the next call should execute
60
- debouncedFn();
61
-
62
- // Wait for the throttle window to pass
63
- jest.advanceTimersByTime(500);
64
- expect(fn).toHaveBeenCalledTimes(2);
65
-
66
- // Calling the throttled function after cancel should execute
67
- debouncedFn();
68
- // ...but cancelled by triggering the cancel function
69
- cancel();
70
- expect(fn).toHaveBeenCalledTimes(2);
71
- });
72
-
73
- it('should debounce and execute the last call within debounce window', () => {
74
- const fn = jest.fn();
75
- const [debouncedFn] = debounce(fn, 100);
76
-
77
- callNTimes(debouncedFn, 3);
78
- jest.advanceTimersByTime(100);
79
- expect(fn).toHaveBeenCalledTimes(1);
80
-
81
- debouncedFn();
82
- jest.advanceTimersByTime(150);
83
- expect(fn).toHaveBeenCalledTimes(2);
84
- });
85
- });
1
+ import { debounce } from '.';
2
+
3
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
+ const callNTimes = (fn: any, n: number) => {
5
+ for (let i = 0; i < n; i++) {
6
+ fn();
7
+ }
8
+ };
9
+
10
+ describe('debounce', () => {
11
+ beforeEach(() => {
12
+ jest.useFakeTimers();
13
+ jest.spyOn(global, 'setTimeout');
14
+ jest.spyOn(global, 'clearTimeout');
15
+ });
16
+
17
+ afterAll(() => {
18
+ jest.useRealTimers();
19
+ jest.restoreAllMocks();
20
+ });
21
+
22
+ it('should cancel immediately without executing', () => {
23
+ const fn = jest.fn();
24
+ const [debouncedFn, cancel] = debounce(fn, 100);
25
+
26
+ debouncedFn();
27
+ // Cancel should not execute any pending call
28
+ cancel();
29
+ jest.advanceTimersByTime(150);
30
+ expect(fn).toHaveBeenCalledTimes(0);
31
+ });
32
+
33
+ it('should execute deferred function on the second time, after cancelling for the first time', () => {
34
+ const fn = jest.fn();
35
+ const [debouncedFn, cancel] = debounce(fn, 100);
36
+
37
+ debouncedFn();
38
+ // Cancel should not execute any pending call
39
+ cancel();
40
+ jest.advanceTimersByTime(150);
41
+ expect(fn).toHaveBeenCalledTimes(0);
42
+
43
+ debouncedFn();
44
+ jest.advanceTimersByTime(150);
45
+ expect(fn).toHaveBeenCalledTimes(1);
46
+ });
47
+
48
+ it('delays a function call', () => {
49
+ const fn = jest.fn();
50
+ const [debouncedFn, cancel] = debounce(fn, 100);
51
+
52
+ // Subsequent calls within the throttle window should be throttled
53
+ callNTimes(debouncedFn, 3);
54
+ jest.advanceTimersByTime(50);
55
+ expect(fn).toHaveBeenCalledTimes(0);
56
+ jest.advanceTimersByTime(500);
57
+ expect(fn).toHaveBeenCalledTimes(1);
58
+
59
+ // After the throttle window, the next call should execute
60
+ debouncedFn();
61
+
62
+ // Wait for the throttle window to pass
63
+ jest.advanceTimersByTime(500);
64
+ expect(fn).toHaveBeenCalledTimes(2);
65
+
66
+ // Calling the throttled function after cancel should execute
67
+ debouncedFn();
68
+ // ...but cancelled by triggering the cancel function
69
+ cancel();
70
+ expect(fn).toHaveBeenCalledTimes(2);
71
+ });
72
+
73
+ it('should debounce and execute the last call within debounce window', () => {
74
+ const fn = jest.fn();
75
+ const [debouncedFn] = debounce(fn, 100);
76
+
77
+ callNTimes(debouncedFn, 3);
78
+ jest.advanceTimersByTime(100);
79
+ expect(fn).toHaveBeenCalledTimes(1);
80
+
81
+ debouncedFn();
82
+ jest.advanceTimersByTime(150);
83
+ expect(fn).toHaveBeenCalledTimes(2);
84
+ });
85
+ });
@@ -1,24 +1,24 @@
1
- type UnknownFn = (...args: any[]) => unknown;
2
-
3
- export const debounce = (func: UnknownFn, wait = 200) => {
4
- let timeoutId: NodeJS.Timeout | null = null;
5
- const executedFunction = (...args: any[]) => {
6
- const postponedFn = () => {
7
- if (timeoutId) {
8
- clearTimeout(timeoutId);
9
- }
10
- func(...args);
11
- };
12
- if (timeoutId) {
13
- clearTimeout(timeoutId);
14
- }
15
- timeoutId = setTimeout(postponedFn, wait);
16
- };
17
-
18
- const cancel = function () {
19
- if (timeoutId) {
20
- clearTimeout(timeoutId);
21
- }
22
- };
23
- return [executedFunction, cancel];
24
- };
1
+ type UnknownFn = (...args: any[]) => unknown;
2
+
3
+ export const debounce = (func: UnknownFn, wait = 200) => {
4
+ let timeoutId: NodeJS.Timeout | null = null;
5
+ const executedFunction = (...args: any[]) => {
6
+ const postponedFn = () => {
7
+ if (timeoutId) {
8
+ clearTimeout(timeoutId);
9
+ }
10
+ func(...args);
11
+ };
12
+ if (timeoutId) {
13
+ clearTimeout(timeoutId);
14
+ }
15
+ timeoutId = setTimeout(postponedFn, wait);
16
+ };
17
+
18
+ const cancel = function () {
19
+ if (timeoutId) {
20
+ clearTimeout(timeoutId);
21
+ }
22
+ };
23
+ return [executedFunction, cancel];
24
+ };
@@ -1 +1 @@
1
- export { debounce } from './debounce';
1
+ export { debounce } from './debounce';
@@ -1,53 +1,53 @@
1
- import { assocPath } from '.';
2
-
3
- describe('utils => objects => assocPath', () => {
4
- it('assocPath should work', () => {
5
- const data = {
6
- a: {
7
- name: 'a',
8
- value: 1,
9
- },
10
- b: {
11
- name: 'b',
12
- value: 2,
13
- },
14
- };
15
- const result = assocPath(['c', 'name'], 'c')(data);
16
- expect(result).toEqual({
17
- a: {
18
- name: 'a',
19
- value: 1,
20
- },
21
- b: {
22
- name: 'b',
23
- value: 2,
24
- },
25
- c: {
26
- name: 'c',
27
- },
28
- });
29
- });
30
- it('assocPath should not change source object', () => {
31
- const data = {
32
- a: {
33
- name: 'a',
34
- value: 1,
35
- },
36
- b: {
37
- name: 'b',
38
- value: 2,
39
- },
40
- };
41
- assocPath(['c', 'name'], 'c')(data);
42
- expect(data).toEqual({
43
- a: {
44
- name: 'a',
45
- value: 1,
46
- },
47
- b: {
48
- name: 'b',
49
- value: 2,
50
- },
51
- });
52
- });
53
- });
1
+ import { assocPath } from '.';
2
+
3
+ describe('utils => objects => assocPath', () => {
4
+ it('assocPath should work', () => {
5
+ const data = {
6
+ a: {
7
+ name: 'a',
8
+ value: 1,
9
+ },
10
+ b: {
11
+ name: 'b',
12
+ value: 2,
13
+ },
14
+ };
15
+ const result = assocPath(['c', 'name'], 'c')(data);
16
+ expect(result).toEqual({
17
+ a: {
18
+ name: 'a',
19
+ value: 1,
20
+ },
21
+ b: {
22
+ name: 'b',
23
+ value: 2,
24
+ },
25
+ c: {
26
+ name: 'c',
27
+ },
28
+ });
29
+ });
30
+ it('assocPath should not change source object', () => {
31
+ const data = {
32
+ a: {
33
+ name: 'a',
34
+ value: 1,
35
+ },
36
+ b: {
37
+ name: 'b',
38
+ value: 2,
39
+ },
40
+ };
41
+ assocPath(['c', 'name'], 'c')(data);
42
+ expect(data).toEqual({
43
+ a: {
44
+ name: 'a',
45
+ value: 1,
46
+ },
47
+ b: {
48
+ name: 'b',
49
+ value: 2,
50
+ },
51
+ });
52
+ });
53
+ });
@@ -1,11 +1,11 @@
1
- export const assocPath =
2
- <T>([first, ...rest]: string[], value: any) =>
3
- (sourceObject: T): T =>
4
- JSON.parse(
5
- JSON.stringify({
6
- ...sourceObject,
7
- [first]: rest.length
8
- ? assocPath(rest, value)((sourceObject as any)[first])
9
- : value,
10
- }),
11
- ) as T;
1
+ export const assocPath =
2
+ <T>([first, ...rest]: string[], value: any) =>
3
+ (sourceObject: T): T =>
4
+ JSON.parse(
5
+ JSON.stringify({
6
+ ...sourceObject,
7
+ [first]: rest.length
8
+ ? assocPath(rest, value)((sourceObject as any)[first])
9
+ : value,
10
+ }),
11
+ ) as T;
@@ -1,49 +1,49 @@
1
- import { dissocPath } from '.';
2
-
3
- describe('utils => objects => dissocPath', () => {
4
- it('dissocPath should work', () => {
5
- const data = {
6
- a: {
7
- name: 'a',
8
- value: 1,
9
- },
10
- b: {
11
- name: 'b',
12
- value: 2,
13
- },
14
- };
15
- const result = dissocPath(['a', 'name'])(data);
16
- expect(result).toEqual({
17
- a: {
18
- value: 1,
19
- },
20
- b: {
21
- name: 'b',
22
- value: 2,
23
- },
24
- });
25
- });
26
- it('dissocPath should not change source object', () => {
27
- const data = {
28
- a: {
29
- name: 'a',
30
- value: 1,
31
- },
32
- b: {
33
- name: 'b',
34
- value: 2,
35
- },
36
- };
37
- dissocPath(['a', 'name'])(data);
38
- expect(data).toEqual({
39
- a: {
40
- name: 'a',
41
- value: 1,
42
- },
43
- b: {
44
- name: 'b',
45
- value: 2,
46
- },
47
- });
48
- });
49
- });
1
+ import { dissocPath } from '.';
2
+
3
+ describe('utils => objects => dissocPath', () => {
4
+ it('dissocPath should work', () => {
5
+ const data = {
6
+ a: {
7
+ name: 'a',
8
+ value: 1,
9
+ },
10
+ b: {
11
+ name: 'b',
12
+ value: 2,
13
+ },
14
+ };
15
+ const result = dissocPath(['a', 'name'])(data);
16
+ expect(result).toEqual({
17
+ a: {
18
+ value: 1,
19
+ },
20
+ b: {
21
+ name: 'b',
22
+ value: 2,
23
+ },
24
+ });
25
+ });
26
+ it('dissocPath should not change source object', () => {
27
+ const data = {
28
+ a: {
29
+ name: 'a',
30
+ value: 1,
31
+ },
32
+ b: {
33
+ name: 'b',
34
+ value: 2,
35
+ },
36
+ };
37
+ dissocPath(['a', 'name'])(data);
38
+ expect(data).toEqual({
39
+ a: {
40
+ name: 'a',
41
+ value: 1,
42
+ },
43
+ b: {
44
+ name: 'b',
45
+ value: 2,
46
+ },
47
+ });
48
+ });
49
+ });
@@ -1,14 +1,14 @@
1
- export const dissocPath =
2
- <T>(path: string[]) =>
3
- (sourceObject: T): T => {
4
- const resultObject = JSON.parse(JSON.stringify(sourceObject));
5
-
6
- path.reduce((acc: any, key, index) => {
7
- if (index === path.length - 1) {
8
- delete acc[key];
9
- }
10
- return acc[key];
11
- }, resultObject);
12
-
13
- return resultObject;
14
- };
1
+ export const dissocPath =
2
+ <T>(path: string[]) =>
3
+ (sourceObject: T): T => {
4
+ const resultObject = JSON.parse(JSON.stringify(sourceObject));
5
+
6
+ path.reduce((acc: any, key, index) => {
7
+ if (index === path.length - 1) {
8
+ delete acc[key];
9
+ }
10
+ return acc[key];
11
+ }, resultObject);
12
+
13
+ return resultObject;
14
+ };
@@ -1,7 +1,7 @@
1
- export * from './mapObjIndexed';
2
- export * from './assocPath';
3
- export * from './dissocPath';
4
- export * from './path';
5
- export * from './pathOr';
6
- export * from './prop';
7
- export * from './propOr';
1
+ export * from './mapObjIndexed';
2
+ export * from './assocPath';
3
+ export * from './dissocPath';
4
+ export * from './path';
5
+ export * from './pathOr';
6
+ export * from './prop';
7
+ export * from './propOr';
@@ -1,33 +1,33 @@
1
- import { mapObjIndexed } from '.';
2
-
3
- describe('utils => objects => mapObjIndexed', () => {
4
- it('mapObjIndexed should work', () => {
5
- const data = {
6
- a: {
7
- name: 'a',
8
- value: 1,
9
- },
10
- b: {
11
- name: 'b',
12
- value: 2,
13
- },
14
- };
15
- const result = mapObjIndexed(
16
- (item, key) => ({
17
- name: key,
18
- value: item.value + key,
19
- }),
20
- data,
21
- );
22
- expect(result).toEqual({
23
- a: {
24
- name: 'a',
25
- value: '1a',
26
- },
27
- b: {
28
- name: 'b',
29
- value: '2b',
30
- },
31
- });
32
- });
33
- });
1
+ import { mapObjIndexed } from '.';
2
+
3
+ describe('utils => objects => mapObjIndexed', () => {
4
+ it('mapObjIndexed should work', () => {
5
+ const data = {
6
+ a: {
7
+ name: 'a',
8
+ value: 1,
9
+ },
10
+ b: {
11
+ name: 'b',
12
+ value: 2,
13
+ },
14
+ };
15
+ const result = mapObjIndexed(
16
+ (item, key) => ({
17
+ name: key,
18
+ value: item.value + key,
19
+ }),
20
+ data,
21
+ );
22
+ expect(result).toEqual({
23
+ a: {
24
+ name: 'a',
25
+ value: '1a',
26
+ },
27
+ b: {
28
+ name: 'b',
29
+ value: '2b',
30
+ },
31
+ });
32
+ });
33
+ });
@@ -1,15 +1,15 @@
1
- type MapObjIndexedFn<T, U> = (
2
- value: T,
3
- key: string,
4
- obj: Record<string, T>,
5
- ) => U;
6
-
7
- export const mapObjIndexed = <T, U>(
8
- fn: MapObjIndexedFn<T, U>,
9
- obj: Record<string, T>,
10
- ): Record<string, U> => {
11
- return Object.keys(obj).reduce((result: Record<string, U>, key: string) => {
12
- result[key] = fn(obj[key], key, obj);
13
- return result;
14
- }, {});
15
- };
1
+ type MapObjIndexedFn<T, U> = (
2
+ value: T,
3
+ key: string,
4
+ obj: Record<string, T>,
5
+ ) => U;
6
+
7
+ export const mapObjIndexed = <T, U>(
8
+ fn: MapObjIndexedFn<T, U>,
9
+ obj: Record<string, T>,
10
+ ): Record<string, U> => {
11
+ return Object.keys(obj).reduce((result: Record<string, U>, key: string) => {
12
+ result[key] = fn(obj[key], key, obj);
13
+ return result;
14
+ }, {});
15
+ };
@@ -1,24 +1,24 @@
1
- import { path } from '.';
2
-
3
- describe('utils => objects => path', () => {
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('path should work with correct path', () => {
16
- const result = path(['a', 'name'])(data);
17
- expect(result).toEqual('a');
18
- });
19
-
20
- it('path should work with incorrect path', () => {
21
- const result = path(['a', 'name', 'value'])(data);
22
- expect(result).toEqual(undefined);
23
- });
24
- });
1
+ import { path } from '.';
2
+
3
+ describe('utils => objects => path', () => {
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('path should work with correct path', () => {
16
+ const result = path(['a', 'name'])(data);
17
+ expect(result).toEqual('a');
18
+ });
19
+
20
+ it('path should work with incorrect path', () => {
21
+ const result = path(['a', 'name', 'value'])(data);
22
+ expect(result).toEqual(undefined);
23
+ });
24
+ });
@@ -1,4 +1,4 @@
1
- export const path =
2
- <T extends Record<string | number, any>, R>(path: string[]) =>
3
- (obj: T): unknown =>
4
- path.reduce((prev, curr: string | number) => prev?.[curr], obj);
1
+ export const path =
2
+ <T extends Record<string | number, any>, R>(path: Array<string | number>) =>
3
+ (obj: T): unknown =>
4
+ path.reduce((prev, curr: string | number) => prev?.[curr], obj);