devix 0.1.2 → 1.0.3

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/dist/index.js ADDED
@@ -0,0 +1,300 @@
1
+ //#region src/typeof.ts
2
+ function getDataType(target) {
3
+ const type = typeof target;
4
+ return type !== "object" ? type : Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
5
+ }
6
+ const typeCheckRules = {
7
+ undefined: (target) => typeof target === "undefined",
8
+ null: (target) => target === null,
9
+ number: (target) => typeof target === "number",
10
+ string: (target) => typeof target === "string",
11
+ boolean: (target) => typeof target === "boolean",
12
+ symbol: (target) => typeof target === "symbol",
13
+ bigint: (target) => typeof target === "bigint",
14
+ object: (target) => target !== null && typeof target === "object",
15
+ array: (target) => Array.isArray(target),
16
+ function: (target) => typeof target === "function",
17
+ date: (target) => target instanceof Date,
18
+ set: (target) => target instanceof Set,
19
+ map: (target) => target instanceof Map,
20
+ regexp: (target) => target instanceof RegExp,
21
+ promise: (target) => target instanceof Promise,
22
+ empty(target) {
23
+ return this.string(target) && target === "";
24
+ }
25
+ };
26
+ const isType = (type, target) => {
27
+ const dataType = typeCheckRules[type] ? typeCheckRules[type](target) : getDataType(target) === type;
28
+ return dataType;
29
+ };
30
+
31
+ //#endregion
32
+ //#region src/clone.ts
33
+ //! Function Shallow Copy
34
+ function shallowClone(source) {
35
+ if (isType("array", source)) return source.slice();
36
+ if (isType("object", source)) return { ...source };
37
+ return source;
38
+ }
39
+ //! Function Deep Copy
40
+ const isFormat = (target) => isType("object", target) || isType("function", target);
41
+ function handleSpeciBoundar(source, dpClone, hash) {
42
+ if (isType("symbol", source)) return Symbol(source.description);
43
+ if (!isFormat(source)) return source;
44
+ if (isType("set", source)) {
45
+ const newSet = /* @__PURE__ */ new Set();
46
+ source.forEach((value) => newSet.add(dpClone(value, hash)));
47
+ return newSet;
48
+ }
49
+ if (isType("map", source)) {
50
+ const newMap = /* @__PURE__ */ new Map();
51
+ source.forEach((value, key) => newMap.set(key, dpClone(value, hash)));
52
+ return newMap;
53
+ }
54
+ return null;
55
+ }
56
+ function deepClone(source, hash = /* @__PURE__ */ new WeakMap()) {
57
+ if (hash.get(source)) return hash.get(source);
58
+ const result = handleSpeciBoundar(source, deepClone, hash);
59
+ if (result) return result;
60
+ const isArray = isType("array", source);
61
+ const cloneObject = isArray ? [] : {};
62
+ hash.set(source, cloneObject);
63
+ if (isArray) source.forEach((item, index) => {
64
+ cloneObject[index] = deepClone(item, hash);
65
+ });
66
+ else {
67
+ Object.keys(source).forEach((key) => {
68
+ cloneObject[key] = deepClone(source[key], hash);
69
+ });
70
+ Object.getOwnPropertySymbols(source).forEach((sym) => {
71
+ cloneObject[Symbol(sym.description)] = deepClone(source[sym], hash);
72
+ });
73
+ }
74
+ return cloneObject;
75
+ }
76
+
77
+ //#endregion
78
+ //#region src/others.ts
79
+ function currying(fn) {
80
+ function curried(...args) {
81
+ if (args.length >= fn.length) return fn.apply(this, args);
82
+ return function subCurried(...args2) {
83
+ return curried.apply(this, args.concat(args2));
84
+ };
85
+ }
86
+ return curried;
87
+ }
88
+ function compose(...fns) {
89
+ const { length } = fns;
90
+ if (length <= 0) return null;
91
+ for (let i = 0; i < length; i++) {
92
+ const fn = fns[i];
93
+ if (typeof fn !== "function") throw new TypeError(`argument with index ${i} is not a function`);
94
+ }
95
+ function executeFn(...args) {
96
+ let index = 0;
97
+ let result = fns[index].apply(this, args);
98
+ while (++index < length) result = fns[index].call(this, result);
99
+ return result;
100
+ }
101
+ return executeFn;
102
+ }
103
+ function insertStr(soure, start, newStr) {
104
+ return soure.slice(0, start) + newStr + soure.slice(start);
105
+ }
106
+ function setCaseType(soure, type) {
107
+ const newStr = soure.slice(0, 1)[type === "upper" ? "toUpperCase" : "toLowerCase"]() + soure.slice(1).toLowerCase();
108
+ return newStr;
109
+ }
110
+ function stringCase(soure, separa = ["", ""], cases = ["upper", "upper"]) {
111
+ const [separator, separate] = separa;
112
+ const [firstCase, argsCase] = cases;
113
+ const newStr = soure.split(separator);
114
+ for (let i = 0; i < newStr.length; i++) newStr[i] = setCaseType(newStr[i], i === 0 ? firstCase : argsCase);
115
+ return newStr.join(separate);
116
+ }
117
+ const transformGetParams = (params) => {
118
+ let result = "";
119
+ for (const propName of Object.keys(params)) {
120
+ const value = params[propName];
121
+ const part = `${encodeURIComponent(propName)}=`;
122
+ if (isType("empty", value)) continue;
123
+ if (!isType("object", value)) {
124
+ result += `${part + encodeURIComponent(value)}&`;
125
+ continue;
126
+ }
127
+ for (const key of Object.keys(value)) {
128
+ if (!isType("empty", value)) continue;
129
+ const paramsStr = `${propName}[${key}]`;
130
+ const subPart = `${encodeURIComponent(paramsStr)}=`;
131
+ result += `${subPart + encodeURIComponent(value[key])}&`;
132
+ }
133
+ }
134
+ return result.slice(0, -1);
135
+ };
136
+
137
+ //#endregion
138
+ //#region src/retalimit.ts
139
+ function debounce(callback, delay = 0, immediate = false) {
140
+ let timer = null;
141
+ let isInvoke = false;
142
+ function subDebounce(...args) {
143
+ return new Promise((resolve, reject) => {
144
+ if (timer !== null) clearTimeout(timer);
145
+ if (immediate && !isInvoke) {
146
+ try {
147
+ const result = callback.apply(this, args);
148
+ resolve(result);
149
+ } catch (error) {
150
+ reject(error);
151
+ }
152
+ isInvoke = true;
153
+ return;
154
+ }
155
+ timer = setTimeout(() => {
156
+ try {
157
+ const result = callback.apply(this, args);
158
+ resolve(result);
159
+ } catch (error) {
160
+ reject(error);
161
+ } finally {
162
+ timer = null;
163
+ isInvoke = false;
164
+ }
165
+ }, delay);
166
+ });
167
+ }
168
+ subDebounce.cancel = () => {
169
+ if (timer !== null) clearTimeout(timer);
170
+ timer = null;
171
+ isInvoke = false;
172
+ };
173
+ return subDebounce;
174
+ }
175
+ function throttle(callback, interval, options = {}) {
176
+ const { leading = true, trailing = false } = options;
177
+ let startTime = 0;
178
+ let timer = null;
179
+ function subThrottle(...args) {
180
+ return new Promise((resolve, reject) => {
181
+ try {
182
+ const nowTime = Date.now();
183
+ let result;
184
+ if (!leading && startTime === 0) startTime = nowTime;
185
+ const waitTime = interval - (nowTime - startTime);
186
+ if (waitTime <= 0) {
187
+ if (timer) clearTimeout(timer);
188
+ result = callback.apply(this, args);
189
+ resolve(result);
190
+ startTime = nowTime;
191
+ timer = null;
192
+ return;
193
+ }
194
+ if (trailing && !timer) timer = setTimeout(() => {
195
+ result = callback.apply(this, args);
196
+ resolve(result);
197
+ startTime = Date.now();
198
+ timer = null;
199
+ }, waitTime);
200
+ } catch (error) {
201
+ reject(error);
202
+ }
203
+ });
204
+ }
205
+ subThrottle.cancel = () => {
206
+ if (timer) clearTimeout(timer);
207
+ startTime = 0;
208
+ timer = null;
209
+ };
210
+ return subThrottle;
211
+ }
212
+
213
+ //#endregion
214
+ //#region src/sort.ts
215
+ let SortType = /* @__PURE__ */ function(SortType$1) {
216
+ SortType$1["ASC"] = "ASC";
217
+ SortType$1["DESC"] = "DESC";
218
+ return SortType$1;
219
+ }({});
220
+ function swap(array, index1, index2) {
221
+ [array[index1], array[index2]] = [array[index2], array[index1]];
222
+ }
223
+ function compare(value1, value2, type) {
224
+ return type === SortType.ASC ? value1 > value2 : value1 < value2;
225
+ }
226
+ function bubblingSort(array, type = "ASC", key) {
227
+ const { length } = array;
228
+ if (length < 2) return array;
229
+ for (let i = 0; i < length - 1; i++) for (let j = 0; j < length - 1 - i; j++) {
230
+ const value1 = key ? array[j][key] : array[j];
231
+ const value2 = key ? array[j + 1][key] : array[j + 1];
232
+ if (compare(value1, value2, type)) swap(array, j, j + 1);
233
+ }
234
+ return array;
235
+ }
236
+
237
+ //#endregion
238
+ //#region src/timer.ts
239
+ const formatRules = new Map([
240
+ ["yyyy", "year"],
241
+ ["MM", "month"],
242
+ ["dd", "day"],
243
+ ["HH", "hours"],
244
+ ["mm", "minutes"],
245
+ ["ss", "seconds"],
246
+ ["W", "week"]
247
+ ]);
248
+ const WeekList = new Map([
249
+ [1, "一"],
250
+ [2, "二"],
251
+ [3, "三"],
252
+ [4, "四"],
253
+ [5, "五"],
254
+ [6, "六"],
255
+ [0, "日"]
256
+ ]);
257
+ function processWeek(weekNum) {
258
+ return WeekList.get(weekNum) || "";
259
+ }
260
+ function formatNumber(value) {
261
+ return value.toString().padStart(2, "0");
262
+ }
263
+ function createTimerFormatObj(date) {
264
+ const dayOfWeek = date.getDay() === 0 ? 7 : date.getDay();
265
+ return {
266
+ year: date.getFullYear().toString(),
267
+ month: formatNumber(date.getMonth() + 1),
268
+ day: formatNumber(date.getDate()),
269
+ hours: formatNumber(date.getHours()),
270
+ minutes: formatNumber(date.getMinutes()),
271
+ seconds: formatNumber(date.getSeconds()),
272
+ week: processWeek(dayOfWeek),
273
+ weekNum: dayOfWeek.toString()
274
+ };
275
+ }
276
+ const formatTimer = (cellValue, formatOption = "yyyy-MM-dd HH:mm:ss") => {
277
+ if (!cellValue) return (/* @__PURE__ */ new Date()).toISOString();
278
+ const date = new Date(cellValue);
279
+ const timerFormatObj = createTimerFormatObj(date);
280
+ if (isType("string", formatOption) && !formatOption.trim()) return timerFormatObj;
281
+ const timerFormatStr = Array.from(formatRules).reduce((currentFormat, [rule, key]) => {
282
+ return currentFormat.replace(new RegExp(rule, "g"), timerFormatObj[key]);
283
+ }, formatOption);
284
+ return timerFormatStr;
285
+ };
286
+ async function setTimer(execute, delay = 0, immediate = false) {
287
+ let timer = null;
288
+ const interval = async () => {
289
+ await execute();
290
+ timer = setTimeout(interval, delay);
291
+ };
292
+ if (immediate) await execute();
293
+ setTimeout(interval, delay);
294
+ return { cancel: () => {
295
+ if (timer !== null) clearTimeout(timer);
296
+ } };
297
+ }
298
+
299
+ //#endregion
300
+ export { SortType, bubblingSort, compose, currying, debounce, deepClone, formatTimer, insertStr, isType, setTimer, shallowClone, stringCase, throttle, transformGetParams };
package/package.json CHANGED
@@ -1,82 +1,51 @@
1
1
  {
2
2
  "name": "devix",
3
- "type": "module",
4
- "version": "0.1.2",
3
+ "version": "1.0.3",
5
4
  "description": "Devix is a comprehensive, powerful, and compact JavaScript utility library.",
6
- "author": "king-3 <w2196592083@gmail.com>",
5
+ "type": "module",
6
+ "keywords": [
7
+ "devix",
8
+ "development tools"
9
+ ],
7
10
  "license": "MIT",
8
11
  "homepage": "https://github.com/OpenKnights/devix#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/OpenKnights/devix/issues"
14
+ },
9
15
  "repository": {
10
16
  "type": "git",
11
17
  "url": "git+https://github.com/OpenKnights/devix.git"
12
18
  },
