|
1 | 1 | import test from 'ava';
|
2 |
| -import filterObject from './index.js'; |
| 2 | +import {includeKeys, excludeKeys} from './index.js'; |
3 | 3 |
|
4 |
| -test('function predicate returns a boolean', t => { |
5 |
| - t.is(Object.keys(filterObject({foo: true, bar: false}, () => true)).length, 2); |
6 |
| - t.is(Object.keys(filterObject({foo: true, bar: false}, () => false)).length, 0); |
| 4 | +test('includeKeys: function predicate returns a boolean', t => { |
| 5 | + t.is(Object.keys(includeKeys({foo: true, bar: false}, () => true)).length, 2); |
| 6 | + t.is(Object.keys(includeKeys({foo: true, bar: false}, () => false)).length, 0); |
7 | 7 | });
|
8 | 8 |
|
9 |
| -test('function predicate passes the key as argument', t => { |
10 |
| - t.true(filterObject({foo: true}, key => key === 'foo').foo); |
| 9 | +test('includeKeys: function predicate passes the key as argument', t => { |
| 10 | + t.true(includeKeys({foo: true}, key => key === 'foo').foo); |
11 | 11 | });
|
12 | 12 |
|
13 |
| -test('function predicate passes the value as argument', t => { |
14 |
| - t.is(filterObject({foo: 'test'}, (key, value) => value === 'test').foo, 'test'); |
| 13 | +test('includeKeys: function predicate passes the value as argument', t => { |
| 14 | + t.is(includeKeys({foo: 'test'}, (key, value) => value === 'test').foo, 'test'); |
15 | 15 | });
|
16 | 16 |
|
17 |
| -test('function predicate passes the object as argument', t => { |
18 |
| - t.true(filterObject({foo: true}, (key, value, object) => object.foo).foo); |
| 17 | +test('includeKeys: function predicate passes the object as argument', t => { |
| 18 | + t.true(includeKeys({foo: true}, (key, value, object) => object.foo).foo); |
19 | 19 | });
|
20 | 20 |
|
21 |
| -test('array predicate', t => { |
22 |
| - t.deepEqual(Object.keys(filterObject({foo: true, bar: false}, ['foo'])), ['foo']); |
| 21 | +test('includeKeys: array predicate', t => { |
| 22 | + t.deepEqual(Object.keys(includeKeys({foo: true, bar: false}, ['foo'])), ['foo']); |
23 | 23 | });
|
24 | 24 |
|
25 |
| -test('symbol properties are omitted', t => { |
| 25 | +test('includeKeys: symbol properties are omitted', t => { |
26 | 26 | const symbol = Symbol('test');
|
27 | 27 | const input = {[symbol]: true};
|
28 |
| - t.is(filterObject(input, () => true)[symbol], undefined); |
| 28 | + t.is(includeKeys(input, () => true)[symbol], undefined); |
29 | 29 | });
|
30 | 30 |
|
31 |
| -test('non-enumerable properties are omitted', t => { |
| 31 | +test('includeKeys: non-enumerable properties are omitted', t => { |
32 | 32 | const input = Object.defineProperty({}, 'test', {value: true, enumerable: false});
|
33 |
| - t.is(filterObject(input, () => true).test, undefined); |
| 33 | + t.is(includeKeys(input, () => true).test, undefined); |
34 | 34 | });
|
35 | 35 |
|
36 |
| -test('inherited properties are omitted', t => { |
| 36 | +test('includeKeys: inherited properties are omitted', t => { |
37 | 37 | const Parent = class {
|
38 | 38 | test() {}
|
39 | 39 | };
|
40 | 40 | const Child = class extends Parent {};
|
41 | 41 | const input = new Child();
|
42 |
| - t.is(filterObject(input, () => true).test, undefined); |
| 42 | + t.is(includeKeys(input, () => true).test, undefined); |
43 | 43 | });
|
44 | 44 |
|
45 |
| -test('__proto__ keys', t => { |
| 45 | +test('includeKeys: __proto__ keys', t => { |
46 | 46 | const input = {__proto__: {foo: true}};
|
47 |
| - t.deepEqual(filterObject(input, () => true), input); |
| 47 | + t.deepEqual(includeKeys(input, () => true), input); |
| 48 | +}); |
| 49 | + |
| 50 | +test('excludeKeys: function predicate returns a boolean', t => { |
| 51 | + t.is(Object.keys(excludeKeys({foo: true, bar: false}, () => true)).length, 0); |
| 52 | + t.is(Object.keys(excludeKeys({foo: true, bar: false}, () => false)).length, 2); |
| 53 | +}); |
| 54 | + |
| 55 | +test('excludeKeys: function predicate passes the key as argument', t => { |
| 56 | + t.true(excludeKeys({foo: true}, key => key !== 'foo').foo); |
| 57 | +}); |
| 58 | + |
| 59 | +test('excludeKeys: function predicate passes the value as argument', t => { |
| 60 | + t.is(excludeKeys({foo: 'test'}, (key, value) => value !== 'test').foo, 'test'); |
| 61 | +}); |
| 62 | + |
| 63 | +test('excludeKeys: function predicate passes the object as argument', t => { |
| 64 | + t.true(excludeKeys({foo: true}, (key, value, object) => !object.foo).foo); |
| 65 | +}); |
| 66 | + |
| 67 | +test('excludeKeys: array predicate', t => { |
| 68 | + t.deepEqual(Object.keys(excludeKeys({foo: true, bar: false}, ['bar'])), ['foo']); |
| 69 | +}); |
| 70 | + |
| 71 | +test('excludeKeys: symbol properties are omitted', t => { |
| 72 | + const symbol = Symbol('test'); |
| 73 | + const input = {[symbol]: true}; |
| 74 | + t.is(excludeKeys(input, () => false)[symbol], undefined); |
| 75 | +}); |
| 76 | + |
| 77 | +test('excludeKeys: non-enumerable properties are omitted', t => { |
| 78 | + const input = Object.defineProperty({}, 'test', {value: true, enumerable: false}); |
| 79 | + t.is(excludeKeys(input, () => false).test, undefined); |
| 80 | +}); |
| 81 | + |
| 82 | +test('excludeKeys: inherited properties are omitted', t => { |
| 83 | + const Parent = class { |
| 84 | + test() {} |
| 85 | + }; |
| 86 | + const Child = class extends Parent {}; |
| 87 | + const input = new Child(); |
| 88 | + t.is(excludeKeys(input, () => false).test, undefined); |
| 89 | +}); |
| 90 | + |
| 91 | +test('excludeKeys: __proto__ keys', t => { |
| 92 | + const input = {__proto__: {foo: true}}; |
| 93 | + t.deepEqual(excludeKeys(input, () => false), input); |
48 | 94 | });
|
0 commit comments