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 plural units in HED 3 code #130

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions validator/schema/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ export class SchemaEntryManager extends Memoizer {
})
}

/**
* Filter the map underlying this manager.
*
* @param {function ([string, T]): boolean} fn The filtering function.
* @returns {Map<string, T>} The filtered map.
*/
filter(fn) {
const pairArray = Array.from(this._definitions.entries())
return new Map(pairArray.filter((entry) => fn(entry)))
Expand Down Expand Up @@ -434,29 +440,45 @@ export class SchemaUnit extends SchemaEntryWithAttributes {
*/
_derivativeUnits

/**
* Constructor.
*
* @param {string} name The name of the unit.
* @param {Set<SchemaAttribute>} booleanAttributes This unit's boolean attributes.
* @param {Map<SchemaAttribute, *>} valueAttributes This unit's key-value attributes.
* @param {SchemaEntryManager<SchemaUnitModifier>} unitModifiers The collection of unit modifiers.
*/
constructor(name, booleanAttributes, valueAttributes, unitModifiers) {
super(name, booleanAttributes, valueAttributes)

this._derivativeUnits = [name]
if (!this.isSIUnit) {
this._pushPluralUnit()
return
}
const matchingModifiers = unitModifiers.filter(([_, unitModifier]) => {
return this.isUnitSymbol === unitModifier.isSIUnitSymbolModifier
})
for (const modifierName of matchingModifiers.keys()) {
this._derivativeUnits.push(modifierName + name)
}
if (!this.isUnitSymbol) {
const pluralUnit = pluralize.plural(name)
this._derivativeUnits.push(pluralUnit)
if (this.isUnitSymbol) {
const SIUnitSymbolModifiers = unitModifiers.getEntriesWithBooleanAttribute('SIUnitSymbolModifier')
for (const modifierName of SIUnitSymbolModifiers.keys()) {
this._derivativeUnits.push(modifierName + name)
}
} else {
const SIUnitModifiers = unitModifiers.getEntriesWithBooleanAttribute('SIUnitModifier')
const pluralUnit = this._pushPluralUnit()
for (const modifierName of SIUnitModifiers.keys()) {
this._derivativeUnits.push(modifierName + pluralUnit)
this._derivativeUnits.push(modifierName + name, modifierName + pluralUnit)
}
}
}

_pushPluralUnit() {
if (!this.isUnitSymbol) {
const pluralUnit = pluralize.plural(this._name)
this._derivativeUnits.push(pluralUnit)
return pluralUnit
}
return null
}

*derivativeUnits() {
for (const unit of this._derivativeUnits) {
yield unit
Expand Down