devix 0.0.22 → 0.0.23-beta.10

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.mjs ADDED
@@ -0,0 +1,292 @@
1
+ function getDataType(target) {
2
+ const type = typeof target;
3
+ return type != "object" ? type : Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
4
+ }
5
+ const typeCheckers = {
6
+ // DataType
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
+ // ObjectType
15
+ object: (target) => target !== null && typeof target === "object",
16
+ array: (target) => Array.isArray(target),
17
+ date: (target) => target instanceof Date,
18
+ function: (target) => typeof target === "function",
19
+ set: (target) => target instanceof Set,
20
+ map: (target) => target instanceof Map,
21
+ regexp: (target) => target instanceof RegExp,
22
+ promise: (target) => target instanceof Promise,
23
+ // ExpandType
24
+ empty: (target) => !(target !== null && target !== "" && typeof target !== "undefined")
25
+ };
26
+ const isType = (type, target) => {
27
+ const dataType = typeCheckers[type] ? typeCheckers[type](target) : getDataType(target) === type;
28
+ return dataType;
29
+ };
30
+
31
+ function currying(fn) {
32
+ function curried(...args) {
33
+ if (args.length >= fn.length) {
34
+ return fn.apply(this, args);
35
+ } else {
36
+ return function(...args2) {
37
+ return curried.apply(this, args.concat(args2));
38
+ };
39
+ }
40
+ }
41
+ return curried;
42
+ }
43
+ function compose(...fns) {
44
+ const length = fns.length;
45
+ if (length <= 0) return;
46
+ for (let i = 0; i < length; i++) {
47
+ const fn = fns[i];
48
+ if (typeof fn !== "function") {
49
+ throw new Error(`argument with index ${i} is not a function`);
50
+ }
51
+ }
52
+ return function(...args) {
53
+ let index = 0;
54
+ let result = fns[index].apply(this, args);
55
+ while (++index < length) {
56
+ result = fns[index].call(this, result);
57
+ }
58
+ return result;
59
+ };
60
+ }
61
+ function insertStr(soure, start, newStr) {
62
+ return soure.slice(0, start) + newStr + soure.slice(start);
63
+ }
64
+ function stringCase(soure, separa = ["", ""], cases = ["upper", "upper"]) {
65
+ const [separator, separate] = separa;
66
+ const [firstCase, argsCase] = cases;
67
+ const newStr = soure.split(separator);
68
+ for (let i = 0; i < newStr.length; i++) {
69
+ newStr[i] = setCaseType(newStr[i], i === 0 ? firstCase : argsCase);
70
+ }
71
+ return newStr.join(separate);
72
+ }
73
+ function setCaseType(soure, caseType) {
74
+ const newStr = soure.slice(0, 1)[caseType === "upper" ? "toUpperCase" : "toLowerCase"]() + soure.slice(1).toLowerCase();
75
+ return newStr;
76
+ }
77
+ const transformGetParams = (params) => {
78
+ let result = "";
79
+ for (const propName of Object.keys(params)) {
80
+ const value = params[propName];
81
+ const part = `${encodeURIComponent(propName)}=`;
82
+ if (isType("empty", value)) continue;
83
+ if (!isType("object", value)) {
84
+ result += `${part + encodeURIComponent(value)}&`;
85
+ continue;
86
+ }
87
+ for (const key of Object.keys(value)) {
88
+ if (isType("empty", value)) continue;
89
+ const params2 = propName + `[${key}]`;
90
+ const subPart = `${encodeURIComponent(params2)}=`;
91
+ result += `${subPart + encodeURIComponent(value[key])}&`;
92
+ }
93
+ }
94
+ return result.slice(0, -1);
95
+ };
96
+
97
+ function debounce(callback, delay = 0, immediate = false) {
98
+ let timer = null;
99
+ let isInvoke = false;
100
+ function _debounce(...args) {
101
+ return new Promise((resolve, reject) => {
102
+ if (timer !== null) clearTimeout(timer);
103
+ if (immediate && !isInvoke) {
104
+ try {
105
+ const result = callback.apply(this, args);
106
+ resolve(result);
107
+ } catch (error) {
108
+ reject(error);
109
+ }
110
+ isInvoke = true;
111
+ return;
112
+ }
113
+ timer = setTimeout(() => {
114
+ try {
115
+ const result = callback.apply(this, args);
116
+ resolve(result);
117
+ } catch (error) {
118
+ reject(error);
119
+ } finally {
120
+ timer = null;
121
+ isInvoke = false;
122
+ }
123
+ }, delay);
124
+ });
125
+ }
126
+ _debounce.cancel = function() {
127
+ if (timer !== null) clearTimeout(timer);
128
+ timer = null;
129
+ isInvoke = false;
130
+ };
131
+ return _debounce;
132
+ }
133
+ function throttle(callback, interval, options = {}) {
134
+ const {
135
+ leading = true,
136
+ trailing = false
137
+ } = options;
138
+ let startTime = 0;
139
+ let timer = null;
140
+ function _throttle(...args) {
141
+ return new Promise((resolve, reject) => {
142
+ try {
143
+ const nowTime = Date.now();
144
+ let result;
145
+ if (!leading && startTime === 0) startTime = nowTime;
146
+ const waitTime = interval - (nowTime - startTime);
147
+ if (waitTime <= 0) {
148
+ if (timer) clearTimeout(timer);
149
+ result = callback.apply(this, args);
150
+ resolve(result);
151
+ startTime = nowTime;
152
+ timer = null;
153
+ return;
154
+ }
155
+ if (trailing && !timer) {
156
+ timer = setTimeout(() => {
157
+ result = callback.apply(this, args);
158
+ resolve(result);
159
+ startTime = Date.now();
160
+ timer = null;
161
+ }, waitTime);
162
+ }
163
+ } catch (error) {
164
+ reject(error);
165
+ }
166
+ });
167
+ }
168
+ _throttle.cancel = function() {
169
+ if (timer) clearTimeout(timer);
170
+ startTime = 0;
171
+ timer = null;
172
+ };
173
+ return _throttle;
174
+ }
175
+
176
+ //! Function Shallow Copy
177
+ function shallowClone(source) {
178
+ if (isType("array", source)) return source.slice();
179
+ if (isType("object", source)) return {
180
+ ...source
181
+ };
182
+ return source;
183
+ }
184
+ //! Function Deep Copy
185
+ const isFormat = (target) => isType("object", target) || isType("function", target);
186
+ function handleSpeciBoundar(source, deepClone2, hash) {
187
+ if (isType("symbol", source)) return Symbol(source.description);
188
+ if (!isFormat(source)) return source;
189
+ if (isType("set", source)) {
190
+ const newSet = /* @__PURE__ */ new Set();
191
+ source.forEach((value) => newSet.add(deepClone2(value, hash)));
192
+ return newSet;
193
+ }
194
+ if (isType("map", source)) {
195
+ const newMap = /* @__PURE__ */ new Map();
196
+ source.forEach((value, key) => newMap.set(key, deepClone2(value, hash)));
197
+ return newMap;
198
+ }
199
+ }
200
+ function deepClone(source, hash = /* @__PURE__ */ new WeakMap()) {
201
+ if (hash.get(source)) return hash.get(source);
202
+ const result = handleSpeciBoundar(source, deepClone, hash);
203
+ if (result) return result;
204
+ const isArray = isType("array", source);
205
+ const cloneObject = isArray ? [] : {};
206
+ hash.set(source, cloneObject);
207
+ if (isArray) {
208
+ source.forEach((item, index) => {
209
+ cloneObject[index] = deepClone(item, hash);
210
+ });
211
+ } else {
212
+ Object.keys(source).forEach((key) => {
213
+ cloneObject[key] = deepClone(source[key], hash);
214
+ });
215
+ Object.getOwnPropertySymbols(source).forEach((sym) => {
216
+ cloneObject[Symbol(sym.description)] = deepClone(source[sym], hash);
217
+ });
218
+ }
219
+ return cloneObject;
220
+ }
221
+
222
+ var SortType;
223
+ (function(SortType2) {
224
+ SortType2["ASC"] = "ASC";
225
+ SortType2["DESC"] = "DESC";
226
+ })(SortType || (SortType = {}));
227
+ function swap(array, index1, index2) {
228
+ [array[index1], array[index2]] = [array[index2], array[index1]];
229
+ }
230
+ function compare(value1, value2, type) {
231
+ return type === SortType.ASC ? value1 > value2 : value1 < value2;
232
+ }
233
+ function bubblingSort(array, type = "ASC", key) {
234
+ const length = array.length;
235
+ if (length < 2) return array;
236
+ for (let i = 0; i < length - 1; i++) {
237
+ for (let j = 0; j < length - 1 - i; j++) {
238
+ const value1 = key ? array[j][key] : array[j];
239
+ const value2 = key ? array[j + 1][key] : array[j + 1];
240
+ if (compare(value1, value2, type)) swap(array, j, j + 1);
241
+ }
242
+ }
243
+ return array;
244
+ }
245
+
246
+ const formatRules = /* @__PURE__ */ new Map([["yyyy", "year"], ["MM", "month"], ["dd", "day"], ["HH", "hours"], ["mm", "minutes"], ["ss", "seconds"], ["W", "week"]]);
247
+ const WeekList = /* @__PURE__ */ new Map([[1, "\u4E00"], [2, "\u4E8C"], [3, "\u4E09"], [4, "\u56DB"], [5, "\u4E94"], [6, "\u516D"], [0, "\u65E5"]]);
248
+ function processWeek(weekNum) {
249
+ return WeekList.get(weekNum) || "";
250
+ }
251
+ function formatNumber(value) {
252
+ return value.toString().padStart(2, "0");
253
+ }
254
+ function createTimerObj(date) {
255
+ const dayOfWeek = date.getDay() === 0 ? 7 : date.getDay();
256
+ return {
257
+ year: date.getFullYear().toString(),
258
+ month: formatNumber(date.getMonth() + 1),
259
+ day: formatNumber(date.getDate()),
260
+ hours: formatNumber(date.getHours()),
261
+ minutes: formatNumber(date.getMinutes()),
262
+ seconds: formatNumber(date.getSeconds()),
263
+ week: processWeek(dayOfWeek),
264
+ weekNum: dayOfWeek.toString()
265
+ };
266
+ }
267
+ const formatTimer = (cellValue, formatType = "yyyy-MM-dd HH:mm:ss") => {
268
+ if (!cellValue) return (/* @__PURE__ */ new Date()).toISOString();
269
+ const date = new Date(cellValue);
270
+ const timerObj = createTimerObj(date);
271
+ if (isType("string", formatType) && !formatType.trim()) return timerObj;
272
+ const timerStr = Array.from(formatRules).reduce((currentFormat, [rule, key]) => {
273
+ return currentFormat.replace(new RegExp(rule, "g"), timerObj[key]);
274
+ }, formatType);
275
+ return timerStr;
276
+ };
277
+ async function setTimer(execute, delay = 0, immediate = false) {
278
+ let timer = null;
279
+ const interval = async () => {
280
+ await execute();
281
+ timer = setTimeout(interval, delay);
282
+ };
283
+ if (immediate) await execute();
284
+ setTimeout(interval, delay);
285
+ return {
286
+ cancel: () => {
287
+ if (timer !== null) clearTimeout(timer);
288
+ }
289
+ };
290
+ }
291
+
292
+ export { SortType, bubblingSort, compose, currying, debounce, deepClone, formatTimer, insertStr, isType, setTimer, shallowClone, stringCase, throttle, transformGetParams };
package/index.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ interface ITimerObj {
2
+ year: string;
3
+ month: string;
4
+ day: string;
5
+ hours: string;
6
+ minutes: string;
7
+ seconds: string;
8
+ week: string;
9
+ weekNum: string;
10
+ }
11
+ type TFormatTimer = (cellValue: string | number | Date, formatType?: string) => string | ITimerObj;
12
+ type ThrottleOptions = {
13
+ leading?: boolean;
14
+ trailing?: boolean;
15
+ };
16
+ type TIsType = (type: string, target: any) => boolean;
17
+ type Tcase = 'upper' | 'lower';
18
+ type TCases = [Tcase, Tcase];
19
+ type TTransGetParams = (params: IDataObject) => string;
20
+ interface IDataObject {
21
+ [key: string]: any;
22
+ }
23
+
24
+ declare function currying(fn: Function): (this: any, ...args: any[]) => any;
25
+ declare function compose(...fns: Function[]): ((this: any, ...args: any[]) => any) | undefined;
26
+ declare function insertStr(soure: string, start: number, newStr: string): string;
27
+ declare function stringCase(soure: string, separa?: string[], cases?: TCases): string;
28
+ declare const transformGetParams: TTransGetParams;
29
+
30
+ declare const isType: TIsType;
31
+
32
+ declare function debounce<T extends (...args: any[]) => any>(callback: T, delay?: number, immediate?: boolean): ((...args: Parameters<T>) => Promise<ReturnType<T>>) & {
33
+ cancel: () => void;
34
+ };
35
+ declare function throttle<T extends (...args: any[]) => any>(callback: T, interval: number, options?: ThrottleOptions): ((...args: Parameters<T>) => Promise<ReturnType<T>>) & {
36
+ cancel: () => void;
37
+ };
38
+
39
+ declare function shallowClone<T = any>(source: T): T;
40
+ declare function deepClone(source: any, hash?: WeakMap<object, any>): any;
41
+
42
+ declare enum SortType {
43
+ ASC = "ASC",
44
+ DESC = "DESC"
45
+ }
46
+ declare function bubblingSort<T>(array: T[], type?: string, key?: keyof T): T[];
47
+
48
+ declare const formatTimer: TFormatTimer;
49
+ declare function setTimer(execute: (...args: any[]) => any, delay?: number, immediate?: boolean): Promise<{
50
+ cancel: () => void;
51
+ }>;
52
+
53
+ export { SortType, bubblingSort, compose, currying, debounce, deepClone, formatTimer, insertStr, isType, setTimer, shallowClone, stringCase, throttle, transformGetParams };
@@ -0,0 +1,24 @@
1
+ declare enum CacheType {
2
+ Local = 0,
3
+ Session = 1
4
+ }
5
+
6
+ /**
7
+ * Encapsulate storage cache class
8
+ *
9
+ * @class StorageCache
10
+ * @template T
11
+ */
12
+ declare class StorageCache<T = any> {
13
+ private storage;
14
+ constructor(type: CacheType);
15
+ getCache(key: string): T;
16
+ setCache(key: string, value: T): void;
17
+ updateCache(key: string, property: string, value: T): void;
18
+ deleteCache(key: string): void;
19
+ clearCache(): void;
20
+ }
21
+ declare const localCache: StorageCache<any>;
22
+ declare const sessionCache: StorageCache<any>;
23
+
24
+ export { localCache, sessionCache };
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const isObject = (target) => target !== null && typeof target === "object";
4
+ var CacheType;
5
+ (function(CacheType2) {
6
+ CacheType2[CacheType2["Local"] = 0] = "Local";
7
+ CacheType2[CacheType2["Session"] = 1] = "Session";
8
+ })(CacheType || (CacheType = {}));
9
+
10
+ class StorageCache {
11
+ constructor(type) {
12
+ this.storage = type === CacheType.Local ? localStorage : sessionStorage;
13
+ }
14
+ getCache(key) {
15
+ const value = this.storage.getItem(key);
16
+ return value ? JSON.parse(value) : null;
17
+ }
18
+ setCache(key, value) {
19
+ this.storage.setItem(key, JSON.stringify(value));
20
+ }
21
+ updateCache(key, property, value) {
22
+ const cache = this.getCache(key);
23
+ if (!isObject(cache)) return;
24
+ cache[property] = value;
25
+ this.setCache(key, cache);
26
+ }
27
+ deleteCache(key) {
28
+ this.storage.removeItem(key);
29
+ }
30
+ clearCache() {
31
+ this.storage.clear();
32
+ }
33
+ }
34
+ const localCache = new StorageCache(CacheType.Local);
35
+ const sessionCache = new StorageCache(CacheType.Session);
36
+
37
+ exports.localCache = localCache;
38
+ exports.sessionCache = sessionCache;
@@ -0,0 +1,35 @@
1
+ const isObject = (target) => target !== null && typeof target === "object";
2
+ var CacheType;
3
+ (function(CacheType2) {
4
+ CacheType2[CacheType2["Local"] = 0] = "Local";
5
+ CacheType2[CacheType2["Session"] = 1] = "Session";
6
+ })(CacheType || (CacheType = {}));
7
+
8
+ class StorageCache {
9
+ constructor(type) {
10
+ this.storage = type === CacheType.Local ? localStorage : sessionStorage;
11
+ }
12
+ getCache(key) {
13
+ const value = this.storage.getItem(key);
14
+ return value ? JSON.parse(value) : null;
15
+ }
16
+ setCache(key, value) {
17
+ this.storage.setItem(key, JSON.stringify(value));
18
+ }
19
+ updateCache(key, property, value) {
20
+ const cache = this.getCache(key);
21
+ if (!isObject(cache)) return;
22
+ cache[property] = value;
23
+ this.setCache(key, cache);
24
+ }
25
+ deleteCache(key) {
26
+ this.storage.removeItem(key);
27
+ }
28
+ clearCache() {
29
+ this.storage.clear();
30
+ }
31
+ }
32
+ const localCache = new StorageCache(CacheType.Local);
33
+ const sessionCache = new StorageCache(CacheType.Session);
34
+
35
+ export { localCache, sessionCache };
package/package.json CHANGED
@@ -1,82 +1,88 @@
1
1
  {
2
2
  "name": "devix",
3
3
  "type": "module",
4
- "version": "0.0.22",
4
+ "version": "0.0.23-beta.10",
5
5
  "description": "Devix is a comprehensive, powerful, and compact JavaScript utility library.",
6
6
  "author": "king-3 <w2196592083@gmail.com>",
7
7
  "license": "MIT",
8
8
  "homepage": "https://github.com/OpenKnights/devix#readme",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/OpenKnights/devix.git"
11
+ "url": "git+https://github.com/OpenKnights/devix.git"
12
12
  },
