@cmtlyt/lingshu-toolkit 0.1.1 → 0.3.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.
- package/README.md +2 -0
- package/dist/607.js +288 -108
- package/dist/707.js +136 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.js +129 -8
- package/dist/react/use-boolean/index.d.ts +6 -0
- package/dist/react/use-boolean/index.js +16 -0
- package/dist/react/use-boolean/index.test.d.ts +1 -0
- package/dist/react/use-controllable-value/index.js +1 -2
- package/dist/react/use-counter/index.js +2 -2
- package/dist/react/use-force-update/index.d.ts +1 -0
- package/dist/react/use-force-update/index.js +6 -0
- package/dist/react/use-force-update/index.test.d.ts +1 -0
- package/dist/react/use-mount/index.d.ts +1 -0
- package/dist/react/use-mount/index.js +16 -0
- package/dist/react/use-mount/index.test.d.ts +1 -0
- package/dist/react/use-ref-state/index.d.ts +8 -0
- package/dist/react/use-ref-state/index.js +35 -0
- package/dist/react/use-ref-state/index.test.d.ts +1 -0
- package/dist/react/use-storage/index.d.ts +2 -0
- package/dist/react/use-storage/index.js +15 -0
- package/dist/react/use-storage/index.test.d.ts +1 -0
- package/dist/react/use-title/index.d.ts +6 -0
- package/dist/react/use-title/index.js +24 -0
- package/dist/react/use-title/index.test.d.ts +1 -0
- package/dist/react/use-toggle/index.d.ts +10 -0
- package/dist/react/use-toggle/index.js +26 -0
- package/dist/react/use-toggle/index.test.d.ts +1 -0
- package/dist/react/use-valid-data/index.d.ts +4 -2
- package/dist/react/use-valid-data/index.js +5 -2
- package/dist/shared/animation/__test__/animation-pause-resume.test.d.ts +1 -0
- package/dist/shared/animation/__test__/animation.test.d.ts +1 -0
- package/dist/shared/animation/__test__/step-animation.test.d.ts +1 -0
- package/dist/shared/animation/__test__/utils.test.d.ts +1 -0
- package/dist/shared/animation/index.d.ts +3 -0
- package/dist/shared/animation/index.js +71 -0
- package/dist/shared/animation/types.d.ts +22 -0
- package/dist/shared/animation/types.js +0 -0
- package/dist/shared/animation/utils.d.ts +16 -0
- package/dist/shared/animation/utils.js +138 -0
- package/dist/shared/condition-merge/index.d.ts +31 -0
- package/dist/shared/condition-merge/index.js +30 -0
- package/dist/shared/condition-merge/index.test-d.d.ts +1 -0
- package/dist/shared/condition-merge/index.test-d.js +108 -0
- package/dist/shared/condition-merge/index.test.d.ts +1 -0
- package/dist/shared/create-storage-handler/index.browser.test.d.ts +1 -0
- package/dist/shared/create-storage-handler/index.d.ts +10 -0
- package/dist/shared/create-storage-handler/index.js +68 -0
- package/dist/shared/create-storage-handler/index.test.d.ts +1 -0
- package/dist/shared/data-handler/index.d.ts +7 -2
- package/dist/shared/data-handler/index.js +4 -4
- package/dist/shared/data-handler/tools.d.ts +36 -12
- package/dist/shared/data-handler/tools.js +8 -2
- package/dist/shared/data-handler/types.d.ts +3 -2
- package/dist/shared/index.d.ts +5 -0
- package/dist/shared/index.js +2 -1
- package/dist/shared/logger/index.d.ts +5 -1
- package/dist/shared/logger/index.js +2 -2
- package/dist/shared/throw-error/index.d.ts +2 -2
- package/dist/shared/throw-error/index.js +4 -4
- package/dist/shared/types/base.d.ts +8 -0
- package/dist/shared/types/base.js +0 -0
- package/dist/shared/types/index.d.ts +2 -2
- package/dist/shared/types/index.js +2 -0
- package/dist/shared/types/pack.d.ts +9 -0
- package/dist/shared/types/pack.js +1 -0
- package/dist/shared/with-resolvers/index.d.ts +6 -0
- package/dist/shared/with-resolvers/index.js +15 -0
- package/dist/shared/with-resolvers/index.test.d.ts +1 -0
- package/dist/test/utils.d.ts +13 -0
- package/dist/vue/index.d.ts +1 -0
- package/dist/vue/index.js +29 -0
- package/dist/vue/use-title/index.d.ts +6 -0
- package/dist/vue/use-title/index.js +29 -0
- package/dist/vue/use-title/index.test.d.ts +1 -0
- package/package.json +16 -4
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
一个支持 shadcn 的工具库
|
|
4
4
|
|
|
5
|
+
[](https://codecov.io/gh/cmtlyt/lingshu-toolkit)
|
|
6
|
+
|
|
5
7
|
## 为什么要支持 shadcn?
|
|
6
8
|
|
|
7
9
|
如果是纯 shadcn 的话, 就会失去长期维护能力, 作者对包的后续修改也很难进行更新, 所以对于这个痛点, 我希望这个包能同时支持 shadcn 和 npm package
|
package/dist/607.js
CHANGED
|
@@ -1,131 +1,311 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
get (target, prop, receiver) {
|
|
9
|
-
const oldLog = Reflect.get(target, prop, receiver).bind(console);
|
|
10
|
-
return (...args)=>{
|
|
11
|
-
oldLog('[@cmtlyt/lingshu-toolkit]:', ...args);
|
|
1
|
+
import { dataHandler, logger, $dt, throwError, $t, throwType } from "./707.js";
|
|
2
|
+
function withResolvers() {
|
|
3
|
+
return 'function' == typeof Promise.withResolvers ? Promise.withResolvers() : (()=>{
|
|
4
|
+
const resolver = {
|
|
5
|
+
promise: null,
|
|
6
|
+
resolve: null,
|
|
7
|
+
reject: null
|
|
12
8
|
};
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
resolver.promise = new Promise((resolve, reject)=>{
|
|
10
|
+
resolver.resolve = resolve;
|
|
11
|
+
resolver.reject = reject;
|
|
12
|
+
});
|
|
13
|
+
return resolver;
|
|
14
|
+
})();
|
|
15
|
+
}
|
|
16
|
+
const noop = ()=>void 0;
|
|
17
|
+
const identity = (_v)=>_v;
|
|
15
18
|
function getType(_v) {
|
|
16
19
|
return Object.prototype.toString.call(_v).slice(8, -1).toLowerCase();
|
|
17
20
|
}
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
21
|
+
function getNextValueHandler(from, to, valueFormatter) {
|
|
22
|
+
const type = getType(from);
|
|
23
|
+
const context = {
|
|
24
|
+
progress: 0,
|
|
25
|
+
valueFormatter
|
|
26
|
+
};
|
|
27
|
+
const baseNextValue = (_from, _to)=>{
|
|
28
|
+
const { valueFormatter: formatter, progress } = context;
|
|
29
|
+
if ('number' !== getType(_from)) return _from;
|
|
30
|
+
return formatter(_from + (_to - _from) * progress);
|
|
31
|
+
};
|
|
32
|
+
const arrayHandler = (_from, _to)=>{
|
|
33
|
+
const result = Array.from(_from, (item, idx)=>{
|
|
34
|
+
if (Array.isArray(item)) return arrayHandler(item, _to[idx]);
|
|
35
|
+
if ('object' === getType(item)) return objectHandler(item, _to[idx]);
|
|
36
|
+
return baseNextValue(item, _to[idx]);
|
|
37
|
+
});
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
const objectHandler = (_from, _to)=>{
|
|
41
|
+
const result = {};
|
|
42
|
+
const keys = Reflect.ownKeys(_from);
|
|
43
|
+
for(let i = 0; i < keys.length; i++){
|
|
44
|
+
const key = keys[i];
|
|
45
|
+
const fromValue = _from[key];
|
|
46
|
+
const toValue = _to[key];
|
|
47
|
+
if (Array.isArray(_from[key])) result[key] = arrayHandler(fromValue, toValue);
|
|
48
|
+
else if ('object' === getType(fromValue)) result[key] = objectHandler(fromValue, toValue);
|
|
49
|
+
else result[key] = baseNextValue(fromValue, toValue);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
53
|
+
let nextValueHandler = baseNextValue;
|
|
54
|
+
if ('array' === type) nextValueHandler = arrayHandler;
|
|
55
|
+
else if ('object' === type) nextValueHandler = objectHandler;
|
|
56
|
+
return (progress)=>{
|
|
57
|
+
context.progress = progress;
|
|
58
|
+
return nextValueHandler(from, to);
|
|
59
|
+
};
|
|
26
60
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
continue;
|
|
61
|
+
function matchValid(from, to, valueParser) {
|
|
62
|
+
const fromType = getType(from);
|
|
63
|
+
const toType = getType(to);
|
|
64
|
+
if (fromType !== toType) throwType('animation/stepAnimation', 'from and to must be the same type');
|
|
65
|
+
if ('array' === fromType) {
|
|
66
|
+
if (from.length !== to.length) throwType('animation/stepAnimation', 'from and to must be the same length');
|
|
67
|
+
const result = [
|
|
68
|
+
Array.from({
|
|
69
|
+
length: from.length
|
|
70
|
+
}),
|
|
71
|
+
Array.from({
|
|
72
|
+
length: to.length
|
|
73
|
+
})
|
|
74
|
+
];
|
|
75
|
+
for(let i = 0; i < from.length; i++){
|
|
76
|
+
const [fromItem, toItem] = matchValid(from[i], to[i], valueParser);
|
|
77
|
+
result[0][i] = fromItem;
|
|
78
|
+
result[1][i] = toItem;
|
|
46
79
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
if ('object' === fromType) {
|
|
83
|
+
const toKeys = Reflect.ownKeys(to);
|
|
84
|
+
const fromKeys = new Set(Reflect.ownKeys(from));
|
|
85
|
+
const result = [
|
|
86
|
+
{},
|
|
87
|
+
{}
|
|
88
|
+
];
|
|
89
|
+
for(let i = 0; i < toKeys.length; i++){
|
|
90
|
+
const key = toKeys[i];
|
|
91
|
+
if (!fromKeys.has(key)) throwType('animation/stepAnimation', `from does not have this key: ${String(key)}`);
|
|
92
|
+
const [fromItem, toItem] = matchValid(from[key], to[key], valueParser);
|
|
93
|
+
result[0][key] = fromItem;
|
|
94
|
+
result[1][key] = toItem;
|
|
51
95
|
}
|
|
52
|
-
|
|
96
|
+
return result;
|
|
53
97
|
}
|
|
54
|
-
return
|
|
98
|
+
if ('number' !== fromType) return [
|
|
99
|
+
valueParser(from),
|
|
100
|
+
valueParser(to)
|
|
101
|
+
];
|
|
102
|
+
return [
|
|
103
|
+
from,
|
|
104
|
+
to
|
|
105
|
+
];
|
|
55
106
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
107
|
+
function createNextTick(resolvers, rcSignal) {
|
|
108
|
+
const nextTick = (()=>{
|
|
109
|
+
if ('function' == typeof globalThis.requestAnimationFrame) return globalThis.requestAnimationFrame;
|
|
110
|
+
return (callback)=>setTimeout(callback, 16);
|
|
111
|
+
})();
|
|
112
|
+
return (callback)=>{
|
|
113
|
+
if (rcSignal.stopSignal) return true;
|
|
114
|
+
nextTick(()=>tryRun(callback, resolvers, (error)=>{
|
|
115
|
+
rcSignal.stop();
|
|
116
|
+
resolvers.reject(error);
|
|
117
|
+
}));
|
|
118
|
+
return false;
|
|
62
119
|
};
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
120
|
+
}
|
|
121
|
+
async function tryRun(callback, resolvers, customErrorHandler) {
|
|
122
|
+
return new Promise((resolve, reject)=>{
|
|
123
|
+
const result = callback();
|
|
124
|
+
if (result && 'function' == typeof result.then) return result.then(resolve, reject);
|
|
125
|
+
resolve();
|
|
126
|
+
}).catch(customErrorHandler || resolvers.reject);
|
|
127
|
+
}
|
|
128
|
+
function createRunningControllerSignal(startFn, options) {
|
|
129
|
+
const { onStart, onStop, onClear } = options;
|
|
130
|
+
const resolvers = withResolvers();
|
|
131
|
+
const ctrl = {
|
|
132
|
+
stopSignal: true,
|
|
133
|
+
resolvers,
|
|
134
|
+
stop: ()=>{
|
|
135
|
+
ctrl.stopSignal = true;
|
|
136
|
+
onStop();
|
|
68
137
|
},
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
138
|
+
start: ()=>{
|
|
139
|
+
ctrl.stopSignal = false;
|
|
140
|
+
onStart();
|
|
141
|
+
startFn();
|
|
142
|
+
},
|
|
143
|
+
clear: ()=>{
|
|
144
|
+
ctrl.stopSignal = true;
|
|
145
|
+
onClear();
|
|
146
|
+
resolvers.resolve(true);
|
|
74
147
|
}
|
|
75
148
|
};
|
|
76
|
-
return
|
|
77
|
-
ctx,
|
|
78
|
-
handler,
|
|
79
|
-
(key)=>({
|
|
80
|
-
assert: (flag, msg = `${String(key)} is not valid`)=>{
|
|
81
|
-
if (!flag) handler.addError(key, msg);
|
|
82
|
-
return flag;
|
|
83
|
-
},
|
|
84
|
-
transform: (value)=>{
|
|
85
|
-
if (!ctx.handledErrorKeys.has(key)) handler.addTransform(key, value);
|
|
86
|
-
return value;
|
|
87
|
-
}
|
|
88
|
-
})
|
|
89
|
-
];
|
|
90
|
-
}
|
|
91
|
-
function transformApply(data, transforms) {
|
|
92
|
-
if (!transforms.length) return;
|
|
93
|
-
for(let i = 0, [key, value] = transforms[i]; i < transforms.length; [key, value] = transforms[++i] || [])data[key] = value;
|
|
149
|
+
return ctrl;
|
|
94
150
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
151
|
+
function* stepAnimation(from, to, step, options = {}) {
|
|
152
|
+
if (!Number.isInteger(step) || step <= 0) throwError('stepAnimation', 'step must be a positive integer', RangeError);
|
|
153
|
+
const { parser: valueParser = identity, formatter: valueFormatter = identity } = options;
|
|
154
|
+
const [validFrom, validTo] = matchValid(from, to, valueParser);
|
|
155
|
+
const getNextValue = getNextValueHandler(validFrom, validTo, valueFormatter);
|
|
156
|
+
for(let i = 0; i <= step; i++){
|
|
157
|
+
const value = getNextValue(i / step);
|
|
158
|
+
yield value;
|
|
99
159
|
}
|
|
100
160
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
161
|
+
const validInfo = $dt({
|
|
162
|
+
autoStart: $t.boolean(true),
|
|
163
|
+
easing: $t["function"](()=>identity),
|
|
164
|
+
onStart: $t["function"](()=>noop),
|
|
165
|
+
onStop: $t["function"](()=>noop),
|
|
166
|
+
onClear: $t["function"](()=>noop),
|
|
167
|
+
onUpdate: $t["function"](()=>noop),
|
|
168
|
+
onComplete: $t["function"](()=>noop),
|
|
169
|
+
formatter: $t["function"](()=>identity),
|
|
170
|
+
parser: $t["function"](()=>identity)
|
|
171
|
+
});
|
|
172
|
+
function animation(from, to, duration, options = {}) {
|
|
173
|
+
if (duration <= 0 || !Number.isInteger(duration)) throwError('animation', 'duration must be a positive integer', RangeError);
|
|
174
|
+
const validOptions = dataHandler(options, validInfo, {
|
|
175
|
+
unwrap: true
|
|
176
|
+
});
|
|
177
|
+
const [validFrom, validTo] = matchValid(from, to, validOptions.parser);
|
|
178
|
+
const { autoStart, easing, onComplete, onUpdate, formatter: valueFormatter } = validOptions;
|
|
179
|
+
const getNextValue = getNextValueHandler(validFrom, validTo, valueFormatter);
|
|
180
|
+
let startTime = 0;
|
|
181
|
+
let hasStarted = false;
|
|
182
|
+
const rcSignal = createRunningControllerSignal(()=>{
|
|
183
|
+
const now = performance.now();
|
|
184
|
+
startTime += now;
|
|
185
|
+
const stopFlag = nextTick(tick);
|
|
186
|
+
if (stopFlag) {
|
|
187
|
+
startTime -= performance.now();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (!hasStarted) {
|
|
191
|
+
onUpdate(getNextValue(0));
|
|
192
|
+
hasStarted = true;
|
|
193
|
+
}
|
|
194
|
+
}, validOptions);
|
|
195
|
+
const { resolvers } = rcSignal;
|
|
196
|
+
const nextTick = createNextTick(resolvers, rcSignal);
|
|
197
|
+
const tick = ()=>{
|
|
198
|
+
const elapsed = performance.now() - startTime;
|
|
199
|
+
const progress = easing(Math.min(elapsed / duration, 1));
|
|
200
|
+
const value = getNextValue(progress);
|
|
201
|
+
onUpdate(value);
|
|
202
|
+
if (elapsed < duration) {
|
|
203
|
+
const stopFlag = nextTick(tick);
|
|
204
|
+
if (stopFlag) startTime = -elapsed;
|
|
205
|
+
} else {
|
|
206
|
+
onComplete();
|
|
207
|
+
resolvers.resolve(false);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
if (false !== autoStart) rcSignal.start();
|
|
211
|
+
return {
|
|
212
|
+
promise: resolvers.promise,
|
|
213
|
+
stop: rcSignal.stop,
|
|
214
|
+
start: rcSignal.start,
|
|
215
|
+
clear: rcSignal.clear
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function getEmpty(_v) {
|
|
219
|
+
return Array.isArray(_v) ? [] : {};
|
|
220
|
+
}
|
|
221
|
+
function valueCheck(_v) {
|
|
222
|
+
return Array.isArray(_v) || 'object' == typeof _v;
|
|
105
223
|
}
|
|
106
|
-
function
|
|
107
|
-
|
|
108
|
-
|
|
224
|
+
function conditionMerge(...input) {
|
|
225
|
+
const conditionItems = (input.length > 1 ? input : input[0]).map((item)=>{
|
|
226
|
+
let result = null;
|
|
227
|
+
if (Array.isArray(item)) {
|
|
228
|
+
const [condition, value, fullback] = item;
|
|
229
|
+
result = {
|
|
230
|
+
condition,
|
|
231
|
+
value,
|
|
232
|
+
fullback
|
|
233
|
+
};
|
|
234
|
+
} else if (Object.getOwnPropertyDescriptor(item, 'condition')) result = item;
|
|
235
|
+
else throwType('conditionMerge', 'input must be an ConditionItem');
|
|
236
|
+
const validValue = valueCheck(result.value);
|
|
237
|
+
const validFullback = void 0 === result.fullback || valueCheck(result.fullback);
|
|
238
|
+
if (!(validValue && validFullback)) throwType('conditionMerge', 'value and fullback must be an array or object');
|
|
239
|
+
return result;
|
|
109
240
|
});
|
|
241
|
+
const result = getEmpty(conditionItems[0].value);
|
|
242
|
+
const mergeFn = Array.isArray(result) ? (a1, a2)=>Reflect.apply(Array.prototype.splice.bind(a1, a1.length, 0), null, a2) : Object.assign;
|
|
243
|
+
for(let i = 0, item = conditionItems[i]; i < conditionItems.length; item = conditionItems[++i])mergeFn(result, item.condition ? item.value : item.fullback || getEmpty(item.value));
|
|
244
|
+
return result;
|
|
110
245
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
246
|
+
const create_storage_handler_validInfo = $dt({
|
|
247
|
+
storageKey: 'validString',
|
|
248
|
+
storageType: $t["enum"]([
|
|
249
|
+
'local',
|
|
250
|
+
'session',
|
|
251
|
+
'memory'
|
|
252
|
+
], 'local'),
|
|
253
|
+
autoSaveInterval: $t.number(0)
|
|
254
|
+
});
|
|
255
|
+
const memoryStorage = {
|
|
256
|
+
data: {},
|
|
257
|
+
getItem (key) {
|
|
258
|
+
return this.data[key];
|
|
259
|
+
},
|
|
260
|
+
setItem (key, value) {
|
|
261
|
+
this.data[key] = value;
|
|
262
|
+
},
|
|
263
|
+
removeItem (key) {
|
|
264
|
+
delete this.data[key];
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
function getStorage(storageType) {
|
|
268
|
+
try {
|
|
269
|
+
if ('memory' === storageType) return memoryStorage;
|
|
270
|
+
return 'local' === storageType ? localStorage : sessionStorage;
|
|
271
|
+
} catch {
|
|
272
|
+
logger.warn('createStorage', 'Failed to access localStorage or sessionStorage, using memoryStorage instead.');
|
|
273
|
+
return memoryStorage;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const CLEAR_FLAG = Symbol('cleared');
|
|
277
|
+
function createStorageHandler(storageKey, initialData, options = {}) {
|
|
278
|
+
const { storageKey: validStorageKey, storageType, autoSaveInterval } = dataHandler({
|
|
279
|
+
storageKey,
|
|
280
|
+
...options
|
|
281
|
+
}, create_storage_handler_validInfo, {
|
|
282
|
+
unwrap: true
|
|
283
|
+
});
|
|
284
|
+
const storage = getStorage(storageType);
|
|
285
|
+
const storageData = storage.getItem(validStorageKey);
|
|
286
|
+
const context = {
|
|
287
|
+
data: storageData ? JSON.parse(storageData) : initialData || {}
|
|
119
288
|
};
|
|
120
|
-
const keys = handlerIsFunction ? Reflect.ownKeys(data) : Reflect.ownKeys(handler);
|
|
121
|
-
const [ctx, actionHandler, getActions] = createActions();
|
|
122
|
-
handleProcess(tempData, keys, handleFn, getActions, actionHandler);
|
|
123
|
-
errorProcess(ctx.errors, errorHandler, strict);
|
|
124
|
-
transformApply(tempData, ctx.transforms);
|
|
125
|
-
filterData(tempData, ctx, defaultValue);
|
|
126
289
|
return {
|
|
127
|
-
|
|
128
|
-
|
|
290
|
+
get (key) {
|
|
291
|
+
if (context.data === CLEAR_FLAG) throwError('createStorageHandler', 'Storage has been cleared.');
|
|
292
|
+
if (null == key) return context.data;
|
|
293
|
+
return context.data[key];
|
|
294
|
+
},
|
|
295
|
+
set (value, key) {
|
|
296
|
+
if (context.data === CLEAR_FLAG) throwError('createStorageHandler', 'Storage has been cleared.');
|
|
297
|
+
if (null == key) context.data = value;
|
|
298
|
+
else context.data[key] = value;
|
|
299
|
+
if (autoSaveInterval > 0) setTimeout(()=>{
|
|
300
|
+
storage.setItem(validStorageKey, JSON.stringify(context.data));
|
|
301
|
+
}, autoSaveInterval);
|
|
302
|
+
else storage.setItem(validStorageKey, JSON.stringify(context.data));
|
|
303
|
+
},
|
|
304
|
+
clear () {
|
|
305
|
+
context.data = CLEAR_FLAG;
|
|
306
|
+
storage.removeItem(validStorageKey);
|
|
307
|
+
}
|
|
129
308
|
};
|
|
130
309
|
}
|
|
131
|
-
|
|
310
|
+
Symbol('__PACK__');
|
|
311
|
+
export { animation, conditionMerge, createStorageHandler, stepAnimation, withResolvers };
|
package/dist/707.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
function throwError(fnName, message, ErrorClass = Error) {
|
|
2
|
+
throw new ErrorClass(`[@cmtlyt/lingshu-toolkit#${fnName}]: ${message}`);
|
|
3
|
+
}
|
|
4
|
+
function throwType(fnName, message) {
|
|
5
|
+
throwError(fnName, message, TypeError);
|
|
6
|
+
}
|
|
7
|
+
const logger = new Proxy(console, {
|
|
8
|
+
get (target, prop, receiver) {
|
|
9
|
+
const oldLog = Reflect.get(target, prop, receiver).bind(console);
|
|
10
|
+
return (fnName, ...args)=>{
|
|
11
|
+
oldLog(`[@cmtlyt/lingshu-toolkit#${fnName}]:`, ...args);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
function getType(_v) {
|
|
16
|
+
return Object.prototype.toString.call(_v).slice(8, -1).toLowerCase();
|
|
17
|
+
}
|
|
18
|
+
function typeHandler(type, verifyFn) {
|
|
19
|
+
return (fullback)=>(_v, actions)=>{
|
|
20
|
+
if (verifyFn ? verifyFn(_v) : getType(_v) === type) return true;
|
|
21
|
+
if (null == fullback) return false;
|
|
22
|
+
let fullbackValue = fullback;
|
|
23
|
+
if ('function' == typeof fullback) fullbackValue = fullback(_v);
|
|
24
|
+
actions.transform(fullbackValue);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const $t = {
|
|
28
|
+
notNullable: typeHandler('notNullable', (_v)=>null != _v),
|
|
29
|
+
string: typeHandler('string'),
|
|
30
|
+
validString: typeHandler('validString', (_v)=>'string' == typeof _v && _v.length > 0),
|
|
31
|
+
number: typeHandler('number'),
|
|
32
|
+
validNumber: typeHandler('validNumber', (_v)=>'number' == typeof _v && !Number.isNaN(_v)),
|
|
33
|
+
boolean: typeHandler('boolean'),
|
|
34
|
+
object: typeHandler('object'),
|
|
35
|
+
array: typeHandler('array'),
|
|
36
|
+
function: typeHandler('function'),
|
|
37
|
+
symbol: typeHandler('symbol'),
|
|
38
|
+
enum: (list, fullback)=>{
|
|
39
|
+
if (!Array.isArray(list)) throwType('$t.enum', 'list must be an array');
|
|
40
|
+
const set = new Set(list);
|
|
41
|
+
return typeHandler('enum', (_v)=>set.has(_v))(fullback);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
function defineTransform(dataInfo) {
|
|
45
|
+
const verifyInfo = {};
|
|
46
|
+
const keys = Reflect.ownKeys(dataInfo);
|
|
47
|
+
for(let i = 0, key = keys[i], item = dataInfo[key]; i < keys.length; key = keys[++i], item = dataInfo[key]){
|
|
48
|
+
if ('function' == typeof item) {
|
|
49
|
+
verifyInfo[key] = item;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const handler = $t[item];
|
|
53
|
+
if (!handler) {
|
|
54
|
+
logger.warn('defineTransform', `${item} is not a valid type`);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
verifyInfo[key] = handler();
|
|
58
|
+
}
|
|
59
|
+
return verifyInfo;
|
|
60
|
+
}
|
|
61
|
+
const $dt = defineTransform;
|
|
62
|
+
function createActions() {
|
|
63
|
+
const ctx = {
|
|
64
|
+
errors: [],
|
|
65
|
+
transforms: [],
|
|
66
|
+
handledErrorKeys: new Set()
|
|
67
|
+
};
|
|
68
|
+
const handler = {
|
|
69
|
+
addError (key, msg = `${String(key)} is not valid`) {
|
|
70
|
+
if (ctx.handledErrorKeys.has(key)) return;
|
|
71
|
+
ctx.handledErrorKeys.add(key);
|
|
72
|
+
ctx.errors.push(msg);
|
|
73
|
+
},
|
|
74
|
+
addTransform (key, value) {
|
|
75
|
+
ctx.transforms.push([
|
|
76
|
+
key,
|
|
77
|
+
value
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
return [
|
|
82
|
+
ctx,
|
|
83
|
+
handler,
|
|
84
|
+
(key)=>({
|
|
85
|
+
assert: (flag, msg = `${String(key)} is not valid`)=>{
|
|
86
|
+
if (!flag) handler.addError(key, msg);
|
|
87
|
+
return flag;
|
|
88
|
+
},
|
|
89
|
+
transform: (value)=>{
|
|
90
|
+
if (!ctx.handledErrorKeys.has(key)) handler.addTransform(key, value);
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
function transformApply(data, transforms) {
|
|
97
|
+
if (!transforms.length) return;
|
|
98
|
+
for(let i = 0, [key, value] = transforms[i]; i < transforms.length; [key, value] = transforms[++i] || [])data[key] = value;
|
|
99
|
+
}
|
|
100
|
+
function handleProcess(data, keys, handleFn, getActions, actionHandlers) {
|
|
101
|
+
for(let i = 0, key = keys[i]; i < keys.length; key = keys[++i]){
|
|
102
|
+
const flag = handleFn(data[key], key, getActions(key), data);
|
|
103
|
+
if (false === flag) actionHandlers.addError(key);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function errorProcess(errors, errorHandler, strict) {
|
|
107
|
+
if (!errors.length) return;
|
|
108
|
+
if (errorHandler) errorHandler(errors);
|
|
109
|
+
else if (strict) throwType('dataHandler', errors.join('\n'));
|
|
110
|
+
}
|
|
111
|
+
function filterData(data, ctx, defaultValue = {}) {
|
|
112
|
+
ctx.handledErrorKeys.forEach((key)=>{
|
|
113
|
+
data[key] = defaultValue[key];
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function dataHandler(data, handler, options) {
|
|
117
|
+
if (!handler) throwType('dataHandler', 'handler is required');
|
|
118
|
+
const { strict = false, errorHandler, defaultValue, unwrap = false } = options || {};
|
|
119
|
+
const handlerIsFunction = 'function' == typeof handler;
|
|
120
|
+
const handleFn = handlerIsFunction ? handler : (value, key, ...args)=>handler[key](value, ...args);
|
|
121
|
+
const tempData = {
|
|
122
|
+
...defaultValue,
|
|
123
|
+
...data
|
|
124
|
+
};
|
|
125
|
+
const keys = handlerIsFunction ? Reflect.ownKeys(data) : Reflect.ownKeys(handler);
|
|
126
|
+
const [ctx, actionHandler, getActions] = createActions();
|
|
127
|
+
handleProcess(tempData, keys, handleFn, getActions, actionHandler);
|
|
128
|
+
errorProcess(ctx.errors, errorHandler, strict);
|
|
129
|
+
transformApply(tempData, ctx.transforms);
|
|
130
|
+
filterData(tempData, ctx, defaultValue);
|
|
131
|
+
return unwrap ? tempData : {
|
|
132
|
+
result: tempData,
|
|
133
|
+
errors: ctx.errors
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
export { $dt, $t, dataHandler, defineTransform, logger, throwError, throwType };
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
export * from './use-boolean';
|
|
1
2
|
export * from './use-controllable-value';
|
|
2
3
|
export * from './use-counter';
|
|
4
|
+
export * from './use-force-update';
|
|
5
|
+
export * from './use-mount';
|
|
6
|
+
export * from './use-ref-state';
|
|
7
|
+
export * from './use-storage';
|
|
8
|
+
export * from './use-title';
|
|
9
|
+
export * from './use-toggle';
|
|
3
10
|
export * from './use-valid-data';
|