ddan-js 2.9.4 → 2.9.6

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.
Files changed (131) hide show
  1. package/bin/types/browser.d.ts +1 -1
  2. package/bin/types/index.d.ts +1 -1
  3. package/bin/types/modules/node/aliyun.d.ts +1 -1
  4. package/bin/types/upload.d.ts +2 -0
  5. package/bin/upload.js +1 -0
  6. package/package.json +18 -13
  7. package/bin/lib/browser.js +0 -111
  8. package/bin/lib/class/event.js +0 -104
  9. package/bin/lib/class/getset.js +0 -12
  10. package/bin/lib/class/icon.js +0 -94
  11. package/bin/lib/class/joker.js +0 -121
  12. package/bin/lib/class/kvalue.js +0 -29
  13. package/bin/lib/class/mapping.js +0 -82
  14. package/bin/lib/class/persist.js +0 -65
  15. package/bin/lib/class/pipeTask.js +0 -42
  16. package/bin/lib/class/pipeparallel.js +0 -24
  17. package/bin/lib/class/store.js +0 -87
  18. package/bin/lib/class/tracker.js +0 -125
  19. package/bin/lib/class/watermark.js +0 -63
  20. package/bin/lib/common/_Symbol.js +0 -5
  21. package/bin/lib/common/_freeGlobal.js +0 -5
  22. package/bin/lib/common/_object.js +0 -40
  23. package/bin/lib/common/_root.js +0 -10
  24. package/bin/lib/common/internal/asciiToArray.js +0 -13
  25. package/bin/lib/common/internal/castSlice.js +0 -18
  26. package/bin/lib/common/internal/createCaseFirst.js +0 -30
  27. package/bin/lib/common/internal/hasUnicode.js +0 -26
  28. package/bin/lib/common/internal/nodeTypes.js +0 -26
  29. package/bin/lib/common/internal/slice.js +0 -46
  30. package/bin/lib/common/internal/stringToArray.js +0 -18
  31. package/bin/lib/common/internal/unicodeToArray.js +0 -40
  32. package/bin/lib/common/internal/unicodeWords.js +0 -67
  33. package/bin/lib/index.js +0 -114
  34. package/bin/lib/modules/browser/ecdh.js +0 -124
  35. package/bin/lib/modules/browser/index.js +0 -4
  36. package/bin/lib/modules/cdn.js +0 -62
  37. package/bin/lib/modules/convert/base.js +0 -125
  38. package/bin/lib/modules/convert/base64.js +0 -114
  39. package/bin/lib/modules/convert/index.js +0 -99
  40. package/bin/lib/modules/convert/md5.js +0 -222
  41. package/bin/lib/modules/convert/pkcs.js +0 -16
  42. package/bin/lib/modules/convert/utf8.js +0 -106
  43. package/bin/lib/modules/crypto/base64.js +0 -14
  44. package/bin/lib/modules/crypto/const.js +0 -7
  45. package/bin/lib/modules/crypto/index.js +0 -30
  46. package/bin/lib/modules/crypto/rsa.js +0 -143
  47. package/bin/lib/modules/crypto/tea.js +0 -79
  48. package/bin/lib/modules/crypto/uuid.js +0 -34
  49. package/bin/lib/modules/css.js +0 -137
  50. package/bin/lib/modules/decorator.js +0 -14
  51. package/bin/lib/modules/fetch.js +0 -82
  52. package/bin/lib/modules/gbk.js +0 -46
  53. package/bin/lib/modules/hook/base.js +0 -60
  54. package/bin/lib/modules/hook/hooker.js +0 -39
  55. package/bin/lib/modules/hook/index.js +0 -49
  56. package/bin/lib/modules/hook/log.js +0 -54
  57. package/bin/lib/modules/hook/modules/bezier.js +0 -121
  58. package/bin/lib/modules/hook/modules/debounce.js +0 -127
  59. package/bin/lib/modules/hook/modules/mutex.js +0 -22
  60. package/bin/lib/modules/hook/modules/pipeline.js +0 -59
  61. package/bin/lib/modules/hook/modules/polling.js +0 -20
  62. package/bin/lib/modules/hook/modules/safeTask.js +0 -25
  63. package/bin/lib/modules/hook/modules/throttle.js +0 -30
  64. package/bin/lib/modules/html.js +0 -293
  65. package/bin/lib/modules/http/index.js +0 -171
  66. package/bin/lib/modules/http/interceptor.js +0 -22
  67. package/bin/lib/modules/list/base.js +0 -37
  68. package/bin/lib/modules/list/index.js +0 -90
  69. package/bin/lib/modules/logger.js +0 -55
  70. package/bin/lib/modules/math/index.js +0 -118
  71. package/bin/lib/modules/mini/diff.js +0 -97
  72. package/bin/lib/modules/mini/http/index.js +0 -155
  73. package/bin/lib/modules/mini/index.js +0 -71
  74. package/bin/lib/modules/node/aliyun.js +0 -156
  75. package/bin/lib/modules/node/brotli.js +0 -46
  76. package/bin/lib/modules/node/child.js +0 -18
  77. package/bin/lib/modules/node/ecdh-web.js +0 -11
  78. package/bin/lib/modules/node/ecdh.js +0 -115
  79. package/bin/lib/modules/node/file.js +0 -25
  80. package/bin/lib/modules/node/index.js +0 -11
  81. package/bin/lib/modules/node/proxy.js +0 -147
  82. package/bin/lib/modules/node/rsa-web.js +0 -19
  83. package/bin/lib/modules/node/socks5.js +0 -424
  84. package/bin/lib/modules/obj/index.js +0 -184
  85. package/bin/lib/modules/qs/base.js +0 -5
  86. package/bin/lib/modules/qs/decode.js +0 -45
  87. package/bin/lib/modules/qs/encode.js +0 -53
  88. package/bin/lib/modules/qs/index.js +0 -152
  89. package/bin/lib/modules/regex.js +0 -16
  90. package/bin/lib/modules/rsa.js +0 -18
  91. package/bin/lib/modules/rule/async-validator/index.js +0 -306
  92. package/bin/lib/modules/rule/async-validator/interface.js +0 -2
  93. package/bin/lib/modules/rule/async-validator/messages.js +0 -59
  94. package/bin/lib/modules/rule/async-validator/rule/enum.js +0 -13
  95. package/bin/lib/modules/rule/async-validator/rule/index.js +0 -16
  96. package/bin/lib/modules/rule/async-validator/rule/pattern.js +0 -27
  97. package/bin/lib/modules/rule/async-validator/rule/range.js +0 -54
  98. package/bin/lib/modules/rule/async-validator/rule/required.js +0 -12
  99. package/bin/lib/modules/rule/async-validator/rule/type.js +0 -98
  100. package/bin/lib/modules/rule/async-validator/rule/url.js +0 -55
  101. package/bin/lib/modules/rule/async-validator/rule/whitespace.js +0 -20
  102. package/bin/lib/modules/rule/async-validator/util.js +0 -255
  103. package/bin/lib/modules/rule/async-validator/validator/any.js +0 -16
  104. package/bin/lib/modules/rule/async-validator/validator/array.js +0 -19
  105. package/bin/lib/modules/rule/async-validator/validator/boolean.js +0 -20
  106. package/bin/lib/modules/rule/async-validator/validator/date.js +0 -31
  107. package/bin/lib/modules/rule/async-validator/validator/enum.js +0 -20
  108. package/bin/lib/modules/rule/async-validator/validator/float.js +0 -20
  109. package/bin/lib/modules/rule/async-validator/validator/index.js +0 -36
  110. package/bin/lib/modules/rule/async-validator/validator/integer.js +0 -20
  111. package/bin/lib/modules/rule/async-validator/validator/method.js +0 -19
  112. package/bin/lib/modules/rule/async-validator/validator/number.js +0 -23
  113. package/bin/lib/modules/rule/async-validator/validator/object.js +0 -19
  114. package/bin/lib/modules/rule/async-validator/validator/pattern.js +0 -19
  115. package/bin/lib/modules/rule/async-validator/validator/regexp.js +0 -19
  116. package/bin/lib/modules/rule/async-validator/validator/required.js +0 -10
  117. package/bin/lib/modules/rule/async-validator/validator/string.js +0 -24
  118. package/bin/lib/modules/rule/async-validator/validator/type.js +0 -20
  119. package/bin/lib/modules/rule/index.js +0 -67
  120. package/bin/lib/modules/string/index.js +0 -121
  121. package/bin/lib/modules/string/words.js +0 -33
  122. package/bin/lib/modules/time/const.js +0 -11
  123. package/bin/lib/modules/time/dtime.js +0 -132
  124. package/bin/lib/modules/time/frame.js +0 -49
  125. package/bin/lib/modules/time/index.js +0 -84
  126. package/bin/lib/typings/index.js +0 -2
  127. package/bin/lib/util/function.js +0 -33
  128. package/bin/lib/util/includes.js +0 -96
  129. package/bin/lib/util/index.js +0 -26
  130. package/bin/lib/util/is.js +0 -148
  131. package/bin/lib/util/to.js +0 -47
