|
3 | 3 | import assert from 'node:assert/strict'; |
4 | 4 | import { describe, it } from 'node:test'; |
5 | 5 |
|
6 | | -import { lazy, isPlainObject, extractPrimitives, deepMerge } from '../misc.mjs'; |
| 6 | +import { lazy, isPlainObject, omitKeys, deepMerge } from '../misc.mjs'; |
7 | 7 |
|
8 | 8 | describe('lazy', () => { |
9 | 9 | it('should call the function only once and cache the result', () => { |
@@ -38,52 +38,31 @@ describe('isPlainObject', () => { |
38 | 38 | }); |
39 | 39 | }); |
40 | 40 |
|
41 | | -describe('extractPrimitives', () => { |
42 | | - it('should keep string, number, boolean, and null values', () => { |
43 | | - const obj = { a: 'hello', b: 42, c: true, d: null }; |
44 | | - assert.deepStrictEqual(extractPrimitives(obj), { |
45 | | - a: 'hello', |
46 | | - b: 42, |
47 | | - c: true, |
48 | | - d: null, |
49 | | - }); |
50 | | - }); |
51 | | - |
52 | | - it('should remove object and function values', () => { |
53 | | - const obj = { |
54 | | - name: 'test', |
55 | | - nested: { foo: 'bar' }, |
56 | | - fn: () => {}, |
57 | | - count: 5, |
58 | | - }; |
59 | | - const result = extractPrimitives(obj); |
60 | | - assert.deepStrictEqual(result, { name: 'test', count: 5 }); |
| 41 | +describe('omitKeys', () => { |
| 42 | + it('should return all properties when no keys are excluded', () => { |
| 43 | + const obj = { a: 'hello', b: 42, c: true }; |
| 44 | + assert.deepStrictEqual(omitKeys(obj), { a: 'hello', b: 42, c: true }); |
61 | 45 | }); |
62 | 46 |
|
63 | | - it('should keep arrays of primitives', () => { |
64 | | - const obj = { tags: ['a', 'b'], name: 'test' }; |
65 | | - assert.deepStrictEqual(extractPrimitives(obj), { |
66 | | - tags: ['a', 'b'], |
67 | | - name: 'test', |
68 | | - }); |
| 47 | + it('should omit specified keys', () => { |
| 48 | + const obj = { a: 1, b: 2, c: 3 }; |
| 49 | + assert.deepStrictEqual(omitKeys(obj, ['a', 'c']), { b: 2 }); |
69 | 50 | }); |
70 | 51 |
|
71 | | - it('should remove arrays containing objects', () => { |
72 | | - const obj = { items: [{ id: 1 }], name: 'test' }; |
73 | | - assert.deepStrictEqual(extractPrimitives(obj), { name: 'test' }); |
| 52 | + it('should ignore keys that do not exist', () => { |
| 53 | + const obj = { a: 1, b: 2 }; |
| 54 | + assert.deepStrictEqual(omitKeys(obj, ['z']), { a: 1, b: 2 }); |
74 | 55 | }); |
75 | 56 |
|
76 | | - it('should keep undefined values', () => { |
77 | | - const obj = { a: undefined, b: 'yes' }; |
78 | | - const result = extractPrimitives(obj); |
79 | | - assert.strictEqual('a' in result, true); |
80 | | - assert.strictEqual(result.a, undefined); |
81 | | - assert.strictEqual(result.b, 'yes'); |
| 57 | + it('should return an empty object when all keys are excluded', () => { |
| 58 | + const obj = { a: 1, b: 2 }; |
| 59 | + assert.deepStrictEqual(omitKeys(obj, ['a', 'b']), {}); |
82 | 60 | }); |
83 | 61 |
|
84 | | - it('should return an empty object when all values are non-primitive', () => { |
85 | | - const obj = { a: {}, b: [{ x: 1 }], c: () => {} }; |
86 | | - assert.deepStrictEqual(extractPrimitives(obj), {}); |
| 62 | + it('should preserve any value type', () => { |
| 63 | + const fn = () => {}; |
| 64 | + const obj = { a: fn, b: new Map(), c: null, d: [1, 2] }; |
| 65 | + assert.deepStrictEqual(omitKeys(obj, ['b']), { a: fn, c: null, d: [1, 2] }); |
87 | 66 | }); |
88 | 67 | }); |
89 | 68 |
|
|
0 commit comments