Skip to content

Commit b9ba369

Browse files
authored
✨ feat: get-keys #12 from use-kit/feat/fns
2 parents b82bf41 + 4aa642a commit b9ba369

File tree

14 files changed

+226
-8
lines changed

14 files changed

+226
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
category: '@Core'
3+
---
4+
5+
# getField
6+
7+
Get object field with multiple default fields.
8+
9+
## Usage
10+
11+
```ts
12+
import { getFieldWithDefault } from '@use-kit/functions'
13+
14+
const obj = { name: 'asuka', age: 23, height: 158 }
15+
const ret = getFieldWithDefault(obj, 'default', obj.id, obj.name, obj.nickname) // 'asuak'
16+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getFieldWithDefault } from '.'
3+
4+
describe('getFieldWithDefault', () => {
5+
it('default', () => {
6+
const obj = { name: 'asuka', age: 23, height: 158 }
7+
expect(getFieldWithDefault(
8+
obj,
9+
'defaultValue',
10+
(obj as any)?.id,
11+
obj?.name,
12+
(obj as any)?.nickname,
13+
)).toMatchInlineSnapshot('"defaultValue"')
14+
})
15+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface ObjectWithFields { [key: string]: any }
2+
3+
// TODO: get field with multiple default fields
4+
export function getFieldWithDefault<T>(
5+
obj: ObjectWithFields,
6+
defaultValue: T,
7+
...fields: string[]
8+
): T {
9+
if (typeof obj !== 'object' || obj === null)
10+
return defaultValue
11+
12+
console.log('fields', fields)
13+
for (const field of fields) {
14+
if (Reflect.has(obj, field))
15+
return field as T
16+
}
17+
console.log('default')
18+
19+
return defaultValue
20+
}

packages/core/getKeys/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
category: '@Core'
3+
---
4+
5+
# getKeys
6+
7+
Get keys.
8+
9+
## Usage
10+
11+
```ts{6,7}
12+
import { getKeys } from '@use-kit/functions'
13+
14+
const p = { name: 'asuka', age: 24 }
15+
const ret = getKeys(p)
16+
// [
17+
// "name",
18+
// "age",
19+
// ]
20+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getKeys } from '.'
3+
4+
describe('getKeys', () => {
5+
it('get keys', () => expect(getKeys({ name: 'asuka', age: 24 })).toMatchInlineSnapshot(`
6+
[
7+
"name",
8+
"age",
9+
]
10+
`))
11+
12+
const map = new Map([
13+
['akashi', { id: 'akashi' }],
14+
['asuka', { id: 'asuka' }],
15+
])
16+
17+
it('get map keys', () => {
18+
expect(getKeys(map)).toMatchInlineSnapshot(`
19+
[
20+
"akashi",
21+
"asuka",
22+
]
23+
`)
24+
})
25+
})

packages/core/getKeys/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { isEmpty, isMap } from '..'
2+
3+
export const getKeys = (
4+
obj: object | Map<string, any> | null | undefined,
5+
): string[] => {
6+
if (isEmpty(obj))
7+
return []
8+
9+
if (isMap(obj))
10+
return Array.from((obj as Map<string, any>).keys())
11+
12+
else
13+
return Object.keys(obj as object)
14+
}

packages/core/getValues/index.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ Get values.
88

99
## Usage
1010

11-
```ts
11+
```ts{6,7}
1212
import { getValues } from '@use-kit/functions'
13+
14+
const p = { name: 'asuka', age: 24 }
15+
const ret = getValues(p)
16+
// [
17+
// "asuka",
18+
// 24,
19+
// ]
1320
```
Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
import { describe, expect, it } from 'vitest'
2+
import { getValues } from '.'
23

34
describe('getField', () => {
4-
it('get values', () => expect(true).toBeTruthy())
5+
it('get values', () => expect(getValues({ name: 'asuka', age: 24 })).toMatchInlineSnapshot(`
6+
[
7+
"asuka",
8+
24,
9+
]
10+
`))
11+
12+
it('get string values', () => {
13+
expect(getValues('akashi')).toMatchInlineSnapshot(`
14+
[
15+
"a",
16+
"k",
17+
"a",
18+
"s",
19+
"h",
20+
"i",
21+
]
22+
`)
23+
})
24+
25+
const map = new Map([
26+
['akashi', { id: 'akashi' }],
27+
['asuka', { id: 'asuka' }],
28+
])
29+
30+
it('get map values', () => {
31+
expect(getValues(map as any)).toMatchInlineSnapshot(`
32+
[
33+
{
34+
"id": "akashi",
35+
},
36+
{
37+
"id": "asuka",
38+
},
39+
]
40+
`)
41+
})
542
})

packages/core/getValues/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
export const getValues = <T>(object: { [key: string]: T } | null | undefined): T[] => {
2-
if (object == null)
1+
import { isEmpty, isMap } from '..'
2+
3+
interface GenericObject<T> {
4+
[key: string]: T
5+
}
6+
7+
export const getValues = <T>(object: {
8+
[key: string]: T
9+
} | Map<string, T> | string | null | undefined): T[] => {
10+
if (isEmpty(object))
311
return []
412

5-
return Object.keys(object).map(key => object[key])
13+
if (isMap(object))
14+
return Array.from((object as Map<string, T>).values())
15+
16+
return Object
17+
.keys((object as GenericObject<T>))
18+
.map(key => (object as GenericObject<T>)[key])
619
}

packages/core/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export * from './getAge'
1313
export * from './getConstellation'
1414
export * from './getField'
1515
export * from './getFileType'
16+
export * from './getKeys'
1617
export * from './getObjectField'
1718
export * from './getTreeNode'
19+
export * from './getValues'
1820

1921
export * from './hasField'
2022
export * from './hideMobile'

0 commit comments

Comments
 (0)