Skip to content

Commit

Permalink
Fix contain(). Closes #342
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Oct 15, 2019
1 parent ec88426 commit c2d748a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/contain.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ internals.object = function (ref, values, options) {
}

if (set.size) {
return !!options.part;
return options.part ? set.size < targets.length : false;
}

return true;
Expand Down Expand Up @@ -254,7 +254,12 @@ internals.string = function (ref, values, options) {
return false;
}

let any = false;
for (const match of map.values()) {
if (match.hits) {
any = true;
}

if (match.hits === match.allowed) {
continue;
}
Expand All @@ -272,7 +277,7 @@ internals.string = function (ref, values, options) {
}
}

return true;
return !!any;
};


Expand Down
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,9 @@ describe('contain()', () => {
expect(Hoek.contain('ab', ['a', 'b', 'c'])).to.be.false();
expect(Hoek.contain('ab', ['a', 'b', 'c'], { only: true })).to.be.false();
expect(Hoek.contain('ab', ['a', 'b', 'c'], { only: true, once: true })).to.be.false();

expect(Hoek.contain('ab', ['c'], { part: true })).to.be.false();
expect(Hoek.contain('ab', ['b'], { part: true })).to.be.true();
});

it('tests arrays', () => {
Expand Down Expand Up @@ -2182,6 +2185,9 @@ describe('contain()', () => {
expect(Hoek.contain(['a', 'b'], ['a', 'b', 'c'])).to.be.false();
expect(Hoek.contain(['a', 'b'], ['a', 'b', 'c'], { only: true })).to.be.false();
expect(Hoek.contain(['a', 'b'], ['a', 'b', 'c'], { only: true, once: true })).to.be.false();

expect(Hoek.contain(['a', 'b'], ['c'], { part: true })).to.be.false();
expect(Hoek.contain(['a', 'b'], ['b'], { part: true })).to.be.true();
});

it('tests objects', () => {
Expand Down Expand Up @@ -2226,6 +2232,9 @@ describe('contain()', () => {
expect(Hoek.contain({ a: 'foo', b: 'bar' }, { a: 'foo', b: 'bar', c: 'x' })).to.be.false();
expect(Hoek.contain({ a: 'foo', b: 'bar' }, { a: 'foo', b: 'bar', c: 'x' }, { only: true })).to.be.false();

expect(Hoek.contain({ a: 1, b: 2 }, ['c'], { part: true })).to.be.false();
expect(Hoek.contain({ a: 1, b: 2 }, ['b'], { part: true })).to.be.true();

// Getter check

{
Expand Down Expand Up @@ -2314,6 +2323,21 @@ describe('contain()', () => {
expect(Hoek.contain([sym], Symbol())).to.be.false();
expect(Hoek.contain({ [sym]: 1 }, Symbol())).to.be.false();
});

it('compares error keys', () => {

const error = new Error('test');
expect(Hoek.contain(error, { x: 1 })).to.be.false();
expect(Hoek.contain(error, { x: 1 }, { part: true })).to.be.false();

error.x = 1;

expect(Hoek.contain(error, { x: 1 })).to.be.true();
expect(Hoek.contain(error, { x: 1 }, { part: true })).to.be.true();

expect(Hoek.contain(error, { x: 1, y: 2 })).to.be.false();
expect(Hoek.contain(error, { x: 1, y: 2 }, { part: true })).to.be.true();
});
});

describe('flatten()', () => {
Expand Down

0 comments on commit c2d748a

Please sign in to comment.