From 24dd440af617c37b84dc6ee7d73684db6c285ebf Mon Sep 17 00:00:00 2001 From: jin-sir <942725119@qq.com> Date: Thu, 21 May 2026 18:08:27 +0800 Subject: [PATCH 1/2] feat: add isEmpty utility function --- docs/api/_media/CHANGELOG.md | 13 ++++ docs/api/functions/isEmpty.md | 46 +++++++++++++ docs/api/globals.md | 4 ++ docs/api/typedoc-sidebar.json | 10 +++ src/index.ts | 1 + src/isEmpty/__test__/index.test.ts | 107 +++++++++++++++++++++++++++++ src/isEmpty/index.ts | 57 +++++++++++++++ 7 files changed, 238 insertions(+) create mode 100644 docs/api/functions/isEmpty.md create mode 100644 src/isEmpty/__test__/index.test.ts create mode 100644 src/isEmpty/index.ts diff --git a/docs/api/_media/CHANGELOG.md b/docs/api/_media/CHANGELOG.md index 8c57168..382bd8d 100644 --- a/docs/api/_media/CHANGELOG.md +++ b/docs/api/_media/CHANGELOG.md @@ -2,6 +2,19 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [2.0.0](https://github.com/DTStack/dt-utils/compare/v1.5.0...v2.0.0) (2026-01-29) + + +### Features + +* **dt-utils:** refactor the dt-utils ([#105](https://github.com/DTStack/dt-utils/issues/105)) ([c96bb7c](https://github.com/DTStack/dt-utils/commit/c96bb7c62a4b4effadfad0ba9d6cb113e083a88d)) +* update cache actions/checkout's version ([cdac32a](https://github.com/DTStack/dt-utils/commit/cdac32a61744aabfb169a7741e236c40e475f5f0)) + + +### Bug Fixes + +* [#100](https://github.com/DTStack/dt-utils/issues/100) await IndexedDB open ([b1c6fc6](https://github.com/DTStack/dt-utils/commit/b1c6fc6d4a8bf9036d2033505868ae87ef895ffa)) + ## [1.5.0](https://github.com/DTStack/dt-utils/compare/v1.4.0...v1.5.0) (2023-11-20) diff --git a/docs/api/functions/isEmpty.md b/docs/api/functions/isEmpty.md new file mode 100644 index 0000000..4b1328f --- /dev/null +++ b/docs/api/functions/isEmpty.md @@ -0,0 +1,46 @@ +[dt-utils](../globals.md) / isEmpty + +# Function: isEmpty() + +> **isEmpty**(`value`): `boolean` + +Defined in: [isEmpty/index.ts:33](https://github.com/DTStack/dt-utils/blob/master/src/isEmpty/index.ts#L33) + +检查一个值是否为空。空值包括:null、undefined、空字符串、空数组和空对象。 + +## Parameters + +### value + +`unknown` + +要检查的值 + +## Returns + +`boolean` + +- 如果值为空,则返回 true;否则返回 false + +## Example + +```typescript +import { isEmpty } from 'dt-utils'; + +isEmpty(null); // true +isEmpty(undefined); // true +isEmpty(''); // true +isEmpty([]); // true +isEmpty({}); // true +isEmpty('hello'); // false +isEmpty([1, 2, 3]); // false +isEmpty({ a: 1 }); // false +isEmpty(new Map()); // false +isEmpty(new Set()); // false +isEmpty(new WeakMap()); // false +isEmpty(new WeakSet()); // false +isEmpty(new Date()); // false +isEmpty(new RegExp()); // false +isEmpty(new Error()); // false +isEmpty(new Foo()); // false +``` diff --git a/docs/api/globals.md b/docs/api/globals.md index 3230db2..2ca232e 100644 --- a/docs/api/globals.md +++ b/docs/api/globals.md @@ -17,6 +17,10 @@ - [~~shouldRender~~](functions/shouldRender.md) - [trim](functions/trim.md) +## 工具函数 + +- [isEmpty](functions/isEmpty.md) + ## 枚举 日期和时间格式模式的枚举 提供标准化的格式标记以实现一致的日期/时间格式化 diff --git a/docs/api/typedoc-sidebar.json b/docs/api/typedoc-sidebar.json index 433d488..60890a9 100644 --- a/docs/api/typedoc-sidebar.json +++ b/docs/api/typedoc-sidebar.json @@ -55,6 +55,16 @@ } ] }, + { + "text": "工具函数", + "collapsed": true, + "items": [ + { + "text": "isEmpty", + "link": "/api/functions/isEmpty.md" + } + ] + }, { "text": "枚举\n日期和时间格式模式的枚举\n提供标准化的格式标记以实现一致的日期/时间格式化", "collapsed": true, diff --git a/src/index.ts b/src/index.ts index 8753a10..7c0a099 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ export { default as generateUrlWithQuery } from './generateUrlWithQuery'; export { default as getQueryParameters } from './getQueryParameters'; export { default as getTypeOfValue } from './getTypeOfValue'; export { default as IndexedDB } from './indexedDB'; +export { default as isEmpty } from './isEmpty'; export { default as isMacOS } from './isMacOS'; export { default as isMobile } from './isMobile'; export { default as isWindows } from './isWindows'; diff --git a/src/isEmpty/__test__/index.test.ts b/src/isEmpty/__test__/index.test.ts new file mode 100644 index 0000000..b07d380 --- /dev/null +++ b/src/isEmpty/__test__/index.test.ts @@ -0,0 +1,107 @@ +import isEmpty from '../index'; + +describe('isEmpty', () => { + describe('should return true for empty values', () => { + test('returns true for null', () => { + expect(isEmpty(null)).toBe(true); + }); + + test('returns true for undefined', () => { + expect(isEmpty(undefined)).toBe(true); + }); + + test('returns true for empty string', () => { + expect(isEmpty('')).toBe(true); + }); + + test('returns true for empty array', () => { + expect(isEmpty([])).toBe(true); + }); + + test('returns true for empty object', () => { + expect(isEmpty({})).toBe(true); + }); + + test('returns false for empty Map instance', () => { + expect(isEmpty(new Map())).toBe(false); + }); + + test('returns false for empty Set instance', () => { + expect(isEmpty(new Set())).toBe(false); + }); + + test('returns false for empty WeakMap instance', () => { + expect(isEmpty(new WeakMap())).toBe(false); + }); + + test('returns false for empty WeakMap instance', () => { + expect(isEmpty(new WeakSet())).toBe(false); + }); + + test('returns false for custom class instance', () => { + class Foo {} + + expect(isEmpty(new Foo())).toBe(false); + }); + + test('returns false for empty RegExp instance', () => { + expect(isEmpty(new RegExp(''))).toBe(false); + }); + + test('returns false for empty Error instance', () => { + expect(isEmpty(new Error())).toBe(false); + }); + + test('returns false for empty Date instance', () => { + expect(isEmpty(new Date())).toBe(false); + }); + }); + + describe('should return false for non-empty values', () => { + test('returns false for non-empty string', () => { + expect(isEmpty('hello')).toBe(false); + }); + + test('returns false for non-empty array', () => { + expect(isEmpty([1, 2, 3])).toBe(false); + }); + + test('returns false for non-empty object', () => { + expect(isEmpty({ a: 1 })).toBe(false); + }); + + test('returns false for number 0', () => { + expect(isEmpty(0)).toBe(false); + }); + + test('returns false for boolean false', () => { + expect(isEmpty(false)).toBe(false); + }); + + test('returns false for NaN', () => { + expect(isEmpty(NaN)).toBe(false); + }); + + test('returns false for Date instance', () => { + expect(isEmpty(new Date())).toBe(false); + }); + + test('returns false for function', () => { + expect(isEmpty(() => {})).toBe(false); + }); + }); + + describe('should handle edge cases correctly', () => { + test('returns false for string with spaces', () => { + expect(isEmpty(' ')).toBe(false); + }); + + test('returns false for object with nested empty object', () => { + expect(isEmpty({ a: {} })).toBe(false); + }); + + test('returns false for array containing empty object', () => { + expect(isEmpty([{}])).toBe(false); + }); + }); +}); diff --git a/src/isEmpty/index.ts b/src/isEmpty/index.ts new file mode 100644 index 0000000..14cb4a7 --- /dev/null +++ b/src/isEmpty/index.ts @@ -0,0 +1,57 @@ +import { isEmpty as _isEmpty, isPlainObject } from 'lodash-es'; + +/** + * 检查一个值是否为空。空值包括:null、undefined、空字符串、空数组和空对象。 + * + * @category 工具函数 + * + * @param {unknown} value - 要检查的值 + * @returns {boolean} - 如果值为空,则返回 true;否则返回 false + * + * @example + * ```typescript + * import { isEmpty } from 'dt-utils'; + * + * isEmpty(null); // true + * isEmpty(undefined); // true + * isEmpty(''); // true + * isEmpty([]); // true + * isEmpty({}); // true + * isEmpty('hello'); // false + * isEmpty([1, 2, 3]); // false + * isEmpty({ a: 1 }); // false + * isEmpty(new Map()); // false + * isEmpty(new Set()); // false + * isEmpty(new WeakMap()); // false + * isEmpty(new WeakSet()); // false + * isEmpty(new Date()); // false + * isEmpty(new RegExp()); // false + * isEmpty(new Error()); // false + * isEmpty(new Foo()); // false + * ``` + */ +const isEmpty = (value: unknown): boolean => { + if (value === null) { + return true; + } + + if (value === undefined) { + return true; + } + + if (value === '') { + return true; + } + + if (Array.isArray(value)) { + return value.length === 0; + } + + if (isPlainObject(value)) { + return _isEmpty(value); + } + + return false; +}; + +export default isEmpty; From aecc87ddf19f73eb4c949fefe2594828f550a428 Mon Sep 17 00:00:00 2001 From: jin-sir <942725119@qq.com> Date: Thu, 21 May 2026 20:13:39 +0800 Subject: [PATCH 2/2] build: update tsconfig and jest config for ESM support --- jest.config.js | 14 ++++++++++---- tsconfig.json | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/jest.config.js b/jest.config.js index 2075c9c..76e2ca4 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,11 +1,17 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'jsdom', - globals: { - 'ts-jest': { - isolatedModules: true, - }, + transform: { + '^.+\\.[tj]s$': [ + 'ts-jest', + { + tsconfig: { + allowJs: true, + }, + }, + ], }, + transformIgnorePatterns: ['/node_modules/(?!(?:\\.pnpm/lodash-es@|lodash-es/))'], testPathIgnorePatterns: ['/node_modules/'], testMatch: ['**/__test__/**/(*.)+test.[jt]s?(x)'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], diff --git a/tsconfig.json b/tsconfig.json index 0eb8a9a..63243a9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,7 @@ "baseUrl": ".", "outDir": "./lib", "esModuleInterop": true, + "isolatedModules": true, "moduleResolution": "Node", "resolveJsonModule": true, "typeRoots": ["node", "node_modules/@types"],