@svta/common-media-library
Version:
A common library for media playback in JavaScript
60 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Settings = void 0;
// A settings object holds key/value pairs and will ignore anything but the first
// assignment to a specific key.
class Settings {
constructor() {
this.values = Object.create(null);
}
// Only accept the first assignment to any key.
set(k, v) {
if (this.get(k) || v === '') {
return;
}
this.values[k] = v;
}
// Return the value for a key, or a default value.
// If 'defaultKey' is passed then 'dflt' is assumed to be an object with
// a number of possible default values as properties where 'defaultKey' is
// the key of the property that will be chosen; otherwise it's assumed to be
// a single value.
get(k, dflt) {
if (this.has(k)) {
return this.values[k];
}
return dflt;
}
// Check whether we have a value for a key.
has(k) {
return k in this.values;
}
// Accept a setting if its one of the given alternatives.
alt(k, v, a) {
for (let n = 0; n < a.length; ++n) {
if (v === a[n]) {
this.set(k, v);
break;
}
}
}
// Accept a setting if its a valid (signed) integer.
integer(k, v) {
if (/^-?\d+$/.test(v)) { // integer
this.set(k, parseInt(v, 10));
}
}
// Accept a setting if its a valid percentage.
percent(k, v) {
if (v.match(/^([\d]{1,3})(\.[\d]*)?%$/)) {
const value = parseFloat(v);
if (value >= 0 && value <= 100) {
this.set(k, value);
return true;
}
}
return false;
}
}
exports.Settings = Settings;
//# sourceMappingURL=Settings.js.map