field-change-effects-calculator
Advanced tools
Comparing version 1.0.3 to 2.0.0
@@ -7,2 +7,4 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _ramda = require('ramda'); | ||
@@ -26,5 +28,5 @@ | ||
/** | ||
* | ||
* | ||
* COMPLEX FUNCTION. CONSIDER CAUTION WHILE MODIFYING. | ||
* | ||
* | ||
* This is a helper function that will used in queue iterations. This function hosts the | ||
@@ -34,5 +36,5 @@ * infinite loop breaking logic. When ever it calculates changes a certain field, it checks | ||
* and add them into the queue so they will be processed in next iterations. | ||
* | ||
* @param { fieldName: Array<FieldChangeEffectRuleType> } FIELD_CHANGE_EFFECT_RULES | ||
* @param queue | ||
* | ||
* @param { fieldName: Array<FieldChangeEffectRuleType> } FIELD_CHANGE_EFFECT_RULES | ||
* @param queue | ||
* @param {Array<FieldChangeObjectType>} fieldChanges | ||
@@ -42,3 +44,3 @@ * @param {string} changedFieldName | ||
* @param state | ||
* | ||
* | ||
* @author Revanth Kumar | ||
@@ -53,7 +55,3 @@ */ | ||
path = _ref2.path, | ||
value = _ref2.value, | ||
editable = _ref2.editable, | ||
readable = _ref2.readable, | ||
required = _ref2.required, | ||
otherProps = _ref2.otherProps; | ||
props = _ref2.props; | ||
@@ -71,7 +69,3 @@ if (containsField(fieldChanges, name)) { | ||
path: path, | ||
value: value(newValue, state), | ||
editable: editable(newValue, state), | ||
readable: readable(newValue, state), | ||
required: required(newValue, state), | ||
otherProps: otherProps ? otherProps(newValue, state) : {} | ||
props: props ? props(newValue, state) : {} | ||
}; | ||
@@ -82,4 +76,4 @@ if (FIELD_CHANGE_EFFECT_RULES[name]) { | ||
* need to change. Hence add it into the queue. | ||
*/ | ||
queue.push({ name: name, newValue: changes.value }); | ||
*/ | ||
queue.push({ name: name, newValue: changes.props.value }); | ||
} | ||
@@ -97,5 +91,5 @@ return changes; | ||
/** | ||
* | ||
* | ||
* COMPLEX FUNCTION. CONSIDER CAUTION WHILE MODIFYING. | ||
* | ||
* | ||
* This function calculates all field change effects if @param {string} name is changed to @param {any} value. | ||
@@ -105,5 +99,5 @@ * It calculates all field changes even if they are n-level deep using a queue iteratively. | ||
* The function also gets @param state which is the global state at that point of time. | ||
* @param {Array<string>} path will be used to fetch the field from the global state. | ||
* @param {Array<string>} path will be used to fetch the field from the global state. | ||
* It has logic built into it to avoid the execution of certain field change effects if an infinite loop has been detected. | ||
* | ||
* | ||
* @author Revanth Kumar | ||
@@ -131,7 +125,3 @@ */ | ||
path: fieldPath, | ||
value: value, | ||
editable: (0, _ramda.pathOr)(false, [].concat(_toConsumableArray(fieldPath), ['editable']), state), | ||
readable: (0, _ramda.pathOr)(false, [].concat(_toConsumableArray(fieldPath), ['readable']), state), | ||
required: (0, _ramda.pathOr)(false, [].concat(_toConsumableArray(fieldPath), ['required']), state), | ||
otherProps: (0, _ramda.pathOr)({}, [].concat(_toConsumableArray(fieldPath), ['otherProps']), state) | ||
props: _extends({}, (0, _ramda.pathOr)({}, [].concat(_toConsumableArray(fieldPath), ['props']), state), { value: value }) | ||
}]; | ||
@@ -138,0 +128,0 @@ var singleFieldChangesCalculator = calculateSingleFieldChanges(FIELD_CHANGE_EFFECT_RULES, queue, fieldChanges); |
{ | ||
"name": "field-change-effects-calculator", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "module": "lib/index.js", |
@@ -5,6 +5,10 @@ # field-change-effects-calculator | ||
It does **multi level changes** at one shot. For instance if there is a rule saying if A changes B has to change and if B changes C has to change, all the calculations are done and returned by the utility function. | ||
It does **multi level changes** at one shot. For instance if there is a rule saying if A changes B has to change and if B changes C has to change, all the calculations are done and returned by the utility function. | ||
It also has **infinite loop mitigation** logic built into it so even if there were some misleading rules set, the algorithm will avoid such loops. | ||
# Example | ||
An REPL has been created and hosted to show the usage of this utility, check it out [here](https://repl.it/@RevanthKumar2/FieldChangesCalculator). | ||
# Usage | ||
@@ -16,16 +20,18 @@ | ||
or | ||
const fieldChangesCalculator = require('field-change-effects-calculator') | ||
Signature: | ||
(rules: { fieldName: Array<FieldChangeEffectRuleType> }) => (state: Object) => (name: string, fieldPathInState: Array<string>, value: any): Array<FieldChangeObjectType> | ||
(rules: { fieldName: Array<FieldChangeEffectRuleType> }) => (state: Object) => (nameOfTheField: string, fieldPathInState: Array<string>, newValue: any): Array<FieldChangeObjectType> | ||
The function when provided with rules, state object, field's name, path and new value, returns the state of fields that have changed as an Array of objects of type `FieldChangeObjectType`. | ||
The function when provided with rules, state object, field's name, path and new value, returns the changed state of the fields that have changed as an Array of objects of type `FieldChangeObjectType`. | ||
type FieldChangeObjectType = { | ||
name: string, | ||
path: Array<string>, | ||
value: any, | ||
editable: boolean, | ||
readable: boolean, | ||
required: boolean, | ||
otherProps: { prop: any } | ||
name: string, | ||
path: Array<string>, | ||
props: { | ||
prop: any | ||
} | ||
} | ||
@@ -56,10 +62,5 @@ | ||
path: ['fields', 'B'], | ||
value: (ANewValue, state) => state.fields.B.value, | ||
editable: (ANewValue, state) => ANewValue === 'Hello' ? 'true' : 'false', | ||
readable: (ANewValue, state) => state.fields.B.readable, | ||
required: (ANewValue, state) => false, | ||
otherProps: (ANewValue, state) => ({ | ||
placeholder: ANewValue.toLowerCase(), | ||
label: ANewValue.toUpperCase() | ||
}) | ||
props: (ANewValue, state) => { | ||
editable: ANewValue === 'World' ? 'true' : 'false', | ||
} | ||
} | ||
@@ -69,3 +70,3 @@ ] | ||
If you want to write more complex rules like changing A changes B and C and changing B changes D, you can write it like this: | ||
If you want to write more complex rules like changing `A` changes `B` and `C` and changing `B` changes `D`, you can write it like this: | ||
@@ -77,9 +78,4 @@ var RULES = { | ||
path: ['fields', 'B'], | ||
value: (ANewValue, state) => state.fields.B.value, | ||
editable: (ANewValue, state) => ANewValue === 'Hello' ? 'true' : 'false', | ||
readable: (ANewValue, state) => state.fields.B.readable, | ||
required: (ANewValue, state) => false, | ||
otherProps: (ANewValue, state) => ({ | ||
placeholder: ANewValue.toLowerCase(), | ||
label: ANewValue.toUpperCase() | ||
props: (ANewValue, state) => ({ | ||
editable: ANewValue === 'World' ? 'true' : 'false', | ||
}) | ||
@@ -90,6 +86,5 @@ }, | ||
path: ['fields', 'C'], | ||
value: (ANewValue, state) => ANewValue.toLowerCase(), | ||
editable: (ANewValue, state) => true, | ||
readable: (ANewValue, state) => state.fields.C.readable, | ||
required: (ANewValue, state) => false | ||
props: (ANewValue, state) => ({ | ||
value: ANewValue.toLowerCase() | ||
}) | ||
} | ||
@@ -101,6 +96,6 @@ ], | ||
path: ['fields', 'D'], | ||
value: (BNewValue, state) => BNewValue.toLowerCase() + '*', | ||
editable: (BNewValue, state) => true, | ||
readable: (BNewValue, state) => state.fields.D.readable, | ||
required: (ANewValue, state) => false | ||
props: () => ({ | ||
value: BNewValue.toLowerCase() + '*', | ||
editable: true, | ||
}) | ||
} | ||
@@ -112,11 +107,7 @@ ] | ||
| Property Name | isRequired | Type | Description | | ||
|--|--|--|--| | ||
| name | true | `string` | Name of the field that will be used to uniquely identify the field. | | ||
| path | true | `Array<string>` | Path of the field where it resides in the state. | | ||
| value | true | `(newValue, state) => any` | Function that will be used to calculate new value for the field. | | ||
| editable | true | `(newValue, state) => boolean` | Function that will be used to calculate new editable value for the field. | | ||
| readable | true | `(newValue, state) => boolean` | Function that will be used to calculate new readable value for the field. | | ||
| required | true | `(newValue, state) => boolean` | Function that will be used to calculate new required value for the field. | | ||
| otherProps | false | `(newValue, state) => {}` | Function that will be used to calculate other props that do not change often like value, editable, readable etc, like label, placeholder, styles etc. | | ||
| Property Name | isRequired | Type | Description | | ||
| ------------- | ---------- | ------------------------- | ------------------------------------------------------------------------- | | ||
| name | `true` | `string` | Name of the field that will be used to uniquely identify the field. | | ||
| path | `true` | `Array<string>` | Path of the field where it resides in the state. | | ||
| props | `true` | `(newValue, state) => {}` | Function that will be used to calculate props/changed props of the field. | | ||
@@ -123,0 +114,0 @@ # Wanna Contribute? |
12912
178
115