Skip to content

Commit

Permalink
feat: 新增 safeGet
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jun 17, 2019
1 parent 26a71f4 commit a80b6e1
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export * from './randomString'
export * from './range'
export * from './repeat'
export * from './round'
export * from './safeGet'
export * from './sample'
export * from './sequential'
export * from './shuffle'
Expand Down
69 changes: 69 additions & 0 deletions src/safeGet.test.ts
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,
)
})
110 changes: 110 additions & 0 deletions src/safeGet.ts
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]]
}

0 comments on commit a80b6e1

Please sign in to comment.