-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add isEmpty utility function #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jin-sir
wants to merge
2
commits into
DTStack:master
Choose a base branch
from
jin-sir:feat_isEmpty
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
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; | ||
|
jin-sir marked this conversation as resolved.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs 应该也要新增 isEmpty |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.