is-object-empty2 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npmpublish.yml +48 -0
- package/LICENSE +21 -0
- package/README.md +73 -0
- package/index.d.ts +17 -0
- package/index.js +25 -0
- package/package.json +32 -0
- package/test/index.test.js +87 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# This workflow will run tests using node and then publish a package to npm using Trusted Publishing (OIDC)
|
|
2
|
+
# For more information see: https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow
|
|
3
|
+
|
|
4
|
+
name: is-object-empty2 NPM Package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ main ]
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [ main ]
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
|
|
16
|
+
# Skip Dependabot PRs for CI/CD
|
|
17
|
+
if: ${{ github.actor != 'dependabot[bot]' }}
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v4
|
|
21
|
+
- uses: actions/setup-node@v4
|
|
22
|
+
with:
|
|
23
|
+
node-version: lts/*
|
|
24
|
+
|
|
25
|
+
- run: npm install
|
|
26
|
+
- run: npm test
|
|
27
|
+
|
|
28
|
+
publish-npm:
|
|
29
|
+
needs: build
|
|
30
|
+
runs-on: ubuntu-latest
|
|
31
|
+
|
|
32
|
+
# Skip Dependabot PRs
|
|
33
|
+
if: ${{ github.actor != 'dependabot[bot]' }}
|
|
34
|
+
|
|
35
|
+
# Required for OIDC Trusted Publishing
|
|
36
|
+
permissions:
|
|
37
|
+
contents: read
|
|
38
|
+
id-token: write # 👈 REQUIRED for Trusted Publishing
|
|
39
|
+
|
|
40
|
+
steps:
|
|
41
|
+
- uses: actions/checkout@v4
|
|
42
|
+
- uses: actions/setup-node@v4
|
|
43
|
+
with:
|
|
44
|
+
node-version: lts/*
|
|
45
|
+
registry-url: https://registry.npmjs.org/
|
|
46
|
+
|
|
47
|
+
- run: npm install
|
|
48
|
+
- run: npm publish
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# is-object-empty2
|
|
2
|
+
|
|
3
|
+
<p align="center"><a href="https://nodei.co/npm/is-object-empty2/"><img src="https://nodei.co/npm/is-object-empty2.png"></a></a></p>
|
|
4
|
+
<p align="center">
|
|
5
|
+
<img src="https://img.shields.io/badge/License-MIT-yellow.svg">
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
# Simple yet robust NPM package that checks if a value is a plain empty object in `JavaScript` and `TypeScript`.
|
|
9
|
+
|
|
10
|
+
- Returns `true` **only** for plain objects with no own enumerable properties (`{}` or `Object.create(null)`).
|
|
11
|
+
- Returns `false` for arrays, `null`, `undefined`, functions, symbols, BigInt, or objects with keys.
|
|
12
|
+
- Correctly handles:
|
|
13
|
+
- Objects created with `Object.create(null)`
|
|
14
|
+
- Frozen or sealed objects
|
|
15
|
+
- Proxy objects (both empty and with keys)
|
|
16
|
+
- Objects with inherited properties
|
|
17
|
+
- Objects with non-enumerable properties
|
|
18
|
+
- Nested or deeply nested objects
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# 📦 Install via [NPM](https://www.npmjs.com/package/is-object-empty2)
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
$ npm i is-object-empty2
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
# 💻 Usage
|
|
28
|
+
|
|
29
|
+
- Returns `true` only for empty objects (`{}`), and `false` for arrays, null, undefined, or objects with keys.
|
|
30
|
+
- See examples below
|
|
31
|
+
|
|
32
|
+
# JavaScript
|
|
33
|
+
```javascript
|
|
34
|
+
const isObjectEmpty2 = require('is-object-empty');
|
|
35
|
+
|
|
36
|
+
console.log(isObjectEmpty2({})); // true
|
|
37
|
+
console.log(isObjectEmpty2({ a: 1 })); // false
|
|
38
|
+
console.log(isObjectEmpty2([])); // false
|
|
39
|
+
console.log(isObjectEmpty2(null)); // false
|
|
40
|
+
console.log(isObjectEmpty2(undefined)); // false
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
# TypeScript
|
|
44
|
+
```javascript
|
|
45
|
+
import isObjectEmpty2 = require('is-object-empty');
|
|
46
|
+
|
|
47
|
+
const testValues: unknown[] = [
|
|
48
|
+
{},
|
|
49
|
+
{ a: 1 },
|
|
50
|
+
[],
|
|
51
|
+
null,
|
|
52
|
+
undefined,
|
|
53
|
+
Object.create(null),
|
|
54
|
+
{ nested: {} }
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
for (const value of testValues) {
|
|
58
|
+
const result: boolean = isObjectEmpty2(value);
|
|
59
|
+
console.log(`${JSON.stringify(value)} → ${result}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
--| Expected values:
|
|
64
|
+
|
|
65
|
+
{} → true
|
|
66
|
+
{"a":1} → false
|
|
67
|
+
[] → false
|
|
68
|
+
null → false
|
|
69
|
+
undefined → false
|
|
70
|
+
{} → true
|
|
71
|
+
{"nested":{}} → false
|
|
72
|
+
*/
|
|
73
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a given value is a plain object and has no own properties.
|
|
3
|
+
* Returns `true` only for empty plain objects (`{}`), and `false` for arrays, null, undefined, or objects with keys.
|
|
4
|
+
*
|
|
5
|
+
* @param {unknown} obj - The value to check.
|
|
6
|
+
* @returns {boolean} - `true` if the value is an empty object, otherwise `false`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* isObjectEmpty2({}); // true
|
|
10
|
+
* isObjectEmpty2({ a: 1 }); // false
|
|
11
|
+
* isObjectEmpty2([]); // false
|
|
12
|
+
* isObjectEmpty2(null); // false
|
|
13
|
+
* isObjectEmpty2(undefined); // false
|
|
14
|
+
*/
|
|
15
|
+
declare const isObjectEmpty2: (obj: unknown) => boolean;
|
|
16
|
+
|
|
17
|
+
export = isObjectEmpty2;
|
package/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* is-object-empty2 - 📦 Tiny utility to check if a value is a plain empty object in JavaScript and TypeScript
|
|
3
|
+
* @version: v1.0.0
|
|
4
|
+
* @link: https://github.com/tutyamxx/is-object-empty2
|
|
5
|
+
* @license: MIT
|
|
6
|
+
**/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Checks if a given value is a plain object with no own properties.
|
|
11
|
+
* Returns `true` only for empty objects (`{}`), and `false` for arrays, null, undefined, or objects with keys.
|
|
12
|
+
*
|
|
13
|
+
* @param {unknown} obj - The value to check.
|
|
14
|
+
* @returns {boolean} `true` if the value is an empty plain object, otherwise `false`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* isObjectEmpty2({}); // true
|
|
18
|
+
* isObjectEmpty2({ a: 1 }); // false
|
|
19
|
+
* isObjectEmpty2([]); // false
|
|
20
|
+
* isObjectEmpty2(null); // false
|
|
21
|
+
* isObjectEmpty2(undefined); // false
|
|
22
|
+
*/
|
|
23
|
+
const isObjectEmpty2 = (obj) => obj !== null && typeof obj === 'object' && !Array.isArray(obj) ? Object.keys(obj).length === 0 : false;
|
|
24
|
+
|
|
25
|
+
module.exports = isObjectEmpty2;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "is-object-empty2",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "📦 Tiny utility to check if a value is a plain empty object in JavaScript or TypeScript",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "jest --verbose"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/tutyamxx/is-object-empty"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"is-object-empty",
|
|
16
|
+
"isemptyobject",
|
|
17
|
+
"is-object-empty",
|
|
18
|
+
"empty-object"
|
|
19
|
+
],
|
|
20
|
+
"author": "tuty",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/tutyamxx/is-object-empty/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/tutyamxx/is-object-empty#readme",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"jest": "^30.2.0"
|
|
28
|
+
},
|
|
29
|
+
"jest": {
|
|
30
|
+
"testEnvironment": "node"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const isObjectEmpty2 = require('../index.js')
|
|
2
|
+
|
|
3
|
+
describe('isObjectEmpty2', () => {
|
|
4
|
+
test('Returns true for an empty object', () => expect(isObjectEmpty2({})).toBe(true));
|
|
5
|
+
|
|
6
|
+
test('Returns false for a non-empty object', () => {
|
|
7
|
+
expect(isObjectEmpty2({ a: 1 })).toBe(false);
|
|
8
|
+
expect(isObjectEmpty2({ key: 'value' })).toBe(false);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test('Returns false for arrays', () => {
|
|
12
|
+
expect(isObjectEmpty2([])).toBe(false);
|
|
13
|
+
expect(isObjectEmpty2([1, 2, 3])).toBe(false);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('Returns false for null and undefined', () => {
|
|
17
|
+
expect(isObjectEmpty2(null)).toBe(false);
|
|
18
|
+
expect(isObjectEmpty2(undefined)).toBe(false);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('Returns false for non-object types', () => {
|
|
22
|
+
expect(isObjectEmpty2('string')).toBe(false);
|
|
23
|
+
expect(isObjectEmpty2(123)).toBe(false);
|
|
24
|
+
expect(isObjectEmpty2(true)).toBe(false);
|
|
25
|
+
expect(isObjectEmpty2(Infinity)).toBe(false);
|
|
26
|
+
expect(isObjectEmpty2(-Infinity)).toBe(false);
|
|
27
|
+
expect(isObjectEmpty2(() => {})).toBe(false);
|
|
28
|
+
expect(isObjectEmpty2(Symbol('sym'))).toBe(false);
|
|
29
|
+
expect(isObjectEmpty2(BigInt(123))).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('Returns true for object created with Object.create(null)', () => {
|
|
33
|
+
const obj = Object.create(null);
|
|
34
|
+
expect(isObjectEmpty2(obj)).toBe(true);
|
|
35
|
+
|
|
36
|
+
obj.a = 1;
|
|
37
|
+
|
|
38
|
+
expect(isObjectEmpty2(obj)).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('Nested empty object still counts as non-empty', () => expect(isObjectEmpty2({ nested: {} })).toBe(false));
|
|
42
|
+
|
|
43
|
+
test('Deeply nested empty objects still count as non-empty', () => {
|
|
44
|
+
const obj = { a: { b: { c: {} } } };
|
|
45
|
+
expect(isObjectEmpty2(obj)).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('Objects with inherited properties are considered non-empty', () => {
|
|
49
|
+
function Parent() {
|
|
50
|
+
this.a = 1;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const child = new Parent();
|
|
54
|
+
expect(isObjectEmpty2(child)).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('Frozen empty objects still return true', () => {
|
|
58
|
+
const obj = Object.freeze({});
|
|
59
|
+
expect(isObjectEmpty2(obj)).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('Sealed empty objects still return true', () => {
|
|
63
|
+
const obj = Object.seal({});
|
|
64
|
+
expect(isObjectEmpty2(obj)).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('Proxy objects with empty target return true', () => {
|
|
68
|
+
const target = {};
|
|
69
|
+
const proxy = new Proxy(target, {});
|
|
70
|
+
|
|
71
|
+
expect(isObjectEmpty2(proxy)).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('Proxy objects with keys return false', () => {
|
|
75
|
+
const target = { a: 1 };
|
|
76
|
+
const proxy = new Proxy(target, {});
|
|
77
|
+
|
|
78
|
+
expect(isObjectEmpty2(proxy)).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('Objects with non-enumerable properties are considered empty', () => {
|
|
82
|
+
const obj = {};
|
|
83
|
+
|
|
84
|
+
Object.defineProperty(obj, 'hidden', { value: 123, enumerable: false });
|
|
85
|
+
expect(isObjectEmpty2(obj)).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
});
|