-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
52 lines (42 loc) · 2.13 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
require('mocha');
const assert = require('assert');
const omitEmpty = require('./');
describe('omit-empty', function() {
it('should omit empty objects.', function() {
assert.deepEqual(omitEmpty({a: {b: {c: 'foo'}, d: {}}}), {a: {b: {c: 'foo'}}});
});
it('should omit empty objects.', function() {
assert.deepEqual(omitEmpty({a: undefined}), null);
assert.deepEqual(omitEmpty({a: null}), null);
assert.deepEqual(omitEmpty({a: ''}), null);
});
it('should omit nested empty objects.', function() {
assert.deepEqual(omitEmpty({a: {b: undefined}}), null);
assert.deepEqual(omitEmpty({a: {b: null}}), null);
assert.deepEqual(omitEmpty({a: {b: ''}}), null);
});
it('should omit empty primitives.', function() {
assert.deepEqual(omitEmpty({a: {b: {c: 'foo'}, d: ''}}), {a: {b: {c: 'foo'}}});
});
it('should omit empty arrays.', function() {
assert.deepEqual(omitEmpty({a: {b: {c: 'foo', d: []}, foo: []}}), {a: {b: {c: 'foo'}}});
assert.deepEqual(omitEmpty({a: {b: {c: 'foo', d: [undefined]}, foo: [null]}}), {a: {b: {c: 'foo'}}});
assert.deepEqual(omitEmpty({a: {b: {c: 'foo', d: ['']}, foo: [null]}}), {a: {b: {c: 'foo'}}});
assert.deepEqual(omitEmpty({a: {z: [''], b: {c: 'foo', d: ['']}, foo: [null]}}), {a: {b: {c: 'foo'}}});
});
it('should not omit `0`.', function() {
assert.deepEqual(omitEmpty({a: {b: {c: 'foo', d: 0}, foo: []}}), {a: {b: {c: 'foo', d: 0}}});
});
it('should not omit `false`.', function() {
assert.deepEqual(omitEmpty({a: {b: {c: 'foo', d: 0}, foo: [], bar: false}}), {a: {b: {c: 'foo', d: 0}, bar: false}});
});
it('should not omit Dates.', function() {
var today = new Date()
assert.deepEqual(omitEmpty({a: {b: {c: 'foo', d: today}, foo: [], bar: false}}), {a: {b: {c: 'foo', d: today}, bar: false}});
});
it('should handle complex objects.', function() {
var o = {a: {b: {c: 'foo', d: 0, e: {f: {g: {}, h: {i: 'i'}}}}, foo: [['bar', 'baz'], []], bar: [], one: 1, two: 2, three: 0 } };
assert.deepEqual(omitEmpty(o), {a: {b: {c: 'foo', d: 0, e: {f: {h: {i: 'i'}}}}, foo: [['bar', 'baz']], one: 1, two: 2, three: 0}});
});
});