Skip to content

Commit c2d748a

Browse files
committed
Fix contain(). Closes #342
1 parent ec88426 commit c2d748a

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/contain.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ internals.object = function (ref, values, options) {
192192
}
193193

194194
if (set.size) {
195-
return !!options.part;
195+
return options.part ? set.size < targets.length : false;
196196
}
197197

198198
return true;
@@ -254,7 +254,12 @@ internals.string = function (ref, values, options) {
254254
return false;
255255
}
256256

257+
let any = false;
257258
for (const match of map.values()) {
259+
if (match.hits) {
260+
any = true;
261+
}
262+
258263
if (match.hits === match.allowed) {
259264
continue;
260265
}
@@ -272,7 +277,7 @@ internals.string = function (ref, values, options) {
272277
}
273278
}
274279

275-
return true;
280+
return !!any;
276281
};
277282

278283

test/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,9 @@ describe('contain()', () => {
21362136
expect(Hoek.contain('ab', ['a', 'b', 'c'])).to.be.false();
21372137
expect(Hoek.contain('ab', ['a', 'b', 'c'], { only: true })).to.be.false();
21382138
expect(Hoek.contain('ab', ['a', 'b', 'c'], { only: true, once: true })).to.be.false();
2139+
2140+
expect(Hoek.contain('ab', ['c'], { part: true })).to.be.false();
2141+
expect(Hoek.contain('ab', ['b'], { part: true })).to.be.true();
21392142
});
21402143

21412144
it('tests arrays', () => {
@@ -2182,6 +2185,9 @@ describe('contain()', () => {
21822185
expect(Hoek.contain(['a', 'b'], ['a', 'b', 'c'])).to.be.false();
21832186
expect(Hoek.contain(['a', 'b'], ['a', 'b', 'c'], { only: true })).to.be.false();
21842187
expect(Hoek.contain(['a', 'b'], ['a', 'b', 'c'], { only: true, once: true })).to.be.false();
2188+
2189+
expect(Hoek.contain(['a', 'b'], ['c'], { part: true })).to.be.false();
2190+
expect(Hoek.contain(['a', 'b'], ['b'], { part: true })).to.be.true();
21852191
});
21862192

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

2235+
expect(Hoek.contain({ a: 1, b: 2 }, ['c'], { part: true })).to.be.false();
2236+
expect(Hoek.contain({ a: 1, b: 2 }, ['b'], { part: true })).to.be.true();
2237+
22292238
// Getter check
22302239

22312240
{
@@ -2314,6 +2323,21 @@ describe('contain()', () => {
23142323
expect(Hoek.contain([sym], Symbol())).to.be.false();
23152324
expect(Hoek.contain({ [sym]: 1 }, Symbol())).to.be.false();
23162325
});
2326+
2327+
it('compares error keys', () => {
2328+
2329+
const error = new Error('test');
2330+
expect(Hoek.contain(error, { x: 1 })).to.be.false();
2331+
expect(Hoek.contain(error, { x: 1 }, { part: true })).to.be.false();
2332+
2333+
error.x = 1;
2334+
2335+
expect(Hoek.contain(error, { x: 1 })).to.be.true();
2336+
expect(Hoek.contain(error, { x: 1 }, { part: true })).to.be.true();
2337+
2338+
expect(Hoek.contain(error, { x: 1, y: 2 })).to.be.false();
2339+
expect(Hoek.contain(error, { x: 1, y: 2 }, { part: true })).to.be.true();
2340+
});
23172341
});
23182342

23192343
describe('flatten()', () => {

0 commit comments

Comments
 (0)