-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { jestExpectEqual } from './enhanceJest' | ||
import { safeGet } from './safeGet' | ||
|
||
test('表现正常', () => { | ||
const obj = { | ||
x: 1, | ||
s: '字符串', | ||
y: { | ||
yy: { | ||
yyyy: { | ||
yyyyy: () => 'hello', | ||
yyyyy2: true, | ||
}, | ||
yyyy2: [0, 1, 2, 3], | ||
yyyy3: /.+/, | ||
}, | ||
}, | ||
z: { | ||
zz: null, | ||
zzz: undefined, | ||
zzzz: 'hoho', | ||
}, | ||
} | ||
|
||
jestExpectEqual( | ||
safeGet(obj, 'x'), | ||
obj.x, | ||
) | ||
|
||
jestExpectEqual( | ||
safeGet(obj, 's', 'length'), | ||
obj.s.length, | ||
) | ||
|
||
jestExpectEqual( | ||
safeGet(obj, 'y', 'yy'), | ||
obj.y.yy, | ||
) | ||
|
||
jestExpectEqual( | ||
safeGet(obj, 'y', 'yy', 'yyyy2'), | ||
obj.y.yy.yyyy2, | ||
) | ||
|
||
jestExpectEqual( | ||
safeGet(obj, 'y', 'yy', 'yyyy2', 'slice'), | ||
obj.y.yy.yyyy2.slice, | ||
) | ||
|
||
jestExpectEqual( | ||
safeGet(obj, 'z', 'zz'), | ||
obj.z.zz, | ||
) | ||
|
||
jestExpectEqual( | ||
safeGet(obj, 'z', 'zzz'), | ||
obj.z.zzz, | ||
) | ||
|
||
jestExpectEqual( | ||
safeGet(obj, 'z', 'zzzz'), | ||
obj.z.zzzz, | ||
) | ||
|
||
jestExpectEqual( | ||
safeGet(obj as any, 'z', 'no', 'no1', 'no2'), | ||
undefined, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { AnyObject } from './enhanceType' | ||
import { isNil } from './is' | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
>(obj: T, k0: K0): T[K0] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
>(obj: T, k0: K0, k1: K1): T[K0][K1] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
K2 extends keyof T[K0][K1], | ||
>(obj: T, k0: K0, k1: K1, k2: K2): T[K0][K1][K2] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
K2 extends keyof T[K0][K1], | ||
K3 extends keyof T[K0][K1][K2], | ||
>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3): T[K0][K1][K2][K3] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
K2 extends keyof T[K0][K1], | ||
K3 extends keyof T[K0][K1][K2], | ||
K4 extends keyof T[K0][K1][K2][K3], | ||
>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4): T[K0][K1][K2][K3][K4] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
K2 extends keyof T[K0][K1], | ||
K3 extends keyof T[K0][K1][K2], | ||
K4 extends keyof T[K0][K1][K2][K3], | ||
K5 extends keyof T[K0][K1][K2][K3][K4], | ||
>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): T[K0][K1][K2][K3][K4][K5] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
K2 extends keyof T[K0][K1], | ||
K3 extends keyof T[K0][K1][K2], | ||
K4 extends keyof T[K0][K1][K2][K3], | ||
K5 extends keyof T[K0][K1][K2][K3][K4], | ||
K6 extends keyof T[K0][K1][K2][K3][K4][K5], | ||
>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): T[K0][K1][K2][K3][K4][K5][K6] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
K2 extends keyof T[K0][K1], | ||
K3 extends keyof T[K0][K1][K2], | ||
K4 extends keyof T[K0][K1][K2][K3], | ||
K5 extends keyof T[K0][K1][K2][K3][K4], | ||
K6 extends keyof T[K0][K1][K2][K3][K4][K5], | ||
K7 extends keyof T[K0][K1][K2][K3][K4][K5][K6], | ||
>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, k7: K7): T[K0][K1][K2][K3][K4][K5][K6][K7] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
K2 extends keyof T[K0][K1], | ||
K3 extends keyof T[K0][K1][K2], | ||
K4 extends keyof T[K0][K1][K2][K3], | ||
K5 extends keyof T[K0][K1][K2][K3][K4], | ||
K6 extends keyof T[K0][K1][K2][K3][K4][K5], | ||
K7 extends keyof T[K0][K1][K2][K3][K4][K5][K6], | ||
K8 extends keyof T[K0][K1][K2][K3][K4][K5][K6][K7], | ||
>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, k7: K7, k8: K8): T[K0][K1][K2][K3][K4][K5][K6][K7][K8] | ||
|
||
export function safeGet< | ||
T extends AnyObject, | ||
K0 extends keyof T, | ||
K1 extends keyof T[K0], | ||
K2 extends keyof T[K0][K1], | ||
K3 extends keyof T[K0][K1][K2], | ||
K4 extends keyof T[K0][K1][K2][K3], | ||
K5 extends keyof T[K0][K1][K2][K3][K4], | ||
K6 extends keyof T[K0][K1][K2][K3][K4][K5], | ||
K7 extends keyof T[K0][K1][K2][K3][K4][K5][K6], | ||
K8 extends keyof T[K0][K1][K2][K3][K4][K5][K6][K7], | ||
K9 extends keyof T[K0][K1][K2][K3][K4][K5][K6][K7][K8], | ||
>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, k7: K7, k8: K8, k9: K9): T[K0][K1][K2][K3][K4][K5][K6][K7][K8][K9] | ||
|
||
export function safeGet(obj: AnyObject, ...path: any[]): any { | ||
let last: any = obj | ||
const end = path.length - 1 | ||
for (let i = 0; i < end; i++) { | ||
last = last[path[i]] | ||
if (isNil(last)) { | ||
last = undefined | ||
break | ||
} | ||
} | ||
return isNil(last) ? last : last[path[end]] | ||
} |