13
13
  "bugs": {
14
14
  "url": "https://github.com/OpenKnights/devix/issues"
15
15
  },
16
16
  "keywords": [
17
- "devix",
18
- "utils",
19
- "tools",
20
- "develop"
17
+ "template",
18
+ "library"
21
19
  ],
22
20
  "exports": {
23
21
  ".": {
24
- "import": "./dist/index.esm.js",
25
- "require": "./dist/index.cjs.js"
22
+ "types": "./index.d.ts",
23
+ "require": "./dist/index.js",
24
+ "import": "./dist/index.mjs"
25
+ },
26
+ "./lib/cache": {
27
+ "types": "./lib/cache/index.d.ts",
28
+ "require": "./lib/cache/index.js",
29
+ "import": "./lib/cache/index.mjs"
30
+ },
31
+ "./*": {
32
+ "types": "./*.d.ts",
33
+ "require": "./*.js",
34
+ "import": "./*.mjs"
26
35
  }
27
36
  },
28
- "main": "./dist/index.cjs.js",
29
- "module": "./dist/index.esm.js",
30
- "umd": "./dist/index.umd.js",
31
- "types": "dist/index.d.ts",
37
+ "types": "./index.d.ts",
38
+ "main": "./dist/index.js",
39
+ "module": "./dist/index.mjs",
32
40
  "files": [
33
41
  "dist/",
42
+ "lib/",
34
43
  "package.json",
35
- "README.md"
44
+ "README.md",
45
+ "index.d.ts"
36
46
  ],
