Skip to content

Commit b82bf41

Browse files
authored
✨ feat: get-values #11 from use-kit/feat/fns
2 parents 57e909f + 1a72915 commit b82bf41

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

packages/browser/download/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export const download = (url: string, name = 'download') => {
2-
// TODO: unprocessed => fetch cors
32
const link = document.createElement('a')
4-
53
link.download = name
64
link.href = url
7-
link.click()
5+
6+
// Check if the browser supports the download attribute
7+
if (typeof link.download === 'undefined')
8+
window.open(url)
9+
10+
else
11+
link.click()
812
}

packages/core/getField/index.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
22
import { getField } from '.'
33

44
describe('getField', () => {
5-
const obj = { name: 'asuka', age: 23, height: 158 }
5+
const obj = { name: 'asuka', age: 23, height: 158, deep: { one: '1' } }
66

77
it('default', () => {
88
expect(getField(obj, 'name')).toMatchInlineSnapshot('"asuka"')
@@ -19,4 +19,12 @@ describe('getField', () => {
1919
it('undefine', () => {
2020
expect(getField(obj, 'test')).toMatchInlineSnapshot('undefined')
2121
})
22+
23+
it('array', () => {
24+
expect(getField(obj, ['name'])).toMatchInlineSnapshot('"asuka"')
25+
})
26+
27+
it('deep array', () => {
28+
expect(getField(obj, ['deep', 'one'])).toMatchInlineSnapshot('"1"')
29+
})
2230
})

packages/core/getField/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
export const getField = (
2-
target: Record<string, any>,
3-
field: string,
4-
preset: unknown = undefined,
5-
) => {
6-
// TODO: use like lodash.get()
1+
// export const getField = (
2+
// target: Record<string, any>,
3+
// field: string,
4+
// preset: unknown = undefined,
5+
// ) => {
6+
// if (Reflect.has(target, field))
7+
// return target[field]
78

8-
if (Reflect.has(target, field))
9-
return target[field]
9+
// return preset
10+
// }
1011

11-
return preset
12+
export function getField<T>(target: any, path: string | string[], preset?: T): T {
13+
const keys = Array.isArray(path) ? path : path.split('.')
14+
let value: any = target
15+
16+
for (const key of keys) {
17+
if (value === null || value === undefined)
18+
return preset!
19+
20+
value = value[key]
21+
}
22+
23+
return value !== undefined ? value : preset
1224
}

packages/core/getValues/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export const getValues = (
2-
target: Record<string, any>,
3-
) => {
4-
// TODO: use like lodash.values()
5-
return []
1+
export const getValues = <T>(object: { [key: string]: T } | null | undefined): T[] => {
2+
if (object == null)
3+
return []
4+
5+
return Object.keys(object).map(key => object[key])
66
}

0 commit comments

Comments
 (0)