UNPKG

type-enforcer

Version:
25 lines (19 loc) 618 B
import { getStoredValue, hasOtherValidValue, processOutput } from './helper'; export default (options) => { const key = Symbol(); return function(newValue, isForcedSave) { const value = getStoredValue.call(this, key, options.init); if (arguments.length) { if (!hasOtherValidValue(options.other, newValue)) { newValue = options.enforce(newValue, value, options); } if (options.compare(newValue, value) || isForcedSave) { options.before.call(this, value); this[key] = newValue; options.set.call(this, newValue); } return this; } return processOutput(value, options); }; };