diff --git a/packages/browser/download/index.ts b/packages/browser/download/index.ts index 8f9b7f2..c47d3a6 100644 --- a/packages/browser/download/index.ts +++ b/packages/browser/download/index.ts @@ -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() } diff --git a/packages/core/getField/index.test.ts b/packages/core/getField/index.test.ts index 7051ebf..d927fb9 100644 --- a/packages/core/getField/index.test.ts +++ b/packages/core/getField/index.test.ts @@ -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"') @@ -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"') + }) }) diff --git a/packages/core/getField/index.ts b/packages/core/getField/index.ts index 93347bf..7d2551d 100644 --- a/packages/core/getField/index.ts +++ b/packages/core/getField/index.ts @@ -1,12 +1,24 @@ -export const getField = ( - target: Record, - field: string, - preset: unknown = undefined, -) => { - // TODO: use like lodash.get() +// export const getField = ( +// target: Record, +// 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(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 } diff --git a/packages/core/getValues/index.ts b/packages/core/getValues/index.ts index 16182de..27b3113 100644 --- a/packages/core/getValues/index.ts +++ b/packages/core/getValues/index.ts @@ -1,6 +1,6 @@ -export const getValues = ( - target: Record, -) => { - // TODO: use like lodash.values() - return [] +export const getValues = (object: { [key: string]: T } | null | undefined): T[] => { + if (object == null) + return [] + + return Object.keys(object).map(key => object[key]) }