Skip to content

Commit ea9a5ab

Browse files
committed
refactoring: use array versions in object versions to reduce bundle size.
1 parent 87497b3 commit ea9a5ab

14 files changed

Lines changed: 449 additions & 564 deletions

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@
3737
"react-dom": "^16.8.0"
3838
},
3939
"devDependencies": {
40-
"@types/jest": "^24.0.11",
41-
"@types/react": "^16.8.13",
40+
"@types/jest": "^24.0.15",
41+
"@types/react": "^16.8.23",
4242
"cross-env": "^5.2.0",
4343
"gh-pages": "^1.2.0",
4444
"react": "^16.8.6",
4545
"react-dom": "^16.8.6",
46-
"react-hooks-testing-library": "^0.4.0",
47-
"react-testing-library": "^6.1.2",
46+
"@testing-library/react": "^8.0.5",
47+
"@testing-library/react-hooks": "^1.1.0",
48+
"react-test-renderer": "^16.8.6",
4849
"rimraf": "^2.6.3",
49-
"typescript": "^3.4.2",
50-
"jest": "^24.5.0",
50+
"typescript": "^3.5.3",
51+
"jest": "^24.8.0",
5152
"ts-jest": "^24.0.2",
52-
"babel-eslint": "10.0.1",
53+
"babel-eslint": "10.0.2",
5354
"@typescript-eslint/parser": "1.5.0",
5455
"@typescript-eslint/eslint-plugin": "1.5.0",
5556
"eslint-plugin-react-hooks": "1.5.1",
@@ -64,7 +65,7 @@
6465
"eslint-plugin-react": "7.12.4",
6566
"eslint-config-react-app": "3.0.8",
6667
"eslint-config-revolut-react": "0.0.22",
67-
"prettier": "1.17.0"
68+
"prettier": "1.18.2"
6869
},
6970
"favoriteScripts": [
7071
"test:watch"

src/array/index.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { renderHook, cleanup, act } from 'react-hooks-testing-library';
1+
import { renderHook, act } from '@testing-library/react-hooks';
2+
import { cleanup } from '@testing-library/react';
23
import { useNumber } from './useNumber';
34
import { useArray } from './useArray';
45
import { useBoolean } from './useBoolean';

src/array/useArray.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function useArray<T = any>(initial: T[]): UseArray<T> {
4242
() => ({
4343
setValue,
4444
add,
45+
push: add,
4546
move,
4647
clear,
4748
removeById,

src/array/useInput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { default as React, SetStateAction, useCallback, useMemo, useState } from
22

33
export type UseInputActions = {
44
setValue: React.Dispatch<SetStateAction<string>>;
5-
onChange: (e: React.SyntheticEvent) => void;
5+
onChange: (e: React.BaseSyntheticEvent) => void;
66
clear: () => void;
77
};
88
export type UseInput = [[string, boolean], UseInputActions];

src/array/useMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export function useMap<K, V>(initialState: MapOrEntries<K, V> = new Map()): UseM
4141
setValue: setMap,
4242
clear,
4343
set,
44+
// TODO: To be removed in the next major release
4445
remove: deleteByKey,
46+
delete: deleteByKey,
4547
initialize,
4648
}),
4749
[clear, deleteByKey, initialize, set],

src/index.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { renderHook, cleanup, act } from 'react-hooks-testing-library';
1+
import { renderHook, act } from '@testing-library/react-hooks';
2+
import { cleanup } from '@testing-library/react';
23
import { useStateful } from './useStateful';
34
import { useNumber } from './useNumber';
45
import { useArray } from './useArray';

src/useArray.ts

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { useCallback, useMemo, useState } from 'react';
1+
import { useMemo } from 'react';
22
import { UseStateful } from './useStateful';
3+
import useArrayArray from './array/useArray';
34

45
export type UseArray<T> = UseStateful<T[]> & {
56
add: (value: T) => void;
@@ -10,43 +11,13 @@ export type UseArray<T> = UseStateful<T[]> & {
1011
};
1112

1213
export function useArray<T = any>(initial: T[]): UseArray<T> {
13-
const [value, setValue] = useState(initial);
14-
const add = useCallback(a => setValue(v => [...v, a]), []);
15-
const move = useCallback(
16-
(from: number, to: number) =>
17-
setValue(it => {
18-
const copy = it.slice();
19-
copy.splice(to < 0 ? copy.length + to : to, 0, copy.splice(from, 1)[0]);
20-
return copy;
21-
}),
22-
[],
23-
);
24-
const clear = useCallback(() => setValue(() => []), []);
25-
const removeById = useCallback(
26-
// @ts-ignore not every array that you will pass down will have object with id field.
27-
id => setValue(arr => arr.filter(v => v && v.id !== id)),
28-
[],
29-
);
30-
const removeIndex = useCallback(
31-
index =>
32-
setValue(v => {
33-
const copy = v.slice();
34-
copy.splice(index, 1);
35-
return copy;
36-
}),
37-
[],
38-
);
14+
const [value, actions] = useArrayArray(initial);
3915
return useMemo(
4016
() => ({
4117
value,
42-
setValue,
43-
add,
44-
move,
45-
clear,
46-
removeById,
47-
removeIndex,
18+
...actions,
4819
}),
49-
[add, clear, move, removeById, removeIndex, value],
20+
[actions, value],
5021
);
5122
}
5223

src/useBoolean.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
2-
import { SetStateAction, useCallback, useMemo, useState } from 'react';
2+
import { SetStateAction, useMemo } from 'react';
3+
import useBooleanArray from './array/useBoolean';
34

45
export type UseBoolean = {
56
value: boolean;
@@ -10,19 +11,13 @@ export type UseBoolean = {
1011
};
1112

1213
export function useBoolean(initial: boolean): UseBoolean {
13-
const [value, setValue] = useState<boolean>(initial);
14-
const toggle = useCallback(() => setValue(v => !v), []);
15-
const setTrue = useCallback(() => setValue(true), []);
16-
const setFalse = useCallback(() => setValue(false), []);
14+
const [value, actions] = useBooleanArray(initial);
1715
return useMemo(
1816
() => ({
1917
value,
20-
setValue,
21-
toggle,
22-
setTrue,
23-
setFalse,
18+
...actions,
2419
}),
25-
[setFalse, setTrue, toggle, value],
20+
[actions, value],
2621
);
2722
}
2823

src/useInput.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { useCallback, useMemo, useState } from 'react';
33
import { UseStateful } from './useStateful';
44

55
export type UseInput = UseStateful<string> & {
6-
onChange: (e: React.SyntheticEvent) => void;
6+
onChange: (e: React.BaseSyntheticEvent) => void;
77
hasValue: boolean;
88
clear: () => void;
99
eventBind: {
10-
onChange: (e: React.SyntheticEvent) => void;
10+
onChange: (e: React.BaseSyntheticEvent) => void;
1111
value: string;
1212
};
1313
valueBind: {

src/useMap.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { useCallback, useMemo, useState } from 'react';
1+
import { useMemo } from 'react';
22
import { UseStateful } from './useStateful';
3+
import useMapArray from './array/useMap';
34

45
export type MapOrEntries<K, V> = Map<K, V> | [K, V][];
56
export type UseMap<K, V> = UseStateful<Map<K, V>> & {
@@ -10,41 +11,13 @@ export type UseMap<K, V> = UseStateful<Map<K, V>> & {
1011
};
1112

1213
export function useMap<K, V>(initialState: MapOrEntries<K, V> = new Map()): UseMap<K, V> {
13-
const [map, setMap] = useState(Array.isArray(initialState) ? new Map(initialState) : initialState);
14-
15-
const set = useCallback((key, value) => {
16-
setMap(aMap => {
17-
const copy = new Map(aMap);
18-
return copy.set(key, value);
19-
});
20-
}, []);
21-
22-
const deleteByKey = useCallback(key => {
23-
setMap(_map => {
24-
const copy = new Map(_map);
25-
copy.delete(key);
26-
return copy;
27-
});
28-
}, []);
29-
30-
const clear = useCallback(() => {
31-
setMap(() => new Map());
32-
}, []);
33-
34-
const initialize = useCallback((mapOrTuple: MapOrEntries<K, V> = []) => {
35-
setMap(() => new Map(mapOrTuple));
36-
}, []);
37-
14+
const [map, actions] = useMapArray(initialState);
3815
return useMemo(
3916
() => ({
4017
value: map,
41-
setValue: setMap,
42-
clear,
43-
set,
44-
remove: deleteByKey,
45-
initialize,
18+
...actions,
4619
}),
47-
[clear, deleteByKey, initialize, map, set],
20+
[actions, map],
4821
);
4922
}
5023

0 commit comments

Comments
 (0)