From d14a8191535eb6ce838d98b8043baee6161f69cd Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Fri, 22 Jan 2021 21:30:36 -0800 Subject: [PATCH 1/2] feat: added contain --- README.md | 14 ++++++++++++++ src/core/contain.js | 27 +++++++++++++++++++++++++++ src/index.js | 8 +++++++- test/core/contain.spec.js | 29 +++++++++++++++++++++++++++++ test/index.spec.js | 9 ++++++--- 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/core/contain.js create mode 100644 test/core/contain.spec.js diff --git a/README.md b/README.md index 2c8a5eb..49d6063 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,17 @@ const ref = { k2: null, k1: null }; align(obj, ref); // obj => { k2: 1, k1: 2 } ``` + +### contain(tree: Object, subtree: Object) + +_Example:_ + +```js +const { contain } = require('object-lib'); + +contain({ a: [1, 2], b: 'c' }, { a: [1, 2] }); +// => true + +contain({ a: [1, 2], b: 'c' }, { a: [1] }); +// => false +``` diff --git a/src/core/contain.js b/src/core/contain.js new file mode 100644 index 0000000..0420184 --- /dev/null +++ b/src/core/contain.js @@ -0,0 +1,27 @@ +const contain = (haystack, needle) => { + const needleType = typeof needle; + const haystackType = typeof haystack; + if (needleType !== haystackType) { + return false; + } + if (needleType === 'object') { + const needleIsArray = Array.isArray(needle); + const haystackIsArray = Array.isArray(haystack); + if (needleIsArray !== haystackIsArray) { + return false; + } + // exact match for arrays + if (needleIsArray) { + if (needle.length !== haystack.length) { + return false; + } + return needle.every((e, idx) => contain(haystack[idx], e)); + } + // subset match for object + return Object.keys(needle).every((key) => contain(haystack[key], needle[key])); + } + // default comparison + return haystack === needle; +}; + +module.exports = contain; diff --git a/src/index.js b/src/index.js index bf90601..e168254 100644 --- a/src/index.js +++ b/src/index.js @@ -1 +1,7 @@ -module.exports = (a, b) => a + b; +const align = require('./core/align'); +const contain = require('./core/contain'); + +module.exports = { + align, + contain +}; diff --git a/test/core/contain.spec.js b/test/core/contain.spec.js new file mode 100644 index 0000000..1305ca1 --- /dev/null +++ b/test/core/contain.spec.js @@ -0,0 +1,29 @@ +const expect = require('chai').expect; +const contain = require('../../src/core/contain'); + +describe('Testing contain', () => { + it('Testing String', () => { + expect(contain('value1', 'value1')).to.equal(true); + expect(contain('value1', 'value2')).to.equal(false); + }); + + it('Testing List', () => { + expect(contain([1, 2, 3], [1, 2, 3])).to.equal(true); + expect(contain([{ key: 'value1' }], [{ key: 'value1' }])).to.equal(true); + expect(contain([{ key: 'value1' }], [{ key: 'value2' }])).to.equal(false); + expect(contain([1, 2, 3], [3, 2, 1])).to.equal(false); + expect(contain([1, 2, 3], [1, 2])).to.equal(false); + expect(contain([], [])).to.equal(true); + }); + + it('Testing Object', () => { + expect(contain({}, {})).to.equal(true); + expect(contain({ key: 'value1' }, { key: 'value1' })).to.equal(true); + expect(contain({ key: 'value1' }, { key: 'value2' })).to.equal(false); + }); + + it('Testing Type Mismatch', () => { + expect(contain({}, '')).to.equal(false); + expect(contain({}, [])).to.equal(false); + }); +}); diff --git a/test/index.spec.js b/test/index.spec.js index ef23587..8584847 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -1,8 +1,11 @@ const expect = require('chai').expect; const index = require('../src/index'); -describe('Testing Package', () => { - it('Testing Addition', () => { - expect(index(7, 9)).to.equal(16); +describe('Testing index.js', () => { + it('Testing exported', () => { + expect(Object.keys(index)).to.deep.equal([ + 'align', + 'contain' + ]); }); }); From e9ef721ede9808564da6b92007f4cc6cec441f4d Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Fri, 22 Jan 2021 21:31:30 -0800 Subject: [PATCH 2/2] amend: minor --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 49d6063..00a3981 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,10 @@ Align the ordering of one object recursively to a reference object. _Example:_ - + ```js +const { align } = require('object-lib'); + const obj = { k1: 1, k2: 2 }; const ref = { k2: null, k1: null }; @@ -32,7 +34,7 @@ align(obj, ref); ### contain(tree: Object, subtree: Object) _Example:_ - + ```js const { contain } = require('object-lib');