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
2 changes: 1 addition & 1 deletion benchmarks/bundle-size/omit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ describe('omit bundle size', () => {

it('es-toolkit/compat', async () => {
const bundleSize = await getBundleSize('es-toolkit/compat', 'omit');
expect(bundleSize).toMatchInlineSnapshot(`8633`);
expect(bundleSize).toMatchInlineSnapshot(`8640`);
});
});
8 changes: 8 additions & 0 deletions src/compat/object/omit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
expect(actual).toEqual({ a: { b: 2 } });
});

it('should treat a key array as a single deep path', () => {
const object = { a: { b: 1, c: 2 }, d: 3 };
// @ts-expect-error - path is a string
expect(omit(object, [['a', 'b']])).toEqual({ a: { c: 2 }, d: 3 });
// @ts-expect-error - path is a string
expect(omit(object, [['a', 'b'], 'd'])).toEqual({ a: { c: 2 } });
});

it('should omit a key over a path', () => {
const object = { 'a.b': 1, a: { b: 2 } };

Expand All @@ -58,7 +66,7 @@
});

// Manipulating prototypes is an anti-pattern
it.skip('should work with a primitive `object`', () => {

Check warning on line 69 in src/compat/object/omit.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Disabled test - if you want to skip a test temporarily, use .todo() instead
stringProto.a = 1;
stringProto.b = 2;

Expand Down
19 changes: 10 additions & 9 deletions src/compat/object/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,19 @@ export function omit<T extends object>(obj: T | null | undefined, ...keysArr: Ar
const result = cloneInOmit(obj, keysArr);

for (let i = 0; i < keysArr.length; i++) {
let keys = keysArr[i];
const keys = keysArr[i];

switch (typeof keys) {
case 'object': {
if (!Array.isArray(keys)) {
keys = Array.from(keys as PropertyKey[]);
}

for (let j = 0; j < keys.length; j++) {
const key = keys[j];

unset(result, key);
if (Array.isArray(keys)) {
// A key array is a single deep path (e.g. `['a', 'b']` targets `a.b`), matching lodash.
unset(result, keys);
} else {
const arrayLikeKeys = Array.from(keys as PropertyKey[]);

for (let j = 0; j < arrayLikeKeys.length; j++) {
unset(result, arrayLikeKeys[j]);
}
}

break;
Expand Down
Loading