@ssa-ui-kit/utils 1.0.0 → 1.1.55-canary-dbf4f10-20250224
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.
- package/dist/index.js +323 -1
- package/dist/index.js.map +1 -1
- package/dist/utils/debounce/debounce.d.ts +1 -3
- package/dist/utils/objects/assocPath.d.ts +1 -1
- package/dist/utils/objects/path.d.ts +1 -1
- package/dist/utils/throttle/throttle.d.ts +1 -4
- package/package.json +4 -8
- package/src/index.ts +0 -8
- package/src/utils/CallAll.ts +0 -5
- package/src/utils/dates/dateFormatters.ts +0 -61
- package/src/utils/dates/dates.spec.tsx +0 -127
- package/src/utils/dates/index.ts +0 -1
- package/src/utils/debounce/debounce.spec.ts +0 -85
- package/src/utils/debounce/debounce.ts +0 -24
- package/src/utils/debounce/index.ts +0 -1
- package/src/utils/objects/assocPath.spec.tsx +0 -53
- package/src/utils/objects/assocPath.ts +0 -11
- package/src/utils/objects/dissocPath.spec.tsx +0 -49
- package/src/utils/objects/dissocPath.ts +0 -14
- package/src/utils/objects/index.ts +0 -7
- package/src/utils/objects/mapObjIndexed.spec.tsx +0 -33
- package/src/utils/objects/mapObjIndexed.ts +0 -15
- package/src/utils/objects/path.spec.tsx +0 -24
- package/src/utils/objects/path.ts +0 -4
- package/src/utils/objects/pathOr.spec.tsx +0 -23
- package/src/utils/objects/pathOr.ts +0 -11
- package/src/utils/objects/prop.spec.tsx +0 -17
- package/src/utils/objects/prop.ts +0 -4
- package/src/utils/objects/propOr.spec.tsx +0 -17
- package/src/utils/objects/propOr.ts +0 -11
- package/src/utils/pagination/generateRange.spec.ts +0 -100
- package/src/utils/pagination/generateRange.ts +0 -91
- package/src/utils/pagination/index.ts +0 -1
- package/src/utils/pagination/types.ts +0 -4
- package/src/utils/throttle/index.ts +0 -1
- package/src/utils/throttle/throttle.spec.ts +0 -56
- package/src/utils/throttle/throttle.ts +0 -43
- package/src/utils/types.ts +0 -4
- package/tsbuildcache +0 -1
- package/tsconfig.build.json +0 -35
- package/tsconfig.json +0 -17
- package/webpack.config.js +0 -16
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { dateFormatters } from './index';
|
|
2
|
-
|
|
3
|
-
const { getTimeAgo } = dateFormatters;
|
|
4
|
-
const date = Date.now();
|
|
5
|
-
const timeValuesArr = [
|
|
6
|
-
{
|
|
7
|
-
input: date - 6000,
|
|
8
|
-
output: 'Just Now',
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
input: date - 60000,
|
|
12
|
-
output: '1 min ago',
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
input: date - 120000,
|
|
16
|
-
output: '2 mins ago',
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
input: date - 6000000,
|
|
20
|
-
output: '1 hour ago',
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
input: date - 8000000,
|
|
24
|
-
output: '2 hours ago',
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
input: date - 120000000,
|
|
28
|
-
output: '1 day ago',
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
input: date - 240000000,
|
|
32
|
-
output: '2 days ago',
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
input: date - 1200000000,
|
|
36
|
-
output: '1 week ago',
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
input: date - 1400000000,
|
|
40
|
-
output: '2 weeks ago',
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
input: date - 3000000000,
|
|
44
|
-
output: '1 month ago',
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
input: date - 6000000000,
|
|
48
|
-
output: '2 months ago',
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
input: date - 36000000000,
|
|
52
|
-
output: '1 year ago',
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
input: date - 64000000000,
|
|
56
|
-
output: '2 years ago',
|
|
57
|
-
},
|
|
58
|
-
];
|
|
59
|
-
|
|
60
|
-
describe('dates', () => {
|
|
61
|
-
describe('dateFormatters', () => {
|
|
62
|
-
const testDateMs = 1681228541711; // 2023-04-11 16:55 GMT+0
|
|
63
|
-
|
|
64
|
-
describe('formatTime()', () => {
|
|
65
|
-
it('formats epoch ms as a time string', () => {
|
|
66
|
-
const result = dateFormatters.formatTime(testDateMs);
|
|
67
|
-
expect(result).toEqual('04:55 PM');
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
describe('formatDayOfWeek()', () => {
|
|
72
|
-
it('formats epoch ms as a day of week string', () => {
|
|
73
|
-
const result = dateFormatters.formatDayOfWeek(testDateMs);
|
|
74
|
-
expect(result).toEqual('Tue');
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
describe('formatDate()', () => {
|
|
79
|
-
it('formats epoch ms as a date string', () => {
|
|
80
|
-
const result = dateFormatters.formatDate(testDateMs);
|
|
81
|
-
expect(result).toEqual('Apr 11');
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
describe('printDayOfTheWeek()', () => {
|
|
86
|
-
it('translate getDay() code to string', () => {
|
|
87
|
-
const sunday = dateFormatters.printDayOfTheWeek(0);
|
|
88
|
-
expect(sunday).toEqual('Sun');
|
|
89
|
-
|
|
90
|
-
const monday = dateFormatters.printDayOfTheWeek(1);
|
|
91
|
-
expect(monday).toEqual('Mon');
|
|
92
|
-
|
|
93
|
-
const tuesday = dateFormatters.printDayOfTheWeek(2);
|
|
94
|
-
expect(tuesday).toEqual('Tue');
|
|
95
|
-
|
|
96
|
-
const wednesday = dateFormatters.printDayOfTheWeek(3);
|
|
97
|
-
expect(wednesday).toEqual('Wed');
|
|
98
|
-
|
|
99
|
-
const thursday = dateFormatters.printDayOfTheWeek(4);
|
|
100
|
-
expect(thursday).toEqual('Thu');
|
|
101
|
-
|
|
102
|
-
const friday = dateFormatters.printDayOfTheWeek(5);
|
|
103
|
-
expect(friday).toEqual('Fri');
|
|
104
|
-
|
|
105
|
-
const saturday = dateFormatters.printDayOfTheWeek(6);
|
|
106
|
-
expect(saturday).toEqual('Sat');
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
describe('getTimeAgo()', () => {
|
|
111
|
-
it('Returns an error when passing an invalid time value', () => {
|
|
112
|
-
expect(() => getTimeAgo('Date')).toThrow(new Error('Invalid date'));
|
|
113
|
-
expect(() => getTimeAgo('20-20-23')).toThrow(new Error('Invalid date'));
|
|
114
|
-
expect(() => getTimeAgo('1618301456781')).toThrow(
|
|
115
|
-
new Error('Invalid date'),
|
|
116
|
-
);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
timeValuesArr.forEach((item) => {
|
|
120
|
-
const periodName = item.output.replace(/[0-9] | ago/g, '');
|
|
121
|
-
it(`Returns the "${periodName}"`, () => {
|
|
122
|
-
expect(getTimeAgo(item.input)).toEqual(item.output);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
});
|
package/src/utils/dates/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * as dateFormatters from './dateFormatters';
|
|
@@ -1,85 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
export { debounce } from './debounce';
|
|
@@ -1,53 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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,33 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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,23 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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,17 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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
|
-
});
|