@vope/utils 1.3.0 → 1.4.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/cjs/encoding.js CHANGED
@@ -2,13 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const utf8_first_prefix = /^1{2,}/g;
4
4
  const utf8_other_prefix = /^10/;
5
- var Encodes;
6
- (function (Encodes) {
7
- Encodes["UTF_8"] = "utf-8";
8
- Encodes["UTF_16LE"] = "utf-16le";
9
- Encodes["UTF_16BE"] = "utf-16be";
10
- Encodes["GBK"] = "gbk";
11
- })(Encodes || (Encodes = {}));
5
+ const Encodes = {
6
+ UTF_8: 'utf-8',
7
+ UTF_16LE: 'utf-16le',
8
+ UTF_16BE: 'utf-16be',
9
+ GBK: 'gbk'
10
+ };
12
11
  const defaults = {
13
12
  utf8: [0xef, 0xbb, 0xbf],
14
13
  };
package/cjs/index.js CHANGED
@@ -3,10 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.wait = exports.connectURLParams = exports.joinPaths = exports.Union = exports.rotateRad = exports.rotateAngle = exports.deconPowersSum = exports.once = exports.MemoryPower = exports.MemoryList = exports.MemoryBasic = exports.isNull = exports.isEmpty = exports.isClass = exports.inputRegex = exports.getEncoding = exports.hash8strings = exports.hash8object = exports.hash8json = exports.deepWeakEqual = exports.compareVersion = exports.classnames = exports.clamp = exports.apiGet = exports.apiPost = void 0;
7
- var api_1 = require("./api");
8
- Object.defineProperty(exports, "apiPost", { enumerable: true, get: function () { return api_1.apiPost; } });
9
- Object.defineProperty(exports, "apiGet", { enumerable: true, get: function () { return api_1.apiGet; } });
6
+ exports.wait = exports.connectURLParams = exports.joinPaths = exports.Union = exports.rotateRad = exports.rotateAngle = exports.deconPowersSum = exports.once = exports.MemoryPower = exports.MemoryList = exports.MemoryBasic = exports.isNull = exports.isEmpty = exports.isClass = exports.inputRegex = exports.getEncoding = exports.hash8strings = exports.hash8object = exports.hash8json = exports.deepWeakEqual = exports.compareVersion = exports.classnames = exports.clamp = void 0;
10
7
  var clamp_1 = require("./clamp");
11
8
  Object.defineProperty(exports, "clamp", { enumerable: true, get: function () { return __importDefault(clamp_1).default; } });
12
9
  var classnames_1 = require("./classnames");
@@ -65,7 +65,7 @@ class MemoryBasic {
65
65
  * 检测存储是否包含某种类型
66
66
  */
67
67
  includes(type) {
68
- return type in this.memorys;
68
+ return this.memorys.has(type);
69
69
  }
70
70
  /**
71
71
  * 获取指定类型存储数据
package/lib/clamp.js CHANGED
@@ -1,12 +1,9 @@
1
- /**
2
- * 获取中间数
3
- */
1
+ "use strict";
4
2
  export default (value, min, max) => {
5
- if (min > max) {
6
- // js位运算的范围为(-2^32,2^32),超出会产生溢出
7
- const swap = max;
8
- max = min;
9
- min = swap;
10
- }
11
- return Math.min(max, Math.max(min, value));
3
+ if (min > max) {
4
+ const swap = max;
5
+ max = min;
6
+ min = swap;
7
+ }
8
+ return Math.min(max, Math.max(min, value));
12
9
  };
package/lib/classnames.js CHANGED
@@ -1,20 +1,14 @@
1
- const add = (a, b) => a
2
- ? a + '\u0020' + b
3
- : b;
4
- /**
5
- * 拼接多个class
6
- */
1
+ "use strict";
2
+ const add = (a, b) => a ? a + " " + b : b;
7
3
  export default (...list) => list.reduce((caches, data) => {
8
- if (typeof data === 'string') {
9
- data && (caches = add(caches, data));
4
+ if (typeof data === "string") {
5
+ data && (caches = add(caches, data));
6
+ } else if (Array.isArray(data)) {
7
+ data.forEach((value) => value && (caches = add(caches, value)));
8
+ } else if (data) {
9
+ for (const key in data) {
10
+ key && data[key] && (caches = add(caches, key));
10
11
  }
11
- else if (Array.isArray(data)) {
12
- data.forEach((value) => value && (caches = add(caches, value)));
13
- }
14
- else if (data) {
15
- for (const key in data) {
16
- key && data[key] && (caches = add(caches, key));
17
- }
18
- }
19
- return caches;
20
- }, '');
12
+ }
13
+ return caches;
14
+ }, "");
@@ -1,19 +1,16 @@
1
- const CHAR = '.';
2
- /**
3
- * 版本号大小比较
4
- * @description from < to = 1,from === to = 0,from > to = -1
5
- */
1
+ "use strict";
2
+ const CHAR = ".";
6
3
  const compareVersion = (from, to) => {
7
- const froms = from.split(CHAR);
8
- const tos = to.split(CHAR);
9
- for (let i = 0, max = Math.max(froms.length, tos.length); i < max; i++) {
10
- const fromV = +froms[i] || 0;
11
- const toV = +tos[i] || 0;
12
- if (fromV > toV)
13
- return -1;
14
- if (fromV < toV)
15
- return 1;
16
- }
17
- return 0;
4
+ const froms = from.split(CHAR);
5
+ const tos = to.split(CHAR);
6
+ for (let i = 0, max = Math.max(froms.length, tos.length); i < max; i++) {
7
+ const fromV = +froms[i] || 0;
8
+ const toV = +tos[i] || 0;
9
+ if (fromV > toV)
10
+ return -1;
11
+ if (fromV < toV)
12
+ return 1;
13
+ }
14
+ return 0;
18
15
  };