37
47
  "scripts": {
38
- "build": " pnpm prettier && pnpm lint && rollup -c",
39
- "dev": "npx --yes tsx --watch example/index.ts",
48
+ "build": "rollup -c",
40
49
  "test": "jest",
41
- "lint": "eslint src/**/*.{js,jsx,ts,tsx,json}",
42
- "prettier": "prettier --config .prettierrc.json --write src/**/*.{js,jsx,ts,tsx,json}",
43
- "releases": "npx bump && tsx publish.ts",
50
+ "lint": "npx eslint src/**/*.ts && npx eslint test/**/*.ts && npx eslint types/**/*.ts",
51
+ "prettier": "npx prettier --config .prettierrc.json --write ./**/*.{ts,json}",
52
+ "releases": "pwsh publish.ps1",
44
53
  "versihint": "npx bump"
45
54
  },
46
55
  "devDependencies": {
47
- "@babel/core": "^7.24.0",
48
- "@babel/preset-env": "^7.24.0",
49
- "@babel/preset-typescript": "^7.23.3",
56
+ "@babel/core": "^7.24.5",
57
+ "@babel/preset-env": "^7.24.5",
58
+ "@babel/preset-typescript": "^7.24.1",
59
+ "@rollup/plugin-alias": "^5.1.0",
50
60
  "@rollup/plugin-babel": "^6.0.4",
51
61
  "@rollup/plugin-commonjs": "^25.0.7",
52
62
  "@rollup/plugin-json": "^6.1.0",
53
63
  "@rollup/plugin-node-resolve": "^15.2.3",
54
- "@rollup/plugin-terser": "^0.4.4",
55
64
  "@types/jest": "^29.5.12",
56
- "@types/koa": "^2.15.0",
57
- "@types/koa-bodyparser": "^4.3.12",
58
- "@types/koa-json": "^2.0.23",
59
- "@types/koa-router": "^7.4.8",
60
- "@types/node": "^20.12.7",
61
- "@typescript-eslint/eslint-plugin": "^7.3.1",
62
- "@typescript-eslint/parser": "^7.3.1",
65
+ "@typescript-eslint/eslint-plugin": "^7.8.0",
66
+ "@typescript-eslint/parser": "^7.8.0",
67
+ "esbuild": "^0.21.0",
63
68
  "eslint": "^8.57.0",
69
+ "eslint-config-airbnb-base": "^15.0.0",
70
+ "eslint-config-airbnb-typescript": "^18.0.0",
64
71
  "eslint-config-prettier": "^9.1.0",
72
+ "eslint-plugin-import": "^2.29.1",
65
73
  "eslint-plugin-prettier": "^5.1.3",
66
- "fsxx": "^0.1.0",
67
74
  "jest": "^29.7.0",
68
- "koa": "^2.15.3",
69
- "koa-bodyparser": "^4.4.1",
70
- "koa-json": "^2.0.2",
71
- "koa-router": "^12.0.1",
72
75
  "prettier": "^3.2.5",
73
- "rollup": "^4.13.0",
76
+ "rollup": "^4.17.2",
74
77
  "rollup-plugin-dts": "^6.1.0",
78
+ "rollup-plugin-esbuild": "^6.1.1",
75
79
  "rollup-plugin-typescript2": "^0.36.0",
76
80
  "ts-jest": "^29.1.2",
77
81
  "tslib": "^2.6.2",
78
- "typescript": "^5.4.2",
79
- "version-bump-prompt": "^6.1.0",
80
- "zx": "^7.2.3"
82
+ "typescript": "^5.4.5",
83
+ "version-bump-prompt": "^6.1.0"
84
+ },
85
+ "dependencies": {
86
+ "devix": "0.0.23-beta.9"
81
87
  }
82
88
  }