add plain object check and omit helpers
This commit is contained in:
3
src/helpers/is_plain_object.js
Normal file
3
src/helpers/is_plain_object.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export default function (obj) {
|
||||||
|
return (typeof obj === 'object' && !Array.isArray(obj) && obj !== null);
|
||||||
|
}
|
||||||
11
src/helpers/object_omit.js
Normal file
11
src/helpers/object_omit.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import isPlainObject from './is_plain_object';
|
||||||
|
|
||||||
|
export default function (obj, props) {
|
||||||
|
if (!isPlainObject(obj)) return obj;
|
||||||
|
if (!Array.isArray(props)) props = [props];
|
||||||
|
|
||||||
|
const newObj = Object.assign({}, obj);
|
||||||
|
|
||||||
|
props.forEach(prop => delete newObj[prop]);
|
||||||
|
return newObj;
|
||||||
|
}
|
||||||
48
test/src/helpers/is_plain_object.js
Normal file
48
test/src/helpers/is_plain_object.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import expect from 'expect.js';
|
||||||
|
import isPlainObject from '../../../lib/helpers/is_plain_object';
|
||||||
|
|
||||||
|
function validateItems(checks, pass) {
|
||||||
|
checks.forEach(check => {
|
||||||
|
expect(isPlainObject(check)).to.be(pass);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('isPlainObject', function () {
|
||||||
|
describe('non-object primitives', function () {
|
||||||
|
it('return false', function () {
|
||||||
|
const checks = [
|
||||||
|
100,
|
||||||
|
true,
|
||||||
|
'i am a string',
|
||||||
|
function noop() {},
|
||||||
|
null,
|
||||||
|
];
|
||||||
|
|
||||||
|
validateItems(checks, false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('arrays', function () {
|
||||||
|
it('return false', function () {
|
||||||
|
const checks = [
|
||||||
|
[],
|
||||||
|
[1,2,3],
|
||||||
|
['just a string'],
|
||||||
|
];
|
||||||
|
|
||||||
|
validateItems(checks, false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('', function () {
|
||||||
|
it('return true', function () {
|
||||||
|
const checks = [
|
||||||
|
{},
|
||||||
|
{one:1},
|
||||||
|
{object:{with:{array:[]}}},
|
||||||
|
];
|
||||||
|
|
||||||
|
validateItems(checks, true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
37
test/src/helpers/omit.js
Normal file
37
test/src/helpers/omit.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import expect from 'expect.js';
|
||||||
|
import objectOmit from '../../../lib/helpers/object_omit';
|
||||||
|
|
||||||
|
describe('object omit', function () {
|
||||||
|
let obj = {};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
obj = {
|
||||||
|
one: 1,
|
||||||
|
two: 2,
|
||||||
|
three: 3,
|
||||||
|
arr: [1,2,3],
|
||||||
|
check: 'aw yeah',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits a single property', function () {
|
||||||
|
const val = objectOmit(obj, 'one');
|
||||||
|
|
||||||
|
expect(val).to.eql({
|
||||||
|
two: 2,
|
||||||
|
three: 3,
|
||||||
|
arr: [1,2,3],
|
||||||
|
check: 'aw yeah',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits multiple properties', function () {
|
||||||
|
const val = objectOmit(obj, ['three', 'check']);
|
||||||
|
|
||||||
|
expect(val).to.eql({
|
||||||
|
one: 1,
|
||||||
|
two: 2,
|
||||||
|
arr: [1,2,3],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user