19
16
  export default compareVersion;
@@ -1,39 +1,32 @@
1
- /**
2
- * 深度相等性比较(弱比较)
3
- */
1
+ "use strict";
4
2
  const deepWeakEqual = (from, to) => {
5
- if (typeof from !== 'object'
6
- || from === null
7
- || typeof to !== 'object'
8
- || to === null)
9
- return from === to;
10
- if (Array.isArray(from) && Array.isArray(to)) {
11
- if (from.length !== to.length)
12
- return false;
13
- return !from.some((t1, index) => {
14
- if (!deepWeakEqual(t1, to[index])) {
15
- return true;
16
- }
17
- });
3
+ if (typeof from !== "object" || from === null || typeof to !== "object" || to === null)
4
+ return from === to;
5
+ if (Array.isArray(from) && Array.isArray(to)) {
6
+ if (from.length !== to.length)
7
+ return false;
8
+ return !from.some((t1, index) => {
9
+ if (!deepWeakEqual(t1, to[index])) {
10
+ return true;
11
+ }
12
+ });
13
+ } else {
14
+ const caches = {};
15
+ for (const key in from) {
16
+ if (!deepWeakEqual(from[key], to[key])) {
17
+ return false;
18
+ } else {
19
+ caches[key] = key;
20
+ }
18
21
  }
19
- else {
20
- const caches = {};
21
- for (const key in from) {
22
- if (!deepWeakEqual(from[key], to[key])) {
23
- return false;
24
- }
25
- else {
26
- caches[key] = key;
27
- }
28
- }
29
- for (const key in to) {
30
- if (key in caches)
31
- continue;
32
- if (!deepWeakEqual(from[key], to[key])) {
33
- return false;
34
- }
35
- }
22
+ for (const key in to) {
23
+ if (key in caches)
24
+ continue;
25
+ if (!deepWeakEqual(from[key], to[key])) {
26
+ return false;
27
+ }
36
28
  }
37
- return true;
29
+ }
30
+ return true;
38
31
  };
39
32
  export default deepWeakEqual;
@@ -1,16 +1,6 @@
1
- /**
2
- * 对象数组转hash(要求主键值为string|number类型)
3
- * @example
4
- * hash8json(
5
- * [
6
- * { key: 'a', value: 1 },
7
- * { key: 'b', value: 2 }
8
- * ],
9
- * 'key'
10
- * ).a.value // 1
11
- */
1
+ "use strict";
12
2
  const hash8json = (list, key) => list.reduce((caches, data) => {
13
- caches[data[key]] = data;
14
- return caches;
3
+ caches[data[key]] = data;
4
+ return caches;
15
5
  }, {});
16
6
  export default hash8json;
@@ -1,16 +1,6 @@
1
- /**
2
- * 对象数组转hash
3
- * @example
4
- * hash8object(
5
- * [
6
- * { key: String, value: 1 },
7
- * { key: Object, value: 2 }
8
- * ],
9
- * 'key'
10
- * ).get(Object).value // 2
11
- */
1
+ "use strict";
12
2
  const hash8object = (list, key) => list.reduce((caches, data) => {
13
- caches.set(data[key], data);
14
- return caches;
15
- }, new Map());
3
+ caches.set(data[key], data);
4
+ return caches;
5
+ }, /* @__PURE__ */ new Map());
16
6
  export default hash8object;
@@ -1,10 +1,6 @@
1
- /**
2
- * 字符数组转hash
3
- * @example
4
- * hash8strings(['a', 'b']).a // 0
5
- */
1
+ "use strict";
6
2
  const hash8strings = (list) => list.reduce((caches, data, index) => {
7
- caches[data] = index;
8
- return caches;
3
+ caches[data] = index;
4
+ return caches;
9
5
  }, {});
10
6
  export default hash8strings;
package/lib/encoding.js CHANGED
@@ -1,63 +1,55 @@
1
+ "use strict";
1
2
  const utf8_first_prefix = /^1{2,}/g;
2
3
  const utf8_other_prefix = /^10/;
3
- var Encodes;
4
- (function (Encodes) {
5
- Encodes["UTF_8"] = "utf-8";
6
- Encodes["UTF_16LE"] = "utf-16le";
7
- Encodes["UTF_16BE"] = "utf-16be";
8
- Encodes["GBK"] = "gbk";
9
- })(Encodes || (Encodes = {}));
4
+ const Encodes = {
5
+ UTF_8: "utf-8",
6
+ UTF_16LE: "utf-16le",
7
+ UTF_16BE: "utf-16be",
8
+ GBK: "gbk"
9
+ };
10
10
  const defaults = {
11
- utf8: [0xef, 0xbb, 0xbf],
11
+ utf8: [239, 187, 191]
12
12
  };
13
13
  const equal = (uint, list) => {
14
- if (uint.length !== list.length)
15
- return false;
16
- return !uint.some((value, index) => value !== list[index]);
14
+ if (uint.length !== list.length)
15
+ return false;
16
+ return !uint.some((value, index) => value !== list[index]);
17
17
  };
