Skip to content

Commit b58113b

Browse files
committed
feat: new function - isKeyOf
1 parent ed45cd7 commit b58113b

File tree

7 files changed

+76
-1
lines changed

7 files changed

+76
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ pnpm add @utopia-utils/share
165165
* isValidUrl [source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isValidUrl.ts)
166166
* isPositiveNumber [source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isPositiveNumber.ts)
167167
* isNegativeNumber [source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isNegativeNumber.ts)
168+
* isKeyOf [source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isKeyOf.ts)
168169
------
169170

170171
### vendor

packages/core/src/yuanFormat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fenToYuan } from './fenToYuan'
44

55
interface Options {
66
/**
7-
* 单位,分或元, 如果单位是分,会自动转换为元再格式化
7+
* 单位,分或元, 如果单位是分,则会将金额转换为元
88
* @default 'fen'
99
*/
1010
unit?: 'fen' | 'yuan'

packages/share/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './isPositiveNumber'
66
export * from './isNegativeNumber'
77
export * from './measurePerformance'
88
export * from './utils'
9+
export * from './isKeyOf'

packages/share/src/isKeyOf.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { isKeyOf } from './isKeyOf'
4+
5+
describe('isKeyOf', () => {
6+
it('should return true when key exists in object', () => {
7+
const obj = { a: 1, b: 2, c: 3 }
8+
expect(isKeyOf(obj, 'a')).toBe(true)
9+
expect(isKeyOf(obj, 'b')).toBe(true)
10+
expect(isKeyOf(obj, 'c')).toBe(true)
11+
})
12+
13+
it('should return false when key does not exist in object', () => {
14+
const obj = { a: 1, b: 2, c: 3 }
15+
expect(isKeyOf(obj, 'd')).toBe(false)
16+
expect(isKeyOf(obj, 'e')).toBe(false)
17+
expect(isKeyOf(obj, 'foo')).toBe(false)
18+
})
19+
20+
it('should work with numeric keys', () => {
21+
const obj = { 1: 'one', 2: 'two' }
22+
expect(isKeyOf(obj, 1)).toBe(true)
23+
expect(isKeyOf(obj, 2)).toBe(true)
24+
expect(isKeyOf(obj, 3)).toBe(false)
25+
})
26+
27+
it('should work with symbol keys', () => {
28+
const symKey = Symbol('key')
29+
const obj = { [symKey]: 'value' }
30+
expect(isKeyOf(obj, symKey)).toBe(true)
31+
expect(isKeyOf(obj, Symbol('another'))).toBe(false)
32+
})
33+
34+
it('should work with inherited properties', () => {
35+
const proto = { a: 1 }
36+
const obj = Object.create(proto)
37+
obj.b = 2
38+
39+
expect(isKeyOf(obj, 'a')).toBe(true)
40+
expect(isKeyOf(obj, 'b')).toBe(true)
41+
expect(isKeyOf(obj, 'toString')).toBe(true)
42+
})
43+
})

packages/share/src/isKeyOf.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Type guard for any key, `k`.
3+
* Marks `k` as a key of `T` if `k` is in `obj`.
4+
*
5+
* @category Object
6+
* @param obj object to query for key `k`
7+
* @param k key to check existence in `obj`
8+
* @example
9+
* ```ts
10+
* const obj = { a: 1, b: 2 }
11+
* const k = 'a'
12+
* const isKey = isKeyOf(obj, k) // true
13+
* ```
14+
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isKeyOf.ts
15+
*/
16+
export function isKeyOf<T extends object>(obj: T, k: keyof any): k is keyof T {
17+
return k in obj
18+
}

packages/share/src/isNegativeNumber.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { isNumber } from './is'
1111
* isNegativeNumber('1') // false
1212
* isNegativeNumber('-1') // true
1313
* ```
14+
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isNegativeNumber.ts
1415
*/
1516
export function isNegativeNumber(val: unknown): boolean {
1617
return isNumber(val) && val < 0

packages/share/src/measurePerformance.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
/* eslint-disable no-console */
2+
3+
/**
4+
* Measure the performance of a function
5+
* @example
6+
* ```ts
7+
* const time = measurePerformance(this, () => {
8+
* // function body
9+
* }, arg1, arg2)
10+
* ```
11+
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/measurePerformance.ts
12+
*/
213
export function measurePerformance<T extends (...args: any[]) => any>(this: ThisParameterType<T>, fn: T, ...args: Parameters<T>): number {
314
const start = performance.now()
415
fn.call(this, ...args)

0 commit comments

Comments
 (0)