Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty ref bug in contain when only is true #324

Merged
merged 5 commits into from Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/contain.js
Expand Up @@ -59,6 +59,16 @@ module.exports = function (ref, values, options = {}) { // options: { dee
}

if (typeof ref === 'string') {
if (ref === '') {
if (values.join('') === '') {
if (values.length === 1 || !options.once) {
return true;
}
}

return false;
}

let pattern = '(';
for (let i = 0; i < values.length; ++i) {
const value = values[i];
Expand All @@ -77,6 +87,10 @@ module.exports = function (ref, values, options = {}) { // options: { dee
misses = !!leftovers;
}
else if (Array.isArray(ref)) {
if (!ref.length) {
return false;
}

const onlyOnce = !!(options.only && options.once);
if (onlyOnce && ref.length !== values.length) {
return false;
Expand All @@ -100,6 +114,10 @@ module.exports = function (ref, values, options = {}) { // options: { dee
}
else {
const keys = Utils.keys(ref, options);
if (!keys.length) {
return false;
}

for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const pos = values.indexOf(key);
Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Expand Up @@ -2091,6 +2091,16 @@ describe('contain()', () => {
expect(Hoek.contain('abb', 'b', { once: true })).to.be.false();
expect(Hoek.contain('abc', ['a', 'd'])).to.be.false();
expect(Hoek.contain('abc', ['ab', 'bc'])).to.be.false(); // Overlapping values not supported

expect(Hoek.contain('', 'a')).to.be.false();
expect(Hoek.contain('', 'a', { only: true })).to.be.false();

expect(Hoek.contain('', '')).to.be.true();
expect(Hoek.contain('', ''), { only: true }).to.be.true();
expect(Hoek.contain('', ''), { once: true }).to.be.true();
expect(Hoek.contain('', ['', ''])).to.be.true();
expect(Hoek.contain('', ['', ''], { only: true })).to.be.true();
expect(Hoek.contain('', ['', ''], { once: true })).to.be.false();
});

it('tests arrays', () => {
Expand Down Expand Up @@ -2121,6 +2131,9 @@ describe('contain()', () => {
expect(Hoek.contain([0, 2, 3], [1, 4], { part: true })).to.be.false();
expect(Hoek.contain([1, 2, 1], [1, 2, 2], { only: true, once: true })).to.be.false();
expect(Hoek.contain([1, 2, 1], [1, 2], { only: true, once: true })).to.be.false();

expect(Hoek.contain([], 1)).to.be.false();
expect(Hoek.contain([], 1, { only: true })).to.be.false();
});

it('tests objects', () => {
Expand Down Expand Up @@ -2157,6 +2170,9 @@ describe('contain()', () => {
expect(Hoek.contain({ a: { b: { c: 1, d: 2 } } }, { a: { b: { c: 1 } } }, { deep: true, part: false })).to.be.false();
expect(Hoek.contain({ a: [1, 2, 3] }, { a: [4, 5, 6] }, { deep: true, part: true })).to.be.false();

expect(Hoek.contain({}, 'a')).to.be.false();
expect(Hoek.contain({}, 'a', { only: true })).to.be.false();

// Getter check
{
const Foo = function (bar) {
Expand Down