UNPKG

type-enforcer

Version:
24 lines (18 loc) 580 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; } return this; } return processOutput(value, options); }; };