final-form-calculate
Advanced tools
Comparing version 1.0.2 to 1.1.0
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
// | ||
@@ -20,21 +22,4 @@ var toPath = function toPath(key) { | ||
// | ||
// | ||
var getIn = function getIn(state, complexKey) { | ||
@@ -77,3 +62,5 @@ // Intentionally using iteration rather than recursion | ||
var tripleEquals$1 = function tripleEquals(a, b) { | ||
return a === b; | ||
}; | ||
var createDecorator = function createDecorator() { | ||
@@ -90,10 +77,17 @@ for (var _len = arguments.length, calculations = Array(_len), _key = 0; _key < _len; _key++) { | ||
form.batch(function () { | ||
var runUpdates = function runUpdates(field, updates) { | ||
var next = getIn(values, field); | ||
var previous = getIn(previousValues, field); | ||
if (next !== previous) { | ||
Object.keys(updates).forEach(function (destField) { | ||
var update = updates[destField]; | ||
form.change(destField, update(next, values)); | ||
}); | ||
var runUpdates = function runUpdates(field, isEqual, updates) { | ||
var next = values && getIn(values, field); | ||
var previous = previousValues && getIn(previousValues, field); | ||
if (!isEqual(next, previous)) { | ||
if (typeof updates === 'function') { | ||
var results = updates(next, field, values); | ||
Object.keys(results).forEach(function (destField) { | ||
form.change(destField, results[destField]); | ||
}); | ||
} else { | ||
Object.keys(updates).forEach(function (destField) { | ||
var update = updates[destField]; | ||
form.change(destField, update(next, values)); | ||
}); | ||
} | ||
} | ||
@@ -104,12 +98,17 @@ }; | ||
var field = _ref2.field, | ||
isEqual = _ref2.isEqual, | ||
updates = _ref2.updates; | ||
if (typeof field === 'string') { | ||
runUpdates(field, updates); | ||
runUpdates(field, isEqual || tripleEquals$1, updates); | ||
} else { | ||
// field is a regex | ||
var regex = field; | ||
// field is a either array or regex | ||
var matches = Array.isArray(field) ? function (name) { | ||
return ~field.indexOf(name); | ||
} : function (name) { | ||
return field.test(name); | ||
}; | ||
fields.forEach(function (fieldName) { | ||
if (regex.test(fieldName)) { | ||
runUpdates(fieldName, updates); | ||
if (matches(fieldName)) { | ||
runUpdates(fieldName, isEqual || tripleEquals$1, updates); | ||
} | ||
@@ -128,2 +127,2 @@ }); | ||
module.exports = createDecorator; | ||
exports.default = createDecorator; |
@@ -18,21 +18,4 @@ // | ||
// | ||
// | ||
var getIn = function getIn(state, complexKey) { | ||
@@ -75,3 +58,5 @@ // Intentionally using iteration rather than recursion | ||
var tripleEquals$1 = function tripleEquals(a, b) { | ||
return a === b; | ||
}; | ||
var createDecorator = function createDecorator() { | ||
@@ -88,10 +73,17 @@ for (var _len = arguments.length, calculations = Array(_len), _key = 0; _key < _len; _key++) { | ||
form.batch(function () { | ||
var runUpdates = function runUpdates(field, updates) { | ||
var next = getIn(values, field); | ||
var previous = getIn(previousValues, field); | ||
if (next !== previous) { | ||
Object.keys(updates).forEach(function (destField) { | ||
var update = updates[destField]; | ||
form.change(destField, update(next, values)); | ||
}); | ||
var runUpdates = function runUpdates(field, isEqual, updates) { | ||
var next = values && getIn(values, field); | ||
var previous = previousValues && getIn(previousValues, field); | ||
if (!isEqual(next, previous)) { | ||
if (typeof updates === 'function') { | ||
var results = updates(next, field, values); | ||
Object.keys(results).forEach(function (destField) { | ||
form.change(destField, results[destField]); | ||
}); | ||
} else { | ||
Object.keys(updates).forEach(function (destField) { | ||
var update = updates[destField]; | ||
form.change(destField, update(next, values)); | ||
}); | ||
} | ||
} | ||
@@ -102,12 +94,17 @@ }; | ||
var field = _ref2.field, | ||
isEqual = _ref2.isEqual, | ||
updates = _ref2.updates; | ||
if (typeof field === 'string') { | ||
runUpdates(field, updates); | ||
runUpdates(field, isEqual || tripleEquals$1, updates); | ||
} else { | ||
// field is a regex | ||
var regex = field; | ||
// field is a either array or regex | ||
var matches = Array.isArray(field) ? function (name) { | ||
return ~field.indexOf(name); | ||
} : function (name) { | ||
return field.test(name); | ||
}; | ||
fields.forEach(function (fieldName) { | ||
if (regex.test(fieldName)) { | ||
runUpdates(fieldName, updates); | ||
if (matches(fieldName)) { | ||
runUpdates(fieldName, isEqual || tripleEquals$1, updates); | ||
} | ||
@@ -126,2 +123,2 @@ }); | ||
export { createDecorator as default }; | ||
export default createDecorator; |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global['final-form-calculate'] = factory()); | ||
}(this, (function () { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global['final-form-calculate'] = {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
// | ||
var toPath = function toPath(key) { | ||
if (key === null || key === undefined) { | ||
return []; | ||
} | ||
if (typeof key !== 'string') { | ||
throw new Error('toPath() expects a string'); | ||
} | ||
return key.length ? key.split(/[.[\]]+/).filter(Boolean) : []; | ||
}; | ||
// | ||
var toPath = function toPath(key) { | ||
if (key === null || key === undefined) { | ||
return []; | ||
} | ||
if (typeof key !== 'string') { | ||
throw new Error('toPath() expects a string'); | ||
} | ||
return key.length ? key.split(/[.[\]]+/).filter(Boolean) : []; | ||
}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
// | ||
var getIn = function getIn(state, complexKey) { | ||
// Intentionally using iteration rather than recursion | ||
var path = toPath(complexKey); | ||
var current = state; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = path[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var key = _step.value; | ||
// | ||
var getIn = function getIn(state, complexKey) { | ||
// Intentionally using iteration rather than recursion | ||
var path = toPath(complexKey); | ||
var current = state; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = path[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var key = _step.value; | ||
if (current === undefined || current === null || (typeof current === 'undefined' ? 'undefined' : _typeof(current)) !== 'object' || Array.isArray(current) && isNaN(key)) { | ||
return undefined; | ||
if (current === undefined || current === null || (typeof current === 'undefined' ? 'undefined' : _typeof(current)) !== 'object' || Array.isArray(current) && isNaN(key)) { | ||
return undefined; | ||
} | ||
current = current[key]; | ||
} | ||
current = current[key]; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
return current; | ||
}; | ||
return current; | ||
}; | ||
// | ||
// | ||
var tripleEquals$1 = function tripleEquals(a, b) { | ||
return a === b; | ||
}; | ||
var createDecorator = function createDecorator() { | ||
for (var _len = arguments.length, calculations = Array(_len), _key = 0; _key < _len; _key++) { | ||
calculations[_key] = arguments[_key]; | ||
} | ||
var createDecorator = function createDecorator() { | ||
for (var _len = arguments.length, calculations = Array(_len), _key = 0; _key < _len; _key++) { | ||
calculations[_key] = arguments[_key]; | ||
} | ||
return function (form) { | ||
var previousValues = {}; | ||
var unsubscribe = form.subscribe(function (_ref) { | ||
var values = _ref.values; | ||
return function (form) { | ||
var previousValues = {}; | ||
var unsubscribe = form.subscribe(function (_ref) { | ||
var values = _ref.values; | ||
form.batch(function () { | ||
var runUpdates = function runUpdates(field, isEqual, updates) { | ||
var next = values && getIn(values, field); | ||
var previous = previousValues && getIn(previousValues, field); | ||
if (!isEqual(next, previous)) { | ||
if (typeof updates === 'function') { | ||
var results = updates(next, field, values); | ||
Object.keys(results).forEach(function (destField) { | ||
form.change(destField, results[destField]); | ||
}); | ||
} else { | ||
Object.keys(updates).forEach(function (destField) { | ||
var update = updates[destField]; | ||
form.change(destField, update(next, values)); | ||
}); | ||
} | ||
} | ||
}; | ||
var fields = form.getRegisteredFields(); | ||
calculations.forEach(function (_ref2) { | ||
var field = _ref2.field, | ||
isEqual = _ref2.isEqual, | ||
updates = _ref2.updates; | ||
form.batch(function () { | ||
var runUpdates = function runUpdates(field, updates) { | ||
var next = getIn(values, field); | ||
var previous = getIn(previousValues, field); | ||
if (next !== previous) { | ||
Object.keys(updates).forEach(function (destField) { | ||
var update = updates[destField]; | ||
form.change(destField, update(next, values)); | ||
}); | ||
} | ||
}; | ||
var fields = form.getRegisteredFields(); | ||
calculations.forEach(function (_ref2) { | ||
var field = _ref2.field, | ||
updates = _ref2.updates; | ||
if (typeof field === 'string') { | ||
runUpdates(field, updates); | ||
} else { | ||
// field is a regex | ||
var regex = field; | ||
fields.forEach(function (fieldName) { | ||
if (regex.test(fieldName)) { | ||
runUpdates(fieldName, updates); | ||
} | ||
}); | ||
} | ||
if (typeof field === 'string') { | ||
runUpdates(field, isEqual || tripleEquals$1, updates); | ||
} else { | ||
// field is a either array or regex | ||
var matches = Array.isArray(field) ? function (name) { | ||
return ~field.indexOf(name); | ||
} : function (name) { | ||
return field.test(name); | ||
}; | ||
fields.forEach(function (fieldName) { | ||
if (matches(fieldName)) { | ||
runUpdates(fieldName, isEqual || tripleEquals$1, updates); | ||
} | ||
}); | ||
} | ||
}); | ||
previousValues = values; | ||
}); | ||
previousValues = values; | ||
}); | ||
}, { values: true }); | ||
return unsubscribe; | ||
}, { values: true }); | ||
return unsubscribe; | ||
}; | ||
}; | ||
}; | ||
// | ||
// | ||
return createDecorator; | ||
exports.default = createDecorator; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
//# sourceMappingURL=final-form-calculate.umd.js.map |
@@ -1,2 +0,2 @@ | ||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):t["final-form-calculate"]=r()}(this,function(){"use strict";var t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r=function(r,e){var n=function(t){if(null===t||void 0===t)return[];if("string"!=typeof t)throw new Error("toPath() expects a string");return t.length?t.split(/[.[\]]+/).filter(Boolean):[]}(e),o=r,i=!0,f=!1,u=void 0;try{for(var a,c=n[Symbol.iterator]();!(i=(a=c.next()).done);i=!0){var l=a.value;if(void 0===o||null===o||"object"!==(void 0===o?"undefined":t(o))||Array.isArray(o)&&isNaN(l))return;o=o[l]}}catch(t){f=!0,u=t}finally{try{!i&&c.return&&c.return()}finally{if(f)throw u}}return o};return function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];return function(t){var n={};return t.subscribe(function(o){var i=o.values;t.batch(function(){var o=function(e,o){var f=r(i,e),u=r(n,e);f!==u&&Object.keys(o).forEach(function(r){var e=o[r];t.change(r,e(f,i))})},f=t.getRegisteredFields();e.forEach(function(t){var r=t.field,e=t.updates;if("string"==typeof r)o(r,e);else{var n=r;f.forEach(function(t){n.test(t)&&o(t,e)})}}),n=i})},{values:!0})}}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t["final-form-calculate"]={})}(this,function(t){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n=function(t,n){var r=function(t){if(null===t||void 0===t)return[];if("string"!=typeof t)throw new Error("toPath() expects a string");return t.length?t.split(/[.[\]]+/).filter(Boolean):[]}(n),o=t,f=!0,i=!1,u=void 0;try{for(var a,c=r[Symbol.iterator]();!(f=(a=c.next()).done);f=!0){var l=a.value;if(void 0===o||null===o||"object"!==(void 0===o?"undefined":e(o))||Array.isArray(o)&&isNaN(l))return;o=o[l]}}catch(t){i=!0,u=t}finally{try{!f&&c.return&&c.return()}finally{if(i)throw u}}return o},r=function(t,e){return t===e};t.default=function(){for(var t=arguments.length,e=Array(t),o=0;o<t;o++)e[o]=arguments[o];return function(t){var o={};return t.subscribe(function(f){var i=f.values;t.batch(function(){var f=function(e,r,f){var u=i&&n(i,e),a=o&&n(o,e);if(!r(u,a))if("function"==typeof f){var c=f(u,e,i);Object.keys(c).forEach(function(e){t.change(e,c[e])})}else Object.keys(f).forEach(function(e){var n=f[e];t.change(e,n(u,i))})},u=t.getRegisteredFields();e.forEach(function(t){var e=t.field,n=t.isEqual,o=t.updates;if("string"==typeof e)f(e,n||r,o);else{var i=Array.isArray(e)?function(t){return~e.indexOf(t)}:function(t){return e.test(t)};u.forEach(function(t){i(t)&&f(t,n||r,o)})}}),o=i})},{values:!0})}},Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=final-form-calculate.umd.min.js.map |
{ | ||
"name": "final-form-calculate", | ||
"version": "1.0.2", | ||
"description": "Decorator for calculating field values based on other field values in 🏁 Final Form", | ||
"version": "1.1.0", | ||
"description": | ||
"Decorator for calculating field values based on other field values in 🏁 Final Form", | ||
"main": "dist/final-form-calculate.cjs.js", | ||
"jsnext:main": "dist/final-form-calculate.es.js", | ||
"module": "dist/final-form-calculate.es.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"files": ["dist"], | ||
"scripts": { | ||
@@ -16,3 +15,4 @@ "start": "nps", | ||
}, | ||
"author": "Erik Rasmussen <rasmussenerik@gmail.com> (http://github.com/erikras)", | ||
"author": | ||
"Erik Rasmussen <rasmussenerik@gmail.com> (http://github.com/erikras)", | ||
"license": "MIT", | ||
@@ -28,4 +28,4 @@ "repository": { | ||
"devDependencies": { | ||
"babel-eslint": "^8.0.3", | ||
"babel-jest": "^22.0.4", | ||
"babel-eslint": "^8.2.2", | ||
"babel-jest": "^22.4.1", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
@@ -35,28 +35,29 @@ "babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-preset-stage-2": "^6.24.1", | ||
"bundlesize": "^0.15.3", | ||
"doctoc": "^1.3.0", | ||
"eslint": "^4.11.0", | ||
"eslint-config-react-app": "^2.0.1", | ||
"bundlesize": "^0.16.0", | ||
"doctoc": "^1.3.1", | ||
"eslint": "^4.19.0", | ||
"eslint-config-react-app": "^2.1.0", | ||
"eslint-plugin-babel": "^4.1.2", | ||
"eslint-plugin-flowtype": "^2.37.0", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-jsx-a11y": "^6.0.2", | ||
"eslint-plugin-react": "^7.5.1", | ||
"final-form": "^1.3.0", | ||
"eslint-plugin-flowtype": "^2.46.1", | ||
"eslint-plugin-import": "^2.9.0", | ||
"eslint-plugin-jsx-a11y": "^6.0.3", | ||
"eslint-plugin-react": "^7.7.0", | ||
"final-form": "^4.3.1", | ||
"flow": "^0.2.3", | ||
"flow-bin": "^0.60.1", | ||
"flow-bin": "^0.68.0", | ||
"glow": "^1.2.2", | ||
"husky": "^0.14.3", | ||
"jest": "^22.0.5", | ||
"lint-staged": "^6.0.0", | ||
"nps": "^5.7.1", | ||
"jest": "^22.4.2", | ||
"lint-staged": "^7.0.0", | ||
"nps": "^5.8.2", | ||
"nps-utils": "^1.5.0", | ||
"prettier": "^1.8.2", | ||
"prettier-eslint-cli": "^4.4.2", | ||
"rollup": "^0.53.4", | ||
"rollup-plugin-babel": "^3.0.2", | ||
"rollup-plugin-commonjs": "^8.2.6", | ||
"prettier": "^1.11.1", | ||
"prettier-eslint-cli": "^4.7.1", | ||
"rollup": "^0.57.1", | ||
"rollup-plugin-babel": "^3.0.3", | ||
"rollup-plugin-commonjs": "^9.1.0", | ||
"rollup-plugin-flow": "^1.1.1", | ||
"rollup-plugin-node-resolve": "^3.0.0", | ||
"rollup-plugin-node-resolve": "^3.3.0", | ||
"rollup-plugin-replace": "^2.0.0", | ||
"rollup-plugin-uglify": "^2.0.1" | ||
"rollup-plugin-uglify": "^3.0.0" | ||
}, | ||
@@ -67,6 +68,3 @@ "peerDependencies": { | ||
"lint-staged": { | ||
"*.{js,json,md,css}": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
"*.{js,json,md,css}": ["prettier --write", "git add"] | ||
}, | ||
@@ -73,0 +71,0 @@ "bundlesize": [ |
@@ -30,3 +30,3 @@ # 🏁 Final Form Calculate | ||
```js | ||
import { createForm } from 'final-form' | ||
import { createForm, getIn } from 'final-form' | ||
import createDecorator from 'final-form-calculate' | ||
@@ -42,8 +42,6 @@ | ||
field: 'foo', // when the value of foo changes... | ||
updates: [ | ||
{ | ||
// ...set field "doubleFoo" to twice the value of foo | ||
doubleFoo: (fooValue, allValues) => fooValue * 2 | ||
} | ||
] | ||
updates: { | ||
// ...set field "doubleFoo" to twice the value of foo | ||
doubleFoo: (fooValue, allValues) => fooValue * 2 | ||
} | ||
}, | ||
@@ -57,2 +55,16 @@ { | ||
} | ||
}, | ||
{ | ||
field: /\.timeFrom/, // when a deeper field matching this pattern changes... | ||
updates: (value, name, allValues) => { | ||
const toField = name.replace('timeFrom', 'timeTo') | ||
const toValue = getIn(allValues, toField) | ||
if (toValue && value > toValue) { | ||
return { | ||
[toField]: value | ||
} | ||
} | ||
return {} | ||
} | ||
} | ||
@@ -103,14 +115,22 @@ ) | ||
### `Calculation: { field: FieldPattern, updates: Updates }` | ||
### `Calculation: { field: FieldPattern, isEqual?: (any, any) => boolean, updates: Updates }` | ||
A calculation to perform | ||
A calculation to perform, with an optional `isEqual` predicate to determine if a value has really changed (defaults to `===`). | ||
### `FieldName: string` | ||
### `FieldPattern: FieldName | RegExp` | ||
### `FieldPattern: FieldName | RegExp | FieldName[]` | ||
A pattern to match a field with. | ||
### `Updates: { [FieldName]: (value: any, allValues: Object) => any }` | ||
### `Updates: UpdatesByName | UpdatesForAll` | ||
Updates to make on other fields. | ||
Either an object of updater functions or a function that generates updates for multiple fields. | ||
### `UpdatesByName: { [FieldName]: (value: any, allValues: Object) => any }` | ||
Updater functions for each calculated field. | ||
### `UpdatesForAll: (value: any, field: string, allValues: Object) => { [FieldName]: any }` | ||
Takes the value and name of the field that just changed, as well as all the values, and returns an object of fields and new values. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
22804
13
336
133
33