add plain object check and omit helpers

This commit is contained in:
2017-02-28 14:45:43 -07:00
parent 5e0cc440ff
commit 64c1c90337
4 changed files with 99 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
export default function (obj) {
return (typeof obj === 'object' && !Array.isArray(obj) && obj !== null);
}

View 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;
}

View 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
View 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],
});
});
});