@shopify/react-form-state
Version:
Manage React forms tersely and type-safely with no magic
80 lines (70 loc) • 2.24 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
var isEqual = require('fast-deep-equal');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
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 _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, rootObject), {}, {
[path[0]]: value
});
} else {
const [current, ...rest] = path;
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.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 = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
return _rollupPluginBabelHelpers.objectSpread2({
checked
}, fieldData);
}
Object.defineProperty(exports, 'isEqual', {
enumerable: true,
get: function () {
return isEqual__default['default'];
}
});
exports.asChoiceField = asChoiceField;
exports.flatMap = flatMap;
exports.mapObject = mapObject;
exports.push = push;
exports.remove = remove;
exports.replace = replace;
exports.set = set;