@shopify/react-form-state
Version:
Manage react forms tersely and type-safe with no magic.
53 lines (52 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var fast_deep_equal_1 = tslib_1.__importDefault(require("fast-deep-equal"));
exports.isEqual = fast_deep_equal_1.default;
function mapObject(input, mapper) {
return Object.entries(input).reduce(function (accumulator, _a) {
var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1];
accumulator[key] = mapper(value, key);
return accumulator;
}, {});
}
exports.mapObject = mapObject;
function push(array) {
var values = [];
for (var _i = 1; _i < arguments.length; _i++) {
values[_i - 1] = arguments[_i];
}
return array.concat(values);
}
exports.push = push;
function remove(array, targetIndex) {
return array.filter(function (_, index) { return index !== targetIndex; });
}
exports.remove = remove;
function replace(array, targetIndex, newValue) {
return array.map(function (value, index) {
if (index !== targetIndex) {
return value;
}
return newValue;
});
}
exports.replace = replace;
function set(rootObject, path, value) {
var _a, _b;
if (path.length === 0) {
return rootObject;
}
else if (path.length === 1) {
return tslib_1.__assign({}, rootObject, (_a = {}, _a[path[0]] = value, _a));
}
else {
var _c = tslib_1.__read(path), current = _c[0], rest = _c.slice(1);
return tslib_1.__assign({}, rootObject, (_b = {}, _b[current] = set(rootObject[current] || {}, rest, value), _b));
}
}
exports.set = set;
function flatMap(array, mapper) {
return array.reduce(function (acc, item, index) { return acc.concat(mapper(item, index)); }, []);
}
exports.flatMap = flatMap;