Skip to content

Commit

Permalink
Merge pull request #7 from simlu/master
Browse files Browse the repository at this point in the history
feat: added contain
  • Loading branch information
simlu authored Jan 23, 2021
2 parents ea2e436 + e9ef721 commit 8bdcbe6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,27 @@
Align the ordering of one object recursively to a reference object.

_Example:_
<!-- eslint-disable no-undef -->
<!-- eslint-disable import/no-unresolved -->
```js
const { align } = require('object-lib');

const obj = { k1: 1, k2: 2 };
const ref = { k2: null, k1: null };

align(obj, ref);
// obj => { k2: 1, k1: 2 }
```

### contain(tree: Object, subtree: Object)

_Example:_
<!-- eslint-disable import/no-unresolved -->
```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
```
27 changes: 27 additions & 0 deletions src/core/contain.js
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
module.exports = (a, b) => a + b;
const align = require('./core/align');
const contain = require('./core/contain');

module.exports = {
align,
contain
};
29 changes: 29 additions & 0 deletions test/core/contain.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
9 changes: 6 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -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'
]);
});
});

0 comments on commit 8bdcbe6

Please sign in to comment.