Skip to content

Commit 4c5a3e0

Browse files
committed
Extract util functions
1 parent bcb3b22 commit 4c5a3e0

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const isObject = o => o != null && typeof o === 'object';
2-
const isEmpty = o => Object.keys(o).length === 0;
1+
import { isEmpty, isObject } from './utils';
32

43
const diff = (lhs, rhs) => {
54
if (lhs === rhs) return {}; // equal return no diff

src/utils/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const isObject = o => o != null && typeof o === 'object';
2+
export const isEmpty = o => Object.keys(o).length === 0;

src/utils/index.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect } from 'chai';
2+
import forEach from 'mocha-each';
3+
4+
import { isEmpty, isObject } from './';
5+
6+
describe('utils', () => {
7+
8+
describe('.isEmpty', () => {
9+
describe('returns true', () => {
10+
it('when given an empty object', () => {
11+
expect(isEmpty({})).to.be.true;
12+
});
13+
14+
it('when given an empty array', () => {
15+
expect(isEmpty([])).to.be.true;
16+
});
17+
});
18+
19+
describe('returns false', () => {
20+
it('when given an empty object', () => {
21+
expect(isEmpty({ a: 1 })).to.be.false;
22+
});
23+
24+
it('when given an empty array', () => {
25+
expect(isEmpty([1])).to.be.false;
26+
});
27+
});
28+
});
29+
30+
describe('.isObject', () => {
31+
it('returns true when value is an object', () => {
32+
expect(isObject({})).to.be.true;
33+
});
34+
35+
it('returns true when value is an array', () => {
36+
expect(isObject([])).to.be.true;
37+
});
38+
39+
forEach([
40+
['int', 1],
41+
['string', 'a'],
42+
['boolean', true],
43+
['null', null],
44+
['undefined', undefined],
45+
['function', () => ({})],
46+
]).it('returns false when value is of type: %s', (type, value) => {
47+
expect(isObject(value)).to.be.false;
48+
});
49+
});
50+
});

0 commit comments

Comments
 (0)