reaxes-utils 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.
@@ -0,0 +1,228 @@
1
+ export { stringify };
2
+
3
+ stringify.stable = deterministicStringify
4
+
5
+ var LIMIT_REPLACE_NODE = '[...]'
6
+ var CIRCULAR_REPLACE_NODE = '[Circular]'
7
+
8
+ var arr = []
9
+ var replacerStack = []
10
+
11
+ function defaultOptions () {
12
+ return {
13
+ depthLimit: Number.MAX_SAFE_INTEGER,
14
+ edgesLimit: Number.MAX_SAFE_INTEGER
15
+ }
16
+ }
17
+
18
+ // Regular stringify
19
+ function stringify (obj, replacer?, spacer?, options?) {
20
+ if (typeof options === 'undefined') {
21
+ options = defaultOptions()
22
+ }
23
+
24
+ decirc(obj, '', 0, [], undefined, 0, options)
25
+ var res
26
+ try {
27
+ if (replacerStack.length === 0) {
28
+ res = JSON.stringify(obj, replacer, spacer)
29
+ } else {
30
+ res = JSON.stringify(obj, replaceGetterValues(replacer), spacer)
31
+ }
32
+ } catch (_) {
33
+ return JSON.stringify('[unable to serialize, circular reference is too complex to analyze]')
34
+ } finally {
35
+ while (arr.length !== 0) {
36
+ var part = arr.pop()
37
+ if (part.length === 4) {
38
+ Object.defineProperty(part[0], part[1], part[3])
39
+ } else {
40
+ part[0][part[1]] = part[2]
41
+ }
42
+ }
43
+ }
44
+ return res
45
+ }
46
+
47
+ function setReplace (replace, val, k, parent) {
48
+ var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k)
49
+ if (propertyDescriptor.get !== undefined) {
50
+ if (propertyDescriptor.configurable) {
51
+ Object.defineProperty(parent, k, { value: replace })
52
+ arr.push([parent, k, val, propertyDescriptor])
53
+ } else {
54
+ replacerStack.push([val, k, replace])
55
+ }
56
+ } else {
57
+ parent[k] = replace
58
+ arr.push([parent, k, val])
59
+ }
60
+ }
61
+
62
+ function decirc (val, k, edgeIndex, stack, parent, depth, options) {
63
+ depth += 1
64
+ var i
65
+ if (typeof val === 'object' && val !== null) {
66
+ for (i = 0; i < stack.length; i++) {
67
+ if (stack[i] === val) {
68
+ setReplace(CIRCULAR_REPLACE_NODE, val, k, parent)
69
+ return
70
+ }
71
+ }
72
+
73
+ if (
74
+ typeof options.depthLimit !== 'undefined' &&
75
+ depth > options.depthLimit
76
+ ) {
77
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent)
78
+ return
79
+ }
80
+
81
+ if (
82
+ typeof options.edgesLimit !== 'undefined' &&
83
+ edgeIndex + 1 > options.edgesLimit
84
+ ) {
85
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent)
86
+ return
87
+ }
88
+
89
+ stack.push(val)
90
+ // Optimize for Arrays. Big arrays could kill the performance otherwise!
91
+ if (Array.isArray(val)) {
92
+ for (i = 0; i < val.length; i++) {
93
+ decirc(val[i], i, i, stack, val, depth, options)
94
+ }
95
+ } else {
96
+ var keys = Object.keys(val)
97
+ for (i = 0; i < keys.length; i++) {
98
+ var key = keys[i]
99
+ decirc(val[key], key, i, stack, val, depth, options)
100
+ }
101
+ }
102
+ stack.pop()
103
+ }
104
+ }
105
+
106
+ // Stable-stringify
107
+ function compareFunction (a, b) {
108
+ if (a < b) {
109
+ return -1
110
+ }
111
+ if (a > b) {
112
+ return 1
113
+ }
114
+ return 0
115
+ }
116
+
117
+ function deterministicStringify (obj, replacer, spacer, options) {
118
+ if (typeof options === 'undefined') {
119
+ options = defaultOptions()
120
+ }
121
+
122
+ var tmp = deterministicDecirc(obj, '', 0, [], undefined, 0, options) || obj
123
+ var res
124
+ try {
125
+ if (replacerStack.length === 0) {
126
+ res = JSON.stringify(tmp, replacer, spacer)
127
+ } else {
128
+ res = JSON.stringify(tmp, replaceGetterValues(replacer), spacer)
129
+ }
130
+ } catch (_) {
131
+ return JSON.stringify('[unable to serialize, circular reference is too complex to analyze]')
132
+ } finally {
133
+ // Ensure that we restore the object as it was.
134
+ while (arr.length !== 0) {
135
+ var part = arr.pop()
136
+ if (part.length === 4) {
137
+ Object.defineProperty(part[0], part[1], part[3])
138
+ } else {
139
+ part[0][part[1]] = part[2]
140
+ }
141
+ }
142
+ }
143
+ return res
144
+ }
145
+
146
+ function deterministicDecirc (val, k, edgeIndex, stack, parent, depth, options) {
147
+ depth += 1
148
+ var i
149
+ if (typeof val === 'object' && val !== null) {
150
+ for (i = 0; i < stack.length; i++) {
151
+ if (stack[i] === val) {
152
+ setReplace(CIRCULAR_REPLACE_NODE, val, k, parent)
153
+ return
154
+ }
155
+ }
156
+ try {
157
+ if (typeof val.toJSON === 'function') {
158
+ return
159
+ }
160
+ } catch (_) {
161
+ return
162
+ }
163
+
164
+ if (
165
+ typeof options.depthLimit !== 'undefined' &&
166
+ depth > options.depthLimit
167
+ ) {
168
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent)
169
+ return
170
+ }
171
+
172
+ if (
173
+ typeof options.edgesLimit !== 'undefined' &&
174
+ edgeIndex + 1 > options.edgesLimit
175
+ ) {
176
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent)
177
+ return
178
+ }
179
+
180
+ stack.push(val)
181
+ // Optimize for Arrays. Big arrays could kill the performance otherwise!
182
+ if (Array.isArray(val)) {
183
+ for (i = 0; i < val.length; i++) {
184
+ deterministicDecirc(val[i], i, i, stack, val, depth, options)
185
+ }
186
+ } else {
187
+ // Create a temporary object in the required way
188
+ var tmp = {}
189
+ var keys = Object.keys(val).sort(compareFunction)
190
+ for (i = 0; i < keys.length; i++) {
191
+ var key = keys[i]
192
+ deterministicDecirc(val[key], key, i, stack, val, depth, options)
193
+ tmp[key] = val[key]
194
+ }
195
+ if (typeof parent !== 'undefined') {
196
+ arr.push([parent, k, val])
197
+ parent[k] = tmp
198
+ } else {
199
+ return tmp
200
+ }
201
+ }
202
+ stack.pop()
203
+ }
204
+ }
205
+
206
+ // wraps replacer function to handle values we couldn't replace
207
+ // and mark them as replaced value
208
+ function replaceGetterValues (replacer) {
209
+ replacer =
210
+ typeof replacer !== 'undefined'
211
+ ? replacer
212
+ : function (k, v) {
213
+ return v
214
+ }
215
+ return function (key, val) {
216
+ if (replacerStack.length > 0) {
217
+ for (var i = 0; i < replacerStack.length; i++) {
218
+ var part = replacerStack[i]
219
+ if (part[1] === key && part[0] === val) {
220
+ val = part[2]
221
+ replacerStack.splice(i, 1)
222
+ break
223
+ }
224
+ }
225
+ }
226
+ return replacer.call(this, key, val)
227
+ }
228
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * @description 节流功能
3
+ * @param {function} fn 要进行防抖处理的function
4
+ * @param {number} wait 间隔时间 ms为单位
5
+ * @param {IOptions} options 配置 leading:false 表示禁用第一次执行 trailing: false 表示禁用停止触发的回调
6
+ * @return {function} 进行防抖处理后的函数
7
+ */
8
+ interface IOptions {
9
+ leading?: boolean;
10
+ trailing?: boolean;
11
+ }
12
+ export default function throttle(func, wait, options: IOptions) {
13
+ var timeout, context, args, result;
14
+ var previous = 0;
15
+ if (!options) options = {};
16
+
17
+ var later = function () {
18
+ previous = options.leading === false ? 0 : new Date().getTime();
19
+ timeout = null;
20
+ func.apply(context, args);
21
+ if (!timeout) context = args = null;
22
+ };
23
+
24
+ var throttled = function () {
25
+ var now = new Date().getTime();
26
+ if (!previous && options.leading === false) previous = now;
27
+ var remaining = wait - (now - previous);
28
+ context = this;
29
+ args = arguments;
30
+ if (remaining <= 0 || remaining > wait) {
31
+ if (timeout) {
32
+ clearTimeout(timeout);
33
+ timeout = null;
34
+ }
35
+ previous = now;
36
+ func.apply(context, args);
37
+ if (!timeout) context = args = null;
38
+ } else if (!timeout && options.trailing !== false) {
39
+ timeout = setTimeout(later, remaining);
40
+ }
41
+ };
42
+ return throttled;
43
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * 准确的计时器 , 原型方法可以被链式调用
3
+ *
4
+ * @format
5
+ */
6
+
7
+ export const Timer = class {
8
+ /*订阅函数列表*/
9
+ #subscribeList = [];
10
+ /*当前剩余毫秒数*/
11
+ #currentTime = 0;
12
+ /*计时器函数*/
13
+ #interval;
14
+ /*当前正在执行的settimeout clearID 用于停止计时器*/
15
+ #currentTimeout;
16
+ /*标记是否正在计时*/
17
+ #status = false;
18
+ /**
19
+ * @param remaining {number}
20
+ */
21
+ constructor() {
22
+ const interval = () => {
23
+ this.#status = true;
24
+ if (this.#currentTime >= 1000) {
25
+ this.#currentTimeout = setTimeout(() => {
26
+ this.#currentTime -= 1000;
27
+ interval();
28
+ this.#subscribeList.forEach(fn => {
29
+ try {
30
+ fn(this.#currentTime);
31
+ } catch (e) {
32
+ console.error(e, `Timer 订阅运行时错误: 当前执行函数名>${fn.name}`);
33
+ }
34
+ });
35
+ }, 1000);
36
+ } else {
37
+ /*当剩余时间小于1000 */
38
+ this.#currentTimeout = setTimeout(() => {
39
+ this.#currentTime = 0;
40
+ this.#subscribeList.forEach(fn => {
41
+ try {
42
+ fn(this.#currentTime);
43
+ } catch (e) {
44
+ console.error(e, `Timer 订阅运行时错误: 当前执行函数名>${fn.name}`);
45
+ }
46
+ });
47
+ }, this.#currentTime);
48
+ this.#status = false;
49
+ }
50
+ /*只要倒计时开始了就直接执行一次订阅*/
51
+ this.#subscribeList.forEach(fn => {
52
+ try {
53
+ fn(this.#currentTime);
54
+ } catch (e) {
55
+ console.error(e, `Timer 订阅运行时错误: 当前执行函数名>${fn.name}`);
56
+ }
57
+ });
58
+ };
59
+ this.#interval = interval;
60
+ }
61
+ start = time => {
62
+ if (time <= 0) {
63
+ return console.error('timer.start值是负数! :>', time);
64
+ }
65
+ /*如果已经在计时中,则start无效*/
66
+ if (this.#status === true) {
67
+ return;
68
+ }
69
+ this.#currentTime = time;
70
+ this.#interval();
71
+ return this;
72
+ };
73
+
74
+ /**
75
+ * 订阅的每过1s就执行一次函数[相同引用的函数只会订阅一次]
76
+ * @param callback {function(number):*}
77
+ */
78
+ subscribe(callback) {
79
+ !this.#subscribeList.includes(callback) && this.#subscribeList.push(callback);
80
+ return this;
81
+ }
82
+ unsubscribe(callback) {
83
+ this.#subscribeList.splice(this.#subscribeList.indexOf(callback), 1);
84
+ return this;
85
+ }
86
+ destroy() {
87
+ //todo
88
+ }
89
+ stop() {
90
+ clearTimeout(this.#currentTimeout);
91
+ this.#currentTime = 0;
92
+ this.#status = false;
93
+ return this;
94
+ }
95
+ };
96
+
97
+ /*@TEST CODE*/
98
+ /*
99
+ t = new Timer();
100
+ console.time('foo');
101
+ t.subscribe((time) => {
102
+ console.log( time );
103
+ }).start(1800);*/
@@ -0,0 +1,59 @@
1
+ /**
2
+ * 强制保留2位小数
3
+ *
4
+ * @format
5
+ * @param x
6
+ * @returns {string|boolean}
7
+ */
8
+
9
+ export const toDecimal2 = x => {
10
+ let f = parseFloat(x);
11
+ if (isNaN(f)) {
12
+ return false;
13
+ }
14
+ f = Math.round(x * 100) / 100;
15
+ let s = f.toString();
16
+ let rs = s.indexOf('.');
17
+ if (rs < 0) {
18
+ rs = s.length;
19
+ s += '.';
20
+ }
21
+ while (s.length <= rs + 2) {
22
+ s += '0';
23
+ }
24
+ return s;
25
+ };
26
+
27
+ /**
28
+ * 分转元
29
+ * @param fen
30
+ * @returns {string | boolean}
31
+ */
32
+ export const fen2yuan = fen => {
33
+ let num = fen;
34
+ num = fen * 0.01;
35
+ num += '';
36
+ const reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g;
37
+ num = num.replace(reg, '$1');
38
+ num = toDecimal2(num);
39
+ return num;
40
+ };
41
+
42
+ /**
43
+ * 元转分
44
+ * @param yuan
45
+ * @param digit
46
+ * @returns {number}
47
+ */
48
+ export const yuan2fen = (yuan, digit = 100) => {
49
+ let m = 0,
50
+ s1 = yuan.toString(),
51
+ s2 = digit.toString();
52
+ try {
53
+ m += s1.split('.')[1].length;
54
+ } catch (e) {}
55
+ try {
56
+ m += s2.split('.')[1].length;
57
+ } catch (e) {}
58
+ return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
59
+ };
@@ -0,0 +1,43 @@
1
+ // @ts-nocheck
2
+ /*global provider*/
3
+ declare const _ : typeof import('lodash');
4
+ declare const React : typeof import('react');
5
+ declare const useState : typeof React.useState;
6
+ declare const useEffect : typeof React.useEffect;
7
+ declare const useRef : typeof React.useRef;
8
+ declare const useLayoutEffect : typeof React.useLayoutEffect;
9
+ declare const useMemo : typeof React.useMemo;
10
+ declare const useCallback : typeof React.useCallback;
11
+ declare const utils : typeof import('#utils');
12
+
13
+
14
+ declare const __IS_MOCK__: boolean;
15
+ declare const __EXPERIMENTAL__: boolean;
16
+ declare const __METHOD__: "server"|"build";
17
+ declare const __NODE_ENV__ : "development"|"production";
18
+ /*DOM*/
19
+ /*CSS*/
20
+ declare module '*.module.less' {
21
+ const classes : {
22
+ readonly [ key: string ]: string;
23
+ };
24
+
25
+ export default classes;
26
+ }
27
+ declare module '*.theme.less' {
28
+ const theme : string;
29
+ export default theme;
30
+ }
31
+
32
+ /*获取数组泛型参数*/
33
+ declare type ArrayElement<ArrayType extends any[]> = ArrayType extends (infer P)[] ? P : never;
34
+ declare interface NodeModule {
35
+ hot?: {
36
+ accept: Function;
37
+ };
38
+ }
39
+
40
+
41
+
42
+ /*todo 后续放入requester插件*/
43
+ declare type PayloadBody<T> = () => Promise<T>;
package/tsconfig.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "include" : [
3
+ "src"
4
+ ] ,
5
+ "compilerOptions" : {
6
+ "allowJs" : true ,
7
+ "skipLibCheck" : true ,
8
+ "allowSyntheticDefaultImports" : true ,
9
+ "allowUnusedLabels" : true ,
10
+ "baseUrl" : "." ,
11
+ "declaration" : true ,
12
+ "esModuleInterop" : true ,
13
+ "experimentalDecorators" : true ,
14
+ "jsx" : "react-jsx" ,
15
+ "resolveJsonModule" : true ,
16
+ "outDir" : "dist" ,
17
+ "strictFunctionTypes" : false ,
18
+ "strict" : false ,
19
+ "strictBindCallApply" : false ,
20
+ "strictNullChecks" : false ,
21
+ "allowUnreachableCode" : true ,
22
+ "module" : "ESNext" ,
23
+ "moduleResolution" : "node" ,
24
+ "emitDeclarationOnly" : true ,
25
+ "noImplicitAny" : false ,
26
+ "noImplicitReturns" : false ,
27
+ "noImplicitUseStrict" : false ,
28
+ "noUnusedLocals" : false ,
29
+ "noUnusedParameters" : false ,
30
+ "paths" : {
31
+ "@@root/*" : ["./*"],
32
+ "@@packages/*" : ["../*"],
33
+ "@@utils/*" : [
34
+ "src/*"
35
+ ] ,
36
+ } ,
37
+ "preserveConstEnums" : true ,
38
+ "removeComments" : true ,
39
+ "target" : "ES2020" ,
40
+ "allowSyntheticDefaultImports" : true ,
41
+ "lib" : [
42
+ "ES5" ,
43
+ "ES6" ,
44
+ "ES2015" ,
45
+ "ES7" ,
46
+ "ES2016" ,
47
+ "ES2017" ,
48
+ "ES2018" ,
49
+ "ESNext" ,
50
+ "DOM.Iterable" ,
51
+ "WebWorker" ,
52
+ "ScriptHost" ,
53
+ "ES2015.Core" ,
54
+ "ES2015.Collection" ,
55
+ "ES2015.Generator" ,
56
+ "ES2015.Iterable" ,
57
+ "ES2015.Promise" ,
58
+ "ES2015.Proxy" ,
59
+ "ES2015.Reflect" ,
60
+ "ES2015.Symbol" ,
61
+ "ES2015.Symbol.WellKnown" ,
62
+ "ES2016.Array.Include" ,
63
+ "ES2017.object" ,
64
+ "ES2017.Intl" ,
65
+ "ES2017.SharedMemory" ,
66
+ "ES2017.String" ,
67
+ "ES2017.TypedArrays" ,
68
+ "ES2018.Intl" ,
69
+ "ES2018.Promise" ,
70
+ "ES2018.RegExp" ,
71
+ "ESNext.AsyncIterable" ,
72
+ "ESNext.Array" ,
73
+ "ESNext.Intl" ,
74
+ "ESNext.Symbol" ,
75
+ "DOM"
76
+ ]
77
+ } ,
78
+ "exclude" : [
79
+ "node_modules" ,
80
+ "node_modules/*" ,
81
+ "node_modules/**" ,
82
+ "**/node_modules/**" ,
83
+ "packages/raexes-real-test",
84
+ "npm" ,
85
+ ".git" ,
86
+ ".idea" ,
87
+ ".svn"
88
+ ]
89
+ }