Skip to content

Commit

Permalink
Merge pull request #12 from blackflux/dev
Browse files Browse the repository at this point in the history
[Gally]: master <- dev
  • Loading branch information
simlu authored Jan 23, 2021
2 parents 38ce429 + 5208adf commit ad3f2ef
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ align(obj, ref);
// obj => { k2: 1, k1: 2 }
```

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

Check if `subtree` is contained in `tree` recursively.

Expand All @@ -50,12 +50,12 @@ All other types are contained if they match exactly (`===`).
_Example:_
<!-- eslint-disable import/no-unresolved -->
```js
const { contain } = require('object-lib');
const { contains } = require('object-lib');

contain({ a: [1, 2], b: 'c' }, { a: [1, 2] });
contains({ a: [1, 2], b: 'c' }, { a: [1, 2] });
// => true

contain({ a: [1, 2], b: 'c' }, { a: [1] });
contains({ a: [1, 2], b: 'c' }, { a: [1] });
// => false
```

Expand Down
8 changes: 4 additions & 4 deletions src/core/contain.js → src/core/contains.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const contain = (haystack, needle) => {
const contains = (haystack, needle) => {
const needleType = typeof needle;
const haystackType = typeof haystack;
if (needleType !== haystackType) {
Expand All @@ -15,13 +15,13 @@ const contain = (haystack, needle) => {
if (needle.length !== haystack.length) {
return false;
}
return needle.every((e, idx) => contain(haystack[idx], e));
return needle.every((e, idx) => contains(haystack[idx], e));
}
// subset match for object
return Object.keys(needle).every((key) => contain(haystack[key], needle[key]));
return Object.keys(needle).every((key) => contains(haystack[key], needle[key]));
}
// default comparison
return haystack === needle;
};

module.exports = contain;
module.exports = contains;
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const align = require('./core/align');
const contain = require('./core/contain');
const contains = require('./core/contains');
const Merge = require('./core/merge');

module.exports = {
align,
contain,
contains,
Merge
};
29 changes: 0 additions & 29 deletions test/core/contain.spec.js

This file was deleted.

29 changes: 29 additions & 0 deletions test/core/contains.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const expect = require('chai').expect;
const contains = require('../../src/core/contains');

describe('Testing contains', () => {
it('Testing String', () => {
expect(contains('value1', 'value1')).to.equal(true);
expect(contains('value1', 'value2')).to.equal(false);
});

it('Testing List', () => {
expect(contains([1, 2, 3], [1, 2, 3])).to.equal(true);
expect(contains([{ key: 'value1' }], [{ key: 'value1' }])).to.equal(true);
expect(contains([{ key: 'value1' }], [{ key: 'value2' }])).to.equal(false);
expect(contains([1, 2, 3], [3, 2, 1])).to.equal(false);
expect(contains([1, 2, 3], [1, 2])).to.equal(false);
expect(contains([], [])).to.equal(true);
});

it('Testing Object', () => {
expect(contains({}, {})).to.equal(true);
expect(contains({ key: 'value1' }, { key: 'value1' })).to.equal(true);
expect(contains({ key: 'value1' }, { key: 'value2' })).to.equal(false);
});

it('Testing Type Mismatch', () => {
expect(contains({}, '')).to.equal(false);
expect(contains({}, [])).to.equal(false);
});
});
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('Testing index.js', () => {
it('Testing exported', () => {
expect(Object.keys(index)).to.deep.equal([
'align',
'contain',
'contains',
'Merge'
]);
});
Expand Down

0 comments on commit ad3f2ef

Please sign in to comment.