@shopify/react-form-state
Version:
Manage React forms tersely and type-safely with no magic
60 lines (54 loc) • 1.65 kB
JavaScript
import { objectWithoutProperties as _objectWithoutProperties, objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js';
export { default as isEqual } from 'fast-deep-equal';
const _excluded = ["value"];
function mapObject(input, mapper) {
return Object.entries(input).reduce((accumulator, [key, value]) => {
accumulator[key] = mapper(value, key);
return accumulator;
}, {});
}
function push(array, ...values) {
return array.concat(values);
}
function remove(array, targetIndex) {
return array.filter((_, index) => index !== targetIndex);
}
function replace(array, targetIndex, newValue) {
return array.map((value, index) => {
if (index !== targetIndex) {
return value;
}
return newValue;
});
}
function set(rootObject, path, value) {
if (path.length === 0) {
return rootObject;
} else if (path.length === 1) {
return _objectSpread2(_objectSpread2({}, rootObject), {}, {
[path[0]]: value
});
} else {
const [current, ...rest] = path;
return _objectSpread2(_objectSpread2({}, rootObject), {}, {
[current]: set(rootObject[current] || {}, rest, value)
});
}
}
function flatMap(array, mapper) {
return array.reduce((acc, item, index) => acc.concat(mapper(item, index)), []);
}
/**
* Transforms a boolean FieldDescriptor object to work with checkboxes and radios.
* @param field
*/
function asChoiceField(_ref) {
let {
value: checked
} = _ref,
fieldData = _objectWithoutProperties(_ref, _excluded);
return _objectSpread2({
checked
}, fieldData);
}
export { asChoiceField, flatMap, mapObject, push, remove, replace, set };