qmwts 1.0.2 → 1.0.5
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.test.js +35 -3
- package/index.ts +6 -3
- package/jest.config.js +4 -4
- package/package.json +2 -3
- package/src/utils/json-utils.ts +42 -0
- package/src/utils/number-utils.ts +22 -6
- package/test.js +9 -0
- package/test.ts +11 -0
- package/tsconfig.json +0 -1
package/index.test.js
CHANGED
|
@@ -1,4 +1,36 @@
|
|
|
1
|
-
const {
|
|
2
|
-
test('
|
|
3
|
-
expect(
|
|
1
|
+
const { NumberUtils, JsonUtils } = require('./index')
|
|
2
|
+
test('isNumber', () => {
|
|
3
|
+
expect(NumberUtils.thousandths(1)).toBe('1.00')
|
|
4
|
+
expect(NumberUtils.thousandths(1, 0)).toBe('1')
|
|
5
|
+
expect(NumberUtils.thousandths(123)).toBe('123.00')
|
|
6
|
+
expect(NumberUtils.thousandths(123123)).toBe('123,123.00')
|
|
7
|
+
expect(NumberUtils.thousandths(-123123)).toBe('-123,123.00')
|
|
8
|
+
expect(NumberUtils.thousandths(54.23 * 54.22)).toBe('2,940.35')
|
|
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
|
+
|
|
4
36
|
})
|
package/index.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
// https://www.jianshu.com/p/8fa2c50720e4
|
|
1
2
|
import NumberUtils from './src/utils/number-utils'
|
|
3
|
+
import JsonUtils from './src/utils/json-utils'
|
|
2
4
|
|
|
3
|
-
export
|
|
4
|
-
NumberUtils
|
|
5
|
+
export {
|
|
6
|
+
NumberUtils,
|
|
7
|
+
JsonUtils,
|
|
5
8
|
}
|
|
6
|
-
|
|
9
|
+
|
package/jest.config.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
preset: 'ts-jest',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
5
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qmwts",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.ts",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"build": "tsc",
|
|
8
7
|
"test": "jest"
|
|
9
8
|
},
|
|
10
9
|
"author": "qmw",
|
|
@@ -12,6 +11,6 @@
|
|
|
12
11
|
"devDependencies": {
|
|
13
12
|
"jest": "^29.5.0",
|
|
14
13
|
"ts-jest": "^29.1.0",
|
|
15
|
-
"typescript": "^5.
|
|
14
|
+
"typescript": "^5.1.3"
|
|
16
15
|
}
|
|
17
16
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export default {
|
|
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
|
|
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
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -1,10 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
isNumber(number: any) {
|
|
1
|
+
export default {
|
|
2
|
+
// 判断是否数字
|
|
3
|
+
ifNaN(number: any, substitute: any): any {
|
|
4
|
+
return this.isNumber(number) ? +number : substitute
|
|
5
|
+
},
|
|
6
|
+
isNumber(number: any): boolean {
|
|
7
7
|
number = String(number).trim()
|
|
8
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)
|
|
9
25
|
}
|
|
10
26
|
}
|
package/test.js
ADDED
package/test.ts
ADDED