devix 0.0.23-beta.9 → 0.1.1

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # devix
2
2
 
3
- > Devix is a comprehensive, powerful, and compact JavaScript utility library.( English | [简体中文](README.md) )
3
+ > Devix is a comprehensive, powerful, and compact JavaScript utility library.( English | [简体中文](README_zh.md) )
4
4
 
5
5
  ## Install
6
6
 
@@ -23,12 +23,7 @@ const { [[ModuleName]] } = require('devix')
23
23
  ## Usage
24
24
 
25
25
  ```javascript
26
- import { localCache, bubblingSort, isType } from 'devix'
27
-
28
- // Using localCache
29
- localCache.setCache('userInfo', { name: 'king', age: 18 })
30
- const userInfo = localCache.get('userInfo')
31
- console.log('userInfo', userInfo)
26
+ import { bubblingSort, isType } from 'devix'
32
27
 
33
28
  // Using bubblingSort、
34
29
  const arr1 = [123, 346, 62, 2456, 56123, 1, 64, 61, 453, 72345]
@@ -54,13 +49,6 @@ console.log(`isType(userInfo.age,'number') -> true`, isType(userInfo, 'number'))
54
49
 
55
50
  ## API
56
51
 
57
- ### Cache Apis
58
-
59
- Provides methods related to cache operations.
60
-
61
- - localCache
62
- - sessionCache
63
-
64
52
  ### Clone Apis
65
53
 
66
54
  Methods for data cloning operations.
@@ -2,7 +2,7 @@
2
2
 
3
3
  function getDataType(target) {
4
4
  const type = typeof target;
5
- return type != "object" ? type : Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
5
+ return type !== "object" ? type : Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
6
6
  }
7
7
  const typeCheckers = {
8
8
  // DataType
@@ -32,37 +32,41 @@ const isType = (type, target) => {
32
32
 
33
33
  function currying(fn) {
34
34
  function curried(...args) {
35
- if (args.length >= fn.length) {
36
- return fn.apply(this, args);
37
- } else {
38
- return function(...args2) {
39
- return curried.apply(this, args.concat(args2));
40
- };
41
- }
35
+ if (args.length >= fn.length) return fn.apply(this, args);
36
+ return function subCurried(...args2) {
37
+ return curried.apply(this, args.concat(args2));
38
+ };
42
39
  }
43
40
  return curried;
44
41
  }
45
42
  function compose(...fns) {
46
- const length = fns.length;
47
- if (length <= 0) return;
43
+ const {
44
+ length
45
+ } = fns;
46
+ if (length <= 0) return null;
48
47
  for (let i = 0; i < length; i++) {
49
48
  const fn = fns[i];
50
49
  if (typeof fn !== "function") {
51
50
  throw new Error(`argument with index ${i} is not a function`);
52
51
  }
53
52
  }
54
- return function(...args) {
53
+ function executeFn(...args) {
55
54
  let index = 0;
56
55
  let result = fns[index].apply(this, args);
57
56
  while (++index < length) {
58
57
  result = fns[index].call(this, result);
59
58
  }
60
59
  return result;
61
- };
60
+ }
61
+ return executeFn;
62
62
  }
63
63
  function insertStr(soure, start, newStr) {
64
64
  return soure.slice(0, start) + newStr + soure.slice(start);
65
65
  }
66
+ function setCaseType(soure, caseType) {
67
+ const newStr = soure.slice(0, 1)[caseType === "upper" ? "toUpperCase" : "toLowerCase"]() + soure.slice(1).toLowerCase();
68
+ return newStr;
69
+ }
66
70
  function stringCase(soure, separa = ["", ""], cases = ["upper", "upper"]) {
67
71
  const [separator, separate] = separa;
68
72
  const [firstCase, argsCase] = cases;
@@ -72,10 +76,6 @@ function stringCase(soure, separa = ["", ""], cases = ["upper", "upper"]) {
72
76
  }
73
77
  return newStr.join(separate);
74
78
  }
75
- function setCaseType(soure, caseType) {
76
- const newStr = soure.slice(0, 1)[caseType === "upper" ? "toUpperCase" : "toLowerCase"]() + soure.slice(1).toLowerCase();
77
- return newStr;
78
- }
79
79
  const transformGetParams = (params) => {
80
80
  let result = "";
81
81
  for (const propName of Object.keys(params)) {
@@ -87,9 +87,9 @@ const transformGetParams = (params) => {
87
87
  continue;
88
88
  }
89
89
  for (const key of Object.keys(value)) {
90
- if (isType("empty", value)) continue;
91
- const params2 = propName + `[${key}]`;
92
- const subPart = `${encodeURIComponent(params2)}=`;
90
+ if (!isType("empty", value)) continue;
91
+ const paramsStr = `${propName}[${key}]`;
92
+ const subPart = `${encodeURIComponent(paramsStr)}=`;
93
93
  result += `${subPart + encodeURIComponent(value[key])}&`;
94
94
  }
95
95
  }
@@ -99,7 +99,7 @@ const transformGetParams = (params) => {
99
99
  function debounce(callback, delay = 0, immediate = false) {
100
100
  let timer = null;
101
101
  let isInvoke = false;
102
- function _debounce(...args) {
102
+ function subDebounce(...args) {
103
103
  return new Promise((resolve, reject) => {
104
104
  if (timer !== null) clearTimeout(timer);
105
105
  if (immediate && !isInvoke) {
@@ -125,12 +125,12 @@ function debounce(callback, delay = 0, immediate = false) {
125
125
  }, delay);
126
126
  });
127
127
  }
128
- _debounce.cancel = function() {
128
+ subDebounce.cancel = () => {
129
129
  if (timer !== null) clearTimeout(timer);
130
130
  timer = null;
131
131
  isInvoke = false;
132
132
  };
133
- return _debounce;
133
+ return subDebounce;
134
134
  }
135
135
  function throttle(callback, interval, options = {}) {
136
136
  const {
@@ -139,7 +139,7 @@ function throttle(callback, interval, options = {}) {
139
139
  } = options;
140
140
  let startTime = 0;
141
141
  let timer = null;
142
- function _throttle(...args) {
142
+ function subThrottle(...args) {
143
143
  return new Promise((resolve, reject) => {
144
144
  try {
145
145
  const nowTime = Date.now();
@@ -167,12 +167,12 @@ function throttle(callback, interval, options = {}) {
167
167
  }
168
168
  });
169
169
  }
170
- _throttle.cancel = function() {
170
+ subThrottle.cancel = () => {
171
171
  if (timer) clearTimeout(timer);
172
172
  startTime = 0;
173
173
  timer = null;
174
174
  };
175
- return _throttle;
175
+ return subThrottle;
176
176
  }
177
177
 
178
178
  //! Function Shallow Copy
@@ -185,19 +185,20 @@ function shallowClone(source) {
185
185
  }
186
186
  //! Function Deep Copy
187
187
  const isFormat = (target) => isType("object", target) || isType("function", target);
188
- function handleSpeciBoundar(source, deepClone2, hash) {
188
+ function handleSpeciBoundar(source, dpClone, hash) {
189
189
  if (isType("symbol", source)) return Symbol(source.description);
190
190
  if (!isFormat(source)) return source;
191
191
  if (isType("set", source)) {
192
192
  const newSet = /* @__PURE__ */ new Set();
193
- source.forEach((value) => newSet.add(deepClone2(value, hash)));
193
+ source.forEach((value) => newSet.add(dpClone(value, hash)));
194
194
  return newSet;
195
195
  }
196
196
  if (isType("map", source)) {
197
197
  const newMap = /* @__PURE__ */ new Map();
198
- source.forEach((value, key) => newMap.set(key, deepClone2(value, hash)));
198
+ source.forEach((value, key) => newMap.set(key, dpClone(value, hash)));
199
199
  return newMap;
200
200
  }
201
+ return null;
201
202
  }
202
203
  function deepClone(source, hash = /* @__PURE__ */ new WeakMap()) {
203
204
  if (hash.get(source)) return hash.get(source);
@@ -233,7 +234,9 @@ function compare(value1, value2, type) {
233
234
  return type === exports.SortType.ASC ? value1 > value2 : value1 < value2;
234
235
  }
235
236
  function bubblingSort(array, type = "ASC", key) {
236
- const length = array.length;
237
+ const {
238
+ length
239
+ } = array;
237
240
  if (length < 2) return array;
238
241
  for (let i = 0; i < length - 1; i++) {
239
242
  for (let j = 0; j < length - 1 - i; j++) {
@@ -1,6 +1,6 @@
1
1
  function getDataType(target) {
2
2
  const type = typeof target;
3
- return type != "object" ? type : Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
3
+ return type !== "object" ? type : Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
4
4
  }
5
5
  const typeCheckers = {
6
6
  // DataType
@@ -30,37 +30,41 @@ const isType = (type, target) => {
30
30
 
31
31
  function currying(fn) {
32
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
- }
33
+ if (args.length >= fn.length) return fn.apply(this, args);
34
+ return function subCurried(...args2) {
35
+ return curried.apply(this, args.concat(args2));
36
+ };
40
37
  }
41
38
  return curried;
42
39
  }
43
40
  function compose(...fns) {
44
- const length = fns.length;
45
- if (length <= 0) return;
41
+ const {
42
+ length
43
+ } = fns;
44
+ if (length <= 0) return null;
46
45
  for (let i = 0; i < length; i++) {
47
46
  const fn = fns[i];
48
47
  if (typeof fn !== "function") {
49
48
  throw new Error(`argument with index ${i} is not a function`);
50
49
  }
51
50
  }
52
- return function(...args) {
51
+ function executeFn(...args) {
53
52
  let index = 0;
54
53
  let result = fns[index].apply(this, args);
55
54
  while (++index < length) {
56
55
  result = fns[index].call(this, result);
57
56
  }
58
57
  return result;
59
- };
58
+ }
59
+ return executeFn;
60
60
  }
61
61
  function insertStr(soure, start, newStr) {
62
62
  return soure.slice(0, start) + newStr + soure.slice(start);
63
63
  }
64
+ function setCaseType(soure, caseType) {
65
+ const newStr = soure.slice(0, 1)[caseType === "upper" ? "toUpperCase" : "toLowerCase"]() + soure.slice(1).toLowerCase();
66
+ return newStr;
67
+ }
64
68
  function stringCase(soure, separa = ["", ""], cases = ["upper", "upper"]) {
65
69
  const [separator, separate] = separa;
66
70
  const [firstCase, argsCase] = cases;
@@ -70,10 +74,6 @@ function stringCase(soure, separa = ["", ""], cases = ["upper", "upper"]) {
70
74
  }
71
75
  return newStr.join(separate);
72
76
  }
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
77
  const transformGetParams = (params) => {
78
78
  let result = "";
79
79
  for (const propName of Object.keys(params)) {
@@ -85,9 +85,9 @@ const transformGetParams = (params) => {
85
85
  continue;
86
86
  }
87
87
  for (const key of Object.keys(value)) {
88
- if (isType("empty", value)) continue;
89
- const params2 = propName + `[${key}]`;
90
- const subPart = `${encodeURIComponent(params2)}=`;
88
+ if (!isType("empty", value)) continue;
89
+ const paramsStr = `${propName}[${key}]`;
90
+ const subPart = `${encodeURIComponent(paramsStr)}=`;
91
91
  result += `${subPart + encodeURIComponent(value[key])}&`;
92
92
  }
93
93
  }
@@ -97,7 +97,7 @@ const transformGetParams = (params) => {
97
97
  function debounce(callback, delay = 0, immediate = false) {
98
98
  let timer = null;
99
99
  let isInvoke = false;
100
- function _debounce(...args) {
100
+ function subDebounce(...args) {
101
101
  return new Promise((resolve, reject) => {
102
102
  if (timer !== null) clearTimeout(timer);
103
103
  if (immediate && !isInvoke) {
@@ -123,12 +123,12 @@ function debounce(callback, delay = 0, immediate = false) {
123
123
  }, delay);
124
124
  });
125
125
  }
126
- _debounce.cancel = function() {
126
+ subDebounce.cancel = () => {
127
127
  if (timer !== null) clearTimeout(timer);
128
128
  timer = null;
129
129
  isInvoke = false;
130
130
  };
131
- return _debounce;
131
+ return subDebounce;
132
132
  }
133
133
  function throttle(callback, interval, options = {}) {
134
134
  const {
@@ -137,7 +137,7 @@ function throttle(callback, interval, options = {}) {
137
137
  } = options;
138
138
  let startTime = 0;
139
139
  let timer = null;
140
- function _throttle(...args) {
140
+ function subThrottle(...args) {
141
141
  return new Promise((resolve, reject) => {
142
142
  try {
143
143
  const nowTime = Date.now();
@@ -165,12 +165,12 @@ function throttle(callback, interval, options = {}) {
165
165
  }
166
166
  });
167
167
  }
168
- _throttle.cancel = function() {
168
+ subThrottle.cancel = () => {
169
169
  if (timer) clearTimeout(timer);
170
170
  startTime = 0;
171
171
  timer = null;
172
172
  };
173
- return _throttle;
173
+ return subThrottle;
174
174
  }
175
175
 
176
176
  //! Function Shallow Copy
@@ -183,19 +183,20 @@ function shallowClone(source) {
183
183
  }
184
184
  //! Function Deep Copy
185
185
  const isFormat = (target) => isType("object", target) || isType("function", target);
186
- function handleSpeciBoundar(source, deepClone2, hash) {
186
+ function handleSpeciBoundar(source, dpClone, hash) {
187
187
  if (isType("symbol", source)) return Symbol(source.description);
188
188
  if (!isFormat(source)) return source;
189
189
  if (isType("set", source)) {
190
190
  const newSet = /* @__PURE__ */ new Set();
191
- source.forEach((value) => newSet.add(deepClone2(value, hash)));
191
+ source.forEach((value) => newSet.add(dpClone(value, hash)));
192
192
  return newSet;
193
193
  }
194
194
  if (isType("map", source)) {
195
195
  const newMap = /* @__PURE__ */ new Map();
196
- source.forEach((value, key) => newMap.set(key, deepClone2(value, hash)));
196
+ source.forEach((value, key) => newMap.set(key, dpClone(value, hash)));
197
197
  return newMap;
198
198
  }
199
+ return null;
199
200
  }
200
201
  function deepClone(source, hash = /* @__PURE__ */ new WeakMap()) {
201
202
  if (hash.get(source)) return hash.get(source);
@@ -231,7 +232,9 @@ function compare(value1, value2, type) {
231
232
  return type === SortType.ASC ? value1 > value2 : value1 < value2;
232
233
  }
233
234
  function bubblingSort(array, type = "ASC", key) {
234
- const length = array.length;
235
+ const {
236
+ length
237
+ } = array;
235
238
  if (length < 2) return array;
236
239
  for (let i = 0; i < length - 1; i++) {
237
240
  for (let j = 0; j < length - 1 - i; j++) {
package/index.d.ts CHANGED
@@ -21,8 +21,8 @@ interface IDataObject {
21
21
  [key: string]: any;
22
22
  }
23
23
 
24
- declare function currying(fn: Function): (this: any, ...args: any[]) => any;
25
- declare function compose(...fns: Function[]): ((this: any, ...args: any[]) => any) | undefined;
24
+ declare function currying(fn: (...args: any[]) => any): (this: any, ...args: any[]) => any;
25
+ declare function compose(...fns: ((...args: any[]) => any)[]): ((this: any, ...args: any[]) => any) | null;
26
26
  declare function insertStr(soure: string, start: number, newStr: string): string;
27
27
  declare function stringCase(soure: string, separa?: string[], cases?: TCases): string;
28
28
  declare const transformGetParams: TTransGetParams;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "devix",
3
3
  "type": "module",
4
- "version": "0.0.23-beta.9",
4
+ "version": "0.1.1",
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",
@@ -20,19 +20,19 @@
20
20
  "exports": {
21
21
  ".": {
22
22
  "types": "./index.d.ts",
23
- "require": "./dist/index.js",
24
- "import": "./dist/index.mjs"
23
+ "require": "./dist/index.cjs.js",
24
+ "import": "./dist/index.esm.js"
25
25
  },
26
26
  "./lib/**": {
27
27
  "types": "./lib/**/index.d.ts",
28
- "require": "./lib/**/index.js",
29
- "import": "./lib/**/index.mjs"
28
+ "require": "./lib/**/index.cjs.js",
29
+ "import": "./lib/**/index.esm.js"
30
30
  },
31
31
  "./*": "./*"
32
32
  },
33
33
  "types": "./index.d.ts",
34
- "main": "./dist/index.js",
35
- "module": "./dist/index.mjs",
34
+ "main": "./dist/index.cjs.js",
35
+ "module": "./dist/index.esm.js",
36
36
  "files": [
37
37
  "dist/",
38
38
  "lib/",
@@ -42,6 +42,7 @@
42
42
  ],
43
43
  "scripts": {
44
44
  "build": "rollup -c",
45
+ "clear": "pwsh clear.ps1",
45
46
  "test": "jest",
46
47
  "lint": "npx eslint src/**/*.ts && npx eslint test/**/*.ts && npx eslint types/**/*.ts",
47
48
  "prettier": "npx prettier --config .prettierrc.json --write ./**/*.{ts,json}",
@@ -77,8 +78,5 @@
77
78
  "tslib": "^2.6.2",
78
79
  "typescript": "^5.4.5",
79
80
  "version-bump-prompt": "^6.1.0"
80
- },
81
- "dependencies": {
82
- "devix": "0.0.23-beta.8"
83
81
  }
84
82
  }
@@ -1,24 +0,0 @@
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 };
@@ -1,38 +0,0 @@
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;
@@ -1,35 +0,0 @@
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 };