UNPKG

react-form-controlled

Version:

Intuitive react forms for building powerful applications

49 lines (37 loc) 1.27 kB
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; }; import isArray from 'lodash/isArray'; import isPlainObject from 'lodash/isPlainObject'; export function normalizePath(originalPath) { const path = String(originalPath); return path.replace(/\[/g, '.').replace(/\]/g, ''); } export function dirty(value) { if (isArray(value)) { return [...value]; } if (isPlainObject(value)) { return _extends({}, value); } return value; } export default function markAsDirty(value, originalPath, updateCallback) { const start = dirty(value); const path = normalizePath(originalPath); const parts = path.split('.'); let current = start; let before; parts.forEach(key => { current[key] = dirty(current[key]); before = current; current = current[key]; }); if (updateCallback) { const newValue = updateCallback(current); if (before !== undefined && parts.length) { const lastKey = parts[parts.length - 1]; before[lastKey] = newValue; } } return start; } //# sourceMappingURL=markAsDirty.js.map