@leafer/data 1.7.0 → 1.9.0
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 +2 -2
- package/src/DataHelper.ts +2 -2
- package/src/LeafData.ts +8 -7
- package/src/data.ts +48 -0
- package/src/index.ts +1 -19
- package/types/index.d.ts +9 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/data",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "@leafer/data",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"leaferjs"
|
|
23
23
|
],
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@leafer/interface": "1.
|
|
25
|
+
"@leafer/interface": "1.9.0"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/src/DataHelper.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IBooleanMap, IObject } from '@leafer/interface'
|
|
2
|
-
|
|
2
|
+
import { isUndefined } from './data'
|
|
3
3
|
|
|
4
4
|
export const DataHelper = {
|
|
5
5
|
|
|
@@ -24,7 +24,7 @@ export const DataHelper = {
|
|
|
24
24
|
|
|
25
25
|
copyAttrs(t: IObject, from: IObject, include: string[]): IObject {
|
|
26
26
|
include.forEach(key => {
|
|
27
|
-
if (from[key]
|
|
27
|
+
if (!isUndefined(from[key])) t[key] = from[key]
|
|
28
28
|
})
|
|
29
29
|
return t
|
|
30
30
|
},
|
package/src/LeafData.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ILeafData, ILeaf, IObject, IValue, IPathCommandData, IJSONOptions } from '@leafer/interface'
|
|
2
|
+
import { isArray, isUndefined } from './data'
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
export class LeafData implements ILeafData {
|
|
@@ -35,7 +36,7 @@ export class LeafData implements ILeafData {
|
|
|
35
36
|
public __get(name: string): any {
|
|
36
37
|
if (this.__input) {
|
|
37
38
|
const value = this.__input[name]
|
|
38
|
-
if (value
|
|
39
|
+
if (!isUndefined(value)) return value
|
|
39
40
|
}
|
|
40
41
|
return (this as IObject)[name]
|
|
41
42
|
}
|
|
@@ -46,7 +47,7 @@ export class LeafData implements ILeafData {
|
|
|
46
47
|
for (let key in this) {
|
|
47
48
|
if (key[0] !== '_') {
|
|
48
49
|
inputValue = __input ? __input[key] : undefined
|
|
49
|
-
data[key] = (inputValue
|
|
50
|
+
data[key] = isUndefined(inputValue) ? this[key] : inputValue
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
return data
|
|
@@ -60,7 +61,7 @@ export class LeafData implements ILeafData {
|
|
|
60
61
|
public __getInput(name: string): any {
|
|
61
62
|
if (this.__input) {
|
|
62
63
|
const value = this.__input[name]
|
|
63
|
-
if (value
|
|
64
|
+
if (!isUndefined(value)) return value
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
if (name === 'path' && !(this as ILeafData).__pathInputed) return // no path mode
|
|
@@ -69,7 +70,7 @@ export class LeafData implements ILeafData {
|
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
public __removeInput(name: string): void {
|
|
72
|
-
if (this.__input && this.__input[name]
|
|
73
|
+
if (this.__input && !isUndefined(this.__input[name])) this.__input[name] = undefined
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
public __getInputData(names?: string[] | IObject, options?: IJSONOptions): IObject {
|
|
@@ -77,7 +78,7 @@ export class LeafData implements ILeafData {
|
|
|
77
78
|
|
|
78
79
|
if (names) {
|
|
79
80
|
|
|
80
|
-
if (names
|
|
81
|
+
if (isArray(names)) {
|
|
81
82
|
for (let name of names) data[name] = this.__getInput(name)
|
|
82
83
|
} else {
|
|
83
84
|
for (let name in names) data[name] = this.__getInput(name)
|
|
@@ -90,12 +91,12 @@ export class LeafData implements ILeafData {
|
|
|
90
91
|
for (let key in this) {
|
|
91
92
|
if (key[0] !== '_') {
|
|
92
93
|
value = (this as IObject)['_' + key]
|
|
93
|
-
if (value
|
|
94
|
+
if (!isUndefined(value)) {
|
|
94
95
|
|
|
95
96
|
if (key === 'path' && !(this as ILeafData).__pathInputed) continue // no path mode
|
|
96
97
|
|
|
97
98
|
inputValue = __input ? __input[key] : undefined
|
|
98
|
-
data[key] = (inputValue
|
|
99
|
+
data[key] = isUndefined(inputValue) ? value : inputValue
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
102
|
}
|
package/src/data.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { IObject } from '@leafer/interface'
|
|
2
|
+
|
|
3
|
+
export enum Answer {
|
|
4
|
+
No = 0,
|
|
5
|
+
Yes = 1,
|
|
6
|
+
NoAndSkip = 2,
|
|
7
|
+
YesAndSkip = 3
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const emptyData: IObject = {}
|
|
11
|
+
|
|
12
|
+
export function isUndefined(value: any): boolean {
|
|
13
|
+
return value === undefined
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isNull(value: any): boolean {
|
|
17
|
+
return value === undefined || value === null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isString<T extends string>(value: any): value is T {
|
|
21
|
+
return typeof value === 'string'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const { isFinite } = Number
|
|
25
|
+
|
|
26
|
+
export function isNumber<T extends number>(value: any): value is T {
|
|
27
|
+
return typeof value === 'number'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const numberReg = /^-?\d+(?:\.\d+)?$/
|
|
31
|
+
|
|
32
|
+
export function tryToNumber(value: any): number {
|
|
33
|
+
return (typeof value === 'string' && numberReg.test(value)) ? +value : value
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const { isArray } = Array
|
|
37
|
+
|
|
38
|
+
export function isObject<T extends object>(value: any): value is T {
|
|
39
|
+
return value && typeof value === 'object' // fix: null is object
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function isData<T extends object>(value: any): value is T { // 检测 {} 对象
|
|
43
|
+
return isObject(value) && !isArray(value) // 排除数组
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isEmptyData(value: any): boolean {
|
|
47
|
+
return JSON.stringify(value) === '{}'
|
|
48
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
import { IObject } from '@leafer/interface'
|
|
2
|
-
|
|
3
1
|
export { DataHelper } from './DataHelper'
|
|
4
2
|
export { LeafData } from './LeafData'
|
|
5
|
-
|
|
6
|
-
export enum Answer {
|
|
7
|
-
No = 0,
|
|
8
|
-
Yes = 1,
|
|
9
|
-
NoAndSkip = 2,
|
|
10
|
-
YesAndSkip = 3
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const emptyData: IObject = {}
|
|
14
|
-
|
|
15
|
-
export function isNull(value: any): boolean {
|
|
16
|
-
return value === undefined || value === null
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function isEmptyData(value: any): boolean {
|
|
20
|
-
return JSON.stringify(value) === '{}'
|
|
21
|
-
}
|
|
3
|
+
export { Answer, emptyData, isUndefined, isNull, isString, isFinite, isNumber, isArray, isObject, isData, isEmptyData, tryToNumber } from './data'
|
package/types/index.d.ts
CHANGED
|
@@ -42,7 +42,15 @@ declare enum Answer {
|
|
|
42
42
|
YesAndSkip = 3
|
|
43
43
|
}
|
|
44
44
|
declare const emptyData: IObject;
|
|
45
|
+
declare function isUndefined(value: any): boolean;
|
|
45
46
|
declare function isNull(value: any): boolean;
|
|
47
|
+
declare function isString<T extends string>(value: any): value is T;
|
|
48
|
+
declare const isFinite: (number: unknown) => boolean;
|
|
49
|
+
declare function isNumber<T extends number>(value: any): value is T;
|
|
50
|
+
declare function tryToNumber(value: any): number;
|
|
51
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
52
|
+
declare function isObject<T extends object>(value: any): value is T;
|
|
53
|
+
declare function isData<T extends object>(value: any): value is T;
|
|
46
54
|
declare function isEmptyData(value: any): boolean;
|
|
47
55
|
|
|
48
|
-
export { Answer, DataHelper, LeafData, emptyData, isEmptyData, isNull };
|
|
56
|
+
export { Answer, DataHelper, LeafData, emptyData, isArray, isData, isEmptyData, isFinite, isNull, isNumber, isObject, isString, isUndefined, tryToNumber };
|