-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from simlu/master
feat: added contain
- Loading branch information
Showing
5 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
]); | ||
}); | ||
}); |