Skip to content

Commit 3aef469

Browse files
committed
Add replacement utils for lodash modules + tests
1 parent fc5621a commit 3aef469

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

src/utils/identity.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function identity(value) {
2+
return value;
3+
}

src/utils/isPlainObject.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function isPlainObject(obj) {
2+
return typeof obj == 'object' && Object.getPrototypeOf(obj) === Object.prototype;
3+
}

src/utils/pick.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function pick(obj, fn) {
2+
return Object.keys(obj).reduce((result, key) => {
3+
if (fn(obj[key])) {
4+
result[key] = obj[key];
5+
}
6+
return result;
7+
}, {});
8+
}

test/utils/identity.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import expect from 'expect';
2+
import identity from '../../src/utils/identity';
3+
4+
describe('Utils', () => {
5+
describe('identity', () => {
6+
it('should return first argument passed to it', () => {
7+
var test = { 'a': 1 };
8+
expect(identity(test, 'test')).toEqual(test);
9+
});
10+
});
11+
});

test/utils/isPlainObject.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import expect from 'expect';
2+
import isPlainObject from '../../src/utils/isPlainObject';
3+
4+
describe('Utils', () => {
5+
describe('isPlainObject', () => {
6+
it('should return true only if plain object', () => {
7+
function Test() {
8+
this.prop = 1;
9+
}
10+
11+
expect(isPlainObject(new Test())).toBe(false);
12+
expect(isPlainObject(new Date())).toBe(false);
13+
expect(isPlainObject([1, 2, 3])).toBe(false);
14+
expect(isPlainObject({ 'x': 1, 'y': 2 })).toBe(true);
15+
});
16+
});
17+
});

test/utils/pick.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import expect from 'expect';
2+
import pick from '../../src/utils/pick';
3+
4+
describe('Utils', () => {
5+
describe('pick', () => {
6+
it('should return object with picked values', () => {
7+
const test = { 'name': 'lily', 'age': 20 };
8+
expect(pick(test, x => typeof x === 'string')).toEqual({ 'name': 'lily' });
9+
});
10+
});
11+
});

0 commit comments

Comments
 (0)