UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

66 lines (65 loc) 1.76 kB
/** * Value is a wrapper around any arbitrary value that adds functionality to default. */ export class Value { value; defaultValue; constructor(value, defaultValue) { this.value = value; this.defaultValue = defaultValue; } static of(value) { return new Value(value, null); } /** Returns a new attr with a different default value. */ withDefault(defaultValue) { return new Value(this.value, defaultValue); } /** Parses the attribute into a string. */ str() { return this.value ?? this.defaultValue; } /** Parses the attribute into a boolean. */ bool() { switch (this.value) { case 'true': return true; case 'false': return false; default: return this.defaultValue; } } /** Parses the attribute into an integer. */ int() { if (typeof this.value !== 'string') { return this.defaultValue; } const result = parseInt(this.value, 10); if (isNaN(result)) { return this.defaultValue; } return result; } /** Parses the attribute into a float. */ float() { if (typeof this.value !== 'string') { return this.defaultValue; } const result = parseFloat(this.value); if (isNaN(result)) { return this.defaultValue; } return result; } /** Parses the attribute into an enum. */ enum(e) { if (typeof this.value !== 'string') { return this.defaultValue; } if (e.includes(this.value)) { return this.value; } return this.defaultValue; } }