-
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.
Add hasKey and omit utility functions to ObjectUtils
- Loading branch information
Showing
7 changed files
with
115 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
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,10 @@ | ||
/** | ||
* Checks if an object has a specific key. | ||
* | ||
* @param obj - The object to check. | ||
* @param key - The key to check for. | ||
* @returns A boolean indicating whether the object has the specified key. | ||
*/ | ||
export function hasKey(obj: object, key: string): boolean { | ||
return Object.hasOwn(obj, key); | ||
} |
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 @@ | ||
/** | ||
* Creates a new object with the specified keys omitted. | ||
* | ||
* @param obj - The object from which to omit keys. | ||
* @param keys - An array of keys to omit from the object. | ||
* @returns A new object with the specified keys omitted. | ||
*/ | ||
export function omit(obj: object, keys: string[]): object { | ||
if (keys.length === 0) { | ||
return obj; | ||
} | ||
|
||
const result: { [key: string]: any } = { ...obj }; | ||
keys.forEach((key) => { | ||
delete result[key]; | ||
}); | ||
return result; | ||
} |
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,22 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { hasKey } from "../../src/object/hasKey"; | ||
|
||
describe("hasKey", () => { | ||
it("should return true if the object has the specified key", () => { | ||
const obj = { name: "John", age: 30 }; | ||
expect(hasKey(obj, "name")).toBe(true); | ||
expect(hasKey(obj, "age")).toBe(true); | ||
}); | ||
|
||
it("should return false if the object does not have the specified key", () => { | ||
const obj = { name: "John", age: 30 }; | ||
expect(hasKey(obj, "email")).toBe(false); | ||
expect(hasKey(obj, "address")).toBe(false); | ||
}); | ||
|
||
it("should return false if the object is empty", () => { | ||
const obj = {}; | ||
expect(hasKey(obj, "name")).toBe(false); | ||
expect(hasKey(obj, "age")).toBe(false); | ||
}); | ||
}); |
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,32 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { omit } from "../../src/object/omit"; | ||
|
||
describe("omit", () => { | ||
it("should return a new object with specified keys omitted", () => { | ||
const obj = { a: 1, b: 2, c: 3, d: 4 }; | ||
const keys = ["b", "d"]; | ||
const result = omit(obj, keys); | ||
expect(result).toEqual({ a: 1, c: 3 }); | ||
}); | ||
|
||
it("should not modify the original object", () => { | ||
const obj = { a: 1, b: 2, c: 3, d: 4 }; | ||
const keys = ["b", "d"]; | ||
omit(obj, keys); | ||
expect(obj).toEqual({ a: 1, b: 2, c: 3, d: 4 }); | ||
}); | ||
|
||
it("should return the same object when no keys are specified", () => { | ||
const obj = { a: 1, b: 2, c: 3, d: 4 }; | ||
const keys: string[] = []; | ||
const result = omit(obj, keys); | ||
expect(result).toBe(obj); | ||
}); | ||
|
||
it("should return an empty object when all keys are specified", () => { | ||
const obj = { a: 1, b: 2, c: 3, d: 4 }; | ||
const keys = ["a", "b", "c", "d"]; | ||
const result = omit(obj, keys); | ||
expect(result).toEqual({}); | ||
}); | ||
}); |