qmwts 1.0.5 → 1.0.6

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 CHANGED
@@ -1,36 +1,9 @@
1
- const { NumberUtils, JsonUtils } = require('./index')
1
+ const { NumberUtils, JsonUtils, UUIDUtils } = require('./index')
2
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
-
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
- NumberUtils,
7
- JsonUtils,
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qmwts",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "scripts": {
@@ -1,42 +1,42 @@
1
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
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
  }
@@ -1,26 +1,26 @@
1
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
- 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
- }
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,9 @@
1
+ export default {
2
+ uuid(): string {
3
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
4
+ const r = (Math.random() * 16) | 0
5
+ const v = c === 'x' ? r : (r & 0x3) | 0x8
6
+ return v.toString(16)
7
+ })
8
+ }
9
+ }
package/test.js DELETED
@@ -1,9 +0,0 @@
1
- var Color;
2
- (function (Color) {
3
- Color[Color["Red"] = 1] = "Red";
4
- Color[Color["Y"] = 1] = "Y";
5
- Color[Color["S"] = 2] = "S";
6
- })(Color || (Color = {}));
7
- var c = Color.S;
8
- console.log(Color.Red);
9
- console.log(Color.Y);
package/test.ts DELETED
@@ -1,11 +0,0 @@
1
- enum Color {Red = 1, Y = 1, S}
2
-
3
- const c = Color.S
4
- console.log(Color.Red)
5
- console.log(Color.Y)
6
-
7
- function a(o: { label?: number }) {
8
- console.log(o.label)
9
- }
10
-
11
- a({})