From 7e85b3565e6039493b463318836459f16cb94a58 Mon Sep 17 00:00:00 2001 From: akashi Date: Mon, 24 Jul 2023 17:12:35 +0800 Subject: [PATCH 1/5] :sparkles: feat: get-values --- packages/core/getValues/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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]) } From 6415661e425225b67835c07fe3ff383d86b6c23f Mon Sep 17 00:00:00 2001 From: akashi Date: Mon, 24 Jul 2023 17:19:48 +0800 Subject: [PATCH 2/5] :zap: chore: check if browser support download --- packages/browser/download/index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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() } From 9b84ae7a2878b43c612a732b795be8e1ac026e69 Mon Sep 17 00:00:00 2001 From: akashi Date: Wed, 26 Jul 2023 10:01:22 +0800 Subject: [PATCH 3/5] :zap: chore: get-field use like lodash.get --- packages/core/getField/index.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/core/getField/index.ts b/packages/core/getField/index.ts index 93347bf..cf92c81 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 const getField = (target: any, path: string, preset?: T): T => { + const keys = 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 } From 49cca845a93809b6720cf511e9a8998ca06fe38a Mon Sep 17 00:00:00 2001 From: akashi Date: Fri, 28 Jul 2023 15:44:36 +0800 Subject: [PATCH 4/5] :zap: chore: get-field support array call --- packages/core/getField/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/getField/index.ts b/packages/core/getField/index.ts index cf92c81..7d2551d 100644 --- a/packages/core/getField/index.ts +++ b/packages/core/getField/index.ts @@ -9,8 +9,8 @@ // return preset // } -export const getField = (target: any, path: string, preset?: T): T => { - const keys = path.split('.') +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) { From 1a72915f7ff951dde9faa17c47a32dd1f7c9928f Mon Sep 17 00:00:00 2001 From: akashi Date: Fri, 28 Jul 2023 16:29:58 +0800 Subject: [PATCH 5/5] :white_check_mark: chore: update test --- packages/core/getField/index.test.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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"') + }) })