Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 666e177

Browse files
committed
[IMP] Core: add contains() on Modifiers
1 parent 20ce823 commit 666e177

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

packages/core/src/Modifiers.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,20 @@ export class Modifiers extends EventMixin {
247247
toggle(modifier: Modifier | Constructor<Modifier>): void {
248248
this.remove(modifier) || this.append(modifier);
249249
}
250+
/**
251+
* Check that all `otherModifiers` ar contained within this ones.
252+
*
253+
* @param otherModifiers
254+
*/
255+
contains(otherModifiers: Modifiers): boolean {
256+
for (const otherModifier of otherModifiers.all()) {
257+
const foundModifier = this.find(m => m.isSameAs(otherModifier));
258+
if (!foundModifier && !otherModifier.isSameAs(undefined)) {
259+
return false;
260+
}
261+
}
262+
return true;
263+
}
250264
/**
251265
* Return true if the modifiers in this array are the same as the modifiers
252266
* in the given array (as defined by the `isSameAs` methods of the
@@ -255,17 +269,7 @@ export class Modifiers extends EventMixin {
255269
* @param otherModifiers
256270
*/
257271
areSameAs(otherModifiers: Modifiers): boolean {
258-
const modifiersMap = new Map(
259-
this._contents?.map(a => [a, otherModifiers.find(b => a.isSameAs(b))]) || [],
260-
);
261-
const aModifiers = Array.from(modifiersMap.keys());
262-
const bModifiers = Array.from(modifiersMap.values());
263-
264-
const allAinB = aModifiers.every(a => a.isSameAs(modifiersMap.get(a)));
265-
const allBinA = otherModifiers.every(
266-
b => bModifiers.includes(b) || b.isSameAs(this.find(b)),
267-
);
268-
return allAinB && allBinA;
272+
return this.contains(otherModifiers) && otherModifiers.contains(this);
269273
}
270274
/**
271275
* Remove all modifiers.
@@ -306,6 +310,12 @@ export class Modifiers extends EventMixin {
306310
map<T>(callbackfn: (value: Modifier, index: number, array: Modifier[]) => T): T[] {
307311
return this._contents?.map(callbackfn) || [];
308312
}
313+
/**
314+
* Get all the modifiers
315+
*/
316+
all(): Modifier[] {
317+
return this._contents || [];
318+
}
309319
/**
310320
* @override
311321
*/

packages/core/test/Modifiers.test.ts

+55
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class ExtendedModifier extends Modifier {
1818
return otherModifier instanceof ExtendedModifier && this.value === otherModifier.value;
1919
}
2020
}
21+
class SameUndefinedModifier extends Modifier {
22+
isSameAs(otherModifier: Modifier): boolean {
23+
return otherModifier instanceof Modifier || typeof otherModifier === 'undefined';
24+
}
25+
}
2126
describe('core', () => {
2227
describe('Modifiers', () => {
2328
describe('constructor()', () => {
@@ -505,6 +510,56 @@ describe('core', () => {
505510
expect(modifiersMap[1] instanceof ExtendedModifier).to.be.true;
506511
});
507512
});
513+
describe('contains()', () => {
514+
it('should contain itself favorably', () => {
515+
const m1 = new Modifier();
516+
const m2 = new Modifier();
517+
const modifiers1 = new Modifiers(m1, m2);
518+
expect(modifiers1.contains(modifiers1)).to.be.true;
519+
});
520+
it('should contain a modifier that has the same favorably', () => {
521+
const m1 = new ExtendedModifier(1);
522+
const m2 = new ExtendedModifier(2);
523+
const modifiers1 = new Modifiers(m1, m2);
524+
const m1bis = new ExtendedModifier(1);
525+
const m2bis = new ExtendedModifier(2);
526+
const modifiers2 = new Modifiers(m1bis);
527+
const modifiers3 = new Modifiers(m2bis);
528+
const modifiers4 = new Modifiers(m1bis, m2bis);
529+
expect(modifiers1.contains(modifiers2)).to.be.true;
530+
expect(modifiers1.contains(modifiers3)).to.be.true;
531+
expect(modifiers1.contains(modifiers4)).to.be.true;
532+
});
533+
it('should contain a modifier that has the same favorably even if their order is different', () => {
534+
const m1 = new ExtendedModifier(1);
535+
const m2 = new ExtendedModifier(2);
536+
const modifiers1 = new Modifiers(m1, m2);
537+
const modifiers2 = new Modifiers(m2, m1);
538+
expect(modifiers1.contains(modifiers2)).to.be.true;
539+
});
540+
it('should contain a modifier that has the same favorably even if their order and instances are different', () => {
541+
const m1 = new ExtendedModifier(1);
542+
const m2 = new ExtendedModifier(2);
543+
const modifiers1 = new Modifiers(m1, m2);
544+
const m1bis = new ExtendedModifier(1);
545+
const m2bis = new ExtendedModifier(2);
546+
const modifiers2 = new Modifiers(m2bis, m1bis);
547+
expect(modifiers1.contains(modifiers2)).to.be.true;
548+
});
549+
it('should match with modifiers that are the same with undefined', () => {
550+
const m1 = new SameUndefinedModifier();
551+
const modifiers1 = new Modifiers();
552+
const modifiers2 = new Modifiers(m1);
553+
expect(modifiers1.contains(modifiers2)).to.be.true;
554+
});
555+
it('should not contain the other modifiers', () => {
556+
const m1 = new ExtendedModifier(1);
557+
const modifiers1 = new Modifiers(m1);
558+
const m2 = new ExtendedModifier(0);
559+
const modifiers2 = new Modifiers(m2);
560+
expect(modifiers1.contains(modifiers2)).to.be.false;
561+
});
562+
});
508563
describe('areSameAs()', () => {
509564
it('should know that an instance of Modifiers is the same as itself', () => {
510565
const m1 = new Modifier();

0 commit comments

Comments
 (0)