react-hook-core 0.1.4 → 0.1.7

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/src/reflect.ts ADDED
@@ -0,0 +1,244 @@
1
+ export function clone(obj: any): any {
2
+ if (!obj) {
3
+ return obj;
4
+ }
5
+ if (obj instanceof Date) {
6
+ return new Date(obj.getTime());
7
+ }
8
+ if (typeof obj !== 'object') {
9
+ return obj;
10
+ }
11
+ if (Array.isArray(obj)) {
12
+ const arr = [];
13
+ for (const sub of obj) {
14
+ const c = clone(sub);
15
+ arr.push(c);
16
+ }
17
+ return arr;
18
+ }
19
+ const x: any = {};
20
+ const keys = Object.keys(obj);
21
+ for (const k of keys) {
22
+ const v = obj[k];
23
+ if (v instanceof Date) {
24
+ x[k] = new Date(v.getTime());
25
+ } else {
26
+ switch (typeof v) {
27
+ case 'object':
28
+ x[k] = clone(v);
29
+ break;
30
+ default:
31
+ x[k] = v;
32
+ break;
33
+ }
34
+ }
35
+ }
36
+ return x;
37
+ }
38
+
39
+ export function getDirectValue(obj: any, key: string): any {
40
+ if (obj && obj.hasOwnProperty(key)) {
41
+ return obj[key];
42
+ }
43
+ return null;
44
+ }
45
+
46
+ export function setValue(obj: any, key: string, value: any): any {
47
+ let replaceKey = key.replace(/\[/g, '.[').replace(/\.\./g, '.');
48
+ if (replaceKey.indexOf('.') === 0) {
49
+ replaceKey = replaceKey.slice(1, replaceKey.length);
50
+ }
51
+ const keys = replaceKey.split('.');
52
+ const firstKey = keys.shift();
53
+ if (!firstKey) {
54
+ return;
55
+ }
56
+ const isArrayKey = /\[([0-9]+)\]/.test(firstKey);
57
+ if (keys.length > 0) {
58
+ const firstKeyValue = obj[firstKey] || {};
59
+ const returnValue = setValue(firstKeyValue, keys.join('.'), value);
60
+ return setKey(obj, isArrayKey, firstKey, returnValue);
61
+ }
62
+ return setKey(obj, isArrayKey, firstKey, value);
63
+ }
64
+ const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: any) => {
65
+ if (_isArrayKey) {
66
+ if (_object.length > _key) {
67
+ _object[_key] = _nextValue;
68
+ } else {
69
+ _object.push(_nextValue);
70
+ }
71
+ } else {
72
+ _object[_key] = _nextValue;
73
+ }
74
+ return _object;
75
+ };
76
+
77
+ export function diff(obj1: any, obj2: any): string[] {
78
+ const fields = [];
79
+ const key1s = Object.keys(obj1);
80
+ for (const k of key1s) {
81
+ const v1 = obj1[k];
82
+ const v2 = obj2[k];
83
+ if (v1 == null) {
84
+ if (v2 != null) {
85
+ fields.push(k);
86
+ }
87
+ } else {
88
+ if (typeof v1 !== 'object') {
89
+ if (v1 !== v2) {
90
+ fields.push(k);
91
+ }
92
+ } else {
93
+ const e = equal(v1, v2);
94
+ if (!e) {
95
+ fields.push(k);
96
+ }
97
+ }
98
+ }
99
+ }
100
+ const key2s = Object.keys(obj2);
101
+ const ni = notIn(key1s, key2s);
102
+ for (const n of ni) {
103
+ fields.push(n);
104
+ }
105
+ return fields;
106
+ }
107
+
108
+ export function makeDiff(obj1: any, obj2: any, keys?: string[], version?: string): any {
109
+ const obj3: any = {};
110
+ const s = diff(obj1, obj2);
111
+ if (s.length === 0) {
112
+ return obj3;
113
+ }
114
+ for (const d of s) {
115
+ obj3[d] = obj2[d];
116
+ }
117
+ if (keys && keys.length > 0) {
118
+ for (const x of keys) {
119
+ if (x.length > 0) {
120
+ obj3[x] = obj1[x];
121
+ }
122
+ }
123
+ }
124
+ if (version && version.length > 0) {
125
+ obj3[version] = obj1[version];
126
+ }
127
+ return obj3;
128
+ }
129
+
130
+ export function notIn(s1: string[], s2: string[]): string[] {
131
+ const r = [];
132
+ for (const s of s2) {
133
+ if (s1.indexOf(s) < 0) {
134
+ r.push(s);
135
+ }
136
+ }
137
+ return r;
138
+ }
139
+
140
+ export function equal(obj1: any, obj2: any): boolean {
141
+ if (obj1 == null && obj2 == null) {
142
+ return true;
143
+ }
144
+ if (obj1 == null || obj2 == null) {
145
+ return false;
146
+ }
147
+ if ((typeof obj1) !== (typeof obj2)) {
148
+ return false;
149
+ }
150
+ if (typeof obj1 !== 'object') {
151
+ return obj1 === obj2;
152
+ }
153
+ if (obj1 instanceof Date) {
154
+ if (!(obj2 instanceof Date)) {
155
+ return false;
156
+ }
157
+ return (obj1.getTime() === obj2.getTime());
158
+ }
159
+ if ((Array.isArray(obj1) && !Array.isArray(obj2))
160
+ || (!Array.isArray(obj1) && Array.isArray(obj2))) {
161
+ return false;
162
+ }
163
+ if (!Array.isArray(obj1) && !Array.isArray(obj2)) {
164
+ const key1s = Object.keys(obj1);
165
+ const key2s = Object.keys(obj2);
166
+ if (key1s.length !== key2s.length) {
167
+ return false;
168
+ }
169
+ for (const k of key1s) {
170
+ const v = obj1[k];
171
+ if (typeof v !== 'object') {
172
+ if (v !== obj2[k]) {
173
+ return false;
174
+ }
175
+ } else {
176
+ const e = equal(v, obj2[k]);
177
+ if (e === false) {
178
+ return false;
179
+ }
180
+ }
181
+ }
182
+ return true;
183
+ }
184
+ return equalArrays(obj1, obj2);
185
+ }
186
+
187
+ export function equalArrays<T>(ar1: T[], ar2: T[]): boolean {
188
+ if (ar1 == null && ar2 == null) {
189
+ return true;
190
+ }
191
+ if (ar1 == null || ar2 == null) {
192
+ return false;
193
+ }
194
+ if (ar1.length !== ar2.length) {
195
+ return false;
196
+ }
197
+ for (let i = 0; i < ar1.length; i++) {
198
+ const e = equal(ar1[i], ar2[i]);
199
+ if (e === false) {
200
+ return false;
201
+ }
202
+ }
203
+ return true;
204
+ }
205
+
206
+ export function getArray<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): T[] {
207
+ const arrs = [];
208
+ if (list) {
209
+ for (const obj of list) {
210
+ if ((obj as any)[name] === v) {
211
+ arrs.push(obj);
212
+ }
213
+ }
214
+ }
215
+ return arrs;
216
+ }
217
+ export function getDiffArray<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): T[] {
218
+ const arrs = [];
219
+ if (list) {
220
+ for (const obj of list) {
221
+ if ((obj as any)[name] !== true) {
222
+ arrs.push(obj);
223
+ }
224
+ }
225
+ }
226
+ return arrs;
227
+ }
228
+ export function setAll<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): void {
229
+ if (list) {
230
+ for (const obj of list) {
231
+ (obj as any)[name] = v;
232
+ }
233
+ }
234
+ }
235
+ export function equalAll<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): boolean {
236
+ if (list) {
237
+ for (const obj of list) {
238
+ if ((obj as any)[name] !== v) {
239
+ return false;
240
+ }
241
+ }
242
+ }
243
+ return true;
244
+ }