Skip to content

Commit f5e6375

Browse files
committed
fix isEmpty bug with bigInt
1 parent 90aa135 commit f5e6375

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/danfojs-base/shared/utils.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,25 @@ export default class Utils {
8686
}
8787

8888
/**
89-
* Checks if a value is empty. Empty means it's either null, undefined or NaN
89+
* Checks if a value is empty. Empty means it's either null, undefined or NaN.
90+
* Empty strings are NOT considered empty.
9091
* @param value The value to check.
91-
* @returns
92+
* @returns boolean indicating if the value is empty
9293
*/
9394
isEmpty<T>(value: T): boolean {
94-
return value === undefined || value === null || (isNaN(value as any) && typeof value !== "string");
95+
if (value === undefined || value === null) {
96+
return true;
97+
}
98+
99+
if (typeof value === 'bigint') {
100+
return false; // BigInt values are never considered empty
101+
}
102+
103+
if (typeof value === 'number') {
104+
return isNaN(value);
105+
}
106+
107+
return false; // All other types (strings, objects, arrays, etc) are not considered empty
95108
}
96109

97110
/**

src/danfojs-node/test/utils.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,49 @@ describe("Utils", function () {
4040
assert.isTrue(utils.isUndefined(arr));
4141
});
4242

43+
describe("isEmpty", function () {
44+
it("should return true for null values", function () {
45+
assert.isTrue(utils.isEmpty(null));
46+
});
47+
48+
it("should return true for undefined values", function () {
49+
assert.isTrue(utils.isEmpty(undefined));
50+
});
51+
52+
it("should return true for NaN values", function () {
53+
assert.isTrue(utils.isEmpty(NaN));
54+
});
55+
56+
it("should return false for strings (including empty strings)", function () {
57+
assert.isFalse(utils.isEmpty(""));
58+
assert.isFalse(utils.isEmpty(" "));
59+
assert.isFalse(utils.isEmpty("hello"));
60+
});
61+
62+
it("should return false for numbers (except NaN)", function () {
63+
assert.isFalse(utils.isEmpty(0));
64+
assert.isFalse(utils.isEmpty(-1));
65+
assert.isFalse(utils.isEmpty(42.5));
66+
});
67+
68+
it("should return false for BigInt values", function () {
69+
assert.isFalse(utils.isEmpty(BigInt(9007199254740991)));
70+
assert.isFalse(utils.isEmpty(BigInt(0)));
71+
});
72+
73+
it("should return false for objects and arrays", function () {
74+
assert.isFalse(utils.isEmpty({}));
75+
assert.isFalse(utils.isEmpty([]));
76+
assert.isFalse(utils.isEmpty({ key: "value" }));
77+
assert.isFalse(utils.isEmpty([1, 2, 3]));
78+
});
79+
80+
it("should return false for boolean values", function () {
81+
assert.isFalse(utils.isEmpty(true));
82+
assert.isFalse(utils.isEmpty(false));
83+
});
84+
});
85+
4386
it("Checks if value is a valid Date object", function () {
4487
let date1 = new Date();
4588
let date2 = "2021-01-01 00:00:00";

0 commit comments

Comments
 (0)