Comparing version 2.1.0 to 2.2.0
"use strict"; | ||
var resolveException = require("../lib/resolve-exception") | ||
, is = require("./is"); | ||
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"); | ||
module.exports = function (value/*, options*/) { | ||
if (is(value)) return value; | ||
var objHasOwnProperty = Object.prototype.hasOwnProperty, invalidItemsLimit = 3; | ||
module.exports = function (value /*, options*/) { | ||
var options = arguments[1]; | ||
var errorMessage = | ||
var mainErrorMessage = | ||
options && options.name ? "Expected an array for %n, received %v" : "%v is not an array"; | ||
return resolveException(value, errorMessage, options); | ||
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; | ||
}; |
@@ -5,2 +5,8 @@ # Changelog | ||
## [2.2.0](https://github.com/medikoo/type/compare/v2.1.0...v2.2.0) (2021-02-11) | ||
### Features | ||
- Support `ensureItem` option in `array/ensure` ([8f74973](https://github.com/medikoo/type/commit/8f749739df9bfebf44087093e09c8f7341a33a09)) | ||
## [2.1.0](https://github.com/medikoo/type/compare/v2.0.0...v2.1.0) (2020-08-21) | ||
@@ -7,0 +13,0 @@ |
@@ -26,3 +26,3 @@ # Array length | ||
ensureArrayLength(12.93); // "12" | ||
ensureArrayLength(9007199254740992); // Thrown TypeError: null is not a valid array length | ||
ensureArrayLength(9007199254740992); // Thrown TypeError: 9007199254740992 is not a valid array length | ||
``` |
@@ -27,1 +27,21 @@ # Array | ||
``` | ||
### Confirming on items | ||
Items can be validated by passing `ensureItem` option. Note that in this case: | ||
- A newly created instance of an array with coerced item values is returned | ||
- Error message lists up to three items which are invalid | ||
```javascript | ||
const ensureString = require("type/string/ensure"); | ||
ensureArray([12], { ensureItem: ensureString }); // ["12"] | ||
/* | ||
Below invocation with crash with: | ||
TypeError: 23, [object Object], [object Object] is not a valid array. | ||
Following items are invalid: [object Object], [object Object] | ||
*/ | ||
ensureArray([23, {}, {}], { ensureItem: ensureString }); | ||
``` |
{ | ||
"name": "type", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Runtime validation and processing of JavaScript types", | ||
@@ -12,11 +12,11 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (https://www.medikoo.com/)", | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"eslint": "^7.7.0", | ||
"eslint-config-medikoo": "^3.1.0", | ||
"chai": "^4.3.0", | ||
"eslint": "^7.19.0", | ||
"eslint-config-medikoo": "^4.0.0", | ||
"git-list-updated": "^1.2.1", | ||
"husky": "^4.2.5", | ||
"lint-staged": "^10.2.11", | ||
"husky": "^5.0.9", | ||
"lint-staged": "^10.5.4", | ||
"mocha": "^6.2.3", | ||
"nyc": "^15.1.0", | ||
"prettier-elastic": "^1.19.1" | ||
"prettier-elastic": "^2.1.2" | ||
}, | ||
@@ -23,0 +23,0 @@ "husky": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64713
77
844
1