@@ -1,171 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const interceptor_1 = require("./interceptor");
4
- const util_1 = require("../../util");
5
- const qs_1 = require("../qs");
6
- const request = ({ url, options }) => {
7
- const headers = {
8
- "Content-Type": "application/json; charset=utf-8",
9
- ...(options.headers || {})
10
- };
11
- options.headers = headers;
12
- return new Promise((resolve, reject) => {
13
- fetch(url, options)
14
- .then(result => {
15
- resolve(result.json());
16
- })
17
- .catch((error) => {
18
- reject(error);
19
- });
20
- });
21
- };
22
- function handleResponse(result) {
23
- const { response } = result;
24
- return response.code === 0
25
- ? Promise.resolve(response)
26
- : Promise.reject(response);
27
- }
28
- function handleDefaultRequest(httpRequest) {
29
- const { url = "", host, options } = httpRequest;
30
- const result = httpRequest;
31
- if (!url.startsWith("http://") && !url.startsWith("https://")) {
32
- result.url = (host || "") + url;
33
- }
34
- if (options.method.toUpperCase() === "GET" &&
35
- options.body) {
36
- const data = JSON.parse(options.body);
37
- if (util_1.default.isPlainObject(data)) {
38
- // const query = Object.keys(data)
39
- // .map(
40
- // (key) => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`
41
- // )
42
- // .join("&");
43
- const query = qs_1.default.stringify(data);
44
- const symbol = url.indexOf("?") === -1 ? "?" : "&";
45
- result.url = `${result.url || ""}${symbol}${query}`;
46
- delete result.options.body;
47
- }
48
- }
49
- if (options && options.plugins) {
50
- delete result.options?.plugins;
51
- }
52
- if (options && options.model) {
53
- delete result.options?.model;
54
- }
55
- return result;
56
- }
57
- class Http {
58
- host = "";
59
- reqConfig = {};
60
- handleError;
61
- interceptors = {
62
- request: new interceptor_1.default(),
63
- response: new interceptor_1.default(),
64
- };
65
- constructor(_) {
66
- this.host = _.host || "";
67
- this.reqConfig = _.config;
68
- this.interceptors.request.use(_.handleRequest || []);
69
- this.interceptors.request.use(handleDefaultRequest);
70
- this.interceptors.response.use(_.handleResponse || handleResponse);
71
- this.handleError = _.handleError;
72
- }
73
- // 阻塞请求的锁id
74
- static blockId = "blockid";
75
- // 请求锁字典
76
- static requestLock = {};
77
- /**
78
- * 加锁
79
- * @param {string} id
80
- * @param {function} func
81
- */
82
- lockR(id, func) {
83
- Http.requestLock[id] = {
84
- lock: true,
85
- request: func(),
86
- };
87
- }
88
- /**
89
- * 解锁
90
- * @param {string} id
91
- */
92
- unlockR(id) {
93
- delete Http.requestLock[id];
94
- }
95
- /**
96
- * 根据URL和请求参数构造唯一标识符
97
- * @param {object} req
98
- */
99
- getIdFromReq(req) {
100
- const { url, data = {} } = req;
101
- return url + JSON.stringify(data);
102
- }
103
- /**
104
- * 根据URL构造唯一标识符
105
- * @param {object} req
106
- */
107
- getIdFromReqUrl(req) {
108
- return req.url || "";
109
- }
110
- async fetch(name, payload) {
111
- const _options = util_1.default.copy(this.reqConfig[name]) || {};
112
- const config = {
113
- host: this.host,
114
- url: _options.url || "",
115
- options: {
116
- ...(_options || {}),
117
- body: JSON.stringify({
118
- ...(payload || {}),
119
- }),
120
- }
121
- };
122
- try {
123
- const reqOptions = await this.interceptors.request.invoke(config);
124
- delete reqOptions.options.plugins;
125
- let result;
126
- // 处理阻塞/去抖/普通请求
127
- if (_options.model === "block") {
128
- this.lockR(Http.blockId, () => request(reqOptions));
129
- result = await Http.requestLock[Http.blockId].request;
130
- // note: 在具体的回调函数中主动 unlock
131
- }
132
- else {
133
- if (Http.requestLock[Http.blockId] &&
134
- Http.requestLock[Http.blockId].lock) {
135
- await Http.requestLock[Http.blockId].request;
136
- }
137
- //去抖模式
138
- if (_options.model === "debounce" || _options.model === "debounceUrl") {
139
- let id;
140
- if (_options.model === "debounceUrl") {
141
- // 仅针对url去抖的临时方案
142
- id = this.getIdFromReqUrl(reqOptions);
143
- }
144
- else {
145
- id = this.getIdFromReq(reqOptions);
146
- }
147
- if (!Http.requestLock[id]) {
148
- this.lockR(id, () => request(reqOptions));
149
- }
150
- result = await Http.requestLock[id].request;
151
- this.unlockR(id);
152
- }
153
- else {
154
- result = await request(reqOptions);
155
- }
156
- }
157
- return this.interceptors.response.invoke({
158
- response: result,
159
- config: _options,
160
- refetch: () => {
161
- return { sig: "REFETCH", result: this.fetch(name, payload) };
162
- },
163
- });
164
- }
165
- catch (error) {
166
- this.handleError && this.handleError(error);
167
- return Promise.reject(error);
168
- }
169
- }
170
- }
171
- exports.default = Http;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class Interceptor {
4
- list = [];
5
- constructor() {
6
- this.invoke = this.invoke.bind(this);
7
- }
8
- use(func) {
9
- this.list = this.list.concat(func);
10
- }
11
- invoke(result) {
12
- return this.list.reduce((acc, func) => acc.then((res) => func(res)), Promise.resolve(result)).catch((res) => {
13
- if (res.sig === "REFETCH") {
14
- return Promise.resolve(res.result);
15
- }
16
- else {
17
- return Promise.reject(res);
18
- }
19
- });
20
- }
21
- }
22
- exports.default = Interceptor;
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /**
4
- * 分组
5
- * @param list 列表
6
- * @param key 关键字
7
- * @returns
8
- */
9
- function groupBy(list, key) {
10
- return list.reduce((prev, current) => {
11
- (prev[current[key]] = prev[current[key]] || []).push(current);
12
- return prev;
13
- }, {});
14
- }
15
- ;
16
- function first(list) {
17
- if (!list)
18
- return undefined;
19
- const len = list.length;
20
- if (len <= 0)
21
- return undefined;
22
- return list[0];
23
- }
24
- function last(list) {
25
- if (!list)
26
- return undefined;
27
- const len = list.length;
28
- if (len <= 0)
29
- return undefined;
30
- return list[len - 1];
31
- }
32
- function toList(val) {
33
- if (Array.isArray(val))
34
- return val;
35
- return val ? [val] : [];
36
- }
37
- exports.default = { groupBy, first, last, toList };
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const is_1 = require("../../util/is");
4
- const math_1 = require("../math");
5
- const base_1 = require("./base");
6
- /**
7
- * 分步动作
8
- * @param list 列表
9
- * @param func 动作function
10
- * @param stepCount 每步数量
11
- * @returns
12
- */
13
- function stepAction(list, func, stepCount = 100) {
14
- if (!is_1.default.isFunction(func))
15
- return;
16
- if (!is_1.default.isArray(list) || list.length <= 0)
17
- return;
18
- let start = 0;
19
- while (list.length > start) {
20
- let endIndex = stepCount + start;
21
- let arr = list.slice(start, endIndex);
22
- func && func(arr);
23
- start = endIndex;
24
- }
25
- }
26
- /**
27
- * 跳过数据
28
- * @param list 列表
29
- * @param count 跳过数量
30
- * @returns
31
- */
32
- function skip(list, count) {
33
- if (!is_1.default.isArray(list) || list.length <= 0)
34
- return [];
35
- const start = count;
36
- return list.slice(start);
37
- }
38
- ;
39
- /**
40
- * 提取数据
41
- * @param list 列表
42
- * @param count 提取数量
43
- * @param skip 跳过数量
44
- * @returns
45
- */
46
- function take(list, count, skip = 0) {
47
- if (!is_1.default.isArray(list) || list.length <= 0)
48
- return [];
49
- const start = skip || 0;
50
- if (start >= list.length)
51
- return [];
52
- const endIndex = count + start;
53
- return list.slice(start, list.length < endIndex ? list.length : endIndex) || [];
54
- }
55
- ;
56
- /**
57
- * 去重
58
- * @param list
59
- * @returns
60
- */
61
- function distinct(list) {
62
- if (!is_1.default.isArray(list) || list.length <= 0)
63
- return [];
64
- return [...new Set(list)];
65
- }
66
- ;
67
- function randoms(list, count = 1, repeat = false) {
68
- const max = list.length || 0;
69
- const arr = math_1.default.randoms(max, count, repeat);
70
- return arr.map(e => list[e]) || [];
71
- }
72
- ;
73
- const toKV = (list, key, value) => {
74
- const result = {};
75
- if (!key || !value)
76
- return result;
77
- (list || []).forEach(ele => {
78
- if (!ele || !is_1.default.isObject(ele))
79
- return;
80
- const _key = ele[key];
81
- if (is_1.default.isString(_key) && _key !== "") {
82
- result[_key] = ele[value];
83
- }
84
- else if (is_1.default.isNumber(_key)) {
85
- result[_key] = ele[value];
86
- }
87
- });
88
- return result;
89
- };
90
- exports.default = { ...base_1.default, stepAction, skip, take, distinct, randoms, toKV };
@@ -1,55 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const time_1 = require("./time");
4
- const __config = {
5
- overrode: false,
6
- tag: false,
7
- format: 'hh:mm:ss.SSS'
8
- };
9
- const override = () => {
10
- if (__config.overrode)
11
- return;
12
- __config.overrode = true;
13
- const rewriteFunctions = ['log', 'info', 'warn', 'error'];
14
- rewriteFunctions.forEach((key) => {
15
- const rewrite = console[key];
16
- console[key] = (...args) => {
17
- _apply(rewrite, key, ...args);
18
- };
19
- });
20
- };
21
- const config = ({ tag = __config.tag, format = __config.format } = {}) => {
22
- __config.tag = tag;
23
- __config.format = format;
24
- };
25
- const _override = (key) => {
26
- return (...args) => {
27
- const _func = console[key];
28
- if (__config.overrode) {
29
- _func(...args);
30
- return;
31
- }
32
- _apply(_func, key, ...args);
33
- };
34
- };
35
- const _apply = (handler, key, ...args) => {
36
- const tDesc = time_1.default.format(new Date(), __config.format);
37
- const kDesc = __config.tag ? `[${key}]` : '';
38
- if (kDesc && tDesc) {
39
- handler.apply(console, [`${tDesc} ${kDesc}`, ...args]);
40
- }
41
- else if (tDesc) {
42
- handler.apply(console, [`${tDesc}`, ...args]);
43
- }
44
- else if (kDesc) {
45
- handler.apply(console, [`${kDesc}`, ...args]);
46
- }
47
- else {
48
- handler.apply(console, [...args]);
49
- }
50
- };
51
- const log = _override("log");
52
- const info = _override("info");
53
- const warn = _override("warn");
54
- const error = _override("error");
55
- exports.default = { override, log, info, warn, error, config };
@@ -1,118 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /**
4
- * 随机取值[0, max)
5
- * @param max 最大值
6
- */
7
- function random(max) {
8
- return Math.floor(Math.random() * max);
9
- }
10
- function randoms(max, count = 1, repeat = false) {
11
- const result = [];
12
- do {
13
- if (result.length >= count)
14
- break;
15
- if (!repeat && result.length >= max)
16
- break;
17
- const idx = random(max);
18
- if (!repeat) {
19
- if (!result.includes(idx)) {
20
- result.push(idx);
21
- }
22
- }
23
- else {
24
- result.push(idx);
25
- }
26
- } while (true);
27
- return result;
28
- }
29
- /**
30
- * 范围随机取值[min, max]
31
- * @param min 最小值
32
- * @param max 最大值
33
- */
34
- function randomRange(min, max) {
35
- return Math.floor(Math.random() * (max - min + 1) + min);
36
- }
37
- /**
38
- * 插值
39
- * @param start
40
- * @param end
41
- * @param t
42
- */
43
- function lerp(start, end, t) {
44
- return start * (1 - t) + end * t;
45
- }
46
- function strip(num, digits = 10) {
47
- return +parseFloat(Number(num).toExponential(digits));
48
- }
49
- function float(num, { digits = 2, fixed = false } = {}) {
50
- const str = strip(num).toFixed(digits);
51
- if (fixed || !digits)
52
- return +str;
53
- return +str.replace(/(\.[1-9]?)0+$/, '$1').replace(/\.$/, '');
54
- }
55
- const radiansToAngles = 180 / Math.PI;
56
- /**
57
- * 弧度转成角度
58
- * @param radians
59
- * @returns
60
- */
61
- function radian2degree(radians) {
62
- return radians * radiansToAngles;
63
- }
64
- /**
65
- * 角度转成弧度
66
- * @param degrees
67
- * @returns
68
- */
69
- function degree2radian(degrees) {
70
- return degrees / radiansToAngles;
71
- }
72
- function calcDataSize(data) {
73
- const result = {
74
- gb: 0,
75
- mb: 0,
76
- kb: 0,
77
- b: 0,
78
- total: data,
79
- desc: '',
80
- };
81
- const units = [
82
- { name: 'gb', divisor: 1 << 30 },
83
- { name: 'mb', divisor: 1 << 20 },
84
- { name: 'kb', divisor: 1 << 10 },
85
- { name: 'b', divisor: 1 },
86
- ];
87
- // 计算每个单位的数量
88
- for (const unit of units) {
89
- const value = Math.floor(data / unit.divisor);
90
- result[unit.name] = value;
91
- data -= value * unit.divisor;
92
- }
93
- // 计算简洁的描述
94
- if (result.gb > 0) {
95
- result.desc = (result.gb + result.mb / 1024).toFixed(2) + 'GB';
96
- }
97
- else if (result.mb > 0) {
98
- result.desc = (result.mb + result.kb / 1024).toFixed(2) + 'MB';
99
- }
100
- else if (result.kb > 0) {
101
- result.desc = (result.kb + result.b / 1024).toFixed(2) + 'KB';
102
- }
103
- else {
104
- result.desc = result.b + 'B';
105
- }
106
- return result;
107
- }
108
- exports.default = {
109
- random,
110
- randomRange,
111
- lerp,
112
- randoms,
113
- strip,
114
- float,
115
- radian2degree,
116
- degree2radian,
117
- calcDataSize,
118
- };
@@ -1,97 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const OBJECT_TYPE = "[object Object]";
4
- const ARRAY_TYPE = "[object Array]";
5
- const getType = (obj) => Object.prototype.toString.call(obj);
6
- const initPath = (data) => {
7
- if (getType(data) !== OBJECT_TYPE)
8
- return;
9
- for (let item in data) {
10
- if (/\w+\.\w+/g.test(item) && item.indexOf("[") === -1) {
11
- const arr = item.split(".");
12
- let result = data, len = arr.length;
13
- for (let i = 0; i < len - 1; i++) {
14
- const arrItem = arr[i];
15
- if (getType(result[arrItem]) !== OBJECT_TYPE) {
16
- result[arrItem] = {};
17
- }
18
- result = result[arrItem];
19
- }
20
- result[arr[len - 1]] = data[item];
21
- delete data[item];
22
- }
23
- }
24
- };
25
- const initData = (cur, pre, root = false) => {
26
- if (cur === pre)
27
- return;
28
- const curType = getType(cur), preType = getType(pre);
29
- if (curType !== preType)
30
- return;
31
- if (curType === ARRAY_TYPE && cur.length >= pre.length) {
32
- for (let i = 0; i < pre.length; i++) {
33
- initData(cur[i], pre[i]);
34
- }
35
- }
36
- else if (curType === OBJECT_TYPE && Object.keys(cur).length >= Object.keys(pre).length) {
37
- for (let key in pre) {
38
- if (!root && cur[key] === undefined) {
39
- cur[key] = null;
40
- }
41
- else {
42
- initData(cur[key], pre[key]);
43
- }
44
- }
45
- }
46
- };
47
- const doDiff = (cur, pre, target, path = "", root = false) => {
48
- if (cur === pre)
49
- return;
50
- const curRootType = getType(cur), preRootType = getType(pre);
51
- if (curRootType === ARRAY_TYPE && preRootType === curRootType && cur.length >= pre.length) {
52
- for (let i = 0; i < cur.length; i++) {
53
- doDiff(cur[i], pre[i], target, `${path}[${i}]`);
54
- }
55
- return;
56
- }
57
- if (curRootType === OBJECT_TYPE &&
58
- preRootType === curRootType &&
59
- (root || Object.keys(cur).length >= Object.keys(pre).length)) {
60
- const keys = Object.keys(cur);
61
- for (let key of keys) {
62
- const curVal = cur[key], preVal = pre[key];
63
- const curType = getType(curVal), preType = getType(preVal);
64
- if (curVal === preVal)
65
- continue;
66
- if (curType === ARRAY_TYPE && preType === curType && curVal.length >= preVal.length) {
67
- for (let i = 0; i < curVal.length; i++) {
68
- doDiff(curVal[i], preVal[i], target, `${path ? path + "." : ""}${key}[${i}]`);
69
- }
70
- continue;
71
- }
72
- if (curType === OBJECT_TYPE && preType === curType && Object.keys(curVal).length >= Object.keys(preVal).length) {
73
- for (let sKey in curVal) {
74
- doDiff(curVal[sKey], preVal[sKey], target, `${path ? path + "." : ""}${key}.${sKey}`);
75
- }
76
- continue;
77
- }
78
- target[`${path ? path + "." : ""}${key}`] = curVal;
79
- }
80
- return;
81
- }
82
- target[path] = cur;
83
- };
84
- /**
85
- * 计算并格式化差异值
86
- * @param data
87
- * @param prevData
88
- * @returns
89
- */
90
- function diff(data, prevData) {
91
- const target = {};
92
- initPath(data);
93
- initData(data, prevData, true);
94
- doDiff(data, prevData, target, "", true);
95
- return target;
96
- }
97
- exports.default = diff;