frontend/.pnpm-store/v3/files/1e/83322a4c8f210cef23115e6167bccbb0abccf809ca51c8806033ca1f4f73351674d721e48c6f364fafb6ce80c234f10cc25ed7d702dcf59133c79c03f87f10

44 lines
1.5 KiB
Plaintext

"use strict";
var resolveException = require("../lib/resolve-exception")
, resolveErrorMessage = require("../lib/resolve-error-message")
, toShortString = require("../lib/to-short-string")
, ensurePlainFunction = require("../plain-function/ensure")
, is = require("./is");
var objHasOwnProperty = Object.prototype.hasOwnProperty, invalidItemsLimit = 3;
module.exports = function (value /*, options*/) {
var options = arguments[1];
var mainErrorMessage =
options && options.name ? "Expected an array for %n, received %v" : "%v is not an array";
if (!is(value)) return resolveException(value, mainErrorMessage, options);
if (!options) return value;
var ensureItem = ensurePlainFunction(options.ensureItem, { isOptional: true });
if (ensureItem) {
var coercedValue = [], invalidItems;
for (var index = 0, length = value.length; index < length; ++index) {
if (!objHasOwnProperty.call(value, index)) continue;
var coercedItem;
try {
coercedItem = ensureItem(value[index]);
} catch (error) {
if (!invalidItems) invalidItems = [];
if (invalidItems.push(toShortString(value[index])) === invalidItemsLimit) break;
}
if (invalidItems) continue;
coercedValue[index] = coercedItem;
}
if (invalidItems) {
throw new TypeError(
resolveErrorMessage(mainErrorMessage, value, options) +
".\n Following items are invalid: " +
invalidItems.join(", ")
);
}
return coercedValue;
}
return value;
};