Replace flat/flatMap implementation to reduce bundle size

master
Sven Slootweg 5 years ago
parent d19a2a5766
commit 295aee347e

@ -3,8 +3,7 @@
const assureArray = require("assure-array"); const assureArray = require("assure-array");
const isPlainObj = require("is-plain-obj"); const isPlainObj = require("is-plain-obj");
const defaultValue = require("default-value"); const defaultValue = require("default-value");
const flat = require("array.prototype.flat"); const flatten = require("flatten");
const flatMap = require("array.prototype.flatmap");
const matchSpecial = require("@validatem/match-special"); const matchSpecial = require("@validatem/match-special");
const isPlainObjectValidator = require("@validatem/is-plain-object"); const isPlainObjectValidator = require("@validatem/is-plain-object");
@ -15,12 +14,13 @@ const hasShape = require("@validatem/has-shape");
module.exports = function normalizeRules(rules, options) { module.exports = function normalizeRules(rules, options) {
let normalizeObjects = defaultValue(options.normalizeObjects, false); let normalizeObjects = defaultValue(options.normalizeObjects, false);
// TODO: Switch to `Array#flat` once it is available // TODO: Switch to `Array#flat` once Node 10.x goes EOL (April 2021)
let flattened = flat(assureArray(rules)); let flattened = flatten(assureArray(rules));
let actualRules = flattened.filter((rule) => rule != null); let actualRules = flattened.filter((rule) => rule != null);
if (normalizeObjects) { if (normalizeObjects) {
return flatMap(actualRules, (rule) => { // TODO: Switch to `Array#flatmap` once Node 10.x goes EOL (April 2021)
let mapped = actualRules.map((rule) => {
if (isPlainObj(rule) && !matchSpecial(rule)) { if (isPlainObj(rule) && !matchSpecial(rule)) {
return [ return [
isPlainObjectValidator, isPlainObjectValidator,
@ -30,6 +30,8 @@ module.exports = function normalizeRules(rules, options) {
return rule; return rule;
} }
}); });
return flatten(mapped);
} else { } else {
return actualRules; return actualRules;
} }

@ -10,10 +10,9 @@
"@validatem/has-shape": "^0.1.0", "@validatem/has-shape": "^0.1.0",
"@validatem/is-plain-object": "^0.1.0", "@validatem/is-plain-object": "^0.1.0",
"@validatem/match-special": "^0.1.0", "@validatem/match-special": "^0.1.0",
"array.prototype.flat": "^1.2.3",
"array.prototype.flatmap": "^1.2.3",
"assure-array": "^1.0.0", "assure-array": "^1.0.0",
"default-value": "^1.0.0", "default-value": "^1.0.0",
"flatten": "^1.0.3",
"is-plain-obj": "^2.1.0" "is-plain-obj": "^2.1.0"
}, },
"devDependencies": { "devDependencies": {

@ -164,15 +164,6 @@ array.prototype.flat@^1.2.3:
define-properties "^1.1.3" define-properties "^1.1.3"
es-abstract "^1.17.0-next.1" es-abstract "^1.17.0-next.1"
array.prototype.flatmap@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/array.prototype.flatmap/-/array.prototype.flatmap-1.2.3.tgz#1c13f84a178566042dd63de4414440db9222e443"
integrity sha512-OOEk+lkePcg+ODXIpvuU9PAryCikCJyo7GlDG1upleEpQRx6mzL9puEBkozQ5iAx20KV0l3DbyQwqciJtqe5Pg==
dependencies:
define-properties "^1.1.3"
es-abstract "^1.17.0-next.1"
function-bind "^1.1.1"
as-expression@^1.0.0: as-expression@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/as-expression/-/as-expression-1.0.0.tgz#7bc620ca4cb2fe0ee90d86729bd6add33b8fd831" resolved "https://registry.yarnpkg.com/as-expression/-/as-expression-1.0.0.tgz#7bc620ca4cb2fe0ee90d86729bd6add33b8fd831"
@ -522,6 +513,11 @@ flatted@^2.0.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.2.tgz#4575b21e2bcee7434aa9be662f4b7b5f9c2b5138" resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.2.tgz#4575b21e2bcee7434aa9be662f4b7b5f9c2b5138"
integrity sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA== integrity sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA==
flatten@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.3.tgz#c1283ac9f27b368abc1e36d1ff7b04501a30356b"
integrity sha512-dVsPA/UwQ8+2uoFe5GHtiBMu48dWLTdsuEd7CKGlZlD78r1TTWBvDuFaFGKCo/ZfEr95Uk56vZoX86OsHkUeIg==
fs.realpath@^1.0.0: fs.realpath@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"

Loading…
Cancel
Save