Skip to content

Commit 292ed0d

Browse files
committed
feat: bump version to 0.6.0 and add omit utility
Updates package version to 0.6.0 in jsr.json and package.json. Introduces the `omit` utility function in the library and updates README with usage examples.
1 parent f3d09da commit 292ed0d

6 files changed

Lines changed: 81 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ function checkFruits(fruit: string) {
107107
}
108108
```
109109

110+
### Omit
111+
112+
```typescript
113+
import { omit } from "@sillvva/utils";
114+
115+
const user = { id: 1, name: "Jane", email: "jane@example.com" };
116+
const safe = omit(user, ["email"]);
117+
console.log(safe); // { id: 1, name: "Jane" }
118+
119+
// Strongly typed keys
120+
type User = { id: number; name: string; email: string };
121+
const u: User = { id: 1, name: "Jane", email: "jane@example.com" };
122+
const withoutEmail = omit(u, ["email"] as const);
123+
// withoutEmail is typed as { id: number; name: string }
124+
```
125+
110126
### Slugify
111127

112128
```typescript

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sillvva/utils",
3-
"version": "0.5.7",
3+
"version": "0.6.0",
44
"exports": {
55
".": "./src/entry.ts"
66
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sillvva/utils",
3-
"version": "0.5.7",
3+
"version": "0.6.0",
44
"description": "A module containing general purpose utility functions and types.",
55
"author": "Matt DeKok",
66
"license": "MIT",

src/entry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from "./debounce";
33
export * from "./isDefined";
44
export * from "./isInstanceOfClass";
55
export * from "./isOneOf";
6+
export * from "./omit";
67
export * from "./slugify";
78
export * from "./sorter";
89
export * from "./substrCount";

src/omit.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { omit } from "./omit";
2+
3+
describe("omit", () => {
4+
it("omits specified keys from a simple object", () => {
5+
const obj = { a: 1, b: 2, c: 3 };
6+
const result = omit(obj, ["b"] as const);
7+
expect(result).toEqual({ a: 1, c: 3 });
8+
});
9+
10+
it("returns a new object and does not mutate the source", () => {
11+
const obj = { a: 1, b: 2 } as const;
12+
const result = omit(obj, ["a"] as const);
13+
expect(result).toEqual({ b: 2 });
14+
expect(obj).toEqual({ a: 1, b: 2 });
15+
expect(result).not.toBe(obj);
16+
});
17+
18+
it("works with empty keys list", () => {
19+
const obj = { a: 1, b: 2 };
20+
const result = omit(obj, [] as const);
21+
expect(result).toEqual({ a: 1, b: 2 });
22+
});
23+
24+
it("ignores keys that are not present on the object", () => {
25+
const obj: Record<string, number> = { a: 1 };
26+
const result = omit(obj, ["b"] as const);
27+
expect(result).toEqual({ a: 1 });
28+
});
29+
30+
it("handles symbol keys by preserving them when not omitted", () => {
31+
const sym = Symbol("x");
32+
// Our omit implementation uses Object.entries, which enumerates string keys.
33+
// This test documents current behavior: symbol-keyed properties are preserved
34+
// when not omitted and are also preserved when omitted since they are not enumerated.
35+
const obj = { a: 1, [sym]: 2 } as Record<PropertyKey, unknown> as { a: number } & { [k: symbol]: number };
36+
const result = omit(obj as { a: number }, ["a"] as const);
37+
expect("a" in result).toBe(false);
38+
// The symbol property remains untouched on the original object
39+
expect((obj as any)[sym]).toBe(2);
40+
});
41+
});

src/omit.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Creates a shallow copy of an object without the specified keys.
3+
*
4+
* This function returns a new object composed of all properties from `obj`
5+
* whose keys are not present in `keys`.
6+
*
7+
* @typeParam T - The source object type.
8+
* @typeParam K - A readonly tuple/array of keys from `T` to omit.
9+
* @param obj - The source object.
10+
* @param keys - The keys to exclude from the resulting object.
11+
* @returns A new object with the provided keys omitted.
12+
*
13+
* @example
14+
* const user = { id: 1, name: "Jane", email: "jane@example.com" };
15+
* const safe = omit(user, ["email"]);
16+
* // Result: { id: 1, name: "Jane" }
17+
*/
18+
export function omit<T extends object, K extends readonly (keyof T)[]>(obj: T, keys: K): Omit<T, K[number]> {
19+
const set = new Set<keyof T>(keys);
20+
return Object.fromEntries(Object.entries(obj as Record<string, unknown>).filter(([k]) => !set.has(k as keyof T))) as Omit<T, K[number]>;
21+
}

0 commit comments

Comments
 (0)