@wzyjs/utils 0.2.14 → 0.2.20

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,2 @@
1
+ export declare const shuffleArray: <T>(array: T[]) => T[];
2
+ export declare const removeSimilarDuplicates: (arr: string[], threshold: number, isLog?: boolean) => string[];
@@ -0,0 +1,68 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/common/array.ts
20
+ var array_exports = {};
21
+ __export(array_exports, {
22
+ removeSimilarDuplicates: () => removeSimilarDuplicates,
23
+ shuffleArray: () => shuffleArray
24
+ });
25
+ module.exports = __toCommonJS(array_exports);
26
+ var import_string = require("./string");
27
+ var shuffleArray = (array) => {
28
+ const shuffledArray = [...array];
29
+ for (let i = shuffledArray.length - 1; i > 0; i--) {
30
+ const j = Math.floor(Math.random() * (i + 1));
31
+ [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
32
+ }
33
+ return shuffledArray;
34
+ };
35
+ var removeSimilarDuplicates = (arr, threshold, isLog) => {
36
+ const uniqueArray = [];
37
+ const similarPairs = [];
38
+ for (const str of arr) {
39
+ const isSimilar = uniqueArray.some((uniqueStr) => {
40
+ const distance = (0, import_string.levenshteinDistance)(str, uniqueStr);
41
+ if (distance <= threshold) {
42
+ similarPairs.push([str, uniqueStr]);
43
+ return true;
44
+ }
45
+ return false;
46
+ });
47
+ if (!isSimilar) {
48
+ uniqueArray.push(str);
49
+ }
50
+ }
51
+ if (isLog) {
52
+ if (similarPairs.length > 0) {
53
+ similarPairs.forEach((pair) => {
54
+ console.log(pair[0]);
55
+ console.log(pair[1]);
56
+ console.log();
57
+ });
58
+ } else {
59
+ console.log("没有找到类似的字符串");
60
+ }
61
+ }
62
+ return uniqueArray;
63
+ };
64
+ // Annotate the CommonJS export names for ESM import in node:
65
+ 0 && (module.exports = {
66
+ removeSimilarDuplicates,
67
+ shuffleArray
68
+ });
@@ -8,5 +8,6 @@ export * from './object';
8
8
  export * from './other';
9
9
  export * from './number';
10
10
  export * from './classs';
11
+ export * from './array';
11
12
  export { default as _ } from 'lodash';
12
13
  export { default as axios } from 'axios';
@@ -66,6 +66,7 @@ __reExport(common_exports, require("./object"), module.exports);
66
66
  __reExport(common_exports, require("./other"), module.exports);
67
67
  __reExport(common_exports, require("./number"), module.exports);
68
68
  __reExport(common_exports, require("./classs"), module.exports);
69
+ __reExport(common_exports, require("./array"), module.exports);
69
70
  var import_lodash2 = __toESM(require("lodash"));
70
71
  var import_axios = __toESM(require("axios"));
71
72
  // Annotate the CommonJS export names for ESM import in node:
@@ -100,5 +101,6 @@ var import_axios = __toESM(require("axios"));
100
101
  ...require("./object"),
101
102
  ...require("./other"),
102
103
  ...require("./number"),
103
- ...require("./classs")
104
+ ...require("./classs"),
105
+ ...require("./array")
104
106
  });
@@ -1,7 +1,10 @@
1
1
  export declare const getChineseByStr: (str: string) => string;
2
2
  export declare const getStrLength: (value: string) => number;
3
3
  export declare const replaceAll: (str: string, searchValue: string, replaceValue: string) => string;
4
- export declare const replaceByRules: (str: string, rules: [string, string][]) => string;
4
+ export declare const replaceByRules: (str: string, rules: [
5
+ string,
6
+ string
7
+ ][]) => string;
5
8
  export declare const getType: (value: any) => string;
6
9
  export declare const amount: (str: string) => string;
7
10
  export declare const jsonParse: (value: string | object) => object;
@@ -15,3 +18,4 @@ export declare const getProxyUrl: (url: string) => string;
15
18
  export declare const getLength: (value: string) => number;
16
19
  export declare const getCookie: (name: string) => string | null;
17
20
  export declare const generateAlphabetArray: (n: number) => string[];
