qmwts 1.0.4 → 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/package.json +1 -1
- package/src/utils/json-utils.ts +7 -7
- package/src/utils/number-utils.ts +4 -4
- package/test.js +9 -0
- package/test.ts +11 -0
package/package.json
CHANGED
package/src/utils/json-utils.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export default {
|
|
2
|
-
isPrototypeObject(o: any) {
|
|
2
|
+
isPrototypeObject(o: any): boolean {
|
|
3
3
|
return Object.prototype.toString.call(o) === '[object Object]'
|
|
4
4
|
},
|
|
5
|
-
isPrototypeArray(o: any) {
|
|
5
|
+
isPrototypeArray(o: any): boolean {
|
|
6
6
|
return Object.prototype.toString.call(o) === '[object Array]'
|
|
7
7
|
},
|
|
8
|
-
isJSONObject(o: any) {
|
|
8
|
+
isJSONObject(o: any): boolean {
|
|
9
9
|
if (this.isPrototypeObject(o))
|
|
10
10
|
return true
|
|
11
11
|
try {
|
|
@@ -14,7 +14,7 @@ export default {
|
|
|
14
14
|
return false
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
|
-
isJSONArray(o: any) {
|
|
17
|
+
isJSONArray(o: any): boolean {
|
|
18
18
|
if (this.isPrototypeArray(o))
|
|
19
19
|
return true
|
|
20
20
|
try {
|
|
@@ -23,17 +23,17 @@ export default {
|
|
|
23
23
|
return false
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
|
-
toJSONObject(o: any) {
|
|
26
|
+
toJSONObject<T>(o: any): T {
|
|
27
27
|
if (this.isPrototypeObject(o))
|
|
28
28
|
return o
|
|
29
29
|
return this.isJSONObject(o) ? JSON.parse(o) : {}
|
|
30
30
|
},
|
|
31
|
-
toJSONArray(o: any) {
|
|
31
|
+
toJSONArray<T>(o: any): T[] {
|
|
32
32
|
if (this.isPrototypeArray(o))
|
|
33
33
|
return o
|
|
34
34
|
return this.isJSONArray(o) ? JSON.parse(o) : []
|
|
35
35
|
},
|
|
36
|
-
optionalChaining(o: any = {}, chain: string) {
|
|
36
|
+
optionalChaining(o: any = {}, chain: string): any {
|
|
37
37
|
const chaining = chain.split('.')
|
|
38
38
|
for (const key of chaining)
|
|
39
39
|
o = o[key] || ''
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
// 判断是否数字
|
|
3
|
-
ifNaN(number: any, substitute: any) {
|
|
3
|
+
ifNaN(number: any, substitute: any): any {
|
|
4
4
|
return this.isNumber(number) ? +number : substitute
|
|
5
5
|
},
|
|
6
|
-
isNumber(number: any) {
|
|
6
|
+
isNumber(number: any): boolean {
|
|
7
7
|
number = String(number).trim()
|
|
8
8
|
return number !== '' && isFinite(+number) && !isNaN(+number)
|
|
9
9
|
},
|
|
10
10
|
// 增加千分位分隔符
|
|
11
|
-
thousandths(number: any, fixed: number = 2) {
|
|
11
|
+
thousandths(number: any, fixed: number = 2): string {
|
|
12
12
|
if (!this.isNumber(number))
|
|
13
13
|
return ''
|
|
14
14
|
number = (+number).toFixed(fixed)
|
|
@@ -18,7 +18,7 @@ export default {
|
|
|
18
18
|
})
|
|
19
19
|
})
|
|
20
20
|
},
|
|
21
|
-
summation(array: any[] = []) {
|
|
21
|
+
summation(array: any[] = []): number {
|
|
22
22
|
return array.reduce((prev, curr) => {
|
|
23
23
|
return prev + this.ifNaN(curr, 0)
|
|
24
24
|
}, 0)
|
package/test.js
ADDED