Skip to content
Open
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
13 changes: 13 additions & 0 deletions docs/api/_media/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
38 changes: 38 additions & 0 deletions docs/api/functions/isEmpty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[dt-utils](../globals.md) / isEmpty

# Function: isEmpty()

> **isEmpty**(`value`): `boolean`

Defined in: [isEmpty/index.ts:26](https://github.com/DTStack/dt-utils/blob/master/src/isEmpty/index.ts#L26)

检查一个值是否为空。空值包括: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
```
4 changes: 4 additions & 0 deletions docs/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
- [~~shouldRender~~](functions/shouldRender.md)
- [trim](functions/trim.md)

## 工具函数

- [isEmpty](functions/isEmpty.md)

## 枚举
日期和时间格式模式的枚举
提供标准化的格式标记以实现一致的日期/时间格式化
Expand Down
10 changes: 10 additions & 0 deletions docs/api/typedoc-sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
}
]
},
{
"text": "工具函数",
"collapsed": true,
"items": [
{
"text": "isEmpty",
"link": "/api/functions/isEmpty.md"
}
]
},
{
"text": "枚举\n日期和时间格式模式的枚举\n提供标准化的格式标记以实现一致的日期/时间格式化",
"collapsed": true,
Expand Down
14 changes: 10 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
107 changes: 107 additions & 0 deletions src/isEmpty/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
57 changes: 57 additions & 0 deletions src/isEmpty/index.ts
Original file line number Diff line number Diff line change
@@ -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
Comment thread
jin-sir marked this conversation as resolved.
Comment thread
jin-sir marked this conversation as resolved.
* 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;
Comment thread
jin-sir marked this conversation as resolved.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs 应该也要新增 isEmpty

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"baseUrl": ".",
"outDir": "./lib",
"esModuleInterop": true,
"isolatedModules": true,
"moduleResolution": "Node",
"resolveJsonModule": true,
"typeRoots": ["node", "node_modules/@types"],
Expand Down
Loading