Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Samoy committed Jun 5, 2024
1 parent d0fe816 commit 4e93964
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
5. [有时间限制的Promise对象](src/promiseandtime/promise-time-limit.ts)
6. [有时间限制的缓存](src/promiseandtime/cache-with-time-limit.ts)
7. [函数防抖](src/promiseandtime/debounce.ts)
8. [并行执行异步函数](src/promiseandtime/execute-asynchronous-functions-in-parallel.ts)
8. [并行执行异步函数](src/promiseandtime/execute-asynchronous-functions-in-parallel.ts)

## JSON

1. [判断对象是否为空](src/json/is-object-empty.ts)
18 changes: 18 additions & 0 deletions src/json/is-object-empty.ts
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;
}
14 changes: 14 additions & 0 deletions test/json/is-object-empty.test.ts
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()
})
})

0 comments on commit 4e93964

Please sign in to comment.