Skip to content

Commit

Permalink
feat: add has
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 16, 2018
1 parent 5a8ceb6 commit e99c75c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/has.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 检查 key 是否是 obj 自身的属性。
*
* @param obj 要检查的对象
* @param key 要检查的键
* @returns 是(true)或否(false)
*/
export default function has(obj: object, key: string): boolean {
return obj != null && Object.prototype.hasOwnProperty.call(obj, key)
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export { default as Disposer } from './Disposer'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as getType } from './getType'
export { default as has } from './has'
export { default as inBrowser } from './inBrowser'
export { default as isArray } from './isArray'
export { default as isBoolean } from './isBoolean'
Expand Down
15 changes: 15 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,18 @@ describe('fill', () => {
expect(vtils.fill(emptyArr, '*', -2, -3)).toEqual([undefined, undefined, undefined])
})
})

describe('has', () => {
test('应只检查自身属性', () => {
const obj: any = { x: 1, y: null }
const fn = () => {}
fn.x = 1
Object.setPrototypeOf(fn, obj)
expect(vtils.has(obj, 'x')).toBeTruthy()
expect(vtils.has(obj, 'y')).toBeTruthy()
expect(vtils.has(obj, 'toString')).toBeFalsy()
expect(vtils.has(fn, 'x')).toBeTruthy()
expect((fn as any).y).toBeNull()
expect(vtils.has(fn, 'y')).toBeFalsy()
})
})

0 comments on commit e99c75c

Please sign in to comment.