diff --git a/src/helpers/is_plain_object.js b/src/helpers/is_plain_object.js new file mode 100644 index 0000000..025587d --- /dev/null +++ b/src/helpers/is_plain_object.js @@ -0,0 +1,3 @@ +export default function (obj) { + return (typeof obj === 'object' && !Array.isArray(obj) && obj !== null); +} \ No newline at end of file diff --git a/src/helpers/object_omit.js b/src/helpers/object_omit.js new file mode 100644 index 0000000..9e160bd --- /dev/null +++ b/src/helpers/object_omit.js @@ -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; +} \ No newline at end of file diff --git a/test/src/helpers/is_plain_object.js b/test/src/helpers/is_plain_object.js new file mode 100644 index 0000000..18cfd48 --- /dev/null +++ b/test/src/helpers/is_plain_object.js @@ -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); + }); + }); +}); diff --git a/test/src/helpers/omit.js b/test/src/helpers/omit.js new file mode 100644 index 0000000..cb5b312 --- /dev/null +++ b/test/src/helpers/omit.js @@ -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], + }); + }); +});