13
- "bugs": {
14
- "url": "https://github.com/OpenKnights/devix/issues"
15
- },
16
- "keywords": [
17
- "template",
18
- "library"
19
+ "author": "king-3 <w2196592083@gmail.com>",
20
+ "files": [
21
+ "dist/"
19
22
  ],
23
+ "main": "./dist/index.cjs",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
20
26
  "exports": {
21
27
  ".": {
22
- "types": "./index.d.ts",
28
+ "types": "./dist/index.d.ts",
23
29
  "require": "./dist/index.cjs.js",
24
30
  "import": "./dist/index.esm.js"
25
31
  },
26
- "./lib/**": {
27
- "types": "./lib/**/index.d.ts",
28
- "require": "./lib/**/index.cjs.js",
29
- "import": "./lib/**/index.esm.js"
30
- },
31
32
  "./*": "./*"
32
33
  },
33
- "types": "./index.d.ts",
34
- "main": "./dist/index.cjs.js",
35
- "module": "./dist/index.esm.js",
36
- "files": [
37
- "dist/",
38
- "lib/",
39
- "package.json",
40
- "README.md",
41
- "index.d.ts"
42
- ],
43
- "scripts": {
44
- "build": "rollup -c",
45
- "clear": "pwsh clear.ps1",
46
- "test": "jest",
47
- "lint": "npx eslint src/**/*.ts && npx eslint test/**/*.ts && npx eslint types/**/*.ts",
48
- "prettier": "npx prettier --config .prettierrc.json --write ./**/*.{ts,json}",
49
- "releases": "pwsh publish.ps1",
50
- "versihint": "npx bump"
51
- },
52
34
  "devDependencies": {
53
- "@babel/core": "^7.24.5",
54
- "@babel/preset-env": "^7.24.5",
55
- "@babel/preset-typescript": "^7.24.1",
56
- "@rollup/plugin-alias": "^5.1.0",
57
- "@rollup/plugin-babel": "^6.0.4",
58
- "@rollup/plugin-commonjs": "^25.0.7",
59
- "@rollup/plugin-json": "^6.1.0",
60
- "@rollup/plugin-node-resolve": "^15.2.3",
61
- "@types/jest": "^29.5.12",
62
- "@typescript-eslint/eslint-plugin": "^7.8.0",
63
- "@typescript-eslint/parser": "^7.8.0",
64
- "esbuild": "^0.21.0",
65
- "eslint": "^8.57.0",
66
- "eslint-config-airbnb-base": "^15.0.0",
67
- "eslint-config-airbnb-typescript": "^18.0.0",
68
- "eslint-config-prettier": "^9.1.0",
69
- "eslint-plugin-import": "^2.29.1",
70
- "eslint-plugin-prettier": "^5.1.3",
71
- "jest": "^29.7.0",
72
- "prettier": "^3.2.5",
73
- "rollup": "^4.17.2",
74
- "rollup-plugin-dts": "^6.1.0",
75
- "rollup-plugin-esbuild": "^6.1.1",
76
- "rollup-plugin-typescript2": "^0.36.0",
77
- "ts-jest": "^29.1.2",
78
- "tslib": "^2.6.2",
79
- "typescript": "^5.4.5",
80
- "version-bump-prompt": "^6.1.0"
35
+ "@king-3/eslint-config": "^1.0.3",
36
+ "bumpp": "^10.2.3",
37
+ "eslint": "^9.33.0",
38
+ "prettier": "^3.6.2",
39
+ "tsdown": "^0.14.1",
40
+ "typescript": "^5.9.2",
41
+ "vitest": "^3.2.4"
42
+ },
43
+ "scripts": {
44
+ "build": "tsdown --clear --dts",
45
+ "test": "vitest run",
46
+ "lint": "eslint",
47
+ "lint:fix": "eslint --fix",
48
+ "format": "prettier --write ./**/*.{ts,json,md}",
49
+ "release": "bumpp && pnpm publish"
81
50
  }
82
- }
51
+ }