Skip to content

Commit

Permalink
feat: add get
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 7, 2018
1 parent 71e2229 commit 43d6035
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
24 changes: 19 additions & 5 deletions src/get.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import forOwn from './forOwn'
import has from './has'
import toPath from './toPath'

export default function get(value: object, path: string | string[], defaultValue?: any): any {
/**
* 根据 obj 对象的路径 path 获取值。
*
* @param obj 要检索的对象
* @param path 属性路径
* @param [defaultValue] 默认值
* @returns 检索结果
*/
export default function get(obj: object, path: string | string[], defaultValue?: any): any {
path = Array.isArray(path) ? path : toPath(path)
const result: any = defaultValue
const last: any = value
let last: any = obj
for (let i = 0, len = path.length; i < len; i++) {

if (has(last, path[i])) {
last = last[path[i]]
if (i === len - 1) {
return last
}
} else {
return defaultValue
}
}
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { default as Disposer } from './Disposer'
export { default as Validator } from './Validator'
export { default as base64Decode } from './base64Decode'
export { default as base64Encode } from './base64Encode'
export { default as base64UrlDecode } from './base64UrlDecode'
Expand All @@ -8,11 +6,13 @@ export { default as bindEvent } from './bindEvent'
export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as cssTransform } from './cssTransform'
export { default as Disposer } from './Disposer'
export { default as endsWith } from './endsWith'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as formatCurrency } from './formatCurrency'
export { default as formatDate } from './formatDate'
export { default as forOwn } from './forOwn'
export { default as get } from './get'
export { default as getType } from './getType'
export { default as has } from './has'
export { default as inBrowser } from './inBrowser'
Expand Down Expand Up @@ -61,4 +61,5 @@ export { default as supportPassiveEventListener } from './supportPassiveEventLis
export { default as toDate } from './toDate'
export { default as toPath } from './toPath'
export { default as upperCaseFirst } from './upperCaseFirst'
export { default as Validator } from './Validator'
export { default as values } from './values'
35 changes: 28 additions & 7 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,12 +1207,33 @@ describe('isEmpty', () => {
})
})

describe('request', () => {
test('ok', async () => {
const data: any = await vtils.request({
url: 'https://jsonplaceholder.typicode.com/todos/1'
})
expect(data.status).toBe(200)
expect(data.data.id).toBe(1)
// describe('request', () => {
// test('ok', async () => {
// const data: any = await vtils.request({
// url: 'https://jsonplaceholder.typicode.com/todos/1'
// })
// expect(data.status).toBe(200)
// expect(data.data.id).toBe(1)
// })
// })

describe('get', () => {
test('ok', () => {
const obj: any = {
x: 1,
y: [1, 2, { z: null }],
z: { we: 'hello' },
'1.2': 1.2
}
expect(vtils.get(obj, 'x')).toBe(1)
expect(vtils.get(obj, 'y[1]')).toBe(2)
expect(vtils.get(obj, 'y[2].z')).toBe(null)
expect(vtils.get(obj, 'z.we')).toBe('hello')
expect(vtils.get(obj, 'z.we[1]')).toBe('e')
expect(vtils.get(obj, 'z.we[1].xxx')).toBe(undefined)
expect(vtils.get(obj, 'yyy')).toBe(undefined)
expect(vtils.get(obj, 'yyy', 2013)).toBe(2013)
expect(vtils.get(obj, '[1.2]')).toBe(1.2)
expect(vtils.get(obj, '1.2')).toBe(undefined)
})
})

0 comments on commit 43d6035

Please sign in to comment.