Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/danfojs-base/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,25 @@ export default class Utils {
}

/**
* Checks if a value is empty. Empty means it's either null, undefined or NaN
* Checks if a value is empty. Empty means it's either null, undefined or NaN.
* Empty strings are NOT considered empty.
* @param value The value to check.
* @returns
* @returns boolean indicating if the value is empty
*/
isEmpty<T>(value: T): boolean {
return value === undefined || value === null || (isNaN(value as any) && typeof value !== "string");
if (value === undefined || value === null) {
return true;
}

if (typeof value === 'bigint') {
return false; // BigInt values are never considered empty
}

if (typeof value === 'number') {
return isNaN(value);
}

return false; // All other types (strings, objects, arrays, etc) are not considered empty
}

/**
Expand Down
43 changes: 43 additions & 0 deletions src/danfojs-node/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,49 @@ describe("Utils", function () {
assert.isTrue(utils.isUndefined(arr));
});

describe("isEmpty", function () {
it("should return true for null values", function () {
assert.isTrue(utils.isEmpty(null));
});

it("should return true for undefined values", function () {
assert.isTrue(utils.isEmpty(undefined));
});

it("should return true for NaN values", function () {
assert.isTrue(utils.isEmpty(NaN));
});

it("should return false for strings (including empty strings)", function () {
assert.isFalse(utils.isEmpty(""));
assert.isFalse(utils.isEmpty(" "));
assert.isFalse(utils.isEmpty("hello"));
});

it("should return false for numbers (except NaN)", function () {
assert.isFalse(utils.isEmpty(0));
assert.isFalse(utils.isEmpty(-1));
assert.isFalse(utils.isEmpty(42.5));
});

it("should return false for BigInt values", function () {
assert.isFalse(utils.isEmpty(BigInt(9007199254740991)));
assert.isFalse(utils.isEmpty(BigInt(0)));
});

it("should return false for objects and arrays", function () {
assert.isFalse(utils.isEmpty({}));
assert.isFalse(utils.isEmpty([]));
assert.isFalse(utils.isEmpty({ key: "value" }));
assert.isFalse(utils.isEmpty([1, 2, 3]));
});

it("should return false for boolean values", function () {
assert.isFalse(utils.isEmpty(true));
assert.isFalse(utils.isEmpty(false));
});
});

it("Checks if value is a valid Date object", function () {
let date1 = new Date();
let date2 = "2021-01-01 00:00:00";
Expand Down