boxed-immutable
Advanced tools
Comparing version 0.3.0 to 0.3.2
@@ -12,3 +12,3 @@ 'use strict'; | ||
default: boxed.box, | ||
_$: boxed.box, | ||
"_$": boxed.box, | ||
box: boxed.box, | ||
@@ -15,0 +15,0 @@ createBox: boxed.createBox, |
@@ -192,2 +192,4 @@ /** internal | ||
module.exports.BOX_ONLY_PROPERTIES = BOX_ONLY_PROPERTIES; | ||
// boxed options context | ||
@@ -404,6 +406,8 @@ function BoxContext(options, parent) { | ||
if (isObject(this.getTransforms)) { | ||
this.value = applyTransforms(value, this.getTransforms, true, true); | ||
this.value = applyTransforms(value, this.getTransforms, true, true, (prop) => { | ||
this.modifiedProps[prop] = true; | ||
}); | ||
} | ||
this.isCopy = false; // set on first modification | ||
this.isCopy = this.value !== value; // is a copy if modified by transforms, full copy | ||
this.isFullCopy = false; // set if copy was a fresh array or object | ||
@@ -615,3 +619,3 @@ } | ||
} | ||
// if (this.getTransforms) { | ||
@@ -634,2 +638,3 @@ // const transform = this.getTransforms[prop]; | ||
boxed.isCopy = true; | ||
boxed.isFullCopy = this.modifiedProps[prop]; | ||
} | ||
@@ -685,12 +690,14 @@ this.boxedProps[prop] = boxed; | ||
function applyTransform(transforms, key, value, oldValue, getValue, setValue, applyFunctions, applyObjects) { | ||
if (transforms) { | ||
if (!isArray(transforms)) transforms = [transforms]; | ||
let jMax = transforms.length; | ||
function applyTransform(transforms, defaultTransforms, key, value, oldValue, getValue, setValue, applyFunctions, applyObjects) { | ||
if (transforms || defaultTransforms) { | ||
let allTransforms = [].concat(transforms, defaultTransforms); | ||
let jMax = allTransforms.length; | ||
for (let j = 0; j < jMax; j++) { | ||
let transform = transforms[j]; | ||
if (applyObjects && isObject(transform)) {// recurse | ||
value = applyTransforms(value, transform, applyFunctions, applyObjects); | ||
} else if (applyFunctions && isFunction(transform)) { | ||
value = transform(value, key, oldValue, getValue, setValue); | ||
let transform = allTransforms[j]; | ||
if (transform) { | ||
if (applyObjects && isObject(transform)) {// recurse | ||
value = applyTransforms(value, transform, applyFunctions, applyObjects); | ||
} else if (applyFunctions && isFunction(transform)) { | ||
value = transform(value, key, oldValue, getValue, setValue); | ||
} | ||
} | ||
@@ -702,6 +709,3 @@ } | ||
function applyTransforms(value, transforms, applyFunctions, applyObjects) { | ||
const defaultTransform = transforms[BOXED_ARRAY_END]; | ||
const keys = Object.keys(transforms); | ||
let i = keys.length; | ||
function applyTransforms(value, transforms, applyFunctions, applyObjects, markModified) { | ||
let isCopy; | ||
@@ -716,2 +720,5 @@ | ||
value[prop] = propValue; | ||
if (markModified) { | ||
markModified(prop); | ||
} | ||
} | ||
@@ -727,31 +734,35 @@ } | ||
while (i--) { | ||
const key = keys[i]; | ||
if (value.hasOwnProperty(key)) { | ||
const transformList = transforms[key]; | ||
const defaultTransform = transforms[BOXED_ARRAY_END]; | ||
const keys = Object.keys(transforms); | ||
let i = keys.length; | ||
if (!defaultTransform || i > 1) { | ||
while (i--) { | ||
const key = keys[i]; | ||
if (value.hasOwnProperty(key)) { | ||
let transformList = transforms[key]; | ||
if (transformList) { | ||
const oldValue = oldValues[key]; | ||
propValue = value[key]; | ||
propValue = applyTransform(transformList, key, propValue, oldValue, getValue, setValue, applyFunctions, applyObjects); | ||
setValue(key, propValue); | ||
if (transformList) { | ||
const oldValue = oldValues[key]; | ||
propValue = value[key]; | ||
propValue = applyTransform(transformList, defaultTransform, key, propValue, oldValue, getValue, setValue, applyFunctions, applyObjects); | ||
setValue(key, propValue); | ||
} | ||
} | ||
} | ||
} | ||
// do the default transform application | ||
if (defaultTransform) { | ||
// may have keys there were not processed above because they only have default transforms | ||
const keys = Object.keys(value); | ||
let propValue; | ||
const transformList = isArray(defaultTransform) ? defaultTransform : [defaultTransform]; | ||
let iMax = keys.length; | ||
for (let i = 0; i < iMax; i++) { | ||
let i = keys.length; | ||
while (i--) { | ||
const key = keys[i]; | ||
const oldValue = oldValues[key]; | ||
propValue = value[key]; | ||
propValue = applyTransform(transformList, key, propValue, oldValue, getValue, setValue, applyFunctions, applyObjects); | ||
setValue(key, propValue); | ||
if (!transforms.hasOwnProperty(key)) { | ||
// was not done above | ||
const oldValue = oldValues[key]; | ||
propValue = value[key]; | ||
propValue = applyTransform(defaultTransform, undefined, key, propValue, oldValue, getValue, setValue, applyFunctions, applyObjects); | ||
setValue(key, propValue); | ||
} | ||
} | ||
} | ||
return value; | ||
@@ -813,31 +824,27 @@ } | ||
if (this.setTransforms) { | ||
if (isArrayEnd) { | ||
prop = util.arrayEndIndex(this.value); | ||
} | ||
this.extraValues = undefined; | ||
this.toTransformValue = value; | ||
this.toTransformKey = prop; | ||
const transform = this.setTransforms[prop]; | ||
const defaultTransform = this.setTransforms[BOXED_ARRAY_END]; | ||
const untransformedValue = value; | ||
if (transform) { | ||
value = applyTransform(transform, prop, value, oldValue, this.getValueProp, this.setValueProp, true, !boxed); | ||
} | ||
if (transform || defaultTransform) { | ||
if (isArrayEnd) { | ||
prop = util.arrayEndIndex(this.value); | ||
} | ||
this.extraValues = undefined; | ||
this.toTransformValue = value; | ||
this.toTransformKey = prop; | ||
if (defaultTransform) { | ||
value = applyTransform(defaultTransform, prop, value, oldValue, this.getValueProp, this.setValueProp, true, !boxed); | ||
} | ||
const untransformedValue = value; | ||
if (this.extraValues) canOptimizeSameValue = false; | ||
value = applyTransform(transform, defaultTransform, prop, value, oldValue, this.getValueProp, this.setValueProp, true, !boxed); | ||
// get the potentially transformed value, if it is changed then it boxed prop is invalid | ||
if (this.toTransformValue !== untransformedValue) { | ||
if (prop !== BOXED_ARRAY_END) { | ||
boxed = undefined; | ||
if (this.extraValues) canOptimizeSameValue = false; | ||
// get the potentially transformed value, if it is changed then it boxed prop is invalid | ||
if (this.toTransformValue !== untransformedValue) { | ||
if (prop !== BOXED_ARRAY_END) { | ||
boxed = undefined; | ||
} | ||
value = this.toTransformValue; | ||
} | ||
value = this.toTransformValue; | ||
this.toTransformValue = undefined; | ||
this.toTransformKey = undefined; | ||
} | ||
this.toTransformValue = undefined; | ||
this.toTransformKey = undefined; | ||
} | ||
@@ -930,4 +937,3 @@ | ||
let value = applyTransform(transform, prop, undefined, oldValue, this.getValueProp, this.setValueProp, true, true); | ||
value = applyTransform(defaultTransform, prop, value, oldValue, this.getValueProp, this.setValueProp, true, true); | ||
let value = applyTransform(transform, defaultTransform, prop, undefined, oldValue, this.getValueProp, this.setValueProp, true, true); | ||
@@ -1501,3 +1507,3 @@ this.toTransformValue = undefined; | ||
return box; | ||
} else if (prop === box.context.boxedInProxy) { | ||
} else if (prop === box.context.boxedInSuffix) { | ||
return box.boxedInProxy; | ||
@@ -1570,5 +1576,35 @@ } | ||
function toBoolean(arg) { | ||
return !!arg; | ||
} | ||
function toBooleanDefaultTrue(arg) { | ||
return arg === UNDEFINED ? true : !!arg; | ||
} | ||
function isPrefixedToBoolean(arg, prop) { | ||
return util.startsWith(prop, 'is') ? !!arg : arg; | ||
} | ||
function regexMatchProp(regex, filter) { | ||
return function () { | ||
const prop = arguments[1]; | ||
if (prop.match(regex)) { | ||
return filter.apply(this, arguments); | ||
} | ||
return arguments[0]; | ||
} | ||
} | ||
const TRANSFORMS = { | ||
toBoolean: toBoolean, | ||
toBooleanDefaultTrue: toBooleanDefaultTrue, | ||
isPrefixedToBoolean: isPrefixedToBoolean, | ||
regexMatchProp: regexMatchProp, | ||
}; | ||
function createBox(options) { | ||
let context = new BoxContext(options); | ||
let boxOnlyOptions = util.copyProperties(options, BOX_ONLY_PROPERTIES); | ||
let boxOptionOverride = undefined; | ||
@@ -1596,3 +1632,11 @@ let boxedIn = function boxedIn() { | ||
let box = new Box(value, UNDEFINED, UNDEFINED, context, boxOnlyOptions); | ||
let box; | ||
if (isObject(boxOptionOverride)) { | ||
box = new Box(value, UNDEFINED, UNDEFINED, context, Object.assign({}, boxOnlyOptions, boxOptionOverride)); | ||
} else { | ||
box = new Box(value, UNDEFINED, UNDEFINED, context, boxOnlyOptions); | ||
} | ||
boxOptionOverride = undefined; | ||
boxedProxy(box); | ||
@@ -1606,3 +1650,11 @@ | ||
function withBoxOptions(options) { | ||
boxOptionOverride = options; | ||
return boxedIn.apply(boxedIn, Array.prototype.slice.call(arguments, 1)); | ||
} | ||
// change to string so that global _$ will be recognized as array end property | ||
boxedIn.withBoxOptions = withBoxOptions.bind(boxedIn); | ||
boxedIn.transform = TRANSFORMS; | ||
if (!globalBoxKey) { | ||
@@ -1613,2 +1665,3 @@ let obj = {}; | ||
} | ||
context.globalBox = globalBoxKey; | ||
@@ -1615,0 +1668,0 @@ boxedIn.boxedContext = context; |
@@ -12,3 +12,6 @@ "use strict"; | ||
function getBoxed() { | ||
return this.boxed || (this.boxed = this.box(this.getState())); | ||
if (!this.boxed) { | ||
this.boxed = this.box.withBoxOptions(this.boxOnlyOptions, this.getState()); | ||
} | ||
return this.boxed; | ||
} | ||
@@ -36,2 +39,8 @@ | ||
function boxOptions(boxOnlyOptions) { | ||
this.boxed = UNDEFINED; | ||
this.boxOnlyOptions = util.hasOwnProperties(boxOnlyOptions) && boxOnlyOptions; | ||
return this.proxiedThis; | ||
} | ||
/** | ||
@@ -51,2 +60,3 @@ * create a new Boxed property or object | ||
let box; | ||
let boxOnlyOptions; | ||
@@ -58,2 +68,3 @@ if (isValidBox(options)) { | ||
box = util.firstDefined(isValidBox(options.box) ? options.box : undefined, boxedImmutable.box); | ||
boxOnlyOptions = util.copyProperties(options, boxedImmutable.BOX_ONLY_PROPERTIES); | ||
} | ||
@@ -63,2 +74,3 @@ | ||
let cancelBoxedProp = util.firstDefined(options.saveBoxedProp, 'cancel'); | ||
let boxOptionsProp = util.firstDefined(options.boxOptionsProp, 'boxOptions'); | ||
@@ -70,2 +82,3 @@ // add context | ||
this.cancelBoxedProp = cancelBoxedProp; | ||
this.boxOptionsProp = boxOptionsProp; | ||
this.getState = getState; | ||
@@ -75,2 +88,3 @@ this.saveState = saveState; | ||
this.proxiedThis = UNDEFINED; | ||
this.boxOnlyOptions = util.hasOwnProperties(boxOnlyOptions) && boxOnlyOptions; | ||
@@ -80,2 +94,3 @@ this.getBoxed = getBoxed.bind(this); | ||
this.cancelBoxed = cancelBoxed.bind(this); | ||
this.boxOptions = boxOptions.bind(this); | ||
} | ||
@@ -88,2 +103,3 @@ | ||
BoxedState.prototype.cancelBoxed = cancelBoxed; | ||
BoxedState.prototype.boxOptions = boxOptions; | ||
@@ -94,2 +110,4 @@ function isBoxedState(arg) { | ||
const BOXED_GET_THIS = boxedImmutable.BOXED_GET_THIS; | ||
const BoxedStateHandler = { | ||
@@ -99,4 +117,6 @@ // target is the object with on demand boxed property | ||
if (isBoxedState(target)) { | ||
if (prop === BOXED_GET_THIS) return target; | ||
if (prop === target.saveBoxedProp) return target.saveBoxed; | ||
if (prop === target.cancelBoxedProp) return target.cancelBoxed; | ||
if (prop === target.boxOptionsProp) return target.boxOptions; | ||
return target.getBoxed()[prop]; | ||
@@ -111,2 +131,3 @@ } | ||
if (prop === target.cancelBoxedProp) return false; | ||
if (prop === target.boxOptionsProp) return false; | ||
return Reflect.set(target.getBoxed(), prop, value, receiver); | ||
@@ -121,2 +142,3 @@ } | ||
if (prop === target.cancelBoxedProp) return false; | ||
if (prop === target.boxOptionsProp) return false; | ||
return Reflect.deleteProperty(target.getBoxed(), prop); | ||
@@ -131,2 +153,3 @@ } | ||
if (prop === target.cancelBoxedProp) return true; | ||
if (prop === target.boxOptionsProp) return true; | ||
return Reflect.has(target.getBoxed(), prop); | ||
@@ -140,3 +163,3 @@ } | ||
const keys = Reflect.ownKeys(target.getBoxed()); | ||
return util.deleteItems(keys, target.saveBoxedProp, target.cancelBoxedProp); | ||
return util.deleteItems(keys, target.saveBoxedProp, target.cancelBoxedProp, target.boxOptionsProp); | ||
} | ||
@@ -150,2 +173,3 @@ throw "BoxedStateHandler: IllegalArgument expected BoxedState target"; | ||
if (prop === target.cancelBoxedProp) return { value: target.cancelBoxed, /*writable: false, enumerable: false, configurable: false, get:UNDEFINED, set:UNDEFINED,*/ }; | ||
if (prop === target.boxOptionsProp) return { value: target.boxOptions, /*writable: false, enumerable: false, configurable: false, get:UNDEFINED, set:UNDEFINED,*/ }; | ||
return Reflect.getOwnPropertyDescriptor(target.getBoxed(), prop); | ||
@@ -152,0 +176,0 @@ } |
@@ -47,3 +47,3 @@ "use strict"; | ||
} | ||
return false; | ||
return UNDEFINED; | ||
} | ||
@@ -69,3 +69,3 @@ | ||
function isObject(param) { | ||
return param && typeof param === "object"; | ||
return !!param && typeof param === "object"; | ||
} | ||
@@ -77,4 +77,8 @@ | ||
function isStringOrNumber(arg) { | ||
return isString(arg) || typeof arg === "number"; | ||
} | ||
function isNumeric(arg) { | ||
const n = Number.parseFloat(arg); | ||
const n = isStringOrNumber(arg) && Number.parseFloat(arg); | ||
return Number.isFinite(n) && +n === n; | ||
@@ -84,3 +88,3 @@ } | ||
function isNumericInteger(arg) { | ||
const n = Number.parseFloat(arg); | ||
const n = isStringOrNumber(arg) && Number.parseFloat(arg); | ||
return Number.isInteger(n) && +n === n; | ||
@@ -90,3 +94,3 @@ } | ||
function toNumber(arg) { | ||
const n = Number.parseFloat(arg); | ||
const n = isStringOrNumber(arg) && Number.parseFloat(arg); | ||
// noinspection EqualityComparisonWithCoercionJS | ||
@@ -97,3 +101,3 @@ return n == arg ? n : arg; | ||
function toNumberOrUndefined(arg) { | ||
const n = Number.parseFloat(arg); | ||
const n = isStringOrNumber(arg) && Number.parseFloat(arg); | ||
// noinspection EqualityComparisonWithCoercionJS | ||
@@ -104,3 +108,3 @@ return n == arg ? n : UNDEFINED; | ||
function toNumericInteger(arg) { | ||
const n = Number.parseFloat(arg); | ||
const n = isStringOrNumber(arg) && Number.parseFloat(arg); | ||
return Number.isInteger(n) && +n === n ? n : UNDEFINED; | ||
@@ -110,3 +114,3 @@ } | ||
function toArrayIndex(arg) { | ||
const n = Number.parseFloat(arg); | ||
const n = isStringOrNumber(arg) && Number.parseFloat(arg); | ||
return Number.isInteger(n) && +n === n && n >= 0 ? n : UNDEFINED; | ||
@@ -120,3 +124,3 @@ } | ||
function toNumberIfArrayIndex(arg) { | ||
const n = Number.parseFloat(arg); | ||
const n = isStringOrNumber(arg) && Number.parseFloat(arg); | ||
return Number.isInteger(n) && +n === n && n >= 0 ? n : arg; | ||
@@ -123,0 +127,0 @@ } |
{ | ||
"name": "boxed-immutable", | ||
"version": "0.3.0", | ||
"version": "0.3.2", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "Immutable proxy wrapper with exception free access to nested properties and auto-instantiation of intermediate containers when nested properties are defined", |
@@ -21,2 +21,46 @@ "use strict"; | ||
describe('boxed on demand internal ops', () => { | ||
let onDemand; | ||
let origVal; | ||
let boxedProxy; | ||
let boxedVal; | ||
let saveCalled; | ||
let getCalled; | ||
beforeAll(() => { | ||
origVal = undefined; | ||
saveCalled = 0; | ||
getCalled = 0; | ||
let vals = createBoxedState(() => { | ||
getCalled++; | ||
return onDemand || origVal; | ||
}, (modified, boxed) => { | ||
saveCalled++; | ||
onDemand = { | ||
state: modified, | ||
delta: boxed.$_delta, | ||
deepDelta: boxed.$_delta, | ||
} | ||
}); | ||
boxedProxy = vals.boxedProxy; | ||
boxedVal = vals.boxedVal; | ||
}); | ||
test('Boxed does not change', () => { | ||
expect(boxedVal.boxed).toBe(undefined); | ||
expect([getCalled, saveCalled]).toEqual([0, 0]); | ||
let boxed = boxedProxy._$; | ||
expect(boxedVal.boxed).not.toBe(undefined); | ||
expect([getCalled, saveCalled]).toEqual([1, 0]); | ||
boxedProxy.cancel(); | ||
expect(boxedVal.boxed).toBe(undefined); | ||
expect([getCalled, saveCalled]).toEqual([1, 0]); | ||
let tmp = boxedProxy.value; | ||
expect(boxedVal.boxed).not.toBe(undefined); | ||
}); | ||
}); | ||
describe('boxed on demand empty no changes', () => { | ||
@@ -26,2 +70,3 @@ let onDemand; | ||
let boxedProxy; | ||
let boxedVal; | ||
let saveCalled; | ||
@@ -48,2 +93,3 @@ let getCalled; | ||
boxedProxy = vals.boxedProxy; | ||
boxedVal = vals.boxedVal; | ||
}); | ||
@@ -50,0 +96,0 @@ |
@@ -16,2 +16,3 @@ "use strict"; | ||
const createBoxed = testUtil.createBoxed; | ||
const createTransformedBoxed = testUtil.createTransformedBoxed; | ||
const createOnDemandBoxed = testUtil.createBoxedState; | ||
@@ -143,2 +144,6 @@ const isNullOrUndefined = boxedImmutable.util.isNullOrUndefined; | ||
function createTransformedBox() { | ||
return createTransformedBoxed({ getTransforms: getTransforms }, ...arguments); | ||
} | ||
describe('getTransforms applied to props', () => { | ||
@@ -152,8 +157,8 @@ let origVal; | ||
beforeAll(() => { | ||
box = boxedImmutable.createBox({ getTransforms: getTransforms }); | ||
}); | ||
// beforeAll(() => { | ||
// box = boxedImmutable.createBox({ getTransforms: getTransforms }); | ||
// }); | ||
test(`getTransforms applied to value`, () => { | ||
const vals = createBoxed(original, box); | ||
const vals = createTransformedBox(original); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -165,10 +170,40 @@ | ||
test(`getTransforms get modified`, () => { | ||
const vals = createBoxed(original, box); | ||
const vals = createTransformedBox(original); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
expect(boxedVal.valueOfModified()).toEqual(undefined); | ||
expect(boxedVal.valueOfModified()).toEqual(boxedVal.value); | ||
}); | ||
test(`getTransforms get delta`, () => { | ||
const vals = createTransformedBox(original); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
expect(boxedVal.unboxedDelta()).toEqual({ | ||
"boolean": true, | ||
"booleanArr": [false, false, false, false, true, true, true, true, true], | ||
"capitalized": "LOWERCASE", | ||
"nested": { | ||
"another": "someValue", | ||
"boolean": false, | ||
"capitalized": 5, | ||
"nested": { "another": "someValue", "boolean": false, "capitalized": null }, | ||
} | ||
}); | ||
}); | ||
test(`getTransforms get deep delta`, () => { | ||
const vals = createTransformedBox(original); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
expect(boxedVal.unboxedDeepDelta()).toEqual({ | ||
"boolean": true, | ||
"booleanArr": [false, false, false, false, true, true, true, true, true], | ||
"capitalized": "LOWERCASE", | ||
"nested": { | ||
"another": "someValue", "boolean": false, "capitalized": 5, | ||
"nested": { "another": "someValue", "boolean": false, "capitalized": null } | ||
}, | ||
}); | ||
}); | ||
test(`with totals`, () => { | ||
const withTotals = [1, 2, 3, 4]; | ||
const vals = createBoxed({withTotals:withTotals}, box); | ||
const vals = createTransformedBox({ withTotals: withTotals }); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -182,5 +217,24 @@ | ||
test(`with root custom`, () => { | ||
const withTotals = { showFlag: null, collapseFlag: 0, untouched: '', isLoadingFlag: 1, }; | ||
const expected = { showFlag: false, collapseFlag: false, untouched: '', isLoadingFlag: true, }; | ||
const vals = createTransformedBoxed({ | ||
getTransforms: { | ||
'': function (value, prop, oldValue, getProp, setProp) { | ||
if (util.endsWith(prop, 'Flag')) { | ||
return !!value; | ||
} | ||
return value; | ||
} | ||
} | ||
}, withTotals); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
// expect(boxedVal.value.withTotals.total).toEqual(withTotals.total); | ||
expect(boxedVal.valueOf()).toEqual(expected); | ||
}); | ||
test(`with rounded mods`, () => { | ||
let withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
const vals = createBoxed({withRounded:withTotals}, box); | ||
const vals = createTransformedBox({ withRounded: withTotals }); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -197,3 +251,3 @@ | ||
const withTotals = [1, 2, 3, 4]; | ||
const vals = createBoxed({withTotals:withTotals}, box); | ||
const vals = createTransformedBox({ withTotals: withTotals }); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -209,6 +263,6 @@ | ||
let withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
const vals = createBoxed({withRounded:withTotals}, box); | ||
const vals = createTransformedBox({ withRounded: withTotals }); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
withTotals = withTotals.map(roundTransform); | ||
// withTotals.total = withTotals.reduce((prev, value, index) => (prev || 0) + value); | ||
@@ -222,5 +276,5 @@ | ||
let withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
const vals = createBoxed({withRoundedTotals:withTotals}, box); | ||
const vals = createTransformedBox({ withRoundedTotals: withTotals }); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
withTotals = withTotals.map(roundTransform); | ||
@@ -227,0 +281,0 @@ withTotals.total = withTotals.reduce((prev, value, index) => (prev || 0) + value); |
@@ -16,2 +16,3 @@ "use strict"; | ||
const createBoxed = testUtil.createBoxed; | ||
const createTransformedBoxed = testUtil.createTransformedBoxed; | ||
const createOnDemandBoxed = testUtil.createBoxedState; | ||
@@ -60,3 +61,3 @@ const isNullOrUndefined = boxedImmutable.util.isNullOrUndefined; | ||
// beforeEach(() => { | ||
// vals = createBoxed(thisTest.value); | ||
// vals = createTransformedBoxed(thisTest.value); | ||
// origVal = vals.origVal; | ||
@@ -88,3 +89,3 @@ // boxedVal = vals.boxedVal; | ||
// beforeEach(() => { | ||
// vals = createBoxed(thisTest.value); | ||
// vals = createTransformedBoxed(thisTest.value); | ||
// origVal = vals.origVal; | ||
@@ -218,2 +219,6 @@ // boxedVal = vals.boxedVal; | ||
function createTransformedBox() { | ||
return createTransformedBoxed({ setTransforms: setTransforms }, ...arguments); | ||
} | ||
describe('setTransforms applied to props', () => { | ||
@@ -227,8 +232,8 @@ let origVal; | ||
beforeAll(() => { | ||
box = boxedImmutable.createBox({ setTransforms: setTransforms }); | ||
}); | ||
// beforeAll(() => { | ||
// box = boxedImmutable.createBox({ setTransforms: setTransforms }); | ||
// }); | ||
// | ||
test(`setTransforms applied to value`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -241,3 +246,3 @@ boxedVal.setValueOf(original); | ||
test(`setTransforms set modified`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -250,3 +255,3 @@ boxedVal.setValueOf(original); | ||
test(`setTransforms delete cancelled`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -264,4 +269,71 @@ boxedVal.setValueOf(original); | ||
test(`with root custom`, () => { | ||
const withTotals = { showFlag: null, collapseFlag: 0, untouched: '', isLoadingFlag: 1, }; | ||
const expected = { showFlag: false, collapseFlag: false, untouched: '', isLoadingFlag: true, }; | ||
const vals = createTransformedBoxed({ setTransforms: {'': function (value, prop, oldValue, getProp, setProp) { | ||
if (util.endsWith(prop,'Flag')) { | ||
return !!value; | ||
} | ||
return value; | ||
}}}, withTotals); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
// expect(boxedVal.value.withTotals.total).toEqual(withTotals.total); | ||
expect(boxedVal.valueOf()).toEqual(withTotals); | ||
}); | ||
test(`with root custom modified`, () => { | ||
const withTotals = { showFlag: null, collapseFlag: 0, untouched: '', isLoadingFlag: 1, }; | ||
const expected = { showFlag: false, collapseFlag: false, untouched: '', isLoadingFlag: true, }; | ||
const vals = createTransformedBoxed({ setTransforms: {'': function (value, prop, oldValue, getProp, setProp) { | ||
if (util.endsWith(prop,'Flag')) { | ||
return !!value; | ||
} | ||
return value; | ||
}}}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
boxedVal.setValueOf(withTotals); | ||
// expect(boxedVal.value.withTotals.total).toEqual(withTotals.total); | ||
expect(boxedVal.valueOf()).toEqual(expected); | ||
}); | ||
test(`with root custom new prop`, () => { | ||
const withTotals = { showFlag: null, collapseFlag: 0, untouched: '', isLoadingFlag: 1, }; | ||
const expected = { showFlag: false, collapseFlag: false, untouched: '', isLoadingFlag: true, newFlag:true }; | ||
const vals = createTransformedBoxed({ setTransforms: {'': function (value, prop, oldValue, getProp, setProp) { | ||
if (util.endsWith(prop,'Flag')) { | ||
return !!value; | ||
} | ||
return value; | ||
}}}, withTotals); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
boxedProxy.newFlag = []; | ||
// expect(boxedVal.value.withTotals.total).toEqual(withTotals.total); | ||
expect(boxedVal.valueOf()).toEqual(Object.assign({}, withTotals, {newFlag:true})); | ||
}); | ||
test(`with root custom modified new prop`, () => { | ||
const withTotals = { showFlag: null, collapseFlag: 0, untouched: '', isLoadingFlag: 1, }; | ||
const expected = { showFlag: false, collapseFlag: false, untouched: '', isLoadingFlag: true, newFlag:true }; | ||
const vals = createTransformedBoxed({ setTransforms: {'': function (value, prop, oldValue, getProp, setProp) { | ||
if (util.endsWith(prop,'Flag')) { | ||
return !!value; | ||
} | ||
return value; | ||
}}}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
boxedVal.setValueOf(withTotals); | ||
boxedProxy.newFlag = []; | ||
// expect(boxedVal.value.withTotals.total).toEqual(withTotals.total); | ||
expect(boxedVal.valueOf()).toEqual(expected); | ||
}); | ||
test(`with totals`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -278,3 +350,3 @@ const withTotals = [1, 2, 3, 4]; | ||
test(`with totals mods`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -294,3 +366,3 @@ const withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
test(`with totals delete`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -310,3 +382,3 @@ const withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
test(`with rounded mods`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -327,3 +399,3 @@ let withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
test(`with rounded totals mods`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -344,3 +416,3 @@ let withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
test(`with object totals`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -357,3 +429,3 @@ const withTotals = [1, 2, 3, 4]; | ||
test(`with object totals mods`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -373,3 +445,3 @@ const withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
test(`with object totals delete`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -389,3 +461,3 @@ const withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
test(`with object rounded mods`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -406,3 +478,3 @@ let withTotals = [1.5, 2.5, 3.5, 4.5]; | ||
test(`with object rounded totals mods`, () => { | ||
const vals = createBoxed({}, box); | ||
const vals = createTransformedBox({}); | ||
const { origVal, boxedVal, boxedProxy } = vals; | ||
@@ -409,0 +481,0 @@ let withTotals = [1.5, 2.5, 3.5, 4.5]; |
@@ -62,2 +62,13 @@ const boxedImmutable = require("boxed-immutable"); | ||
function createTransformedBoxed(options, val, box) { | ||
box = box || _$; | ||
const boxedProxy = box.withBoxOptions(options, val); | ||
const boxVal = boxedProxy[BOXED_GET_THIS]; | ||
return { | ||
origVal: val, | ||
boxedProxy: boxedProxy, | ||
boxedVal: boxVal, | ||
}; | ||
} | ||
function createBoxedState(get, set) { | ||
@@ -67,2 +78,3 @@ const boxedProxy = boxState(get, set); | ||
boxedProxy: boxedProxy, | ||
boxedVal: boxedProxy[BOXED_GET_THIS], | ||
}; | ||
@@ -92,3 +104,4 @@ } | ||
module.exports.createBoxed = createBoxed; | ||
module.exports.createTransformedBoxed = createTransformedBoxed; | ||
module.exports.createBoxedState = createBoxedState; | ||
module.exports.arrayToObject = arrayToObject; |
@@ -5,2 +5,3 @@ # Version History | ||
- [0.3.2](#032) | ||
- [0.3.0](#030) | ||
@@ -21,2 +22,11 @@ - [0.2.8](#028) | ||
## 0.3.2 | ||
* Add: `.withBoxOptions(options)` function to box creation functions so that a transforms | ||
customized box can be created from a single box function without needing to create one for | ||
each type of transform. | ||
* Add: box options to `boxState(getState, saveState, options)` so that it will create customized | ||
boxes if any box specific options are passed in options. Eliminates the need to have a custom | ||
created box to provide transforms. | ||
## 0.3.0 | ||
@@ -23,0 +33,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
201549
4753
0