From 0d67141a6bfe5e5099395cfbaf95c5e5eadd7ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=9B=A8=E6=B0=B4=E8=BF=87=E6=BB=A4=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E6=B0=94-Rain?= <958414905@qq.com> Date: Mon, 9 Oct 2023 08:46:46 +0800 Subject: [PATCH] fix(variable): should not return undefined when parsing 0 (#2766) --- packages/core/utils/src/__tests__/getValuesByPath.test.ts | 6 ++++++ packages/core/utils/src/getValuesByPath.ts | 2 +- tsconfig.json | 2 +- vitest.config.ts | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/core/utils/src/__tests__/getValuesByPath.test.ts b/packages/core/utils/src/__tests__/getValuesByPath.test.ts index 482215909..365e64fd5 100644 --- a/packages/core/utils/src/__tests__/getValuesByPath.test.ts +++ b/packages/core/utils/src/__tests__/getValuesByPath.test.ts @@ -94,4 +94,10 @@ describe('getValuesByPath', () => { const result = getValuesByPath(arr, 'b', []); expect(result).toEqual([1, 2]); }); + + it('should return 0 when the initial value is 0', () => { + const obj = { a: 0 }; + const result = getValuesByPath(obj, 'a'); + expect(result).toBe(0); + }); }); diff --git a/packages/core/utils/src/getValuesByPath.ts b/packages/core/utils/src/getValuesByPath.ts index af0e60c58..1fd619868 100644 --- a/packages/core/utils/src/getValuesByPath.ts +++ b/packages/core/utils/src/getValuesByPath.ts @@ -29,7 +29,7 @@ export const getValuesByPath = (obj: object, path: string, defaultValue?: any) = } } - result = result.filter(Boolean); + result = result.filter((item) => item != null); if (result.length === 0) { return defaultValue; diff --git a/tsconfig.json b/tsconfig.json index e77110905..45d57e364 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,7 +24,7 @@ "module": "commonjs" } }, - "include": ["packages/**/*", ".dumi/**/*", ".dumirc.ts", "scripts/*", "playwright.config.ts"], + "include": ["packages/**/*", ".dumi/**/*", ".dumirc.ts", "scripts/*", "playwright.config.ts", "vitest.config.ts"], "exclude": [ "packages/**/node_modules", "packages/**/dist", diff --git a/vitest.config.ts b/vitest.config.ts index f36387ae6..792bc24ac 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -34,7 +34,7 @@ export default defineConfig({ { find: /^~antd\/(.*)/, replacement: 'antd/$1' }, ...alias, ], - include: ['packages/**/{dumi-theme-nocobase,sdk,client}/**/__tests__/**/*.{test,spec}.{ts,tsx}'], + include: ['packages/**/{dumi-theme-nocobase,sdk,client,utils}/**/__tests__/**/*.{test,spec}.{ts,tsx}'], exclude: [ '**/node_modules/**', '**/dist/**',