Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/browser/download/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export const download = (url: string, name = 'download') => {
// TODO: unprocessed => fetch cors
const link = document.createElement('a')

link.download = name
link.href = url
link.click()

// Check if the browser supports the download attribute
if (typeof link.download === 'undefined')
window.open(url)

else
link.click()
}
10 changes: 9 additions & 1 deletion packages/core/getField/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
import { getField } from '.'

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

it('default', () => {
expect(getField(obj, 'name')).toMatchInlineSnapshot('"asuka"')
Expand All @@ -19,4 +19,12 @@ describe('getField', () => {
it('undefine', () => {
expect(getField(obj, 'test')).toMatchInlineSnapshot('undefined')
})

it('array', () => {
expect(getField(obj, ['name'])).toMatchInlineSnapshot('"asuka"')
})

it('deep array', () => {
expect(getField(obj, ['deep', 'one'])).toMatchInlineSnapshot('"1"')
})
})
30 changes: 21 additions & 9 deletions packages/core/getField/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
export const getField = (
target: Record<string, any>,
field: string,
preset: unknown = undefined,
) => {
// TODO: use like lodash.get()
// export const getField = (
// target: Record<string, any>,
// field: string,
// preset: unknown = undefined,
// ) => {
// if (Reflect.has(target, field))
// return target[field]

if (Reflect.has(target, field))
return target[field]
// return preset
// }

return preset
export function getField<T>(target: any, path: string | string[], preset?: T): T {
const keys = Array.isArray(path) ? path : path.split('.')
let value: any = target

for (const key of keys) {
if (value === null || value === undefined)
return preset!

value = value[key]
}

return value !== undefined ? value : preset
}
10 changes: 5 additions & 5 deletions packages/core/getValues/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const getValues = (
target: Record<string, any>,
) => {
// TODO: use like lodash.values()
return []
export const getValues = <T>(object: { [key: string]: T } | null | undefined): T[] => {
if (object == null)
return []

return Object.keys(object).map(key => object[key])
}