18
- /**
19
- * 根据文件内容判断文件的编码
20
- * @param uint 文件内容Uint8Array格式
21
- * @param limit 用于约束GBK和utf_8的判定上限
22
- */
23
18
  const getEncoding = (uint, limit = 1024) => {
24
- let encoding;
25
- let offset = 0;
26
- if (equal(uint.slice(0, 3), defaults.utf8)) {
27
- offset = defaults.utf8.length;
28
- encoding = Encodes.UTF_8;
29
- }
30
- else if (equal(uint.slice(0, 2), [0xff, 0xfe])) {
31
- encoding = Encodes.UTF_16LE;
32
- }
33
- else if (equal(uint.slice(0, 2), [0xfe, 0xff])) {
34
- encoding = Encodes.UTF_16BE;
35
- }
36
- else {
37
- let isBreak = false;
38
- top: for (let i = 0; i < Math.min(uint.length, limit); i++) {
39
- const data = uint[i];
40
- if (data < 128)
41
- continue;
42
- const match = data.toString(2).match(utf8_first_prefix);
43
- if (match) {
44
- for (let m = 0; m < match[0].length - 1; m++) {
45
- const target = uint[++i];
46
- if (target == void 0 || !utf8_other_prefix.test(target.toString(2))) {
47
- encoding = Encodes.GBK;
48
- isBreak = true;
49
- break top;
50
- }
51
- }
52
- }
53
- else {
54
- encoding = Encodes.GBK;
55
- isBreak = true;
56
- break;
19
+ let encoding;
20
+ let offset = 0;
21
+ if (equal(uint.slice(0, 3), defaults.utf8)) {
22
+ offset = defaults.utf8.length;
23
+ encoding = Encodes.UTF_8;
24
+ } else if (equal(uint.slice(0, 2), [255, 254])) {
25
+ encoding = Encodes.UTF_16LE;
26
+ } else if (equal(uint.slice(0, 2), [254, 255])) {
27
+ encoding = Encodes.UTF_16BE;
28
+ } else {
29
+ let isBreak = false;
30
+ top:
31
+ for (let i = 0; i < Math.min(uint.length, limit); i++) {
32
+ const data = uint[i];
33
+ if (data < 128)
34
+ continue;
35
+ const match = data.toString(2).match(utf8_first_prefix);
36
+ if (match) {
37
+ for (let m = 0; m < match[0].length - 1; m++) {
38
+ const target = uint[++i];
39
+ if (target == void 0 || !utf8_other_prefix.test(target.toString(2))) {
40
+ encoding = Encodes.GBK;
41
+ isBreak = true;
42
+ break top;
57
43
  }
44
+ }
45
+ } else {
46
+ encoding = Encodes.GBK;
47
+ isBreak = true;
48
+ break;
58
49
  }
59
- isBreak || (encoding = Encodes.UTF_8);
60
- }
61
- return { encoding, offset };
50
+ }
51
+ isBreak || (encoding = Encodes.UTF_8);
52
+ }
53
+ return { encoding, offset };
62
54
  };
63
55
  export default getEncoding;
package/lib/index.js CHANGED
@@ -1,24 +1,24 @@
1
- export { apiPost, apiGet } from './api';
2
- export { default as clamp } from './clamp';
3
- export { default as classnames } from './classnames';
4
- export { default as compareVersion } from './compare/version';
5
- export { default as deepWeakEqual } from './compare/weak-equal';
6
- export { default as hash8json } from './convert/json';
7
- export { default as hash8object } from './convert/object';
8
- export { default as hash8strings } from './convert/strings';
9
- export { default as getEncoding } from './encoding';
10
- export { default as inputRegex } from './input-regex';
11
- export { default as isClass } from './is/class';
12
- export { default as isEmpty } from './is/empty';
13
- export { default as isNull } from './is/null';
14
- export { default as MemoryBasic } from './memory/basic';
15
- export { default as MemoryList } from './memory/list';
16
- export { default as MemoryPower } from './memory/power';
17
- export { default as once } from './once';
18
- export { default as deconPowersSum } from './power/decon';
19
- export { default as rotateAngle } from './rotate/angle';
20
- export { default as rotateRad } from './rotate/rad';
21
- export { default as Union } from './union';
22
- export { default as joinPaths } from './url/join';
23
- export { default as connectURLParams } from './url/params';
24
- export { default as wait } from './wait';
1
+ "use strict";
2
+ export { default as clamp } from "./clamp";
3
+ export { default as classnames } from "./classnames";
4
+ export { default as compareVersion } from "./compare/version";
5
+ export { default as deepWeakEqual } from "./compare/weak-equal";
6
+ export { default as hash8json } from "./convert/json";
7
+ export { default as hash8object } from "./convert/object";
8
+ export { default as hash8strings } from "./convert/strings";
9
+ export { default as getEncoding } from "./encoding";
10
+ export { default as inputRegex } from "./input-regex";
11
+ export { default as isClass } from "./is/class";
12
+ export { default as isEmpty } from "./is/empty";
13
+ export { default as isNull } from "./is/null";
14
+ export { default as MemoryBasic } from "./memory/basic";
15
+ export { default as MemoryList } from "./memory/list";
16
+ export { default as MemoryPower } from "./memory/power";
17
+ export { default as once } from "./once";
18
+ export { default as deconPowersSum } from "./power/decon";
19
+ export { default as rotateAngle } from "./rotate/angle";
20
+ export { default as rotateRad } from "./rotate/rad";
21
+ export { default as Union } from "./union";
22
+ export { default as joinPaths } from "./url/join";
23
+ export { default as connectURLParams } from "./url/params";
24
+ export { default as wait } from "./wait";
@@ -1,8 +1,7 @@
1
+ "use strict";
1
2
  const RE_REGEX = /[$()*+.[?\\^{|]/g;
2
3
  const RE_STRING = /[$()*+.[?\\^{|-]/g;
3
4
  function inputRegex(text, flags) {
4
- return flags === false
5
- ? text.replace(RE_STRING, ($0) => '\\' + $0)
6
- : new RegExp(text.replace(RE_REGEX, ($0) => '\\' + $0), flags);
5
+ return flags === false ? text.replace(RE_STRING, ($0) => "\\" + $0) : new RegExp(text.replace(RE_REGEX, ($0) => "\\" + $0), flags);
7
6
  }
8
7
  export default inputRegex;
package/lib/is/class.js CHANGED
@@ -1,30 +1,27 @@
1
+ "use strict";
1
2
  const re_anonymous = /^function\s+\(|^function\s+anonymous\(/;
2
3
  const re_function = /^function\s+[A-Z]/;
3
4
  const re_this = /\b\(this\b|\bthis[\.\[]\b/;
4
5
  const re_check = /classCallCheck\(this/;
5
6
  const re_default = /^function\sdefault_\d+\s*\(/;
6
- /**
7
- * 检测是否为class
8
- */
9
7
  const isClass = (value, strict = true) => {
10
- if (typeof value !== 'function')
11
- return false;
12
- const proto = value.prototype;
13
- if (!proto || proto.constructor !== value)
14
- return false;
15
- const str = value.toString();
16
- if (str.slice(0, 5) === 'class'
17
- || Object.getOwnPropertyNames(proto).length >= 2)
18
- return true;
19
- if (re_anonymous.test(str))
20
- return false;
21
- if (strict && re_function.test(str))
22
- return true;
23
- if (re_this.test(str)) {
24
- if (!strict || re_check.test(str))
25
- return true;
26
- return re_default.test(str);
27
- }
8
+ if (typeof value !== "function")
28
9
  return false;
10
+ const proto = value.prototype;
11
+ if (!proto || proto.constructor !== value)
12
+ return false;
13
+ const str = value.toString();
14
+ if (str.slice(0, 5) === "class" || Object.getOwnPropertyNames(proto).length >= 2)
15
+ return true;
16
+ if (re_anonymous.test(str))
17
+ return false;
18
+ if (strict && re_function.test(str))
19
+ return true;
20
+ if (re_this.test(str)) {
21
+ if (!strict || re_check.test(str))
22
+ return true;
23
+ return re_default.test(str);
24
+ }
25
+ return false;
29
26
  };
30
27
  export default isClass;
package/lib/is/empty.js CHANGED
@@ -1,7 +1,4 @@
1
- import isNull from './null';
2
- /**
3
- * 检测是否为空(undefined/null/'')
4
- */
5
- const isEmpty = (value) => isNull(value)
6
- || value === '';
1
+ "use strict";
2
+ import isNull from "./null";
3
+ const isEmpty = (value) => isNull(value) || value === "";
7
4
  export default isEmpty;
package/lib/is/null.js CHANGED
@@ -1,6 +1,3 @@
1
- /**
2
- * 检测是否无值(undefined/null)
3
- */
4
- const isNull = (value) => value === void 0
5
- || value === null;
1
+ "use strict";
2
+ const isNull = (value) => value === void 0 || value === null;
6
3
  export default isNull;
@@ -1,142 +1,136 @@
1
- import hash8strings from '../convert/strings';
2
- /**
3
- * 分类存储(通过继承,可用于拓展存储方式)
4
- */
1
+ "use strict";
2
+ import hash8strings from "../convert/strings";
5
3
  export default class MemoryBasic {
6
- key;
7
- #mount;
8
- #reflex;
9
- list = [];
10
- memorys = new Map();
11
- /**
12
- * 返回存储长度
13
- */
14
- get size() {
15
- return this.list.length;
4
+ /**
5
+ * @param key 用于对数据进行分类的key
6
+ * @param config
7
+ */
8
+ constructor(key, config) {
9
+ this.key = key;
10
+ this.#mount = config.mount;
11
+ this.#reflex = config.reflex;
12
+ }
13
+ #mount;
14
+ #reflex;
15
+ list = [];
16
+ memorys = /* @__PURE__ */ new Map();
17
+ /**
18
+ * 返回存储长度
19
+ */
20
+ get size() {
21
+ return this.list.length;
22
+ }
23
+ getType(data) {
24
+ const { key } = this;
25
+ return typeof key === "function" ? key(data) : data[key];
26
+ }
27
+ push(...list) {
28
+ const { memorys } = this;
29
+ list.forEach((data) => {
30
+ const type = this.getType(data);
31
+ const memory = memorys.get(type);
32
+ const index = this.list.push(data) - 1;
33
+ const result = this.#mount(index, memory);
34
+ memory === result || memorys.set(type, result);
35
+ });
36
+ return this;
37
+ }
38
+ /**
39
+ * 遍历所有存储数据(按入栈顺序)
40
+ */
41
+ forEach(callback) {
42
+ this.list.forEach(callback);
43
+ return this;
44
+ }
45
+ /**
46
+ * 遍历所有存储类型
47
+ */
48
+ forType(callback) {
49
+ const { memorys, list } = this;
50
+ for (const [type, memory] of memorys) {
51
+ const caches = this.#reflex(memory).map((index) => list[index]);
52
+ callback(caches, type);
16
53
  }
17
- /**
18
- * @param key 用于对数据进行分类的key
19
- * @param config
20
- */
21
- constructor(key, config) {
22
- this.key = key;
23
- this.#mount = config.mount;
24
- this.#reflex = config.reflex;
25
- }
26
- getType(data) {
27
- const { key } = this;
28
- return typeof key === 'function' ? key(data) : data[key];
29
- }
30
- push(...list) {
31
- const { memorys } = this;
32
- list.forEach((data) => {
33
- const type = this.getType(data);
34
- const memory = memorys.get(type);
35
- const index = this.list.push(data) - 1;
36
- const result = this.#mount(index, memory);
37
- memory === result || memorys.set(type, result);
38
- });
39
- return this;
40
- }
41
- /**
42
- * 遍历所有存储数据(按入栈顺序)
43
- */
44
- forEach(callback) {
45
- this.list.forEach(callback);
46
- return this;
47
- }
48
- /**
49
- * 遍历所有存储类型
50
- */
51
- forType(callback) {
52
- const { memorys, list } = this;
53
- for (const [type, memory] of memorys) {
54
- const caches = this.#reflex(memory).map((index) => list[index]);
55
- callback(caches, type);
56
- }
57
- return this;
58
- }
59
- /**
60
- * 检测存储是否包含某种类型
61
- */
62
- includes(type) {
63
- return type in this.memorys;
64
- }
65
- /**
66
- * 获取指定类型存储数据
67
- */
68
- get(type) {
69
- const memory = this.memorys.get(type);
70
- return memory === void 0
71
- ? null
72
- : this.#reflex(memory).map((count) => this.list[count]);
73
- }
74
- /**
75
- * 删除指定存储数据
76
- */
77
- delete(data) {
78
- const { memorys, list } = this;
79
- let index = -1;
80
- if (!list.some((value, i) => {
81
- if (data === value) {
82
- index = i;
83
- list.splice(index, 1);
84
- const type = this.getType(value);
85
- const memory = this.#reflex(memorys.get(type));
86
- if (memory.length === 1) {
87
- memorys.delete(type);
88
- }
89
- else {
90
- memory.some((count, j) => {
91
- if (count === i) {
92
- memory.splice(j, 1);
93
- return true;
94
- }
95
- });
96
- }
97
- return true;
98
- }
99
- }))
100
- return this;
101
- for (const [type, target] of memorys) {
102
- const memory = this.#reflex(target);
103
- memory.forEach((value, index) => {
104
- if (value >= index) {
105
- memory[index] = value - 1;
106
- }
107
- });
108
- if (memory !== target) {
109
- let reactor;
110
- memory.forEach((count) => {
111
- reactor = this.#mount(count, reactor);
112
- });
113
- memorys.set(type, reactor);
54
+ return this;
55
+ }
56
+ /**
57
+ * 检测存储是否包含某种类型
58
+ */
59
+ includes(type) {
60
+ return this.memorys.has(type);
61
+ }
62
+ /**
63
+ * 获取指定类型存储数据
64
+ */
65
+ get(type) {
66
+ const memory = this.memorys.get(type);
67
+ return memory === void 0 ? null : this.#reflex(memory).map((count) => this.list[count]);
68
+ }
69
+ /**
70
+ * 删除指定存储数据
71
+ */
72
+ delete(data) {
73
+ const { memorys, list } = this;
74
+ let index = -1;
75
+ if (!list.some((value, i) => {
76
+ if (data === value) {
77
+ index = i;
78
+ list.splice(index, 1);
79
+ const type = this.getType(value);
80
+ const memory = this.#reflex(memorys.get(type));
81
+ if (memory.length === 1) {
82
+ memorys.delete(type);
83
+ } else {
84
+ memory.some((count, j) => {
85
+ if (count === i) {
86
+ memory.splice(j, 1);
87
+ return true;
114
88
  }
89
+ });
115
90
  }
116
- return this;
117
- }
118
- /**
119
- * 删除指定类型存储数据
120
- */
121
- deleteType(type) {
122
- const { memorys, list } = this;
123
- if (!memorys.has(type))
124
- return this;
125
- const local = hash8strings(this.#reflex(memorys.get(type)));
126
- const Memory = this.constructor;
127
- const imitator = new Memory(this.key);
128
- list.forEach((data, index) => {
129
- index in local || imitator.push(data);
91
+ return true;
92
+ }
93
+ }))
94
+ return this;
95
+ for (const [type, target] of memorys) {
96
+ const memory = this.#reflex(target);
97
+ memory.forEach((value, index2) => {
98
+ if (value >= index2) {
99
+ memory[index2] = value - 1;
100
+ }
101
+ });
102
+ if (memory !== target) {
103
+ let reactor;
104
+ memory.forEach((count) => {
105
+ reactor = this.#mount(count, reactor);
130
106
  });
131
- this.list = imitator.list;
132
- this.memorys = imitator.memorys;
133
- return this;
134
- }
135
- /**
136
- * 清理存储
137
- */
138
- clear() {
139
- this.list = [];
140
- this.memorys.clear();
107
+ memorys.set(type, reactor);
108
+ }
141
109
  }
110
+ return this;
111
+ }
112
+ /**
113
+ * 删除指定类型存储数据
114
+ */
115
+ deleteType(type) {
116
+ const { memorys, list } = this;
117
+ if (!memorys.has(type))
118
+ return this;
119
+ const local = hash8strings(this.#reflex(memorys.get(type)));
120
+ const Memory2 = this.constructor;
121
+ const imitator = new Memory2(this.key);
122
+ list.forEach((data, index) => {
123
+ index in local || imitator.push(data);
124
+ });
125
+ this.list = imitator.list;
126
+ this.memorys = imitator.memorys;
127
+ return this;
128
+ }
129
+ /**
130
+ * 清理存储
131
+ */
132
+ clear() {
133
+ this.list = [];
134
+ this.memorys.clear();
135
+ }
142
136
  }
@@ -1,14 +1,10 @@
1
- import MemoryBasic from './basic';
2
- /**
3
- * 分类存储(基于普通的键值存储)
4
- */
1
+ "use strict";
2
+ import MemoryBasic from "./basic";
5
3
  export default class MemoryList extends MemoryBasic {
6
- constructor(key) {
7
- super(key, {
8
- mount: (index, reactor) => reactor
9
- ? (reactor.push(index), reactor)
10
- : [index],
11
- reflex: (reactor) => reactor,
12
- });
13
- }
4
+ constructor(key) {
5
+ super(key, {
6
+ mount: (index, reactor) => reactor ? (reactor.push(index), reactor) : [index],
7
+ reflex: (reactor) => reactor
8
+ });
9
+ }
14
10
  }
@@ -1,13 +1,11 @@
1
- import deconPowersSum from '../power/decon';
2
- import MemoryBasic from './basic';
3
- /**
4
- * 分类存储(基于2的n次幂之和构建唯一键进行存储)
5
- */
1
+ "use strict";
2
+ import deconPowersSum from "../power/decon";
3
+ import MemoryBasic from "./basic";
6
4
  export default class MemoryPower extends MemoryBasic {
7
- constructor(key) {
8
- super(key, {
9
- mount: (index, reactor) => (reactor || 0) + 2 ** index,
10
- reflex: (reactor) => deconPowersSum(reactor, 'asc'),
11
- });
12
- }
5
+ constructor(key) {
6
+ super(key, {
7
+ mount: (index, reactor) => (reactor || 0) + 2 ** index,
8
+ reflex: (reactor) => deconPowersSum(reactor, "asc")
9
+ });
10
+ }
13
11
  }
@@ -0,0 +1 @@
1
+ "use strict";
package/lib/once.js CHANGED
@@ -1,13 +1,6 @@
1
- /**
2
- * 缓存函数返回值
3
- * @param callback 运行返回值的函数
4
- * @param thisArg 绑定到运行函数的this对象
5
- */
1
+ "use strict";
6
2
  export default (func, thisArg) => {
7
- let local;
8
- let inited = false;
9
- return (...list) => inited
10
- ? local
11
- : (inited = true,
12
- local = func.apply(thisArg, list));
3
+ let local;
4
+ let inited = false;
5
+ return (...list) => inited ? local : (inited = true, local = func.apply(thisArg, list));
13
6
  };
@@ -1,29 +1,21 @@
1
+ "use strict";
1
2
  const ERROR_MESSAGE = {
2
- INT: '参数必须是整数',
3
- MIN: '多个2的正整数幂(两两不相等)之和必定大于等于1',
3
+ INT: "\u53C2\u6570\u5FC5\u987B\u662F\u6574\u6570",
4
+ MIN: "\u591A\u4E2A2\u7684\u6B63\u6574\u6570\u5E42(\u4E24\u4E24\u4E0D\u76F8\u7B49)\u4E4B\u548C\u5FC5\u5B9A\u5927\u4E8E\u7B49\u4E8E1"
4
5
  };
5
- /**
6
- * 解构多个2的正整数幂(两两不相等)之和
7
- * @param sum 幂和
8
- * @param sort 排序方式(默认降序)
9
- * @description sum > 2**53则会因为精度问题计算报错
10
- */
11
6
  const deconPowersSum = (sum, sort) => {
12
- if (sum < 1) {
13
- throw new Error(ERROR_MESSAGE.MIN);
14
- }
15
- else if (sum !== Math.floor(sum)) {
16
- throw new Error(ERROR_MESSAGE.INT);
17
- }
18
- const list = [];
19
- let cache = sum;
20
- while (cache) {
21
- const power = Math.log2(cache) ^ 0;
22
- list.push(power);
23
- cache -= Math.pow(2, power);
24
- }
25
- return sort === 'asc'
26
- ? list.reverse()
27
- : list;
7
+ if (sum < 1) {
8
+ throw new Error(ERROR_MESSAGE.MIN);
9
+ } else if (sum !== Math.floor(sum)) {
10
+ throw new Error(ERROR_MESSAGE.INT);
11
+ }
12
+ const list = [];
13
+ let cache = sum;
14
+ while (cache) {
15
+ const power = Math.log2(cache) ^ 0;
16
+ list.push(power);
17
+ cache -= Math.pow(2, power);
18
+ }
19
+ return sort === "asc" ? list.reverse() : list;
28
20
  };
29
21
  export default deconPowersSum;
@@ -1,9 +1,4 @@
1
- import rotateRad from './rad';
2
- /**
3
- * 返回点被旋转指定角度后新的位置
4
- * @param from 旋转点
5
- * @param angle 旋转角度
6
- * @param visit 旋转原点
7
- */
1
+ "use strict";
2
+ import rotateRad from "./rad";
8
3
  const rotateAngle = (from, angle, visit) => rotateRad(from, angle / 180 * Math.PI, visit);
9
4
  export default rotateAngle;
package/lib/rotate/rad.js CHANGED
@@ -1,15 +1,10 @@
1
- /**
2
- * 返回点被旋转指定弧度后新的位置
3
- * @param from 旋转点
4
- * @param rad 旋转弧度
5
- * @param visit 旋转原点
6
- */
1
+ "use strict";
7
2
  const rotateRad = (from, rad, visit = [0, 0]) => {
8
- const x = from[0] - visit[0];
9
- const y = from[1] - visit[1];
10
- return [
11
- x * Math.cos(rad) + y * Math.sin(rad) + visit[0],
12
- y * Math.cos(rad) - x * Math.sin(rad) + visit[1],
13
- ];
3
+ const x = from[0] - visit[0];
4
+ const y = from[1] - visit[1];
5
+ return [
6
+ x * Math.cos(rad) + y * Math.sin(rad) + visit[0],
7
+ y * Math.cos(rad) - x * Math.sin(rad) + visit[1]
8
+ ];
14
9
  };
15
10
  export default rotateRad;
package/lib/union.js CHANGED
@@ -1,26 +1,22 @@
1
- /**
2
- * 数据联合(用于需要对加入的数据进行智能提示)
3
- */
1
+ "use strict";
4
2
  export default class Union {
5
- key;
6
- formatter;
7
- #data = {};
8
- #options = {};
9
- get data() {
10
- return this.#data;
11
- }
12
- get options() {
13
- return this.#options;
14
- }
15
- constructor(key, formatter) {
16
- this.key = key;
17
- this.formatter = formatter;
18
- }
19
- add(data) {
20
- const key = data[this.key];
21
- const result = this.formatter(data);
22
- this.#data[key] = result.data;
23
- this.#options[key] = result.option;
24
- return this;
25
- }
3
+ constructor(key, formatter) {
4
+ this.key = key;
5
+ this.formatter = formatter;
6
+ }
7
+ #data = {};
8
+ #options = {};
9
+ get data() {
10
+ return this.#data;
11
+ }
12
+ get options() {
13
+ return this.#options;
14
+ }
15
+ add(data) {
16
+ const key = data[this.key];
17
+ const result = this.formatter(data);
18
+ this.#data[key] = result.data;
19
+ this.#options[key] = result.option;
20
+ return this;
21
+ }
26
22
  }
package/lib/url/join.js CHANGED
@@ -1,13 +1,9 @@
1
+ "use strict";
1
2
  const re = /([^:])\/{2,}/g;
2
- /**
3
- * 实现多个路径拼接
4
- */
5
3
  const joinPaths = (...list) => {
6
- const { length } = list;
7
- if (length === 0)
8
- return '';
9
- return length === 1
10
- ? list[0]
11
- : list.join('/').replace(re, ($0, $1) => `${$1}/`);
4
+ const { length } = list;
5
+ if (length === 0)
6
+ return "";
7
+ return length === 1 ? list[0] : list.join("/").replace(re, ($0, $1) => `${$1}/`);
12
8
  };
13
9
  export default joinPaths;
package/lib/url/params.js CHANGED
@@ -1,35 +1,23 @@
1
- /**
2
- * url拼接参数
3
- * @param url
4
- * @param params 需要让url携带的参数
5
- * @param isOpenType 是否开启类型处理
6
- * @description 开始类型处理的情况下,undefined会被转成null
7
- */
1
+ "use strict";
8
2
  const connectURLParams = (url, params, isOpenType) => {
9
- if (!params)
10
- return url;
11
- const list = [];
12
- for (const key in params) {
13
- const value = params[key];
14
- if (value === void 0) {
15
- list.push(key);
16
- }
17
- else if (Array.isArray(value)) {
18
- list.push(key
19
- + '='
20
- + (isOpenType
21
- ? JSON.stringify(value).slice(1, -1)
22
- : value.join(',')));
23
- }
24
- else {
25
- list.push(key + '=' + (isOpenType ? JSON.stringify(value) : value.toString()));
26
- }
3
+ if (!params)
4
+ return url;
5
+ const list = [];
6
+ for (const key in params) {
7
+ const value = params[key];
8
+ if (value === void 0) {
9
+ list.push(key);
10
+ } else if (Array.isArray(value)) {
11
+ list.push(
12
+ key + "=" + (isOpenType ? JSON.stringify(value).slice(1, -1) : value.join(","))
13
+ );
14
+ } else {
15
+ list.push(key + "=" + (isOpenType ? JSON.stringify(value) : value.toString()));
27
16
  }
28
- if (!list.length)
29
- return url;
30
- const caches = list.join('&');
31
- return url.includes('?')
32
- ? url + '&' + caches
33
- : url + '?' + caches;
17
+ }
18
+ if (!list.length)
19
+ return url;
20
+ const caches = list.join("&");
21
+ return url.includes("?") ? url + "&" + caches : url + "?" + caches;
34
22
  };
35
23
  export default connectURLParams;
package/lib/wait.js CHANGED
@@ -1,4 +1,2 @@
1
- /**
2
- * 等待
3
- */
1
+ "use strict";
4
2
  export default (delay = 0) => new Promise((resolve) => window.setTimeout(resolve, delay));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vope/utils",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -13,7 +13,7 @@
13
13
  "scripts": {
14
14
  "build": "npm run cjs | npm run esm | npm run types",
15
15
  "cjs": "rimraf cjs & npx tsc -p ./tsconfig.cjs.json",
16
- "esm": "rimraf lib & npx tsc -p ./tsconfig.esm.json",
16
+ "esm": "rimraf lib & npx esbuild ./src/**/*.ts --outdir=lib",
17
17
  "types": "rimraf types & npx tsc -p ./tsconfig.types.json & node ./__devs/type.cjs",
18
18
  "trove": "node ./__devs/trove.cjs",
19
19
  "trove:build": "npm run devs & npm run trove",
@@ -43,6 +43,7 @@
43
43
  "@types/mocha": "^10.0.1",
44
44
  "@types/node": "^20.4.5",
45
45
  "@vope/types": "^1.0.2",
46
+ "esbuild": "^0.19.4",
46
47
  "iconv-lite": "^0.6.3",
47
48
  "mocha": "^10.2.0",
48
49
  "rimraf": "^5.0.1",
@@ -50,9 +51,6 @@
50
51
  "tslib": "^2.6.1",
51
52
  "typescript": "^5.1.6"
52
53
  },
53
- "dependencies": {
54
- "axios": "^1.4.0"
55
- },
56
54
  "files": [
57
55
  "cjs",
58
56
  "lib",
@@ -1,16 +1,10 @@
1
- declare enum Encodes {
2
- UTF_8 = "utf-8",
3
- UTF_16LE = "utf-16le",
4
- UTF_16BE = "utf-16be",
5
- GBK = "gbk"
6
- }
7
1
  /**
8
2
  * 根据文件内容判断文件的编码
9
3
  * @param uint 文件内容Uint8Array格式
10
4
  * @param limit 用于约束GBK和utf_8的判定上限
11
5
  */
12
6
  declare const getEncoding: (uint: Uint8Array, limit?: number) => {
13
- encoding: Encodes;
7
+ encoding: "utf-8" | "utf-16le" | "utf-16be" | "gbk";
14
8
  offset: number;
15
9
  };
16
10
  export default getEncoding;
package/types/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export { apiPost, apiGet } from './api';
2
1
  export { default as clamp } from './clamp';
3
2
  export { default as classnames } from './classnames';
4
3
  export { default as compareVersion } from './compare/version';
package/cjs/api.js DELETED
@@ -1,56 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.apiGet = exports.apiPost = void 0;
7
- const params_1 = __importDefault(require("./url/params"));
8
- const cut = (config, key) => {
9
- if (key in config) {
10
- const value = config[key];
11
- delete config[key];
12
- return value;
13
- }
14
- };
15
- const apiPost = (axios, handle) => {
16
- function post(url, options, config) {
17
- let twist;
18
- let rejectValue = false;
19
- if (config) {
20
- twist = cut(config, 'twist');
21
- const rejectValue0 = cut(config, 'rejectValue');
22
- rejectValue0 !== void 0 && (rejectValue = rejectValue0);
23
- }
24
- return new Promise((resolve) => {
25
- axios.post(url, options, config).then(
26
- // @ts-ignore
27
- resolve, (e) => {
28
- twist ? twist(e) : handle(e);
29
- resolve(rejectValue);
30
- });
31
- });
32
- }
33
- return post;
34
- };
35
- exports.apiPost = apiPost;
36
- const apiGet = (axios, handle) => {
37
- function get(url, options, config) {
38
- let twist;
39
- let rejectValue = false;
40
- if (config) {
41
- twist = cut(config, 'twist');
42
- const rejectValue0 = cut(config, 'rejectValue');
43
- rejectValue0 !== void 0 && (rejectValue = rejectValue0);
44
- }
45
- return new Promise((resolve) => {
46
- axios.get((0, params_1.default)(url, options), config).then(
47
- // @ts-ignore
48
- resolve, (e) => {
49
- twist ? twist(e) : handle(e);
50
- resolve(rejectValue);
51
- });
52
- });
53
- }
54
- return get;
55
- };
56
- exports.apiGet = apiGet;
package/lib/api.js DELETED
@@ -1,48 +0,0 @@
1
- import connectURLParams from './url/params';
2
- const cut = (config, key) => {
3
- if (key in config) {
4
- const value = config[key];
5
- delete config[key];
6
- return value;
7
- }
8
- };
9
- export const apiPost = (axios, handle) => {
10
- function post(url, options, config) {
11
- let twist;
12
- let rejectValue = false;
13
- if (config) {
14
- twist = cut(config, 'twist');
15
- const rejectValue0 = cut(config, 'rejectValue');
16
- rejectValue0 !== void 0 && (rejectValue = rejectValue0);
17
- }
18
- return new Promise((resolve) => {
19
- axios.post(url, options, config).then(
20
- // @ts-ignore
21
- resolve, (e) => {
22
- twist ? twist(e) : handle(e);
23
- resolve(rejectValue);
24
- });
25
- });
26
- }
27
- return post;
28
- };
29
- export const apiGet = (axios, handle) => {
30
- function get(url, options, config) {
31
- let twist;
32
- let rejectValue = false;
33
- if (config) {
34
- twist = cut(config, 'twist');
35
- const rejectValue0 = cut(config, 'rejectValue');
36
- rejectValue0 !== void 0 && (rejectValue = rejectValue0);
37
- }
38
- return new Promise((resolve) => {
39
- axios.get(connectURLParams(url, options), config).then(
40
- // @ts-ignore
41
- resolve, (e) => {
42
- twist ? twist(e) : handle(e);
43
- resolve(rejectValue);
44
- });
45
- });
46
- }
47
- return get;
48
- };
package/types/api.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import { ReviseRequired } from '@vope/types';
2
- import { AxiosInstance, AxiosRequestConfig } from 'axios';
3
- import connectURLParams from './url/params';
4
- interface Config<V> extends AxiosRequestConfig {
5
- /** 变更默认错误返回值 */
6
- rejectValue?: V;
7
- /** 错误拦截 */
8
- twist?: (e: Error) => void;
9
- }
10
- type Options = NonNullable<Parameters<typeof connectURLParams>[1]>;
11
- export declare const apiPost: (axios: AxiosInstance, handle: (e: Error) => void) => {
12
- <T, V extends false = false>(url: string, options?: Options, config?: Config<V> | undefined): Promise<T | V>;
13
- <T_1, V_1 extends string | symbol>(url: string, options: Options | undefined, config: ReviseRequired<Config<V_1>, "rejectValue">): Promise<T_1 | V_1>;
14
- };
15
- export declare const apiGet: (axios: AxiosInstance, handle: (e: Error) => void) => {
16
- <T, V extends false = false>(url: string, options?: Options, config?: Config<V> | undefined): Promise<T | V>;
17
- <T_1, V_1 extends string | symbol>(url: string, options: Options | undefined, config: ReviseRequired<Config<V_1>, "rejectValue">): Promise<T_1 | V_1>;
18
- };
19
- export {};