pax_global_header00006660000000000000000000000064135476516330014527gustar00rootroot0000000000000052 comment=8912d712e6225cff8b4fd94f984d9afd9b76cee1 type-2.0.0/000077500000000000000000000000001354765163300125075ustar00rootroot00000000000000type-2.0.0/.editorconfig000066400000000000000000000004401354765163300151620ustar00rootroot00000000000000# EditorConfig is awesome: http://EditorConfig.org # top-most EditorConfig file root = true [*] charset = utf-8 end_of_line = lf insert_final_newline = true indent_style = tab trim_trailing_whitespace = true [*.md] indent_size = 2 indent_style = space trim_trailing_whitespace = false type-2.0.0/.gitignore000066400000000000000000000001061354765163300144740ustar00rootroot00000000000000/.nyc_output /coverage /node_modules npm-debug.log /package-lock.json type-2.0.0/CHANGELOG.md000066400000000000000000000134141354765163300143230ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. ## [2.0.0](https://github.com/medikoo/type/compare/v1.2.0...v2.0.0) (2019-10-10) ### Features - `allowedKeys` option for plain-object/ensure ([f81e72e](https://github.com/medikoo/type/commit/f81e72e)) - `ensurePropertyValue` option for plain-object/ensure ([c5ff8fb](https://github.com/medikoo/type/commit/c5ff8fb)) - Replace `coerceItem` with `ensureItem` option in iterable/ensure ([721494f](https://github.com/medikoo/type/commit/721494f)) - Seclude lib/resolve-error-message ([12636d9](https://github.com/medikoo/type/commit/12636d9)) - Validate options.ensureItem in iterable/ensure ([78da6c1](https://github.com/medikoo/type/commit/78da6c1)) ### BREAKING CHANGES - iterable/ensure no longer supports `coerceItem` option. Instead `ensureItem` was introduced ## [1.2.0](https://github.com/medikoo/type/compare/v1.1.0...v1.2.0) (2019-09-20) ### Bug Fixes - Improve error message so it's not confusing ([97cd6b9](https://github.com/medikoo/type/commit/97cd6b9)) ### Features - 'coerceItem' option for iterable/ensure ([0818860](https://github.com/medikoo/type/commit/0818860)) ## [1.1.0](https://github.com/medikoo/type/compare/v1.0.3...v1.1.0) (2019-09-20) ### Features - `denyEmpty` option for iterables validation ([301d071](https://github.com/medikoo/type/commit/301d071)) ### [1.0.3](https://github.com/medikoo/type/compare/v1.0.2...v1.0.3) (2019-08-06) ### Bug Fixes - Recognize custom built ES5 era errors ([6462fac](https://github.com/medikoo/type/commit/6462fac)) ### [1.0.2](https://github.com/medikoo/type/compare/v1.0.1...v1.0.2) (2019-08-06) ### Bug Fixes - Recognize host errors (e.g. DOMException) ([96ef399](https://github.com/medikoo/type/commit/96ef399)) ## [1.0.1](https://github.com/medikoo/type/compare/v1.0.0...v1.0.1) (2019-04-08) # 1.0.0 (2019-04-05) ### Bug Fixes - ensure 'is' functions can't crash ([59ceb78](https://github.com/medikoo/type/commit/59ceb78)) ### Features - array-length/coerce ([af8ddec](https://github.com/medikoo/type/commit/af8ddec)) - array-length/ensure ([d313eb6](https://github.com/medikoo/type/commit/d313eb6)) - array-like/ensure ([45f1ddd](https://github.com/medikoo/type/commit/45f1ddd)) - array-like/is ([9a026a5](https://github.com/medikoo/type/commit/9a026a5)) - array/ensure ([9db1515](https://github.com/medikoo/type/commit/9db1515)) - array/is ([9672839](https://github.com/medikoo/type/commit/9672839)) - date/ensure ([44e25a0](https://github.com/medikoo/type/commit/44e25a0)) - date/is ([0316558](https://github.com/medikoo/type/commit/0316558)) - ensure to not crash ([3998348](https://github.com/medikoo/type/commit/3998348)) - ensure/number ([134b5cb](https://github.com/medikoo/type/commit/134b5cb)) - error/ensure ([d5c8a30](https://github.com/medikoo/type/commit/d5c8a30)) - error/is-error ([4d6b899](https://github.com/medikoo/type/commit/4d6b899)) - finite/coerce ([accaad1](https://github.com/medikoo/type/commit/accaad1)) - finite/ensure ([51e4174](https://github.com/medikoo/type/commit/51e4174)) - function/ensure ([b624c9a](https://github.com/medikoo/type/commit/b624c9a)) - function/is ([dab8026](https://github.com/medikoo/type/commit/dab8026)) - integer/coerce ([89dea2e](https://github.com/medikoo/type/commit/89dea2e)) - integer/ensure ([44a7071](https://github.com/medikoo/type/commit/44a7071)) - iterable/ensure ([3d48841](https://github.com/medikoo/type/commit/3d48841)) - iterable/is ([cf09513](https://github.com/medikoo/type/commit/cf09513)) - lib/is-to-string-tag-supported ([c8c001d](https://github.com/medikoo/type/commit/c8c001d)) - natural-number/coerce ([d08fdd9](https://github.com/medikoo/type/commit/d08fdd9)) - natural-number/ensure ([6c24d12](https://github.com/medikoo/type/commit/6c24d12)) - number/coerce ([86ccf08](https://github.com/medikoo/type/commit/86ccf08)) - object/ensure ([a9e8eed](https://github.com/medikoo/type/commit/a9e8eed)) - object/is ([d2d7251](https://github.com/medikoo/type/commit/d2d7251)) - plain-function/ensure ([5186518](https://github.com/medikoo/type/commit/5186518)) - plain-function/is ([51bc791](https://github.com/medikoo/type/commit/51bc791)) - plain-object/ensure ([91cf5e5](https://github.com/medikoo/type/commit/91cf5e5)) - plain-object/is ([4dcf393](https://github.com/medikoo/type/commit/4dcf393)) - promise/ensure ([8d096a4](https://github.com/medikoo/type/commit/8d096a4)) - promise/is ([a00de02](https://github.com/medikoo/type/commit/a00de02)) - prototype/is ([b23bdcc](https://github.com/medikoo/type/commit/b23bdcc)) - reg-exp/ensure ([6f7bbcb](https://github.com/medikoo/type/commit/6f7bbcb)) - reg-exp/is ([9728519](https://github.com/medikoo/type/commit/9728519)) - safe-integer/coerce ([b8549c4](https://github.com/medikoo/type/commit/b8549c4)) - safe-integer/ensure ([a70ef3f](https://github.com/medikoo/type/commit/a70ef3f)) - string/coerce ([b25c71f](https://github.com/medikoo/type/commit/b25c71f)) - string/ensure ([b62577d](https://github.com/medikoo/type/commit/b62577d)) - support 'default' in resolveException ([e08332a](https://github.com/medikoo/type/commit/e08332a)) - switch config to ES3 based ([37606d9](https://github.com/medikoo/type/commit/37606d9)) - thenable/ensure ([6762c0d](https://github.com/medikoo/type/commit/6762c0d)) - thenable/is ([2711d70](https://github.com/medikoo/type/commit/2711d70)) - time-value/coerce ([27fd109](https://github.com/medikoo/type/commit/27fd109)) - time-value/ensure ([1f6a8ea](https://github.com/medikoo/type/commit/1f6a8ea)) - **string/coerce:** restrict toString acceptance ([2a87100](https://github.com/medikoo/type/commit/2a87100)) - value/ensure ([dd6d8cb](https://github.com/medikoo/type/commit/dd6d8cb)) - value/is ([fdf4763](https://github.com/medikoo/type/commit/fdf4763)) type-2.0.0/LICENSE000066400000000000000000000014001354765163300135070ustar00rootroot00000000000000ISC License Copyright (c) 2019, Mariusz Nowak, @medikoo, medikoo.com Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. type-2.0.0/README.md000066400000000000000000000466521354765163300140030ustar00rootroot00000000000000[![*nix build status][nix-build-image]][nix-build-url] [![Windows build status][win-build-image]][win-build-url] [![Tests coverage][cov-image]][cov-url] [![npm version][npm-image]][npm-url] # type ## Runtime validation and processing of JavaScript types - Respects language nature and acknowledges its quirks - Allows coercion in restricted forms (rejects clearly invalid input, normalizes permissible type deviations) - No transpilation implied, written to work in all ECMAScript 3+ engines ### Example usage Bulletproof input arguments normalization and validation: ```javascript const ensureString = require('type/string/ensure') , ensureDate = require('type/date/ensure') , ensureNaturalNumber = require('type/natural-number/ensure') , isObject = require('type/object/is'); module.exports = (path, options = { min: 0 }) { path = ensureString(path, { errorMessage: "%v is not a path" }); if (!isObject(options)) options = {}; const min = ensureNaturalNumber(options.min, { default: 0 }) , max = ensureNaturalNumber(options.max, { isOptional: true }) , startTime = ensureDate(options.startTime, { isOptional: true }); // ...logic }; ``` ### Installation ```bash npm install type ``` ## Utilities Serves following kind of utilities: ##### `*/coerce` Restricted coercion into primitive type. Returns coerced value or `null` if value is not coercible per rules. ##### `*/is` Object type/kind confirmation, returns either `true` or `false`. ##### `*/ensure` Value validation. Returns input value (in primitive cases possibly coerced) or if value doesn't meet the constraints throws `TypeError` . Each `*/ensure` utility, accepts following options (eventually passed with second argument): - `isOptional` - Makes `null` or `undefined` accepted as valid value. In such case instead of `TypeError` being thrown, `null` is returned. - `default` - A value to be returned if `null` or `undefined` is passed as an input value. - `errorMessage` - Custom error message (`%v` can be used as a placeholder for input value) --- ### Value _Value_, any value that's neither `null` nor `undefined` . #### `value/is` Confirms whether passed argument is a _value_ ```javascript const isValue = require("type/value/is"); isValue({}); // true isValue(null); // false ``` #### `value/ensure` Ensures if given argument is a _value_. If it's a value it is returned back, if not `TypeError` is thrown ```javascript const ensureValue = require("type/value/ensure"); const obj = {}; ensureValue(obj); // obj ensureValue(null); // Thrown TypeError: Cannot use null ``` --- ### Object _Object_, any non-primitive value #### `object/is` Confirms if passed value is an object ```javascript const isObject = require("type/object/is"); isObject({}); // true isObject(true); // false isObject(null); // false ``` #### `object/ensure` If given argument is an object, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensureObject = require("type/object/ensure"); const obj = {}; ensureObject(obj); // obj ensureString(null); // Thrown TypeError: null is not an object ``` --- ### String _string_ primitive #### `string/coerce` Restricted string coercion. Returns string presentation for every value that follows below constraints - is implicitly coercible to string - is neither`null` nor `undefined` - its `toString` method is not `Object.prototype.toString` For all other values `null` is returned ```javascript const coerceToString = require("type/string/coerce"); coerceToString(12); // "12" coerceToString(undefined); // null ``` #### `string/ensure` If given argument is a string coercible value (via [`string/coerce`](#stringcoerce)) returns result string. Otherwise `TypeError` is thrown. ```javascript const ensureString = require("type/string/ensure"); ensureString(12); // "12" ensureString(null); // Thrown TypeError: null is not a string ``` --- ### Number _number_ primitive #### `number/coerce` Restricted number coercion. Returns number presentation for every value that follows below constraints - is implicitly coercible to number - is neither `null` nor `undefined` - is not `NaN` and doesn't coerce to `NaN` For all other values `null` is returned ```javascript const coerceToNumber = require("type/number/coerce"); coerceToNumber("12"); // 12 coerceToNumber({}); // null coerceToNumber(null); // null ``` #### `number/ensure` If given argument is a number coercible value (via [`number/coerce`](#numbercoerce)) returns result number. Otherwise `TypeError` is thrown. ```javascript const ensureNumber = require("type/number/ensure"); ensureNumber(12); // "12" ensureNumber(null); // Thrown TypeError: null is not a number ``` --- #### Finite Number Finite _number_ primitive ##### `finite/coerce` Follows [`number/coerce`](#numbercoerce) additionally rejecting `Infinity` and `-Infinity` values (`null` is returned if given values coerces to them) ```javascript const coerceToFinite = require("type/finite/coerce"); coerceToFinite("12"); // 12 coerceToFinite(Infinity); // null coerceToFinite(null); // null ``` ##### `finite/ensure` If given argument is a finite number coercible value (via [`finite/coerce`](#finitecoerce)) returns result number. Otherwise `TypeError` is thrown. ```javascript const ensureFinite = require("type/finite/ensure"); ensureFinite(12); // "12" ensureFinite(null); // Thrown TypeError: null is not a finite number ``` --- #### Integer Number Integer _number_ primitive ##### `integer/coerce` Follows [`finite/coerce`](#finitecoerce) additionally stripping decimal part from the number ```javascript const coerceToInteger = require("type/integer/coerce"); coerceToInteger("12.95"); // 12 coerceToInteger(Infinity); // null coerceToInteger(null); // null ``` ##### `integer/ensure` If given argument is an integer coercible value (via [`integer/coerce`](#integercoerce)) returns result number. Otherwise `TypeError` is thrown. ```javascript const ensureInteger = require("type/integer/ensure"); ensureInteger(12.93); // "12" ensureInteger(null); // Thrown TypeError: null is not an integer ``` --- #### Safe Integer Number Safe integer _number_ primitive ##### `safe-integer/coerce` Follows [`integer/coerce`](#integercoerce) but returns `null` in place of values which are beyond `Number.MIN_SAFE_INTEGER` and `Number.MAX_SAFE_INTEGER` range. ```javascript const coerceToSafeInteger = require("type/safe-integer/coerce"); coerceToInteger("12.95"); // 12 coerceToInteger(9007199254740992); // null coerceToInteger(null); // null ``` ##### `safe-integer/ensure` If given argument is a safe integer coercible value (via [`safe-integer/coerce`](#safe-integercoerce)) returns result number. Otherwise `TypeError` is thrown. ```javascript const ensureSafeInteger = require("type/safe-integer/ensure"); ensureSafeInteger(12.93); // "12" ensureSafeInteger(9007199254740992); // Thrown TypeError: null is not a safe integer ``` --- #### Natural Number Natural _number_ primitive ##### `natural-number/coerce` Follows [`integer/coerce`](#integercoerce) but returns `null` for values below `0` ```javascript const coerceToNaturalNumber = require("type/natural-number/coerce"); coerceToNaturalNumber("12.95"); // 12 coerceToNaturalNumber(-120); // null coerceToNaturalNumber(null); // null ``` ##### `natural-number/ensure` If given argument is a natural number coercible value (via [`natural-number/coerce`](#natural-numbercoerce)) returns result number. Otherwise `TypeError` is thrown. ```javascript const ensureNaturalNumber = require("type/natural-number/ensure"); ensureNaturalNumber(12.93); // "12" ensureNaturalNumber(-230); // Thrown TypeError: null is not a natural number ``` --- ### Plain Object A _plain object_ - Inherits directly from `Object.prototype` or `null` - Is not a constructor's `prototype` property #### `plain-object/is` Confirms if given object is a _plain object_ ```javascript const isPlainObject = require("type/plain-object/is"); isPlainObject({}); // true isPlainObject(Object.create(null)); // true isPlainObject([]); // false ``` #### `plain-object/ensure` If given argument is a plain object it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensurePlainObject = require("type/plain-object/ensure"); ensurePlainObject({}); // {} ensurePlainObject("foo"); // Thrown TypeError: foo is not a plain object ``` ##### Confirming on keys Keys can be validated by passing `allowedKeys` option. Note that in this case: - Error message lists up to three invalid keys ```javascript const allowedKeys = ["foo"]; ensurePlainObject({}, { allowedKeys }); // {} ensurePlainObject({ foo: "bar" }, { allowedKeys }); // { foo: 'bar' } /* Below invocation with crash with: TypeError: [object Object] is not a valid plain object. Following keys are unexpected: lorem, ipsum */ ensurePlainObject({ foo: "bar", lorem: 1, ipsum: 2 }, { allowedKeys }); ``` ##### Confirming on property values Property values can be validated by passing `ensurePropertyValue` option. Note that in this case: - A newly created instance of plain object with coerced values is returned - Error message lists up to three keys that contain invalid values ```javascript const ensureString = require("type/string/ensure"); ensurePlainObject({ foo: 12 }, { ensurePropertyValue: ensureString }); // { foo: '12' } /* Below invocation with crash with: TypeError: [object Object] is not a valid plain object. Valuees for following keys are invalid: lorem, ipsum */ ensurePlainObject({ foo: 23, lorem: {}, ipsum: {} }, { ensurePropertyValue: ensureString }); ``` --- ### Array _Array_ instance #### `array/is` Confirms if given object is a native array ```javascript const isArray = require("type/array/is"); isArray([]); // true isArray({}); // false isArray("foo"); // false ``` #### `array/ensure` If given argument is an array, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensureArray = require("type/array/ensure"); ensureArray(["foo"]); // ["foo"] ensureArray("foo"); // Thrown TypeError: foo is not an array ``` --- #### Array Like _Array-like_ value (any value with `length` property) #### `array-like/is` Restricted _array-like_ confirmation. Returns true for every value that meets following contraints - is an _object_ (or with `allowString` option, a _string_) - is not a _function_ - Exposes `length` that meets [`array-length`](#array-lengthcoerce) constraints ```javascript const isArrayLike = require("type/array-like/is"); isArrayLike([]); // true isArrayLike({}); // false isArrayLike({ length: 0 }); // true isArrayLike("foo"); // false isArrayLike("foo", { allowString: true }); // true ``` #### `array-like/ensure` If given argument is an _array-like_, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensureArrayLike = require("type/array-like/ensure"); ensureArrayLike({ length: 0 }); // { length: 0 } ensureArrayLike("foo", { allowString: true }); // "foo" ensureArrayLike({}); // Thrown TypeError: null is not an iterable ``` --- #### Array length _number_ primitive that conforms as valid _array length_ ##### `array-length/coerce` Follows [`safe-integer/coerce`](#safe-integercoerce) but returns `null` in place of values which are below `0` ```javascript const coerceToArrayLength = require("type/safe-integer/coerce"); coerceToArrayLength("12.95"); // 12 coerceToArrayLength(9007199254740992); // null coerceToArrayLength(null); // null ``` ##### `array-length/ensure` If given argument is an _array length_ coercible value (via [`array-length/coerce`](#array-lengthcoerce)) returns result number. Otherwise `TypeError` is thrown. ```javascript const ensureArrayLength = require("type/array-length/ensure"); ensureArrayLength(12.93); // "12" ensureArrayLength(9007199254740992); // Thrown TypeError: null is not a valid array length ``` --- ### Iterable Value which implements _iterable_ protocol #### `iterable/is` Confirms if given object is an _iterable_ and is not a _string_ (unless `allowString` option is passed) ```javascript const isIterable = require("type/iterable/is"); isIterable([]); // true isIterable({}); // false isIterable("foo"); // false isIterable("foo", { allowString: true }); // true ``` Supports also `denyEmpty` option ```javascript isIterable([], { denyEmpty: true }); // false isIterable(["foo"], { denyEmpty: true }); // true ``` #### `iterable/ensure` If given argument is an _iterable_, it is returned back. Otherwise `TypeError` is thrown. By default _string_ primitives are rejected unless `allowString` option is passed. ```javascript const ensureIterable = require("type/iterable/ensure"); ensureIterable([]); // [] ensureIterable("foo", { allowString: true }); // "foo" ensureIterable({}); // Thrown TypeError: null is not expected iterable ``` ##### Denying empty iterables Pass `denyEmpty` option to require non empty iterables ```javascript ensureIterable([], { denyEmpty: true }); // Thrown TypeError: [] is not expected iterable ``` ##### Confirming on items Items can be validated by passing `ensureItem` option. Note that in this case: - A newly created instance of array with coerced values is returned - Error message lists up to three invalid items ```javascript const ensureString = require("type/string/ensure"); ensureIterable(new Set(["foo", 12]), { ensureItem: ensureString }); // ["foo", "12"] /* Below invocation with crash with: TypeError: [object Set] is not expected iterable value. Following items are invalid: - [object Object] */ ensureIterable(new Set(["foo", {}]), { ensureItem: ensureString }); ``` --- ### Date _Date_ instance #### `date/is` Confirms if given object is a native date, and is not an _Invalid Date_ ```javascript const isDate = require("type/date/is"); isDate(new Date()); // true isDate(new Date("Invalid date")); // false isDate(Date.now()); // false isDate("foo"); // false ``` #### `date/ensure` If given argument is a date object, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensureDate = require("type/date/ensure"); const date = new Date(); ensureDate(date); // date ensureDate(123123); // Thrown TypeError: 123123 is not a date object ``` --- ### Time value _number_ primitive which is a valid _time value_ (as used internally in _Date_ instances) #### `time-value/coerce` Follows [`integer/coerce`](#integercoerce) but returns `null` in place of values which go beyond 100 000 0000 days from unix epoch ```javascript const coerceToTimeValue = require("type/time-value/coerce"); coerceToTimeValue(12312312); // true coerceToTimeValue(Number.MAX_SAFE_INTEGER); // false coerceToTimeValue("foo"); // false ``` ##### `time-value/ensure` If given argument is a _time value_ coercible value (via [`time-value/coerce`](#time-valuecoerce)) returns result number. Otherwise `TypeError` is thrown. ```javascript const ensureTimeValue = require("type/time-value/ensure"); ensureTimeValue(12.93); // "12" ensureTimeValue(Number.MAX_SAFE_INTEGER); // Thrown TypeError: null is not a natural number ``` --- ### Function _Function_ instance #### `function/is` Confirms if given object is a native function ```javascript const isFunction = require("type/function/is"); isFunction(function () {}); // true isFunction(() => {}); // true isFunction(class {}); // true isFunction("foo"); // false ``` #### `function/ensure` If given argument is a function object, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensureFunction = require("type/function/ensure"); const fn = function () {}; ensureFunction(fn); // fn ensureFunction(/foo/); // Thrown TypeError: /foo/ is not a function ``` --- #### Plain Function A _Function_ instance that is not a _Class_ ##### `plain-function/is` Confirms if given object is a _plain function_ ```javascript const isPlainFunction = require("type/plain-function/is"); isPlainFunction(function () {}); // true isPlainFunction(() => {}); // true isPlainFunction(class {}); // false isPlainFunction("foo"); // false ``` ##### `plain-function/ensure` If given argument is a _plain function_ object, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensurePlainFunction = require("type/function/ensure"); const fn = function () {}; ensurePlainFunction(fn); // fn ensurePlainFunction(class {}); // Thrown TypeError: class is not a plain function ``` --- ### RegExp _RegExp_ instance #### `reg-exp/is` Confirms if given object is a native regular expression object ```javascript const isRegExp = require("type/reg-exp/is"); isRegExp(/foo/); isRegExp({}); // false isRegExp("foo"); // false ``` #### `reg-exp/ensure` If given argument is a regular expression object, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensureRegExp = require("type/reg-exp/ensure"); ensureRegExp(/foo/); // /foo/ ensureRegExp("foo"); // Thrown TypeError: null is not a regular expression object ``` --- ### Promise _Promise_ instance #### `promise/is` Confirms if given object is a native _promise_ ```javascript const isPromise = require("type/promise/is"); isPromise(Promise.resolve()); // true isPromise({ then: () => {} }); // false isPromise({}); // false ``` ##### `promise/ensure` If given argument is a promise, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensurePromise = require("type/promise/ensure"); const promise = Promise.resolve(); ensurePromise(promise); // promise eensurePromise({}); // Thrown TypeError: [object Object] is not a promise ``` --- #### Thenable _Thenable_ object (an object with `then` method) ##### `thenable/is` Confirms if given object is a _thenable_ ```javascript const isThenable = require("type/thenable/is"); isThenable(Promise.resolve()); // true isThenable({ then: () => {} }); // true isThenable({}); // false ``` ##### `thenable/ensure` If given argument is a _thenable_ object, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensureThenable = require("type/thenable/ensure"); const promise = Promise.resolve(); ensureThenable(promise); // promise ensureThenable({}); // Thrown TypeError: [object Object] is not a thenable object ``` --- ### Error _Error_ instance #### `error/is` Confirms if given object is a native error object ```javascript const isError = require("type/error/is"); isError(new Error()); // true isError({ message: "Fake error" }); // false ``` #### `error/ensure` If given argument is an error object, it is returned back. Otherwise `TypeError` is thrown. ```javascript const ensureError = require("type/error/ensure"); const someError = new Error("Some error"); ensureError(someError); // someError ensureError({ message: "Fake error" }); // Thrown TypeError: [object Object] is not an error object ``` --- ### Prototype Some constructor's `prototype` property #### `prototype/is` Confirms if given object serves as a _prototype_ property ```javascript const isPrototype = require("type/prototype/is"); isPrototype({}); // false isPrototype(Object.prototype); // true isPrototype(Array.prototype); // true ``` ### Tests $ npm test [nix-build-image]: https://semaphoreci.com/api/v1/medikoo-org/type/branches/master/shields_badge.svg [nix-build-url]: https://semaphoreci.com/medikoo-org/type [win-build-image]: https://ci.appveyor.com/api/projects/status/8nrtluuwsb5k9l8d?svg=true [win-build-url]: https://ci.appveyor.com/api/project/medikoo/type [cov-image]: https://img.shields.io/codecov/c/github/medikoo/type.svg [cov-url]: https://codecov.io/gh/medikoo/type [npm-image]: https://img.shields.io/npm/v/type.svg [npm-url]: https://www.npmjs.com/package/type type-2.0.0/array-length/000077500000000000000000000000001354765163300151045ustar00rootroot00000000000000type-2.0.0/array-length/coerce.js000066400000000000000000000003401354765163300166770ustar00rootroot00000000000000"use strict"; var coerceToSafeInteger = require("../safe-integer/coerce"); module.exports = function (value) { value = coerceToSafeInteger(value); if (!value) return value; if (value < 0) return null; return value; }; type-2.0.0/array-length/ensure.js000066400000000000000000000005011354765163300167370ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , coerce = require("./coerce"); module.exports = function (value/*, options*/) { var coerced = coerce(value); if (coerced !== null) return coerced; return resolveException(value, "%v is not a valid array length", arguments[1]); }; type-2.0.0/array-like/000077500000000000000000000000001354765163300145475ustar00rootroot00000000000000type-2.0.0/array-like/ensure.js000066400000000000000000000004431354765163300164070ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value, arguments[1])) return value; return resolveException(value, "%v is not an array like value", arguments[1]); }; type-2.0.0/array-like/is.js000066400000000000000000000010371354765163300155210ustar00rootroot00000000000000"use strict"; var coerceToArrayLength = require("../array-length/coerce") , isObject = require("../object/is"); module.exports = function (value/*, options*/) { if (!isObject(value)) { var options = arguments[1]; if (isObject(options) && options.allowString && typeof value === "string") return true; return false; } if (typeof value === "function") return false; var length; try { length = value.length; } catch (error) { return false; } if (coerceToArrayLength(length) === null) return false; return true; }; type-2.0.0/array/000077500000000000000000000000001354765163300136255ustar00rootroot00000000000000type-2.0.0/array/ensure.js000066400000000000000000000004211354765163300154610ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not an array object", arguments[1]); }; type-2.0.0/array/is.js000066400000000000000000000014301354765163300145740ustar00rootroot00000000000000"use strict"; var isPrototype = require("../prototype/is"); var isArray; if (typeof Array.isArray === "function") { isArray = Array.isArray; } else { var objectToString = Object.prototype.toString, objectTaggedString = objectToString.call([]); isArray = function (value) { return objectToString.call(value) === objectTaggedString; }; } module.exports = function (value) { if (!isArray(value)) return false; // Sanity check (reject objects which do not expose common Array interface) if (!hasOwnProperty.call(value, "length")) return false; try { if (typeof value.length !== "number") return false; if (typeof value.push !== "function") return false; if (typeof value.splice !== "function") return false; } catch (error) { return false; } return !isPrototype(value); }; type-2.0.0/date/000077500000000000000000000000001354765163300134245ustar00rootroot00000000000000type-2.0.0/date/ensure.js000066400000000000000000000004171354765163300152650ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not a date object", arguments[1]); }; type-2.0.0/date/is.js000066400000000000000000000012321354765163300143730ustar00rootroot00000000000000"use strict"; var isPrototype = require("../prototype/is"); var dateValueOf = Date.prototype.valueOf; module.exports = function (value) { if (!value) return false; try { // Sanity check (reject objects which do not expose common Date interface) if (typeof value.getFullYear !== "function") return false; if (typeof value.getTimezoneOffset !== "function") return false; if (typeof value.setFullYear !== "function") return false; // Ensure its native Date object (has [[DateValue]] slot) dateValueOf.call(value); } catch (error) { return false; } // Ensure it hosts valid date if (isNaN(value)) return false; return !isPrototype(value); }; type-2.0.0/error/000077500000000000000000000000001354765163300136405ustar00rootroot00000000000000type-2.0.0/error/ensure.js000066400000000000000000000004211354765163300154740ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not an error object", arguments[1]); }; type-2.0.0/error/is.js000066400000000000000000000030341354765163300146110ustar00rootroot00000000000000"use strict"; var isPrototype = require("../prototype/is") , isPlainObject = require("../plain-object/is"); var objectToString = Object.prototype.toString; // Recognize host specific errors (e.g. DOMException) var errorTaggedStringRe = /^\[object .*(?:Error|Exception)\]$/ , errorNameRe = /^[^\s]*(?:Error|Exception)$/; module.exports = function (value) { if (!value) return false; var name; // Sanity check (reject objects which do not expose common Error interface) try { name = value.name; if (typeof name !== "string") return false; if (typeof value.message !== "string") return false; } catch (error) { return false; } // Ensure its a native-like Error object // (has [[ErrorData]] slot, or was created to resemble one) // Note: It's not a 100% bulletproof check of confirming that as: // - In ES2015+ string tag can be overriden via Symbol.toStringTag property // - Host errors do not share native error tag. Still we rely on assumption that // tag for each error will end either with `Error` or `Exception` string // - In pre ES2015 era, no custom errors will share the error tag. if (!errorTaggedStringRe.test(objectToString.call(value))) { // Definitely not an ES2015 error instance, but could still be an error // (created via e.g. CustomError.prototype = Object.create(Error.prototype)) try { if (name !== value.constructor.name) return false; } catch (error) { return false; } if (!errorNameRe.test(name)) return false; if (isPlainObject(value)) return false; } return !isPrototype(value); }; type-2.0.0/finite/000077500000000000000000000000001354765163300137655ustar00rootroot00000000000000type-2.0.0/finite/coerce.js000066400000000000000000000002611354765163300155620ustar00rootroot00000000000000"use strict"; var coerceToNumber = require("../number/coerce"); module.exports = function (value) { value = coerceToNumber(value); return isFinite(value) ? value : null; }; type-2.0.0/finite/ensure.js000066400000000000000000000004741354765163300156310ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , coerce = require("./coerce"); module.exports = function (value/*, options*/) { var coerced = coerce(value); if (coerced !== null) return coerced; return resolveException(value, "%v is not a finite number", arguments[1]); }; type-2.0.0/function/000077500000000000000000000000001354765163300143345ustar00rootroot00000000000000type-2.0.0/function/ensure.js000066400000000000000000000004141354765163300161720ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not a function", arguments[1]); }; type-2.0.0/function/is.js000066400000000000000000000006771354765163300153170ustar00rootroot00000000000000"use strict"; var isPrototype = require("../prototype/is"); module.exports = function (value) { if (typeof value !== "function") return false; if (!hasOwnProperty.call(value, "length")) return false; try { if (typeof value.length !== "number") return false; if (typeof value.call !== "function") return false; if (typeof value.apply !== "function") return false; } catch (error) { return false; } return !isPrototype(value); }; type-2.0.0/integer/000077500000000000000000000000001354765163300141445ustar00rootroot00000000000000type-2.0.0/integer/coerce.js000066400000000000000000000003771354765163300157510ustar00rootroot00000000000000"use strict"; var coerceToFinite = require("../finite/coerce"); var abs = Math.abs, floor = Math.floor; module.exports = function (value) { value = coerceToFinite(value); if (!value) return value; return (value > 0 ? 1 : -1) * floor(abs(value)); }; type-2.0.0/integer/ensure.js000066400000000000000000000004671354765163300160120ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , coerce = require("./coerce"); module.exports = function (value/*, options*/) { var coerced = coerce(value); if (coerced !== null) return coerced; return resolveException(value, "%v is not an integer", arguments[1]); }; type-2.0.0/iterable/000077500000000000000000000000001354765163300142765ustar00rootroot00000000000000type-2.0.0/iterable/ensure.js000066400000000000000000000027131354765163300161400ustar00rootroot00000000000000"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 invalidItemsLimit = 3, defaultErrorMessage = "%v is not expected iterable value"; module.exports = function (value/*, options*/) { var options = arguments[1]; if (!is(value, options)) return resolveException(value, defaultErrorMessage, options); if (!options) return value; var ensureItem = ensurePlainFunction(options.ensureItem, { isOptional: true }); if (ensureItem) { var coercedValue = []; var iterator = value[Symbol.iterator](); var item, invalidItems; while (!(item = iterator.next()).done) { var newItemValue; try { newItemValue = ensureItem(item.value); } catch (error) { if (!invalidItems) invalidItems = []; if (invalidItems.push(item.value) === invalidItemsLimit) break; } if (invalidItems) continue; coercedValue.push(newItemValue); } if (invalidItems) { var errorMessage = resolveErrorMessage(defaultErrorMessage, value, options) + ".\n Following items are invalid:"; for (var i = 0; i < invalidItems.length; ++i) { errorMessage += "\n - " + toShortString(invalidItems[i]); } throw new TypeError(errorMessage); } return coercedValue; } return value; }; type-2.0.0/iterable/is.js000066400000000000000000000013601354765163300152470ustar00rootroot00000000000000// Polyfills friendly, therefore ES5 syntax "use strict"; var isObject = require("../object/is"); var iteratorSymbol = Symbol.iterator; if (!iteratorSymbol) { throw new Error("Cannot initialize iterator/is due to Symbol.iterator not being implemented"); } module.exports = function (value/*, options*/) { var options = arguments[1]; if (!isObject(value)) { if (!isObject(options) || !options.allowString || typeof value !== "string") return false; } try { if (typeof value[iteratorSymbol] !== "function") return false; } catch (error) { return false; } if (!options) return true; if (options.denyEmpty) { try { if (value[iteratorSymbol]().next().done) return false; } catch (error) { return false; } } return true; }; type-2.0.0/lib/000077500000000000000000000000001354765163300132555ustar00rootroot00000000000000type-2.0.0/lib/is-to-string-tag-supported.js000066400000000000000000000001501354765163300207420ustar00rootroot00000000000000"use strict"; module.exports = typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol"; type-2.0.0/lib/resolve-error-message.js000066400000000000000000000007131354765163300200440ustar00rootroot00000000000000"use strict"; var isObject = require("../object/is") , stringCoerce = require("../string/coerce") , toShortString = require("./to-short-string"); module.exports = function (errorMessage, value, inputOptions) { var customErrorMessage; if (isObject(inputOptions) && inputOptions.errorMessage) { customErrorMessage = stringCoerce(inputOptions.errorMessage); } return (customErrorMessage || errorMessage).replace("%v", toShortString(value)); }; type-2.0.0/lib/resolve-exception.js000066400000000000000000000007501354765163300172700ustar00rootroot00000000000000"use strict"; var isValue = require("../value/is") , isObject = require("../object/is") , resolveErrorMessage = require("./resolve-error-message"); module.exports = function (value, defaultMessage, inputOptions) { if (isObject(inputOptions) && !isValue(value)) { if ("default" in inputOptions) return inputOptions["default"]; if (inputOptions.isOptional) return null; } throw new TypeError(resolveErrorMessage(defaultMessage, value, inputOptions)); }; type-2.0.0/lib/safe-to-string.js000066400000000000000000000002601354765163300164530ustar00rootroot00000000000000"use strict"; module.exports = function (value) { try { return value.toString(); } catch (error) { try { return String(value); } catch (error2) { return null; } } }; type-2.0.0/lib/to-short-string.js000066400000000000000000000012561354765163300167020ustar00rootroot00000000000000"use strict"; var safeToString = require("./safe-to-string"); var reNewLine = /[\n\r\u2028\u2029]/g; module.exports = function (value) { var string = safeToString(value); if (string === null) return ""; // Trim if too long if (string.length > 100) string = string.slice(0, 99) + "…"; // Replace eventual new lines string = string.replace(reNewLine, function (char) { switch (char) { case "\n": return "\\n"; case "\r": return "\\r"; case "\u2028": return "\\u2028"; case "\u2029": return "\\u2029"; /* istanbul ignore next */ default: throw new Error("Unexpected character"); } }); return string; }; type-2.0.0/natural-number/000077500000000000000000000000001354765163300154435ustar00rootroot00000000000000type-2.0.0/natural-number/coerce.js000066400000000000000000000003231354765163300172370ustar00rootroot00000000000000"use strict"; var coerceToInteger = require("../integer/coerce"); module.exports = function (value) { value = coerceToInteger(value); if (!value) return value; if (value < 0) return null; return value; }; type-2.0.0/natural-number/ensure.js000066400000000000000000000004751354765163300173100ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , coerce = require("./coerce"); module.exports = function (value/*, options*/) { var coerced = coerce(value); if (coerced !== null) return coerced; return resolveException(value, "%v is not a natural number", arguments[1]); }; type-2.0.0/number/000077500000000000000000000000001354765163300137775ustar00rootroot00000000000000type-2.0.0/number/coerce.js000066400000000000000000000004111354765163300155710ustar00rootroot00000000000000"use strict"; var isValue = require("../value/is"); module.exports = function (value) { if (!isValue(value)) return null; try { value = +value; // Ensure implicit coercion } catch (error) { return null; } if (isNaN(value)) return null; return value; }; type-2.0.0/number/ensure.js000066400000000000000000000004651354765163300156430ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , coerce = require("./coerce"); module.exports = function (value/*, options*/) { var coerced = coerce(value); if (coerced !== null) return coerced; return resolveException(value, "%v is not a number", arguments[1]); }; type-2.0.0/object/000077500000000000000000000000001354765163300137555ustar00rootroot00000000000000type-2.0.0/object/ensure.js000066400000000000000000000004131354765163300156120ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not an object", arguments[1]); }; type-2.0.0/object/is.js000066400000000000000000000004571354765163300147340ustar00rootroot00000000000000"use strict"; var isValue = require("../value/is"); // prettier-ignore var possibleTypes = { "object": true, "function": true, "undefined": true /* document.all */ }; module.exports = function (value) { if (!isValue(value)) return false; return hasOwnProperty.call(possibleTypes, typeof value); }; type-2.0.0/package.json000066400000000000000000000037751354765163300150110ustar00rootroot00000000000000{ "name": "type", "version": "2.0.0", "description": "Runtime validation and processing of JavaScript types", "author": "Mariusz Nowak (https://www.medikoo.com/)", "keywords": [ "type", "coercion" ], "repository": "medikoo/type", "devDependencies": { "chai": "^4.2.0", "eslint": "^6.4.0", "eslint-config-medikoo": "^2.5.1", "git-list-updated": "^1.2.1", "husky": "^3.0.5", "lint-staged": "^9.2.5", "mocha": "^6.2.0", "nyc": "^14.1.1", "prettier-elastic": "^1.18.2" }, "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "*.js": [ "eslint" ], "*.{css,html,js,json,md,yaml,yml}": [ "prettier -c" ] }, "eslintConfig": { "extends": "medikoo/es3", "root": true, "globals": { "Map": true, "Promise": true, "Set": true, "Symbol": true }, "overrides": [ { "files": "test/**/*.js", "env": { "mocha": true }, "rules": { "no-eval": "off", "no-new-wrappers": "off" } }, { "files": [ "string/coerce.js", "number/coerce.js" ], "rules": { "no-implicit-coercion": "off" } }, { "files": "plain-object/is.js", "rules": { "no-proto": "off" } } ] }, "prettier": { "printWidth": 100, "tabWidth": 4, "overrides": [ { "files": [ "*.md" ], "options": { "tabWidth": 2 } } ] }, "scripts": { "coverage": "nyc --reporter=lcov --reporter=html --reporter=text-summary npm test", "check-coverage": "npm run coverage && nyc check-coverage --statements 80 --function 80 --branches 80 --lines 80", "lint": "eslint --ignore-path=.gitignore .", "lint-updated": "pipe-git-updated --ext=js -- eslint --ignore-pattern '!*'", "prettier-check-updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c", "prettify": "prettier --write --ignore-path .gitignore '**/*.{css,html,js,json,md,yaml,yml}'", "test": "mocha --recursive" }, "license": "ISC" } type-2.0.0/plain-function/000077500000000000000000000000001354765163300154355ustar00rootroot00000000000000type-2.0.0/plain-function/ensure.js000066400000000000000000000004221354765163300172720ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not a plain function", arguments[1]); }; type-2.0.0/plain-function/is.js000066400000000000000000000004521354765163300164070ustar00rootroot00000000000000"use strict"; var isFunction = require("../function/is"); var classRe = /^\s*class[\s{/}]/, functionToString = Function.prototype.toString; module.exports = function (value) { if (!isFunction(value)) return false; if (classRe.test(functionToString.call(value))) return false; return true; }; type-2.0.0/plain-object/000077500000000000000000000000001354765163300150565ustar00rootroot00000000000000type-2.0.0/plain-object/ensure.js000066400000000000000000000037651354765163300167300ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , resolveErrorMessage = require("../lib/resolve-error-message") , ensurePlainFunction = require("../plain-function/ensure") , ensureArray = require("../array/ensure") , is = require("./is"); var objHasOwnProperty = Object.prototype.hasOwnProperty , invalidItemsLimit = 3 , defaultErrorMessage = "%v is not a valid plain object"; module.exports = function (value/*, options*/) { var options = arguments[1]; if (!is(value)) return resolveException(value, defaultErrorMessage, options); if (!options) return value; var invalidKeys, key, errorMessage; var allowedKeys = ensureArray(options.allowedKeys, { isOptional: true }); if (allowedKeys) { for (key in value) { if (!objHasOwnProperty.call(value, key)) continue; if (allowedKeys.indexOf(key) > -1) continue; if (!invalidKeys) invalidKeys = []; if (invalidKeys.push(key) === invalidItemsLimit) break; } if (invalidKeys) { errorMessage = resolveErrorMessage(defaultErrorMessage, value, options) + ".\n Following keys are unexpected: " + invalidKeys.join(", "); throw new TypeError(errorMessage); } } var ensurePropertyValue = ensurePlainFunction(options.ensurePropertyValue, { isOptional: true }); if (ensurePropertyValue) { var coercedValue = {}; for (key in value) { if (!objHasOwnProperty.call(value, key)) continue; var coercedPropertyValue; try { coercedPropertyValue = ensurePropertyValue(value[key]); } catch (error) { if (!invalidKeys) invalidKeys = []; if (invalidKeys.push(key) === invalidItemsLimit) break; } if (invalidKeys) continue; coercedValue[key] = coercedPropertyValue; } if (invalidKeys) { errorMessage = resolveErrorMessage(defaultErrorMessage, value, options) + ".\n Valuees for following keys are invalid: " + invalidKeys.join(", "); throw new TypeError(errorMessage); } return coercedValue; } return value; }; type-2.0.0/plain-object/is.js000066400000000000000000000014071354765163300160310ustar00rootroot00000000000000"use strict"; var isObject = require("../object/is") , isPrototype = require("../prototype/is"); var getPrototypeOf; if (typeof Object.getPrototypeOf === "function") { getPrototypeOf = Object.getPrototypeOf; } else if ({}.__proto__ === Object.prototype) { getPrototypeOf = function (object) { return object.__proto__; }; } module.exports = function (value) { if (!isObject(value)) return false; var prototype; if (getPrototypeOf) { prototype = getPrototypeOf(value); } else { try { var valueConstructor = value.constructor; if (valueConstructor) prototype = valueConstructor.prototype; } catch (error) { return false; } } if (prototype && !hasOwnProperty.call(prototype, "propertyIsEnumerable")) return false; return !isPrototype(value); }; type-2.0.0/promise/000077500000000000000000000000001354765163300141655ustar00rootroot00000000000000type-2.0.0/promise/ensure.js000066400000000000000000000004131354765163300160220ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not a promise", arguments[1]); }; type-2.0.0/promise/is.js000066400000000000000000000015641354765163300151440ustar00rootroot00000000000000"use strict"; var isPrototype = require("../prototype/is"); // In theory we could rely on Symbol.toStringTag directly, // still early native implementation (e.g. in FF) predated symbols var objectToString = Object.prototype.toString , objectTaggedString = objectToString.call(Promise.resolve()); module.exports = function (value) { if (!value) return false; // Sanity check (reject objects which do not expose common Promise interface) try { if (typeof value.then !== "function") return false; if (typeof value["catch"] !== "function") return false; } catch (error) { return false; } // Ensure its native Promise object (has [[PromiseState]] slot) // Note: it's not 100% precise as string tag may be overriden // and other objects could be hacked to expose it if (objectToString.call(value) !== objectTaggedString) return false; return !isPrototype(value); }; type-2.0.0/prototype/000077500000000000000000000000001354765163300145545ustar00rootroot00000000000000type-2.0.0/prototype/is.js000066400000000000000000000004111354765163300155210ustar00rootroot00000000000000"use strict"; var isObject = require("../object/is"); module.exports = function (value) { if (!isObject(value)) return false; try { if (!value.constructor) return false; return value.constructor.prototype === value; } catch (error) { return false; } }; type-2.0.0/reg-exp/000077500000000000000000000000001354765163300140565ustar00rootroot00000000000000type-2.0.0/reg-exp/ensure.js000066400000000000000000000004351354765163300157170ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not a regular expression object", arguments[1]); }; type-2.0.0/reg-exp/is.js000066400000000000000000000022761354765163300150360ustar00rootroot00000000000000"use strict"; var isToStringTagSupported = require("../lib/is-to-string-tag-supported") , isPrototype = require("../prototype/is"); var regExpTest = RegExp.prototype.test , objectToString = Object.prototype.toString , objectTaggedString = objectToString.call(/a/); module.exports = function (value) { if (!value) return false; // Sanity check (reject objects which do not expose common RegExp interface) if (!hasOwnProperty.call(value, "lastIndex")) return false; try { if (typeof value.lastIndex !== "number") return false; if (typeof value.test !== "function") return false; if (typeof value.exec !== "function") return false; } catch (error) { return false; } // Ensure its native RegExp object (has [[RegExpMatcher]] slot) if (isToStringTagSupported && typeof value[Symbol.toStringTag] === "string") { // Edge case (possibly a regExp with custom Symbol.toStringTag) try { var lastIndex = value.lastIndex; regExpTest.call(value, ""); if (value.lastIndex !== lastIndex) value.lastIndex = lastIndex; return true; } catch (error) { return false; } } if (objectToString.call(value) !== objectTaggedString) return false; return !isPrototype(value); }; type-2.0.0/safe-integer/000077500000000000000000000000001354765163300150605ustar00rootroot00000000000000type-2.0.0/safe-integer/coerce.js000066400000000000000000000005361354765163300166620ustar00rootroot00000000000000"use strict"; var coerceToInteger = require("../integer/coerce"); var MAX_SAFE_INTEGER = 9007199254740991, MIN_SAFE_INTEGER = -9007199254740991; module.exports = function (value) { value = coerceToInteger(value); if (!value) return value; if (value > MAX_SAFE_INTEGER) return null; if (value < MIN_SAFE_INTEGER) return null; return value; }; type-2.0.0/safe-integer/ensure.js000066400000000000000000000004731354765163300167230ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , coerce = require("./coerce"); module.exports = function (value/*, options*/) { var coerced = coerce(value); if (coerced !== null) return coerced; return resolveException(value, "%v is not a safe integer", arguments[1]); }; type-2.0.0/string/000077500000000000000000000000001354765163300140155ustar00rootroot00000000000000type-2.0.0/string/coerce.js000066400000000000000000000013211354765163300156100ustar00rootroot00000000000000"use strict"; var isValue = require("../value/is") , isObject = require("../object/is"); var objectToString = Object.prototype.toString; module.exports = function (value) { if (!isValue(value)) return null; if (isObject(value)) { // Reject Object.prototype.toString coercion var valueToString = value.toString; if (typeof valueToString !== "function") return null; if (valueToString === objectToString) return null; // Note: It can be object coming from other realm, still as there's no ES3 and CSP compliant // way to resolve its realm's Object.prototype.toString it's left as not addressed edge case } try { return "" + value; // Ensure implicit coercion } catch (error) { return null; } }; type-2.0.0/string/ensure.js000066400000000000000000000004651354765163300156610ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , coerce = require("./coerce"); module.exports = function (value/*, options*/) { var coerced = coerce(value); if (coerced !== null) return coerced; return resolveException(value, "%v is not a string", arguments[1]); }; type-2.0.0/test/000077500000000000000000000000001354765163300134665ustar00rootroot00000000000000type-2.0.0/test/_lib/000077500000000000000000000000001354765163300143735ustar00rootroot00000000000000type-2.0.0/test/_lib/arrow-function-if-supported.js000066400000000000000000000001151354765163300223220ustar00rootroot00000000000000"use strict"; try { module.exports = eval("(() => {})"); } catch (error) {} type-2.0.0/test/_lib/class-if-supported.js000066400000000000000000000001151354765163300204520ustar00rootroot00000000000000"use strict"; try { module.exports = eval("(class {})"); } catch (error) {} type-2.0.0/test/array-length/000077500000000000000000000000001354765163300160635ustar00rootroot00000000000000type-2.0.0/test/array-length/coerce.js000066400000000000000000000033321354765163300176620ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , coerceToArrayLength = require("../../array-length/coerce"); describe("array-length/coerce", function () { it("Should coerce float", function () { assert.equal(coerceToArrayLength(123.123), 123); assert.equal(coerceToArrayLength(123.823), 123); }); it("Should coerce string", function () { assert.equal(coerceToArrayLength("12.123"), 12); }); it("Should coerce booleans", function () { assert.equal(coerceToArrayLength(true), 1); }); it("Should coerce number objects", function () { assert.equal(coerceToArrayLength(new Number(343)), 343); }); it("Should coerce objects", function () { assert.equal(coerceToArrayLength({ valueOf: function () { return 23; } }), 23); }); it("Should reject infinite number", function () { assert.equal(coerceToArrayLength(Infinity), null); }); it("Should reject number beyond Number.MAX_SAFE_INTEGER", function () { assert.equal(coerceToArrayLength(9007199254740992), null); }); it("Should reject negative number", function () { assert.equal(coerceToArrayLength(-9), null); }); it("Should reject NaN", function () { assert.equal(coerceToArrayLength(NaN), null); }); if (typeof Object.create === "function") { it("Should not coerce objects with no number representation", function () { assert.equal(coerceToArrayLength(Object.create(null)), null); }); } it("Should not coerce null", function () { assert.equal(coerceToArrayLength(null), null); }); it("Should not coerce undefined", function () { assert.equal(coerceToArrayLength(undefined), null); }); if (typeof Symbol === "function") { it("Should not coerce symbols", function () { assert.equal(coerceToArrayLength(Symbol("foo")), null); }); } }); type-2.0.0/test/array-length/ensure.js000066400000000000000000000010221354765163300177150ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureArrayLength = require("../../array-length/ensure"); describe("array-length/ensure", function () { it("Should return coerced value", function () { assert.equal(ensureArrayLength("12.23"), 12); }); it("Should crash on no value", function () { try { ensureArrayLength(-20); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "-20 is not a valid array length"); } }); }); type-2.0.0/test/array-like/000077500000000000000000000000001354765163300155265ustar00rootroot00000000000000type-2.0.0/test/array-like/ensure.js000066400000000000000000000012761354765163300173730ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureArrayLike = require("../../array-like/ensure"); describe("array-like/ensure", function () { it("Should return input value", function () { var value = []; assert.equal(ensureArrayLike(value), value); }); it("Should allow strings with allowString option", function () { var value = "foo"; assert.equal(ensureArrayLike(value, { allowString: true }), value); }); it("Should crash on invalid value", function () { try { ensureArrayLike("foo"); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not an array like value")); } }); }); type-2.0.0/test/array-like/is.js000066400000000000000000000033411354765163300165000ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isArrayLike = require("../../array-like/is"); describe("array-like/is", function () { it("Should return true on array", function () { assert.equal(isArrayLike([]), true); }); it("Should return true on array-like object", function () { assert.equal(isArrayLike({ length: 1 }), true); }); it("Should by default return false on string", function () { assert.equal(isArrayLike("foo"), false); }); it("Should accept strings if specified", function () { assert.equal(isArrayLike("foo", { allowString: true }), true); }); it("Should return false on objects with negative length", function () { assert.equal(isArrayLike({ length: -1 }), false); }); it("Should return false on plain object", function () { assert.equal(isArrayLike({}), false); }); it("Should return false on function", function () { assert.equal(isArrayLike(function () { return true; }), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isArrayLike(Object.create(null)), false); }); } it("Should return false on number", function () { assert.equal(isArrayLike(123), false); }); it("Should return false on NaN", function () { assert.equal(isArrayLike(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isArrayLike(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isArrayLike(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isArrayLike(null), false); }); it("Should return false on undefined", function () { assert.equal(isArrayLike(void 0), false); }); }); type-2.0.0/test/array/000077500000000000000000000000001354765163300146045ustar00rootroot00000000000000type-2.0.0/test/array/ensure.js000066400000000000000000000007751354765163300164540ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureArray = require("../../array/ensure"); describe("array/ensure", function () { it("Should return input value", function () { var value = []; assert.equal(ensureArray(value), value); }); it("Should crash on invalid value", function () { try { ensureArray(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not an array object")); } }); }); type-2.0.0/test/array/is.js000066400000000000000000000032141354765163300155550ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isArray = require("../../array/is"); describe("array/is", function () { it("Should return true on array", function () { assert.equal(isArray([]), true); }); it("Should return false on array with no common API exposed", function () { var value = []; value.push = null; assert.equal(isArray(value), false); }); it("Should return false on Array.prototype", function () { assert.equal(isArray(Array.prototype), false); }); it("Should return false on plain object", function () { assert.equal(isArray({}), false); }); it("Should return false on function", function () { assert.equal(isArray(function () { return true; }), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isArray(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isArray("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isArray(""), false); }); it("Should return false on number", function () { assert.equal(isArray(123), false); }); it("Should return false on NaN", function () { assert.equal(isArray(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isArray(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isArray(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isArray(null), false); }); it("Should return false on undefined", function () { assert.equal(isArray(void 0), false); }); }); type-2.0.0/test/date/000077500000000000000000000000001354765163300144035ustar00rootroot00000000000000type-2.0.0/test/date/ensure.js000066400000000000000000000007751354765163300162530ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureDate = require("../../date/ensure"); describe("date/ensure", function () { it("Should return input value", function () { var value = new Date(); assert.equal(ensureDate(value), value); }); it("Should crash on invalid value", function () { try { ensureDate(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not a date object")); } }); }); type-2.0.0/test/date/is.js000066400000000000000000000036731354765163300153650ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isDate = require("../../date/is"); describe("date/is", function () { it("Should return true on date", function () { assert.equal(isDate(new Date()), true); }); it("Should return false on invalid date", function () { assert.equal(isDate(new Date("foo")), false); }); it("Should return false on native date with no common API exposed", function () { var value = new Date(); value.getFullYear = null; assert.equal(isDate(value), false); }); it("Should return false on Date.prototype", function () { assert.equal(isDate(Date.prototype), false); }); it("Should return false on time value", function () { assert.equal(isDate(12312313), false); }); it("Should return false on plain object", function () { assert.equal(isDate({}), false); }); it("Should return false on function", function () { assert.equal(isDate(function () { return true; }), false); }); it("Should return false on array", function () { assert.equal(isDate([]), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isDate(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isDate("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isDate(""), false); }); it("Should return false on number", function () { assert.equal(isDate(123), false); }); it("Should return false on NaN", function () { assert.equal(isDate(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isDate(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isDate(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isDate(null), false); }); it("Should return false on undefined", function () { assert.equal(isDate(void 0), false); }); }); type-2.0.0/test/error/000077500000000000000000000000001354765163300146175ustar00rootroot00000000000000type-2.0.0/test/error/ensure.js000066400000000000000000000010061354765163300164530ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureError = require("../../error/ensure"); describe("error/ensure", function () { it("Should return input value", function () { var value = new Error(); assert.equal(ensureError(value), value); }); it("Should crash on invalid value", function () { try { ensureError(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not an error object")); } }); }); type-2.0.0/test/error/is.js000066400000000000000000000037771354765163300156060ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isError = require("../../error/is"); describe("error/is", function () { it("Should return true on error", function () { assert.equal(isError(new Error()), true); }); it("Should return false on native error with no common API exposed", function () { var value = new Error(); value.message = null; assert.equal(isError(value), false); }); it("Should return false on Error.prototype", function () { assert.equal(isError(Error.prototype), false); }); if (typeof Object.create === "function") { it("Should return true on custom built ES5 era error", function () { var CustomEs5Error = function () { Error.call(this); }; CustomEs5Error.prototype = Object.create(Error.prototype); assert.equal(isError(new CustomEs5Error()), true); }); it("Should return false on object with no prototype", function () { assert.equal(isError(Object.create(null)), false); }); } it("Should return false on plain object", function () { assert.equal(isError({}), false); }); it("Should return false on function", function () { assert.equal(isError(function () { return true; }), false); }); it("Should return false on array", function () { assert.equal(isError([]), false); }); it("Should return false on string", function () { assert.equal(isError("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isError(""), false); }); it("Should return false on number", function () { assert.equal(isError(123), false); }); it("Should return false on NaN", function () { assert.equal(isError(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isError(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isError(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isError(null), false); }); it("Should return false on undefined", function () { assert.equal(isError(void 0), false); }); }); type-2.0.0/test/finite/000077500000000000000000000000001354765163300147445ustar00rootroot00000000000000type-2.0.0/test/finite/coerce.js000066400000000000000000000025571354765163300165530ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , coerceToFinite = require("../../finite/coerce"); describe("finite/coerce", function () { it("Should return input number", function () { assert.equal(coerceToFinite(123.123), 123.123); }); it("Should coerce string", function () { assert.equal(coerceToFinite("12"), 12); }); it("Should coerce booleans", function () { assert.equal(coerceToFinite(true), 1); }); it("Should coerce number objects", function () { assert.equal(coerceToFinite(new Number(343)), 343); }); it("Should coerce objects", function () { assert.equal(coerceToFinite({ valueOf: function () { return 23; } }), 23); }); it("Should reject infinite number", function () { assert.equal(coerceToFinite(Infinity), null); }); it("Should reject NaN", function () { assert.equal(coerceToFinite(NaN), null); }); if (typeof Object.create === "function") { it("Should not coerce objects with no number representation", function () { assert.equal(coerceToFinite(Object.create(null)), null); }); } it("Should not coerce null", function () { assert.equal(coerceToFinite(null), null); }); it("Should not coerce undefined", function () { assert.equal(coerceToFinite(undefined), null); }); if (typeof Symbol === "function") { it("Should not coerce symbols", function () { assert.equal(coerceToFinite(Symbol("foo")), null); }); } }); type-2.0.0/test/finite/ensure.js000066400000000000000000000007571354765163300166140ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureFinite = require("../../finite/ensure"); describe("finite/ensure", function () { it("Should return coerced value", function () { assert.equal(ensureFinite("12.23"), 12.23); }); it("Should crash on no value", function () { try { ensureFinite(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "null is not a finite number"); } }); }); type-2.0.0/test/function/000077500000000000000000000000001354765163300153135ustar00rootroot00000000000000type-2.0.0/test/function/ensure.js000066400000000000000000000010441354765163300171510ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureFunction = require("../../function/ensure"); describe("function/ensure", function () { it("Should return input value", function () { var value = function () { return true; }; assert.equal(ensureFunction(value), value); }); it("Should crash on invalid value", function () { try { ensureFunction(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not a function")); } }); }); type-2.0.0/test/function/is.js000066400000000000000000000037451354765163300162750ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isFunction = require("../../function/is") , arrowFunctionIfSupported = require("../_lib/arrow-function-if-supported") , classIfSupported = require("../_lib/class-if-supported"); describe("function/is", function () { it("Should return true on function", function () { assert.equal(isFunction(function () { return true; }), true); }); if (arrowFunctionIfSupported) { it("Should return true on arrow function", function () { assert.equal(isFunction(arrowFunctionIfSupported), true); }); } if (classIfSupported) { it("Should return true on class", function () { assert.equal(isFunction(classIfSupported), true); }); } it("Should return false on reg-exp", function () { assert.equal(isFunction(/foo/), false); }); it("Should return false on plain object", function () { assert.equal(isFunction({}), false); }); it("Should return false on array", function () { assert.equal(isFunction([]), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isFunction(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isFunction("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isFunction(""), false); }); it("Should return false on number", function () { assert.equal(isFunction(123), false); }); it("Should return false on NaN", function () { assert.equal(isFunction(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isFunction(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isFunction(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isFunction(null), false); }); it("Should return false on undefined", function () { assert.equal(isFunction(void 0), false); }); }); type-2.0.0/test/integer/000077500000000000000000000000001354765163300151235ustar00rootroot00000000000000type-2.0.0/test/integer/coerce.js000066400000000000000000000034731354765163300167300ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , coerceToInteger = require("../../integer/coerce"); describe("integer/coerce", function () { it("Should coerce float to integer", function () { assert.equal(coerceToInteger(123.123), 123); assert.equal(coerceToInteger(123.823), 123); assert.equal(coerceToInteger(-123.123), -123); assert.equal(coerceToInteger(-123.823), -123); }); it("Should coerce string", function () { assert.equal(coerceToInteger("12.123"), 12); }); it("Should coerce booleans", function () { assert.equal(coerceToInteger(true), 1); }); it("Should coerce number objects", function () { assert.equal(coerceToInteger(new Number(343)), 343); }); it("Should coerce objects", function () { assert.equal(coerceToInteger({ valueOf: function () { return 23; } }), 23); }); it("Should coerce number beyond Number.MAX_SAFE_INTEGER", function () { assert.equal(coerceToInteger(9007199254740992), 9007199254740992); }); it("Should coerce number beyond Number.MIN_SAFE_INTEGER", function () { assert.equal(coerceToInteger(-9007199254740992), -9007199254740992); }); it("Should reject infinite number", function () { assert.equal(coerceToInteger(Infinity), null); }); it("Should reject NaN", function () { assert.equal(coerceToInteger(NaN), null); }); if (typeof Object.create === "function") { it("Should not coerce objects with no number representation", function () { assert.equal(coerceToInteger(Object.create(null)), null); }); } it("Should not coerce null", function () { assert.equal(coerceToInteger(null), null); }); it("Should not coerce undefined", function () { assert.equal(coerceToInteger(undefined), null); }); if (typeof Symbol === "function") { it("Should not coerce symbols", function () { assert.equal(coerceToInteger(Symbol("foo")), null); }); } }); type-2.0.0/test/integer/ensure.js000066400000000000000000000007551354765163300167710ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureInteger = require("../../integer/ensure"); describe("integer/ensure", function () { it("Should return coerced value", function () { assert.equal(ensureInteger("12.23"), 12); }); it("Should crash on no value", function () { try { ensureInteger(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "null is not an integer"); } }); }); type-2.0.0/test/iterable/000077500000000000000000000000001354765163300152555ustar00rootroot00000000000000type-2.0.0/test/iterable/ensure.js000066400000000000000000000025751354765163300171250ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureString = require("../../string/ensure") , isArray = require("../../array/is") , ensureIterable = require("../../iterable/ensure"); describe("iterable/ensure", function () { it("Should return input value", function () { var value = []; assert.equal(ensureIterable(value), value); }); it("Should allow strings with allowString option", function () { var value = "foo"; assert.equal(ensureIterable(value, { allowString: true }), value); }); it("Should crash on invalid value", function () { try { ensureIterable("foo"); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not expected iterable value")); } }); describe("Should support 'ensureItem' option", function () { it("Should resolve coerced array", function () { var coercedValue = ensureIterable(new Set(["foo", 12]), { ensureItem: ensureString }); assert(isArray(coercedValue)); assert.deepEqual(coercedValue, ["foo", "12"]); }); it("Should crash if some item is invalid", function () { try { ensureIterable(["foo", {}], { ensureItem: ensureString }); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not expected iterable value")); } }); }); }); type-2.0.0/test/iterable/is.js000066400000000000000000000045701354765163300162340ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isIterable = require("../../iterable/is"); describe("iterable/is", function () { it("Should return true on array", function () { assert.equal(isIterable([]), true); }); it("Should return true on arguments", function () { assert.equal(isIterable((function () { return arguments; })()), true); }); it("Should by default return false on string", function () { assert.equal(isIterable("foo"), false); }); it("Should accept strings if specified", function () { assert.equal(isIterable("foo", { allowString: true }), true); }); it("Should support denyEmpty option", function () { assert.equal(isIterable([], { denyEmpty: true }), false); assert.equal(isIterable([null], { denyEmpty: true }), true); assert.equal(isIterable("", { allowString: true, denyEmpty: true }), false); assert.equal(isIterable("foo", { allowString: true, denyEmpty: true }), true); }); if (typeof Set === "function") { it("Should return true on set", function () { assert.equal(isIterable(new Set()), true); }); } if (typeof Map === "function") { it("Should return true on set", function () { assert.equal(isIterable(new Map()), true); }); } it("Should return false on plain object", function () { assert.equal(isIterable({}), false); }); it("Should return false on function", function () { assert.equal(isIterable(function () { return true; }), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isIterable(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isIterable("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isIterable(""), false); }); it("Should return false on number", function () { assert.equal(isIterable(123), false); }); it("Should return false on NaN", function () { assert.equal(isIterable(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isIterable(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isIterable(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isIterable(null), false); }); it("Should return false on undefined", function () { assert.equal(isIterable(void 0), false); }); }); type-2.0.0/test/lib/000077500000000000000000000000001354765163300142345ustar00rootroot00000000000000type-2.0.0/test/lib/is-to-string-tag-supported.js000066400000000000000000000004661354765163300217330ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isToStringTagSupported = require("../../lib/is-to-string-tag-supported"); describe("lib/is-to-string-tag-supported", function () { it("Should return boolean", function () { assert(typeof isToStringTagSupported === "boolean"); }); }); type-2.0.0/test/lib/resolve-error-message.js000066400000000000000000000010451354765163300210220ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , resolveErrorMessage = require("../../lib/resolve-error-message"); describe("lib/resolve-error-message", function () { it("Should insert value", function () { assert.equal(resolveErrorMessage("%v is invalid", 12), "12 is invalid"); }); it("Should support custome error message via inputOptions.errorMessage", function () { assert.equal( resolveErrorMessage("%v is invalid", null, { errorMessage: "%v is not supported age" }), "null is not supported age" ); }); }); type-2.0.0/test/lib/resolve-exception.js000066400000000000000000000017541354765163300202540ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , handleException = require("../../lib/resolve-exception"); describe("lib/handle-exception", function () { it("Should throw TypeError", function () { try { handleException(12, "Invalid value"); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "Invalid value"); } }); it("Should resolve value in default message", function () { try { handleException(12, "%v is invalid", {}); throw new Error("Unexpected"); } catch (error) { assert.equal(error.message, "12 is invalid"); } }); it("Should support optional values via inputOptions.isOptional", function () { assert.equal(handleException(null, "%v is invalid", { isOptional: true }, null)); }); it("Should support optional values via inputOptions.default", function () { // prettier-ignore assert.equal(handleException(null, "%v is invalid", { "default": "bar" }), "bar"); }); }); type-2.0.0/test/lib/safe-to-string.js000066400000000000000000000022541354765163300174370ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , safeToString = require("../../lib/safe-to-string"); describe("lib/safe-to-string", function () { it("Should return input string", function () { assert.equal(safeToString("foo"), "foo"); }); it("Should coerce numbers", function () { assert.equal(safeToString(12), "12"); }); it("Should coerce booleans", function () { assert.equal(safeToString(true), "true"); }); it("Should coerce string objects", function () { assert.equal(safeToString(new String("bar")), "bar"); }); it("Should coerce objects", function () { assert.equal( safeToString({ toString: function () { return "Some object"; } }), "Some object" ); }); it("Should coerce null", function () { assert.equal(safeToString(null), "null"); }); it("Should coerce undefined", function () { assert.equal(safeToString(undefined), "undefined"); }); if (typeof Symbol === "function") { it("Should coerce symbols", function () { // eslint-disable-next-line no-undef assert.equal(safeToString(Symbol()), "Symbol()"); }); } it("Should return null for non coercible values", function () { assert.equal(safeToString({ toString: null }), null); }); }); type-2.0.0/test/lib/to-short-string.js000066400000000000000000000030431354765163300176550ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , toShortString = require("../../lib/to-short-string"); describe("lib/to-short-string", function () { it("Should return input string", function () { assert.equal(toShortString("foo"), "foo"); }); it("Should coerce numbers", function () { assert.equal(toShortString(12), "12"); }); it("Should coerce booleans", function () { assert.equal(toShortString(true), "true"); }); it("Should coerce string objects", function () { assert.equal(toShortString(new String("bar")), "bar"); }); it("Should coerce objects", function () { assert.equal( toShortString({ toString: function () { return "Some object"; } }), "Some object" ); }); it("Should coerce null", function () { assert.equal(toShortString(null), "null"); }); it("Should coerce undefined", function () { assert.equal(toShortString(undefined), "undefined"); }); if (typeof Symbol === "function") { it("Should coerce symbols", function () { // eslint-disable-next-line no-undef assert.equal(toShortString(Symbol()), "Symbol()"); }); } it("Should return replacement non coercible values", function () { assert.equal(toShortString({ toString: null }), ""); }); it("Should replace new line characters", function () { assert.equal(toShortString("foo\n\r\u2028\u2029bar"), "foo\\n\\r\\u2028\\u2029bar"); }); it("Should truncate long string", function () { var str = Math.random().toString(36); while (str.length < 200) str += str; assert.equal(toShortString(str).length, 100); }); }); type-2.0.0/test/natural-number/000077500000000000000000000000001354765163300164225ustar00rootroot00000000000000type-2.0.0/test/natural-number/coerce.js000066400000000000000000000034251354765163300202240ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , coerceToNaturalNumber = require("../../natural-number/coerce"); describe("natural-number/coerce", function () { it("Should coerce float to integer", function () { assert.equal(coerceToNaturalNumber(123.123), 123); assert.equal(coerceToNaturalNumber(123.823), 123); }); it("Should coerce string", function () { assert.equal(coerceToNaturalNumber("12.123"), 12); }); it("Should coerce booleans", function () { assert.equal(coerceToNaturalNumber(true), 1); }); it("Should coerce number objects", function () { assert.equal(coerceToNaturalNumber(new Number(343)), 343); }); it("Should coerce objects", function () { assert.equal(coerceToNaturalNumber({ valueOf: function () { return 23; } }), 23); }); it("Should coerce number beyond Number.MAX_SAFE_INTEGER", function () { assert.equal(coerceToNaturalNumber(9007199254740992), 9007199254740992); }); it("Should reject negative number", function () { assert.equal(coerceToNaturalNumber(-1), null); }); it("Should reject infinite number", function () { assert.equal(coerceToNaturalNumber(Infinity), null); }); it("Should reject NaN", function () { assert.equal(coerceToNaturalNumber(NaN), null); }); if (typeof Object.create === "function") { it("Should not coerce objects with no number representation", function () { assert.equal(coerceToNaturalNumber(Object.create(null)), null); }); } it("Should not coerce null", function () { assert.equal(coerceToNaturalNumber(null), null); }); it("Should not coerce undefined", function () { assert.equal(coerceToNaturalNumber(undefined), null); }); if (typeof Symbol === "function") { it("Should not coerce symbols", function () { assert.equal(coerceToNaturalNumber(Symbol("foo")), null); }); } }); type-2.0.0/test/natural-number/ensure.js000066400000000000000000000010321354765163300202550ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureNaturalNumber = require("../../natural-number/ensure"); describe("natural-number/ensure", function () { it("Should return coerced value", function () { assert.equal(ensureNaturalNumber("12.23"), 12); }); it("Should crash on no value", function () { try { ensureNaturalNumber(-20); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "-20 is not a natural number"); } }); }); type-2.0.0/test/number/000077500000000000000000000000001354765163300147565ustar00rootroot00000000000000type-2.0.0/test/number/coerce.js000066400000000000000000000025711354765163300165610ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , coerceToNumber = require("../../number/coerce"); describe("number/coerce", function () { it("Should return input number", function () { assert.equal(coerceToNumber(123.123), 123.123); }); it("Should return input infinite number", function () { assert.equal(coerceToNumber(Infinity), Infinity); }); it("Should coerce string", function () { assert.equal(coerceToNumber("12"), 12); }); it("Should coerce booleans", function () { assert.equal(coerceToNumber(true), 1); }); it("Should coerce number objects", function () { assert.equal(coerceToNumber(new Number(343)), 343); }); it("Should coerce objects", function () { assert.equal(coerceToNumber({ valueOf: function () { return 23; } }), 23); }); it("Should reject NaN", function () { assert.equal(coerceToNumber(NaN), null); }); if (typeof Object.create === "function") { it("Should not coerce objects with no number representation", function () { assert.equal(coerceToNumber(Object.create(null)), null); }); } it("Should not coerce null", function () { assert.equal(coerceToNumber(null), null); }); it("Should not coerce undefined", function () { assert.equal(coerceToNumber(undefined), null); }); if (typeof Symbol === "function") { it("Should not coerce symbols", function () { assert.equal(coerceToNumber(Symbol("foo")), null); }); } }); type-2.0.0/test/number/ensure.js000066400000000000000000000007501354765163300166170ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureNumber = require("../../number/ensure"); describe("number/ensure", function () { it("Should return coerced value", function () { assert.equal(ensureNumber("12.23"), 12.23); }); it("Should crash on no value", function () { try { ensureNumber(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "null is not a number"); } }); }); type-2.0.0/test/object/000077500000000000000000000000001354765163300147345ustar00rootroot00000000000000type-2.0.0/test/object/ensure.js000066400000000000000000000007721354765163300166010ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureObject = require("../../object/ensure"); describe("object/ensure", function () { it("Should return input value", function () { var value = {}; assert.equal(ensureObject(value), value); }); it("Should crash on no value", function () { try { ensureObject(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "null is not an object"); } }); }); type-2.0.0/test/object/is.js000066400000000000000000000025731354765163300157140ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isObject = require("../../object/is"); describe("object/is", function () { it("Should return true on object", function () { assert.equal(isObject({}), true); }); it("Should return true on function", function () { assert.equal(isObject(function () { return true; }), true); }); it("Should return true on array", function () { assert.equal(isObject([]), true); }); if (typeof Object.create === "function") { it("Should return true on object with no prototype", function () { assert.equal(isObject(Object.create(null)), true); }); } it("Should return false on string", function () { assert.equal(isObject("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isObject(""), false); }); it("Should return false on number", function () { assert.equal(isObject(123), false); }); it("Should return false on NaN", function () { assert.equal(isObject(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isObject(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isObject(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isObject(null), false); }); it("Should return false on undefined", function () { assert.equal(isObject(void 0), false); }); }); type-2.0.0/test/plain-function/000077500000000000000000000000001354765163300164145ustar00rootroot00000000000000type-2.0.0/test/plain-function/ensure.js000066400000000000000000000011121354765163300202460ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensurePlainFunction = require("../../plain-function/ensure"); describe("plain-function/ensure", function () { it("Should return input value", function () { var value = function () { return true; }; assert.equal(ensurePlainFunction(value), value); }); it("Should crash on invalid value", function () { try { ensurePlainFunction(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not a plain function")); } }); }); type-2.0.0/test/plain-function/is.js000066400000000000000000000041151354765163300173660ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isPlainFunction = require("../../plain-function/is") , arrowFunctionIfSupported = require("../_lib/arrow-function-if-supported") , classIfSupported = require("../_lib/class-if-supported"); describe("plain-function/is", function () { it("Should return true on function", function () { assert.equal(isPlainFunction(function () { return true; }), true); }); if (arrowFunctionIfSupported) { it("Should return true on arrow function", function () { assert.equal(isPlainFunction(arrowFunctionIfSupported), true); }); } if (classIfSupported) { it("Should return false on class", function () { assert.equal(isPlainFunction(classIfSupported), false); }); } it("Should return false on reg-exp", function () { assert.equal(isPlainFunction(/foo/), false); }); it("Should return false on plain object", function () { assert.equal(isPlainFunction({}), false); }); it("Should return false on array", function () { assert.equal(isPlainFunction([]), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isPlainFunction(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isPlainFunction("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isPlainFunction(""), false); }); it("Should return false on number", function () { assert.equal(isPlainFunction(123), false); }); it("Should return false on NaN", function () { assert.equal(isPlainFunction(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isPlainFunction(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isPlainFunction(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isPlainFunction(null), false); }); it("Should return false on undefined", function () { assert.equal(isPlainFunction(void 0), false); }); }); type-2.0.0/test/plain-object/000077500000000000000000000000001354765163300160355ustar00rootroot00000000000000type-2.0.0/test/plain-object/ensure.js000066400000000000000000000030031354765163300176700ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureString = require("../../string/ensure") , ensurePlainObject = require("../../plain-object/ensure"); describe("plain-object/ensure", function () { it("Should return input value", function () { var value = {}; assert.equal(ensurePlainObject(value), value); }); it("Should crash on invalid value", function () { try { ensurePlainObject(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "null is not a valid plain object"); } }); it("Should support allowedKeys option", function () { var value = { foo: "bar", marko: "elo" }; assert.equal(ensurePlainObject(value, { allowedKeys: ["foo", "marko"] }), value); try { ensurePlainObject(value, { allowedKeys: ["marko"] }); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message.indexOf("is not a valid plain object") !== -1, true); } }); it("Should support ensurePropertyValue option", function () { assert.deepEqual( ensurePlainObject({ foo: "bar", marko: 12 }, { ensurePropertyValue: ensureString }), { foo: "bar", marko: "12" } ); try { ensurePlainObject({ foo: "bar", marko: {} }, { ensurePropertyValue: ensureString }); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message.indexOf("is not a valid plain object") !== -1, true); } }); }); type-2.0.0/test/plain-object/is.js000066400000000000000000000035121354765163300170070ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isPlainObject = require("../../plain-object/is"); describe("plain-object/is", function () { it("Should return true on plain object", function () { assert.equal(isPlainObject({}), true); }); if (typeof Object.create === "function") { it("Should return true on object with no prototype", function () { assert.equal(isPlainObject(Object.create(null)), true); }); it( "Should return false on object that inherits from object with no prototype", function () { assert.equal(isPlainObject(Object.create(Object.create(null))), false); } ); } it("Should return false on Object.prototype", function () { assert.equal(isPlainObject(Object.prototype), false); }); it("Should return false on prototype that derives from Object.prototype", function () { assert.equal(isPlainObject(RegExp.prototype), false); }); it("Should return false on function", function () { assert.equal(isPlainObject(function () { return true; }), false); }); it("Should return false on string", function () { assert.equal(isPlainObject("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isPlainObject(""), false); }); it("Should return false on number", function () { assert.equal(isPlainObject(123), false); }); it("Should return false on NaN", function () { assert.equal(isPlainObject(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isPlainObject(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isPlainObject(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isPlainObject(null), false); }); it("Should return false on undefined", function () { assert.equal(isPlainObject(void 0), false); }); }); type-2.0.0/test/promise/000077500000000000000000000000001354765163300151445ustar00rootroot00000000000000type-2.0.0/test/promise/ensure.js000066400000000000000000000010301354765163300167750ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensurePromise = require("../../promise/ensure"); describe("promise/ensure", function () { it("Should return input value", function () { var value = Promise.resolve(); assert.equal(ensurePromise(value), value); }); it("Should crash on no value", function () { try { ensurePromise({}); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "[object Object] is not a promise"); } }); }); type-2.0.0/test/promise/is.js000066400000000000000000000033021354765163300161130ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isPromise = require("../../promise/is"); describe("promise/is", function () { if (typeof Promise === "function") { it("Should return true on promise", function () { assert.equal(isPromise(Promise.resolve()), true); }); } it("Should return false on non-promise thenable", function () { assert.equal(isPromise({ then: function () { return true; } }), false); }); it("Should return false on plain object", function () { assert.equal(isPromise({}), false); }); it("Should return false on function", function () { assert.equal(isPromise(function () { return true; }), false); }); it("Should return false on array", function () { assert.equal(isPromise([]), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isPromise(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isPromise("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isPromise(""), false); }); it("Should return false on number", function () { assert.equal(isPromise(123), false); }); it("Should return false on NaN", function () { assert.equal(isPromise(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isPromise(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isPromise(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isPromise(null), false); }); it("Should return false on undefined", function () { assert.equal(isPromise(void 0), false); }); }); type-2.0.0/test/prototype/000077500000000000000000000000001354765163300155335ustar00rootroot00000000000000type-2.0.0/test/prototype/is.js000066400000000000000000000030611354765163300165040ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isPrototype = require("../../prototype/is"); describe("prototype/is", function () { it("Should return true on prototype", function () { assert.equal(isPrototype(Object.prototype), true); }); it("Should return false on plain object", function () { assert.equal(isPrototype({}), false); }); it("Should return false on function", function () { assert.equal(isPrototype(function () { return true; }), false); }); it("Should return false on array", function () { assert.equal(isPrototype([]), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isPrototype(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isPrototype("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isPrototype(""), false); }); it("Should return false on number", function () { assert.equal(isPrototype(123), false); }); it("Should return false on NaN", function () { assert.equal(isPrototype(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isPrototype(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isPrototype(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isPrototype(null), false); }); it("Should return false on undefined", function () { assert.equal(isPrototype(void 0), false); }); }); type-2.0.0/test/reg-exp/000077500000000000000000000000001354765163300150355ustar00rootroot00000000000000type-2.0.0/test/reg-exp/ensure.js000066400000000000000000000010241354765163300166710ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureRegExp = require("../../reg-exp/ensure"); describe("reg-exp/ensure", function () { it("Should return input value", function () { var value = /foo/; assert.equal(ensureRegExp(value), value); }); it("Should crash on invalid value", function () { try { ensureRegExp(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert(error.message.includes("is not a regular expression object")); } }); }); type-2.0.0/test/reg-exp/is.js000066400000000000000000000036121354765163300160100ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isRegExp = require("../../reg-exp/is"); describe("reg-exp/is", function () { it("Should return true on regular expression", function () { assert.equal(isRegExp(/foo/), true); }); it("Should return false on native regular expression with no common API exposed", function () { var re = /foo/; re.test = null; assert.equal(isRegExp(re), false); }); it("Should return false on RegExp.prototype", function () { assert.equal(isRegExp(RegExp.prototype), false); }); it("Should return false on regexp like string", function () { assert.equal(isRegExp("/foo/"), false); }); it("Should return false on plain object", function () { assert.equal(isRegExp({}), false); }); it("Should return false on function", function () { assert.equal(isRegExp(function () { return true; }), false); }); it("Should return false on array", function () { assert.equal(isRegExp([]), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isRegExp(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isRegExp("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isRegExp(""), false); }); it("Should return false on number", function () { assert.equal(isRegExp(123), false); }); it("Should return false on NaN", function () { assert.equal(isRegExp(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isRegExp(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isRegExp(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isRegExp(null), false); }); it("Should return false on undefined", function () { assert.equal(isRegExp(void 0), false); }); }); type-2.0.0/test/safe-integer/000077500000000000000000000000001354765163300160375ustar00rootroot00000000000000type-2.0.0/test/safe-integer/coerce.js000066400000000000000000000035641354765163300176450ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , coerceToSafeInteger = require("../../safe-integer/coerce"); describe("safe-integer/coerce", function () { it("Should coerce float to integer", function () { assert.equal(coerceToSafeInteger(123.123), 123); assert.equal(coerceToSafeInteger(123.823), 123); assert.equal(coerceToSafeInteger(-123.123), -123); assert.equal(coerceToSafeInteger(-123.823), -123); }); it("Should coerce string", function () { assert.equal(coerceToSafeInteger("12.123"), 12); }); it("Should coerce booleans", function () { assert.equal(coerceToSafeInteger(true), 1); }); it("Should coerce number objects", function () { assert.equal(coerceToSafeInteger(new Number(343)), 343); }); it("Should coerce objects", function () { assert.equal(coerceToSafeInteger({ valueOf: function () { return 23; } }), 23); }); it("Should reject infinite number", function () { assert.equal(coerceToSafeInteger(Infinity), null); }); it("Should reject number beyond Number.MAX_SAFE_INTEGER", function () { assert.equal(coerceToSafeInteger(9007199254740992), null); }); it("Should reject number beyond Number.MIN_SAFE_INTEGER", function () { assert.equal(coerceToSafeInteger(-9007199254740992), null); }); it("Should reject NaN", function () { assert.equal(coerceToSafeInteger(NaN), null); }); if (typeof Object.create === "function") { it("Should not coerce objects with no number representation", function () { assert.equal(coerceToSafeInteger(Object.create(null)), null); }); } it("Should not coerce null", function () { assert.equal(coerceToSafeInteger(null), null); }); it("Should not coerce undefined", function () { assert.equal(coerceToSafeInteger(undefined), null); }); if (typeof Symbol === "function") { it("Should not coerce symbols", function () { assert.equal(coerceToSafeInteger(Symbol("foo")), null); }); } }); type-2.0.0/test/safe-integer/ensure.js000066400000000000000000000010161354765163300176740ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureSafeInteger = require("../../safe-integer/ensure"); describe("safe-integer/ensure", function () { it("Should return coerced value", function () { assert.equal(ensureSafeInteger("12.23"), 12); }); it("Should crash on no value", function () { try { ensureSafeInteger(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "null is not a safe integer"); } }); }); type-2.0.0/test/string/000077500000000000000000000000001354765163300147745ustar00rootroot00000000000000type-2.0.0/test/string/coerce.js000066400000000000000000000024721354765163300165770ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , coerceToString = require("../../string/coerce"); describe("string/coerce", function () { it("Should return input string", function () { assert.equal(coerceToString("foo"), "foo"); }); it("Should coerce numbers", function () { assert.equal(coerceToString(12), "12"); }); it("Should coerce booleans", function () { assert.equal(coerceToString(true), "true"); }); it("Should coerce string objects", function () { assert.equal(coerceToString(new String("bar")), "bar"); }); it("Should coerce objects", function () { assert.equal( coerceToString({ toString: function () { return "Some object"; } }), "Some object" ); }); if (typeof Object.create === "function") { it("Should not coerce objects with no toString", function () { assert.equal(coerceToString(Object.create(null)), null); }); } it("Should not coerce objects with no custom toString", function () { assert.equal(coerceToString({}), null); }); it("Should not coerce null", function () { assert.equal(coerceToString(null), null); }); it("Should not coerce undefined", function () { assert.equal(coerceToString(undefined), null); }); if (typeof Symbol === "function") { it("Should not coerce symbols", function () { assert.equal(coerceToString(Symbol("foo")), null); }); } }); type-2.0.0/test/string/ensure.js000066400000000000000000000007421354765163300166360ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureString = require("../../string/ensure"); describe("string/ensure", function () { it("Should return coerced value", function () { assert.equal(ensureString(12), "12"); }); it("Should crash on no value", function () { try { ensureString(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "null is not a string"); } }); }); type-2.0.0/test/thenable/000077500000000000000000000000001354765163300152505ustar00rootroot00000000000000type-2.0.0/test/thenable/ensure.js000066400000000000000000000010731354765163300171100ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureThenable = require("../../thenable/ensure"); describe("thenable/ensure", function () { it("Should return input value", function () { var value = { then: function () { return true; } }; assert.equal(ensureThenable(value), value); }); it("Should crash on no value", function () { try { ensureThenable({}); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "[object Object] is not a thenable object"); } }); }); type-2.0.0/test/thenable/is.js000066400000000000000000000035451354765163300162300ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isThenable = require("../../thenable/is"); describe("thenable/is", function () { it("Should return true on object with `then` method", function () { assert.equal(isThenable({ then: function () { return true; } }), true); }); if (typeof Promise === "function") { it("Should return true on promise", function () { assert.equal(isThenable(Promise.resolve()), true); }); } it("Should return false on object with `then` non callable property", function () { assert.equal(isThenable({ then: {} }), false); }); it("Should return false on plain object", function () { assert.equal(isThenable({}), false); }); it("Should return false on function", function () { assert.equal(isThenable(function () { return true; }), false); }); it("Should return false on array", function () { assert.equal(isThenable([]), false); }); if (typeof Object.create === "function") { it("Should return false on object with no prototype", function () { assert.equal(isThenable(Object.create(null)), false); }); } it("Should return false on string", function () { assert.equal(isThenable("foo"), false); }); it("Should return false on empty string", function () { assert.equal(isThenable(""), false); }); it("Should return false on number", function () { assert.equal(isThenable(123), false); }); it("Should return false on NaN", function () { assert.equal(isThenable(NaN), false); }); it("Should return false on boolean", function () { assert.equal(isThenable(true), false); }); if (typeof Symbol === "function") { it("Should return false on symbol", function () { assert.equal(isThenable(Symbol("foo")), false); }); } it("Should return false on null", function () { assert.equal(isThenable(null), false); }); it("Should return false on undefined", function () { assert.equal(isThenable(void 0), false); }); }); type-2.0.0/test/time-value/000077500000000000000000000000001354765163300155365ustar00rootroot00000000000000type-2.0.0/test/time-value/coerce.js000066400000000000000000000033771354765163300173460ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , coerceToTimeValue = require("../../time-value/coerce"); describe("time-value/coerce", function () { it("Should coerce float to time value", function () { assert.equal(coerceToTimeValue(123.123), 123); assert.equal(coerceToTimeValue(123.823), 123); assert.equal(coerceToTimeValue(-123.123), -123); assert.equal(coerceToTimeValue(-123.823), -123); }); it("Should coerce string", function () { assert.equal(coerceToTimeValue("12.123"), 12); }); it("Should coerce booleans", function () { assert.equal(coerceToTimeValue(true), 1); }); it("Should coerce number objects", function () { assert.equal(coerceToTimeValue(new Number(343)), 343); }); it("Should coerce objects", function () { assert.equal(coerceToTimeValue({ valueOf: function () { return 23; } }), 23); }); it("Should reject infinite number", function () { assert.equal(coerceToTimeValue(Infinity), null); }); it("Should reject number beyond 100,000,000 days from unix epox", function () { assert.equal(coerceToTimeValue(8.64e15 + 1), null); assert.equal(coerceToTimeValue(-8.64e15 - 1), null); }); it("Should reject NaN", function () { assert.equal(coerceToTimeValue(NaN), null); }); if (typeof Object.create === "function") { it("Should not coerce objects with no number representation", function () { assert.equal(coerceToTimeValue(Object.create(null)), null); }); } it("Should not coerce null", function () { assert.equal(coerceToTimeValue(null), null); }); it("Should not coerce undefined", function () { assert.equal(coerceToTimeValue(undefined), null); }); if (typeof Symbol === "function") { it("Should not coerce symbols", function () { assert.equal(coerceToTimeValue(Symbol("foo")), null); }); } }); type-2.0.0/test/time-value/ensure.js000066400000000000000000000007751354765163300174060ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureTimeValue = require("../../time-value/ensure"); describe("time-value/ensure", function () { it("Should return coerced value", function () { assert.equal(ensureTimeValue("12.23"), 12); }); it("Should crash on no value", function () { try { ensureTimeValue("foo"); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "foo is not a time value"); } }); }); type-2.0.0/test/value/000077500000000000000000000000001354765163300146025ustar00rootroot00000000000000type-2.0.0/test/value/ensure.js000066400000000000000000000007561354765163300164510ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , ensureValue = require("../../value/ensure"); describe("value/ensure", function () { it("Should return input value", function () { var value = {}; assert.equal(ensureValue(value), value); }); it("Should crash on no value", function () { try { ensureValue(null); throw new Error("Unexpected"); } catch (error) { assert.equal(error.name, "TypeError"); assert.equal(error.message, "Cannot use null"); } }); }); type-2.0.0/test/value/is.js000066400000000000000000000025751354765163300155640ustar00rootroot00000000000000"use strict"; var assert = require("chai").assert , isValue = require("../../value/is"); describe("value/is", function () { it("Should return true on object", function () { assert.equal(isValue({}), true); }); it("Should return true on function", function () { assert.equal(isValue(function () { return true; }), true); }); it("Should return true on array", function () { assert.equal(isValue([]), true); }); if (typeof Object.create === "function") { it("Should return true on object with no prototype", function () { assert.equal(isValue(Object.create(null)), true); }); } it("Should return true on string", function () { assert.equal(isValue("foo"), true); }); it("Should return true on empty string", function () { assert.equal(isValue(""), true); }); it("Should return true on number", function () { assert.equal(isValue(123), true); }); it("Should return true on NaN", function () { assert.equal(isValue(NaN), true); }); it("Should return true on boolean", function () { assert.equal(isValue(false), true); }); if (typeof Symbol === "function") { // eslint-disable-next-line no-undef it("Should return true on symbol", function () { assert.equal(isValue(Symbol()), true); }); } it("Should return false on null", function () { assert.equal(isValue(null), false); }); it("Should return false on undefined", function () { assert.equal(isValue(void 0), false); }); }); type-2.0.0/thenable/000077500000000000000000000000001354765163300142715ustar00rootroot00000000000000type-2.0.0/thenable/ensure.js000066400000000000000000000004231354765163300161270ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "%v is not a thenable object", arguments[1]); }; type-2.0.0/thenable/is.js000066400000000000000000000003271354765163300152440ustar00rootroot00000000000000"use strict"; var isObject = require("../object/is"); module.exports = function (value) { if (!isObject(value)) return false; try { return typeof value.then === "function"; } catch (error) { return false; } }; type-2.0.0/time-value/000077500000000000000000000000001354765163300145575ustar00rootroot00000000000000type-2.0.0/time-value/coerce.js000066400000000000000000000003631354765163300163570ustar00rootroot00000000000000"use strict"; var coerceToInteger = require("../integer/coerce"); var abs = Math.abs; module.exports = function (value) { value = coerceToInteger(value); if (!value) return value; if (abs(value) > 8.64e15) return null; return value; }; type-2.0.0/time-value/ensure.js000066400000000000000000000004711354765163300164200ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , coerce = require("./coerce"); module.exports = function (value/*, options*/) { var coerced = coerce(value); if (coerced !== null) return coerced; return resolveException(value, "%v is not a time value", arguments[1]); }; type-2.0.0/value/000077500000000000000000000000001354765163300136235ustar00rootroot00000000000000type-2.0.0/value/ensure.js000066400000000000000000000004051354765163300154610ustar00rootroot00000000000000"use strict"; var resolveException = require("../lib/resolve-exception") , is = require("./is"); module.exports = function (value/*, options*/) { if (is(value)) return value; return resolveException(value, "Cannot use %v", arguments[1]); }; type-2.0.0/value/is.js000066400000000000000000000002131354765163300145700ustar00rootroot00000000000000"use strict"; // ES3 safe var _undefined = void 0; module.exports = function (value) { return value !== _undefined && value !== null; };