21
+ export declare const levenshteinDistance: (a: string, b: string) => number;
@@ -33,6 +33,7 @@ __export(string_exports, {
33
33
  getType: () => getType,
34
34
  isJson: () => isJson,
35
35
  jsonParse: () => jsonParse,
36
+ levenshteinDistance: () => levenshteinDistance,
36
37
  replaceAll: () => replaceAll,
37
38
  replaceByRules: () => replaceByRules,
38
39
  toString: () => toString
@@ -140,6 +141,32 @@ var getCookie = (name) => {
140
141
  var generateAlphabetArray = (n) => {
141
142
  return Array.from({ length: n }, (_, i) => String.fromCharCode(97 + i));
142
143
  };
144
+ var levenshteinDistance = (a, b) => {
145
+ const matrix = [];
146
+ for (let i = 0; i <= b.length; i++) {
147
+ matrix[i] = [i];
148
+ }
149
+ for (let j = 0; j <= a.length; j++) {
150
+ matrix[0][j] = j;
151
+ }
152
+ for (let i = 1; i <= b.length; i++) {
153
+ for (let j = 1; j <= a.length; j++) {
154
+ if (b[i - 1] === a[j - 1]) {
155
+ matrix[i][j] = matrix[i - 1][j - 1];
156
+ } else {
157
+ matrix[i][j] = Math.min(
158
+ matrix[i - 1][j - 1] + 1,
159
+ // 替换
160
+ matrix[i][j - 1] + 1,
161
+ // 插入
162
+ matrix[i - 1][j] + 1
163
+ // 删除
164
+ );
165
+ }
166
+ }
167
+ }
168
+ return matrix[b.length][a.length];
169
+ };
143
170
  // Annotate the CommonJS export names for ESM import in node:
144
171
  0 && (module.exports = {
145
172
  amount,
@@ -156,6 +183,7 @@ var generateAlphabetArray = (n) => {
156
183
  getType,
157
184
  isJson,
158
185
  jsonParse,
186
+ levenshteinDistance,
159
187
  replaceAll,
160
188
  replaceByRules,
161
189
  toString
@@ -0,0 +1,8 @@
1
+ {
2
+ "larkToken": {
3
+ "code": 0,
4
+ "expire": 1722045305833,
5
+ "msg": "ok",
6
+ "tenant_access_token": "t-g1047r7TWH2YXAQUACCHV5UTGPH35SJ5ROXFDUZI"
7
+ }
8
+ }
@@ -0,0 +1,2 @@
1
+ export declare const shuffleArray: <T>(array: T[]) => T[];
2
+ export declare const removeSimilarDuplicates: (arr: string[], threshold: number, isLog?: boolean) => string[];
@@ -0,0 +1,65 @@
1
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
2
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
3
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
5
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
6
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
7
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
8
+ import { levenshteinDistance } from "./string";
9
+
10
+ // 数组随机变换位置
11
+ export var shuffleArray = function shuffleArray(array) {
12
+ var shuffledArray = _toConsumableArray(array); // 创建数组的副本以避免修改原数组
13
+ for (var i = shuffledArray.length - 1; i > 0; i--) {
14
+ var j = Math.floor(Math.random() * (i + 1));
15
+ var _ref = [shuffledArray[j], shuffledArray[i]];
16
+ shuffledArray[i] = _ref[0];
17
+ shuffledArray[j] = _ref[1];
18
+ }
19
+ return shuffledArray;
20
+ };
21
+
22
+ // 去重函数,接受一个字符串数组,返回一个相似程度高的去重后的数组
23
+ export var removeSimilarDuplicates = function removeSimilarDuplicates(arr, threshold, isLog) {
24
+ var uniqueArray = [];
25
+ var similarPairs = []; // 用于存储相似的字符串对
26
+ var _iterator = _createForOfIteratorHelper(arr),
27
+ _step;
28
+ try {
29
+ var _loop = function _loop() {
30
+ var str = _step.value;
31
+ var isSimilar = uniqueArray.some(function (uniqueStr) {
32
+ var distance = levenshteinDistance(str, uniqueStr);
33
+ if (distance <= threshold) {
34
+ similarPairs.push([str, uniqueStr]); // 记录相似字符串对
35
+ return true;
36
+ }
37
+ return false;
38
+ });
39
+ if (!isSimilar) {
40
+ uniqueArray.push(str);
41
+ }
42
+ };
43
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
44
+ _loop();
45
+ }
46
+
47
+ // 打印相似的字符串
48
+ } catch (err) {
49
+ _iterator.e(err);
50
+ } finally {
51
+ _iterator.f();
52
+ }
53
+ if (isLog) {
54
+ if (similarPairs.length > 0) {
55
+ similarPairs.forEach(function (pair) {
56
+ console.log(pair[0]);
57
+ console.log(pair[1]);
58
+ console.log();
59
+ });
60
+ } else {
61
+ console.log('没有找到类似的字符串');
62
+ }
63
+ }
64
+ return uniqueArray;
65
+ };
@@ -8,5 +8,6 @@ export * from './object';
8
8
  export * from './other';
9
9
  export * from './number';
10
10
  export * from './classs';
11
+ export * from './array';
11
12
  export { default as _ } from 'lodash';
12
13
  export { default as axios } from 'axios';
@@ -9,5 +9,6 @@ export * from "./object";
9
9
  export * from "./other";
10
10
  export * from "./number";
11
11
  export * from "./classs";
12
+ export * from "./array";
12
13
  export { default as _ } from 'lodash';
13
14
  export { default as axios } from 'axios';
@@ -1,7 +1,10 @@
1
1
  export declare const getChineseByStr: (str: string) => string;
2
2
  export declare const getStrLength: (value: string) => number;
3
3
  export declare const replaceAll: (str: string, searchValue: string, replaceValue: string) => string;
4
- export declare const replaceByRules: (str: string, rules: [string, string][]) => string;
4
+ export declare const replaceByRules: (str: string, rules: [
5
+ string,
6
+ string
7
+ ][]) => string;
5
8
  export declare const getType: (value: any) => string;
6
9
  export declare const amount: (str: string) => string;
7
10
  export declare const jsonParse: (value: string | object) => object;
@@ -15,3 +18,4 @@ export declare const getProxyUrl: (url: string) => string;
15
18
  export declare const getLength: (value: string) => number;
16
19
  export declare const getCookie: (name: string) => string | null;
17
20
  export declare const generateAlphabetArray: (n: number) => string[];
21
+ export declare const levenshteinDistance: (a: string, b: string) => number;
@@ -146,4 +146,35 @@ export var generateAlphabetArray = function generateAlphabetArray(n) {
146
146
  }, function (_, i) {
147
147
  return String.fromCharCode(97 + i);
148
148
  });
149
+ };
150
+
151
+ // 计算文本相似度 (Levenshtein 距离算法)
152
+ export var levenshteinDistance = function levenshteinDistance(a, b) {
153
+ var matrix = [];
154
+
155
+ // 初始化矩阵
156
+ for (var i = 0; i <= b.length; i++) {
157
+ matrix[i] = [i];
158
+ }
159
+ for (var j = 0; j <= a.length; j++) {
160
+ matrix[0][j] = j;
161
+ }
162
+
163
+ // 填充矩阵
164
+ for (var _i2 = 1; _i2 <= b.length; _i2++) {
165
+ for (var _j = 1; _j <= a.length; _j++) {
166
+ if (b[_i2 - 1] === a[_j - 1]) {
167
+ matrix[_i2][_j] = matrix[_i2 - 1][_j - 1];
168
+ } else {
169
+ matrix[_i2][_j] = Math.min(matrix[_i2 - 1][_j - 1] + 1,
170
+ // 替换
171
+ matrix[_i2][_j - 1] + 1,
172
+ // 插入
173
+ matrix[_i2 - 1][_j] + 1 // 删除
174
+ );
175
+ }
176
+ }
177
+ }
178
+
179
+ return matrix[b.length][a.length];
149
180
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wzyjs/utils",
3
- "version": "0.2.14",
3
+ "version": "0.2.20",
4
4
  "description": "description",
5
5
  "author": "wzy",
6
6
  "scripts": {
@@ -64,5 +64,5 @@
64
64
  "type": "git",
65
65
  "url": "https://gitee.com/wang-zhenyu/app.git"
66
66
  },
67
- "gitHead": "4debf634852c7e5375d4dc5616e4e607005761c2"
67
+ "gitHead": "9664b6dbd9a3cb761c1eceae76b8615f3ab4c018"
68
68
  }
@@ -1,19 +0,0 @@
1
- import md5 from 'md5';
2
- import classnames from 'classnames';
3
- import copy from 'copy-to-clipboard';
4
- import anime from 'animejs';
5
- export { copy, classnames, md5, anime };
6
- export declare const localforage: {
7
- config: {
8
- (options: LocalForageOptions): boolean;
9
- (options: string): any;
10
- (): LocalForageOptions;
11
- };
12
- setItem: <T>(key: string, value: T, callback?: ((err: any, value: T) => void) | undefined) => Promise<T>;
13
- getItem: <T_1>(key: string, callback?: ((err: any, value: T_1 | null) => void) | undefined) => Promise<T_1 | null>;
14
- removeItem: (key: string, callback?: ((err: any) => void) | undefined) => Promise<void>;
15
- };
16
- export * from './style';
17
- export * from './element';
18
- export * from './other';
19
- export * from './initHttpClient';
@@ -1,4 +0,0 @@
1
- export declare const initHttpClient: (setupHttpClient: any, option: {
2
- timeout: number;
3
- onError: any;
4
- }) => void;
@@ -1,19 +0,0 @@
1
- import dayjs from 'dayjs';
2
- import isBetween from 'dayjs/plugin/isBetween';
3
- import updateLocale from 'dayjs/plugin/updateLocale';
4
- import timezone from 'dayjs/plugin/timezone';
5
- import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
6
- import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
7
- import 'dayjs/locale/zh-cn';
8
- export declare enum Timezone {
9
- AsiaShanghai = "Asia/Shanghai"
10
- }
11
- export { Dayjs } from 'dayjs';
12
- declare const _default: typeof dayjs & {
13
- isBetween: typeof isBetween;
14
- isSameOrAfter: typeof isSameOrAfter;
15
- isSameOrBefore: typeof isSameOrBefore;
16
- tz: typeof timezone;
17
- updateLocale: typeof updateLocale;
18
- };
19
- export default _default;
@@ -1 +0,0 @@
1
- export declare const replaceContentInFile: (filePath: string, targetContent: string, replacement: string) => Promise<void>;
@@ -1,19 +0,0 @@
1
- import md5 from 'md5';
2
- import classnames from 'classnames';
3
- import copy from 'copy-to-clipboard';
4
- import anime from 'animejs';
5
- export { copy, classnames, md5, anime };
6
- export declare const localforage: {
7
- config: {
8
- (options: LocalForageOptions): boolean;
9
- (options: string): any;
10
- (): LocalForageOptions;
11
- };
12
- setItem: <T>(key: string, value: T, callback?: ((err: any, value: T) => void) | undefined) => Promise<T>;
13
- getItem: <T_1>(key: string, callback?: ((err: any, value: T_1 | null) => void) | undefined) => Promise<T_1 | null>;
14
- removeItem: (key: string, callback?: ((err: any) => void) | undefined) => Promise<void>;
15
- };
16
- export * from './style';
17
- export * from './element';
18
- export * from './other';
19
- export * from './initHttpClient';
@@ -1,4 +0,0 @@
1
- export declare const initHttpClient: (setupHttpClient: any, option: {
2
- timeout: number;
3
- onError: any;
4
- }) => void;
@@ -1,19 +0,0 @@
1
- import dayjs from 'dayjs';
2
- import isBetween from 'dayjs/plugin/isBetween';
3
- import updateLocale from 'dayjs/plugin/updateLocale';
4
- import timezone from 'dayjs/plugin/timezone';
5
- import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
6
- import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
7
- import 'dayjs/locale/zh-cn';
8
- export declare enum Timezone {
9
- AsiaShanghai = "Asia/Shanghai"
10
- }
11
- export { Dayjs } from 'dayjs';
12
- declare const _default: typeof dayjs & {
13
- isBetween: typeof isBetween;
14
- isSameOrAfter: typeof isSameOrAfter;
15
- isSameOrBefore: typeof isSameOrBefore;
16
- tz: typeof timezone;
17
- updateLocale: typeof updateLocale;
18
- };
19
- export default _default;
@@ -1 +0,0 @@
1
- export declare const replaceContentInFile: (filePath: string, targetContent: string, replacement: string) => Promise<void>;