"use strict"; const assureArray = require("assure-array"); const isPlainObj = require("is-plain-obj"); const defaultValue = require("default-value"); const flatten = require("flatten"); const matchSpecial = require("@validatem/match-special"); const isPlainObjectValidator = require("@validatem/is-plain-object"); const hasShape = require("@validatem/has-shape"); // FIXME: Write an example.js module.exports = function normalizeRules(rules, options) { let normalizeObjects = defaultValue(options.normalizeObjects, false); // TODO: Switch to `Array#flat` once Node 10.x goes EOL (April 2021) let flattened = flatten(assureArray(rules)); let actualRules = flattened.filter((rule) => rule != null); if (normalizeObjects) { // TODO: Switch to `Array#flatmap` once Node 10.x goes EOL (April 2021) // TODO: Better input validation of some sort here let mapped = actualRules.map((rule) => { // TODO: Figure out why isPlainObj in the below line breaks within a component in Shayu. Something something cross-realm objects something? // if (isPlainObj(rule) && !matchSpecial(rule)) { if (typeof rule === "object" && !matchSpecial(rule)) { return [ isPlainObjectValidator, hasShape(rule) ]; } else { return rule; } }); return flatten(mapped); } else { return actualRules; } };