type-enforcer
Version:
Type enforcement library for javascript
22 lines (16 loc) • 499 B
JavaScript
import { getStoredValue, processOutput } from './helper';
export default (options) => {
const key = Symbol();
return function(newValue, isForcedSave) {
const value = getStoredValue.call(this, key, options.init);
if (arguments.length) {
newValue = options.enforce(newValue, value, options);
if (options.compare(newValue, value) || isForcedSave) {
this[key] = newValue;
options.set.call(this, newValue);
}
return this;
}
return processOutput(value, options);
};
};