qmwts 1.0.5 → 1.0.7
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/index.d.ts +4 -0
- package/index.js +10 -0
- package/index.test.js +7 -34
- package/index.ts +4 -2
- package/jest.config.js +4 -4
- package/package.json +2 -2
- package/src/utils/json-utils.d.ts +10 -0
- package/src/utils/json-utils.js +49 -0
- package/src/utils/json-utils.ts +39 -39
- package/src/utils/number-utils.d.ts +7 -0
- package/src/utils/number-utils.js +31 -0
- package/src/utils/number-utils.ts +24 -24
- package/src/utils/uuid-utils.d.ts +4 -0
- package/src/utils/uuid-utils.js +11 -0
- package/src/utils/uuid-utils.ts +9 -0
- package/test.js +0 -9
- package/test.ts +0 -11
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UUIDUtils = exports.JsonUtils = exports.NumberUtils = void 0;
|
|
4
|
+
// https://www.jianshu.com/p/8fa2c50720e4
|
|
5
|
+
var number_utils_1 = require("./src/utils/number-utils");
|
|
6
|
+
exports.NumberUtils = number_utils_1.default;
|
|
7
|
+
var json_utils_1 = require("./src/utils/json-utils");
|
|
8
|
+
exports.JsonUtils = json_utils_1.default;
|
|
9
|
+
var uuid_utils_1 = require("./src/utils/uuid-utils");
|
|
10
|
+
exports.UUIDUtils = uuid_utils_1.default;
|
package/index.test.js
CHANGED
|
@@ -1,36 +1,9 @@
|
|
|
1
|
-
const { NumberUtils, JsonUtils } = require('./index')
|
|
1
|
+
const { NumberUtils, JsonUtils, UUIDUtils } = require('./index')
|
|
2
2
|
test('isNumber', () => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
expect(NumberUtils.thousandths(-89.36 * 56.58)).toBe('-5,055.99')
|
|
10
|
-
expect(NumberUtils.thousandths(-89.36 * 56.58, 0)).toBe('-5,056')
|
|
11
|
-
expect(NumberUtils.ifNaN('asd', 1)).toBe(1)
|
|
12
|
-
expect(NumberUtils.ifNaN('123', 1)).toBe(123)
|
|
13
|
-
expect(NumberUtils.ifNaN('123a', 1)).toBe(1)
|
|
14
|
-
expect(NumberUtils.ifNaN('123a', 1)).toBe(1)
|
|
15
|
-
|
|
16
|
-
expect(JsonUtils.toJSONArray("{\"1\":1}")).toStrictEqual([])
|
|
17
|
-
expect(JsonUtils.toJSONArray(123)).toStrictEqual([])
|
|
18
|
-
expect(JsonUtils.toJSONArray([1, 2, 3])).toStrictEqual([1, 2, 3])
|
|
19
|
-
expect(JsonUtils.toJSONArray('[3,2,1]')).toStrictEqual([3, 2, 1])
|
|
20
|
-
|
|
21
|
-
expect(JsonUtils.toJSONObject([3, 2, 1])).toStrictEqual({})
|
|
22
|
-
expect(JsonUtils.toJSONObject({ a: 1, b: 2 })).toStrictEqual({ b: 2, a: 1 })
|
|
23
|
-
expect(JsonUtils.toJSONObject('{ "a": 1, "b": 2 }')).toStrictEqual({ b: 2, a: 1 })
|
|
24
|
-
expect(JsonUtils.toJSONObject('{ "a": 1, "b": 2, }')).toStrictEqual({})
|
|
25
|
-
|
|
26
|
-
expect(JsonUtils.optionalChaining({a:1}, 'a')).toBe(1)
|
|
27
|
-
expect(JsonUtils.optionalChaining([1,2,3], '0')).toBe(1)
|
|
28
|
-
expect(JsonUtils.optionalChaining([1,2,3], '2')).toBe(3)
|
|
29
|
-
expect(JsonUtils.optionalChaining({a:{b:{c:'123'}}}, 'a.b.c')).toBe('123')
|
|
30
|
-
|
|
31
|
-
expect(NumberUtils.summation([1,2,3])).toBe(6)
|
|
32
|
-
expect(NumberUtils.summation([1,'1',3])).toBe(5)
|
|
33
|
-
expect(NumberUtils.summation([1,'1a',3])).toBe(4)
|
|
34
|
-
expect(NumberUtils.summation([])).toBe(0)
|
|
35
|
-
|
|
3
|
+
expect(NumberUtils.thousandths(1)).toBe('1.00')
|
|
4
|
+
expect(JsonUtils.toJSONArray("{\"1\":1}")).toStrictEqual([])
|
|
5
|
+
console.log(UUIDUtils.uuid())
|
|
6
|
+
console.log(UUIDUtils.uuid())
|
|
7
|
+
console.log(UUIDUtils.uuid())
|
|
8
|
+
console.log(UUIDUtils.uuid())
|
|
36
9
|
})
|
package/index.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// https://www.jianshu.com/p/8fa2c50720e4
|
|
2
2
|
import NumberUtils from './src/utils/number-utils'
|
|
3
3
|
import JsonUtils from './src/utils/json-utils'
|
|
4
|
+
import UUIDUtils from './src/utils/uuid-utils'
|
|
4
5
|
|
|
5
6
|
export {
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
NumberUtils,
|
|
8
|
+
JsonUtils,
|
|
9
|
+
UUIDUtils
|
|
8
10
|
}
|
|
9
11
|
|
package/jest.config.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
preset: 'ts-jest',
|
|
4
|
-
testEnvironment: 'node',
|
|
1
|
+
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
preset: 'ts-jest',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
5
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
isPrototypeObject(o: any): boolean;
|
|
3
|
+
isPrototypeArray(o: any): boolean;
|
|
4
|
+
isJSONObject(o: any): boolean;
|
|
5
|
+
isJSONArray(o: any): boolean;
|
|
6
|
+
toJSONObject<T>(o: any): T;
|
|
7
|
+
toJSONArray<T_1>(o: any): T_1[];
|
|
8
|
+
optionalChaining(o: any, chain: string): any;
|
|
9
|
+
};
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = {
|
|
4
|
+
isPrototypeObject: function (o) {
|
|
5
|
+
return Object.prototype.toString.call(o) === '[object Object]';
|
|
6
|
+
},
|
|
7
|
+
isPrototypeArray: function (o) {
|
|
8
|
+
return Object.prototype.toString.call(o) === '[object Array]';
|
|
9
|
+
},
|
|
10
|
+
isJSONObject: function (o) {
|
|
11
|
+
if (this.isPrototypeObject(o))
|
|
12
|
+
return true;
|
|
13
|
+
try {
|
|
14
|
+
return this.isPrototypeObject(JSON.parse(o));
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
isJSONArray: function (o) {
|
|
21
|
+
if (this.isPrototypeArray(o))
|
|
22
|
+
return true;
|
|
23
|
+
try {
|
|
24
|
+
return this.isPrototypeArray(JSON.parse(o));
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
toJSONObject: function (o) {
|
|
31
|
+
if (this.isPrototypeObject(o))
|
|
32
|
+
return o;
|
|
33
|
+
return this.isJSONObject(o) ? JSON.parse(o) : {};
|
|
34
|
+
},
|
|
35
|
+
toJSONArray: function (o) {
|
|
36
|
+
if (this.isPrototypeArray(o))
|
|
37
|
+
return o;
|
|
38
|
+
return this.isJSONArray(o) ? JSON.parse(o) : [];
|
|
39
|
+
},
|
|
40
|
+
optionalChaining: function (o, chain) {
|
|
41
|
+
if (o === void 0) { o = {}; }
|
|
42
|
+
var chaining = chain.split('.');
|
|
43
|
+
for (var _i = 0, chaining_1 = chaining; _i < chaining_1.length; _i++) {
|
|
44
|
+
var key = chaining_1[_i];
|
|
45
|
+
o = o[key] || '';
|
|
46
|
+
}
|
|
47
|
+
return o;
|
|
48
|
+
}
|
|
49
|
+
};
|
package/src/utils/json-utils.ts
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
export default {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
isJSONArray(o: any): boolean {
|
|
18
|
-
if (this.isPrototypeArray(o))
|
|
19
|
-
return true
|
|
20
|
-
try {
|
|
21
|
-
return this.isPrototypeArray(JSON.parse(o))
|
|
22
|
-
} catch (e) {
|
|
23
|
-
return false
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
toJSONObject<T>(o: any): T {
|
|
27
|
-
if (this.isPrototypeObject(o))
|
|
28
|
-
return o
|
|
29
|
-
return this.isJSONObject(o) ? JSON.parse(o) : {}
|
|
30
|
-
},
|
|
31
|
-
toJSONArray<T>(o: any): T[] {
|
|
32
|
-
if (this.isPrototypeArray(o))
|
|
33
|
-
return o
|
|
34
|
-
return this.isJSONArray(o) ? JSON.parse(o) : []
|
|
35
|
-
},
|
|
36
|
-
optionalChaining(o: any = {}, chain: string): any {
|
|
37
|
-
const chaining = chain.split('.')
|
|
38
|
-
for (const key of chaining)
|
|
39
|
-
o = o[key] || ''
|
|
40
|
-
return o
|
|
2
|
+
isPrototypeObject(o: any): boolean {
|
|
3
|
+
return Object.prototype.toString.call(o) === '[object Object]'
|
|
4
|
+
},
|
|
5
|
+
isPrototypeArray(o: any): boolean {
|
|
6
|
+
return Object.prototype.toString.call(o) === '[object Array]'
|
|
7
|
+
},
|
|
8
|
+
isJSONObject(o: any): boolean {
|
|
9
|
+
if (this.isPrototypeObject(o))
|
|
10
|
+
return true
|
|
11
|
+
try {
|
|
12
|
+
return this.isPrototypeObject(JSON.parse(o))
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return false
|
|
41
15
|
}
|
|
16
|
+
},
|
|
17
|
+
isJSONArray(o: any): boolean {
|
|
18
|
+
if (this.isPrototypeArray(o))
|
|
19
|
+
return true
|
|
20
|
+
try {
|
|
21
|
+
return this.isPrototypeArray(JSON.parse(o))
|
|
22
|
+
} catch (e) {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
toJSONObject<T>(o: any): T {
|
|
27
|
+
if (this.isPrototypeObject(o))
|
|
28
|
+
return <T>o
|
|
29
|
+
return this.isJSONObject(o) ? <T>JSON.parse(o) : <T>{}
|
|
30
|
+
},
|
|
31
|
+
toJSONArray<T>(o: any): T[] {
|
|
32
|
+
if (this.isPrototypeArray(o))
|
|
33
|
+
return <T[]>o
|
|
34
|
+
return this.isJSONArray(o) ? <T[]>JSON.parse(o) : <T[]>[]
|
|
35
|
+
},
|
|
36
|
+
optionalChaining(o: any = {}, chain: string): any {
|
|
37
|
+
const chaining = chain.split('.')
|
|
38
|
+
for (const key of chaining)
|
|
39
|
+
o = o[key] || ''
|
|
40
|
+
return o
|
|
41
|
+
}
|
|
42
42
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = {
|
|
4
|
+
// 判断是否数字
|
|
5
|
+
ifNaN: function (number, substitute) {
|
|
6
|
+
return this.isNumber(number) ? +number : substitute;
|
|
7
|
+
},
|
|
8
|
+
isNumber: function (number) {
|
|
9
|
+
number = String(number).trim();
|
|
10
|
+
return number !== '' && isFinite(+number) && !isNaN(+number);
|
|
11
|
+
},
|
|
12
|
+
// 增加千分位分隔符
|
|
13
|
+
thousandths: function (number, fixed) {
|
|
14
|
+
if (fixed === void 0) { fixed = 2; }
|
|
15
|
+
if (!this.isNumber(number))
|
|
16
|
+
return '';
|
|
17
|
+
number = (+number).toFixed(fixed);
|
|
18
|
+
return number.replace(/\d+/, function (x) {
|
|
19
|
+
return x.replace(/(\d)(?=(\d{3})+$)/g, function (y) {
|
|
20
|
+
return y + ',';
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
summation: function (array) {
|
|
25
|
+
var _this = this;
|
|
26
|
+
if (array === void 0) { array = []; }
|
|
27
|
+
return array.reduce(function (prev, curr) {
|
|
28
|
+
return prev + _this.ifNaN(curr, 0);
|
|
29
|
+
}, 0);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
export default {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
2
|
+
// 判断是否数字
|
|
3
|
+
ifNaN(number: any, substitute: any): any {
|
|
4
|
+
return this.isNumber(number) ? +number : substitute
|
|
5
|
+
},
|
|
6
|
+
isNumber(number: any): boolean {
|
|
7
|
+
number = String(number).trim()
|
|
8
|
+
return number !== '' && isFinite(+number) && !isNaN(+number)
|
|
9
|
+
},
|
|
10
|
+
// 增加千分位分隔符
|
|
11
|
+
thousandths(number: any, fixed: number = 2): string {
|
|
12
|
+
if (!this.isNumber(number))
|
|
13
|
+
return ''
|
|
14
|
+
number = (+number).toFixed(fixed)
|
|
15
|
+
return number.replace(/\d+/, (x: string) => {
|
|
16
|
+
return x.replace(/(\d)(?=(\d{3})+$)/g, (y: string) => {
|
|
17
|
+
return y + ','
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
},
|
|
21
|
+
summation(array: any[] = []): number {
|
|
22
|
+
return array.reduce((prev, curr) => {
|
|
23
|
+
return prev + this.ifNaN(curr, 0)
|
|
24
|
+
}, 0)
|
|
25
|
+
}
|
|
26
26
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = {
|
|
4
|
+
uuid: function () {
|
|
5
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
6
|
+
var r = (Math.random() * 16) | 0;
|
|
7
|
+
var v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
8
|
+
return v.toString(16);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
};
|
package/test.js
DELETED