-
Notifications
You must be signed in to change notification settings - Fork 3
master-modifier-contains-nby #453
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { EventMixin } from '../../utils/src/EventMixin'; | |
import { VersionableArray } from './Memory/VersionableArray'; | ||
import { makeVersionable } from './Memory/Versionable'; | ||
|
||
export class Modifiers extends EventMixin { | ||
export class Modifiers extends EventMixin implements Iterable<Modifier> { | ||
private _contents: Modifier[]; | ||
constructor(...modifiers: Array<Modifier | Constructor<Modifier>>) { | ||
super(); | ||
|
@@ -249,6 +249,20 @@ export class Modifiers extends EventMixin { | |
toggle(modifier: Modifier | Constructor<Modifier>): void { | ||
this.remove(modifier) || this.append(modifier); | ||
} | ||
/** | ||
* Check that all `otherModifiers` ar contained within this ones. | ||
Goaman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param otherModifiers | ||
Goaman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
contains(otherModifiers: Modifiers): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Du coup pour rester dans l'esprit des autres méthodes, ce serait bien d'aussi accepter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I specifically need to check if Including On the other hand I could use contains(modifiers: Modifiers | Modifier[]): boolean {
//... or: contains(...args: [Modifiers] | Modifier[]): boolean {
const modifiers = args[0] instanceof Modifiers ? args[0] : (args as Modifier[]);
//... Which one you prefer @dmo-odoo @Zinston ? I've push the first version as I found it more elegant but I'll change it if you have a different taste. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I suggested to include the other types was that most methods in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what do I do? If I take it all it means the function would become this: contains(...args: [Modifiers] | (Modifier | Constructor<Modifier>)[]): boolean {
if (args[0] instanceof Modifiers || args[0] instanceof Modifier) {
const modifiers =
args[0] instanceof Modifiers
? args[0]
: (args as (Modifier)[]);
for (const modifier of modifiers) {
const foundModifier = this.find(m => m.isSameAs(modifier));
// Modifier.isSameAs(undefined) may return true in the case of
// modifiers that are themselves containers of values. Then we might
// want to equate the presence of an "empty" modifier with the
// absence of that modifier.
if (!foundModifier && !modifier.isSameAs(undefined)) {
return false;
}
} else {
const modifierClasses = [...new Set(args as Constructor<Modifier>[])];
return modifierClasses.every(this.find.bind(this))
}
}
return true;
} Which becomes complicated to read for not so much value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To summarize some of the options:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tested I it does not work. You need contains(...modifiers: Modifier[] | Constructor<Modifier>[]): boolean {
if (modifiers[0] instanceof Modifier) {
for (const modifier of modifiers as Modifier[]) {
const foundModifier = this.find(m => m.isSameAs(modifier));
// Modifier.isSameAs(undefined) may return true in the case of
// modifiers that are themselves containers of values. Then we might
// want to equate the presence of an "empty" modifier with the
// absence of that modifier.
if (!foundModifier && !modifier.isSameAs(undefined)) {
return false;
}
}
return true;
} else {
const modifierClasses = [...new Set(modifiers as Constructor<Modifier>[])];
return modifierClasses.every(this.find.bind(this));
}
}
areSameAs(otherModifiers: Modifiers): boolean {
return this.contains(...[...otherModifiers]) && otherModifiers.contains(...[...this]);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmmh... Then what about this:
Wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already tried. I've tested in javascript and it would work. The problem is typescript. And if you do
|
||
for (const otherModifier of otherModifiers) { | ||
const foundModifier = this.find(m => m.isSameAs(otherModifier)); | ||
if (!foundModifier && !otherModifier.isSameAs(undefined)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* Return true if the modifiers in this array are the same as the modifiers | ||
* in the given array (as defined by the `isSameAs` methods of the | ||
|
@@ -257,17 +271,7 @@ export class Modifiers extends EventMixin { | |
* @param otherModifiers | ||
*/ | ||
areSameAs(otherModifiers: Modifiers): boolean { | ||
const modifiersMap = new Map( | ||
this._contents?.map(a => [a, otherModifiers.find(b => a.isSameAs(b))]) || [], | ||
); | ||
const aModifiers = Array.from(modifiersMap.keys()); | ||
const bModifiers = Array.from(modifiersMap.values()); | ||
|
||
const allAinB = aModifiers.every(a => a.isSameAs(modifiersMap.get(a))); | ||
const allBinA = otherModifiers.every( | ||
b => bModifiers.includes(b) || b.isSameAs(this.find(b)), | ||
); | ||
return allAinB && allBinA; | ||
return this.contains(otherModifiers) && otherModifiers.contains(this); | ||
Goaman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
/** | ||
* Remove all modifiers. | ||
|
@@ -308,6 +312,20 @@ export class Modifiers extends EventMixin { | |
map<T>(callbackfn: (value: Modifier, index: number, array: Modifier[]) => T): T[] { | ||
return this._contents?.map(callbackfn) || []; | ||
} | ||
/** | ||
* Iterate through all modifiers. | ||
*/ | ||
[Symbol.iterator](): Iterator<Modifier> { | ||
Goaman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let index = -1; | ||
const data = this._contents || []; | ||
|
||
return { | ||
next: (): IteratorResult<Modifier> => ({ | ||
value: data[++index], | ||
done: !(index in data), | ||
}), | ||
}; | ||
} | ||
/** | ||
* @override | ||
*/ | ||
|
Uh oh!
There was an error while loading. Please reload this page.