mobx-autoform
Version:
Ridiculously simple form state management with mobx
36 lines (35 loc) • 923 B
JavaScript
import _ from "lodash/fp.js";
import { reduceTreePost, treePath } from "./futil.js";
let tokenizePath = (path) => {
if (_.isNumber(path))
path = [_.toString(path)];
else if (_.isEmpty(path))
path = [];
else if (_.isString(path))
path = path.split(/\.(?![^\[]+\])/g);
return _.map(_.toString, path);
};
let safeJoinPaths = _.flow(
_.map(_.toString),
_.map((x) => x.includes(".") && !x.includes("[") ? `["${x}"]` : x),
_.join(".")
);
let gatherFormValues = (form) => reduceTreePost((x) => x[form.keys.fields])(
(tree, x, ...xs) => (
// Only walk leaf nodes
!_.isEmpty(x[form.keys.fields]) ? tree : _.set(treePath(x, ...xs), x.value, tree)
)
)({})(form);
class ValidationError extends Error {
name = "ValidationError";
constructor(message, errors) {
super(message);
this.cause = errors;
}
}
export {
ValidationError,
gatherFormValues,
safeJoinPaths,
tokenizePath
};