Skip to content

Commit

Permalink
Add hasKey and omit utility functions to ObjectUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
0pilatos0 committed Mar 4, 2024
1 parent 3640a7f commit 2558c21
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
26 changes: 26 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ Here are some of the utility functions and classes available in this library:
- [deepClone](#deepclone)
- [pick](#pick)
- [keys](#keys)
- [hasKey](#haskey)
- [values](#values)
- [merge](#merge)
- [omit](#omit)

- [StringUtils](#stringutils)
- [capitalize](#capitalize)
Expand Down Expand Up @@ -760,6 +762,18 @@ the following utility functions are available in the `ObjectUtils` class, they c
```


### hasKey
```typescript
/**
* 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.
*/
```


### values
```typescript
/**
Expand All @@ -785,6 +799,18 @@ the following utility functions are available in the `ObjectUtils` class, they c
```


### omit
```typescript
/**
* 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.
*/
```



## StringUtils

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typelegend",
"version": "0.1.12",
"version": "0.1.13",
"module": "dist/index.mjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
10 changes: 10 additions & 0 deletions src/object/hasKey.ts
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);
}
6 changes: 6 additions & 0 deletions src/object/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
import { deepClone } from "./deepClone";
import { pick } from "./pick";
import { keys } from "./keys";
import { hasKey } from "./hasKey";
import { values } from "./values";
import { merge } from "./merge";
import { omit } from "./omit";

export class ObjectUtils {
static deepClone: typeof deepClone = deepClone;
static pick: typeof pick = pick;
static keys: typeof keys = keys;
static hasKey: typeof hasKey = hasKey;
static values: typeof values = values;
static merge: typeof merge = merge;
static omit: typeof omit = omit;
}

export { deepClone } from "./deepClone";
export { pick } from "./pick";
export { keys } from "./keys";
export { hasKey } from "./hasKey";
export { values } from "./values";
export { merge } from "./merge";
export { omit } from "./omit";

18 changes: 18 additions & 0 deletions src/object/omit.ts
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;
}
22 changes: 22 additions & 0 deletions test/object/hasKey.test.ts
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);
});
});
32 changes: 32 additions & 0 deletions test/object/omit.test.ts
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({});
});
});

0 comments on commit 2558c21

Please sign in to comment.