-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,18 @@ | ||
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue }; | ||
type Obj = Record<string, JSONValue> | JSONValue[] | ||
|
||
/** | ||
* 给定一个对象或数组,判断它是否为空。 | ||
* | ||
* 一个空对象不包含任何键值对。 | ||
* 一个空数组不包含任何元素。 | ||
* 你可以假设对象或数组是通过 JSON.parse 解析得到的。 | ||
* @link https://leetcode.cn/problems/is-object-empty/description/?envType=study-plan-v2&envId=30-days-of-javascript | ||
* @param obj | ||
*/ | ||
export function isEmpty(obj: Obj): boolean { | ||
for (const _ in obj) { | ||
return false; | ||
} | ||
return true; | ||
} |
This file contains 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,14 @@ | ||
import {describe, expect, test} from "vitest"; | ||
import {isEmpty} from "@/json/is-object-empty"; | ||
|
||
describe('Test object is empty', () => { | ||
test('Test case 1', () => { | ||
expect(isEmpty({"x": 5, "y": 42})).toBeFalsy() | ||
}) | ||
test('Test case 2', () => { | ||
expect(isEmpty({})).toBeTruthy() | ||
}) | ||
test('Test case 3', () => { | ||
expect(isEmpty([null, false, 0])).toBeFalsy() | ||
}) | ||
}) |