core-functions
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "core-functions", | ||
"version": "1.1.1", | ||
"description": "Core functions and utilities for working with primitives and built-in objects, including strings, functions, booleans, Promises, base 64, etc.", | ||
"version": "1.2.0", | ||
"description": "Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including strings, functions, booleans, Promises, base 64, Arrays, Objects, standard AppErrors, etc.", | ||
"author": "Byron du Preez", | ||
@@ -6,0 +6,0 @@ "license": "Apache-2.0", |
@@ -10,2 +10,4 @@ 'use strict'; | ||
module.exports = { | ||
/** Returns true if the given value is a Promise or at least a "then-able"; otherwise false. */ | ||
isPromise: isPromise, | ||
/** Returns a function that will wrap and convert a node-style function into a Promise-returning function */ | ||
@@ -21,3 +23,17 @@ wrap: wrap, | ||
const timers = require('./timers'); | ||
/** | ||
* Returns true if the given value is a Promise or at least a "then-able"; otherwise false. | ||
* @param {*} value - the value to check | ||
* @returns {boolean|*} true if its a promise (or a "then-able"); false otherwise | ||
*/ | ||
function isPromise(value) { | ||
return value instanceof Promise || (value.then && typeof value.then === 'function'); | ||
} | ||
if (!Promise.isPromise) { // polyfill-safe guard check | ||
Promise.isPromise = isPromise; | ||
} | ||
/** | ||
* Wraps and converts the given node-style function into a Promise-returning function, which when invoked must be passed | ||
@@ -58,3 +74,3 @@ * all of the wrapped function's arguments other than its last callback argument. | ||
* | ||
* @param {Function} fn a Node-callback style function to promisify | ||
* @param {Function} fn - a Node-callback style function to promisify | ||
* @returns {Function} a function, which when invoked will return a new Promise that will resolve or reject based on the | ||
@@ -112,4 +128,4 @@ * outcome of the callback | ||
* | ||
* @param {Object} obj the object on which to execute the given method | ||
* @param {Function} method a Node-callback style method (of the given object) to promisify | ||
* @param {Object} obj - the object on which to execute the given method | ||
* @param {Function} method - a Node-callback style method (of the given object) to promisify | ||
* @returns {Function} a function, which when invoked will return a new Promise that will resolve or reject based on the | ||
@@ -165,2 +181,5 @@ * outcome of the callback | ||
* Promise.try(functionToTry); | ||
* | ||
* @param {Function} fn - the function to execute within a promise | ||
* @returns {Promise} the promise to execute the given function | ||
*/ | ||
@@ -175,8 +194,40 @@ function tryFn(fn) { | ||
/** | ||
* Starts a simple timeout Promise, which will resolve after the specified delay in milliseconds. | ||
* @param {number} ms the number of milliseconds to delay | ||
* Starts a simple timeout Promise, which will resolve after the specified delay in milliseconds. If any object is | ||
* passed into this function as the cancellable argument, then this function will install a cancelTimeout method on it, | ||
* which accepts a single optional mustResolve argument and which if subsequently invoked will cancel the timeout and | ||
* either resolve the promise (if mustResolve) or reject the promise (default), but ONLY if the timeout | ||
* has not triggered yet. | ||
* | ||
* @param {number} ms - the number of milliseconds to delay | ||
* @param {Object|undefined|null} [cancellable] - an arbitrary object onto which a cancelTimeout method will be installed | ||
* @returns {Promise} the timeout Promise | ||
*/ | ||
function delay(ms) { | ||
return new Promise((resolve, reject) => setTimeout(resolve, ms)) | ||
function delay(ms, cancellable) { | ||
let triggered = false; | ||
return new Promise((resolve, reject) => { | ||
const timeout = setTimeout(() => { | ||
triggered = true; | ||
resolve(triggered); | ||
}, ms); | ||
// Set up a cancel method on the given cancellable object | ||
if (cancellable && typeof cancellable === 'object') { | ||
cancellable.cancelTimeout = (mustResolve) => { | ||
if (!triggered) { | ||
try { | ||
clearTimeout(timeout); | ||
} catch (err) { | ||
console.error('Failed to clear timeout', err); | ||
} finally { | ||
if (mustResolve) { | ||
resolve(triggered); | ||
} else { | ||
reject(triggered); | ||
} | ||
} | ||
} | ||
return triggered; | ||
} | ||
} | ||
}); | ||
} | ||
@@ -183,0 +234,0 @@ if (!Promise.delay) { // polyfill-safe guard check |
103
README.md
@@ -1,12 +0,17 @@ | ||
# core-functions v1.1.1 | ||
# core-functions v1.2.0 | ||
Core functions and utilities for working with Node/JavaScript primitives and built-in objects, including strings, functions, booleans, Promises, base 64, etc. | ||
Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including | ||
strings, functions, booleans, Promises, base 64, Arrays, Objects, standard AppErrors, etc. | ||
Currently includes: | ||
- strings.js - string utilities | ||
- numbers.js - number utilities | ||
- app-errors.js - a collection of standard application AppError subclasses for the more commonly used HTTP status codes | ||
- arrays.js - Array utilities | ||
- base64.js - utilities for encoding from UTF-8 to Base 64 and vice-versa | ||
- booleans.js - boolean utilities | ||
- functions.js - function utilities | ||
- numbers.js - number utilities | ||
- objects.js - Object utilities | ||
- promises.js - native Promise utilities | ||
- base64.js - utilities for encoding from UTF-8 to Base 64 and vice-versa | ||
- strings.js - string utilities | ||
- timers.js - Timer/timeout utilities | ||
@@ -24,22 +29,78 @@ This module is exported as a [Node.js](https://nodejs.org/) module. | ||
In Node.js: | ||
To use the string utilities | ||
```js | ||
// To use the string utilties | ||
const Strings = require('core-functions/strings'); | ||
// To use the number utilties | ||
``` | ||
To use the number utilities | ||
```js | ||
const Numbers = require('core-functions/numbers'); | ||
``` | ||
// To use the boolean utilties | ||
To use the boolean utilities | ||
```js | ||
const Booleans = require('core-functions/booleans'); | ||
``` | ||
// To use the function utilties | ||
To use the function utilities | ||
```js | ||
const Functions = require('core-functions/functions'); | ||
``` | ||
// To use the Base 64 encoding and decoding utilities | ||
To use the Base 64 encoding and decoding utilities | ||
```js | ||
const base64 = require('core-functions/base64'); | ||
``` | ||
// To use the Promise utilties, which currently add static methods to the native `Promise` class | ||
To use the Promise utilities (as static methods on the native `Promise` class) | ||
```js | ||
require('core-functions/promises'); | ||
``` | ||
To use the Promise utilities (as exported functions) | ||
```js | ||
const promises = require('core-functions/promises'); | ||
``` | ||
To use the Object utilities | ||
```js | ||
const Objects = require('core-functions/objects'); | ||
``` | ||
To use the Array utilities | ||
```js | ||
const Arrays = require('core-functions/arrays'); | ||
``` | ||
To use the Timer utilities | ||
```js | ||
const timers = require('core-functions/timers'); | ||
``` | ||
To use the standard application errors | ||
```js | ||
const appErrors = require('../app-errors'); | ||
const AppError = appErrors.AppError; | ||
// 400-series | ||
const BadRequest = appErrors.BadRequest; | ||
const Unauthorized = appErrors.Unauthorized; | ||
const Forbidden = appErrors.Forbidden; | ||
const NotFound = appErrors.NotFound; | ||
const RequestTimeout = appErrors.RequestTimeout; | ||
const TooManyRequests = appErrors.TooManyRequests; | ||
// 500-series | ||
const InternalServerError = appErrors.InternalServerError; | ||
const BadGateway = appErrors.BadGateway; | ||
const ServiceUnavailable = appErrors.ServiceUnavailable; | ||
const GatewayTimeout = appErrors.GatewayTimeout; | ||
// HTTP status codes with explicit class support and allowed to pass through to API Gateway by default | ||
const supportedHttpStatusCodes = appErrors.supportedHttpStatusCodes; | ||
// Error conversion functions | ||
const toAppError = appErrors.toAppError; | ||
const toAppErrorForApiGateway = appErrors.toAppErrorForApiGateway; | ||
``` | ||
## Unit tests | ||
@@ -53,2 +114,18 @@ This module's unit tests were developed with and must be run with [tape](https://www.npmjs.com/package/tape). The unit tests have been tested on [Node.js v4.3.2](https://nodejs.org/en/blog/release/v4.3.2/). | ||
- strings: Added `trimOrEmpty` function | ||
- strings: Renamed `safeTrim` function to `trim` and changed `safeTrim` to an alias for `trim` | ||
- strings: Renamed `safeTrim` function to `trim` and changed `safeTrim` to an alias for `trim` | ||
### 1.1.1 | ||
- Simple increment of version number to fix issue of 1.1.0 tag pointing to wrong version | ||
### 1.2.0 | ||
- strings: | ||
- Improvements to `stringify` function to better handle functions and arrays | ||
- promises: | ||
- Added `isPromise` function | ||
- Added optional `cancellable` argument to `delay` function to enable cancellation of delay's timeout | ||
- Added new modules: | ||
- app-errors | ||
- arrays | ||
- objects | ||
- timers | ||
- Added unit tests for existing and new functions and classes. |
@@ -27,3 +27,3 @@ 'use strict'; | ||
/** Returns the given value as a string with special case handling for various types */ | ||
stringify: stringify | ||
stringify: stringify, | ||
}; | ||
@@ -89,5 +89,7 @@ | ||
typeof value === 'string' ? value : | ||
value instanceof String ? stringify(value.valueOf()) : | ||
Numbers.isSpecialNumber(value) || value instanceof Error ? `${value}` : JSON.stringify(value); | ||
value instanceof String ? value.valueOf() : | ||
Numbers.isSpecialNumber(value) || value instanceof Error ? `${value}` : | ||
typeof value === 'function' ? isNotBlank(value.name) ? `[Function: ${value.name}]` : '[Function: anonymous]' : | ||
value instanceof Array ? `[${value.map(stringify).join(", ")}]` : | ||
JSON.stringify(value); | ||
} | ||
'use strict'; | ||
/** | ||
* Unit tests for core-functions/base64.js | ||
* @author Byron du Preez | ||
*/ | ||
const test = require('tape'); | ||
@@ -65,3 +70,3 @@ | ||
// TODO Arrays don't seem to throw errors, but also don't return original value | ||
// Arrays don't seem to throw errors, but they also don't return the original value | ||
// checkToBase64AndBackToUtf8([], t); // returns '' | ||
@@ -68,0 +73,0 @@ // checkToBase64AndBackToUtf8(['abc', '123'], t); // returns '\x00{' |
'use strict'; | ||
/** | ||
* Unit tests for core-functions/booleans.js | ||
* @author Byron du Preez | ||
*/ | ||
const test = require('tape'); | ||
@@ -7,149 +12,155 @@ | ||
function stringify(value, v) { | ||
const val = value === +Infinity || value === -Infinity || (typeof value === 'number' && Number.isNaN(value)) ? | ||
value : JSON.stringify(value); | ||
return v instanceof Boolean ? `Boolean(${val}) with valueOf (${v.valueOf()})` : val; | ||
const Strings = require('../strings'); | ||
const testing = require('./testing'); | ||
// const okNotOk = testing.okNotOk; | ||
const checkOkNotOk = testing.checkOkNotOk; | ||
// const checkMethodOkNotOk = testing.checkMethodOkNotOk; | ||
// const equal = testing.equal; | ||
const checkEqual = testing.checkEqual; | ||
// const checkMethodEqual = testing.checkMethodEqual; | ||
function wrap(value, wrapInBoolean) { | ||
return wrapInBoolean && !(value instanceof Boolean) ? new Boolean(value) : value; | ||
} | ||
function checkOkNotOk(fn, value, expected, wrapInBoolean, okSuffix, notOkSuffix, t) { | ||
const v = wrapInBoolean && !(value instanceof Boolean) ? new Boolean(value) : value; | ||
if (expected) { | ||
t.ok(fn(v), `${stringify(value, v)}${okSuffix}`); | ||
} else { | ||
t.notOk(fn(v), `${stringify(value, v)}${notOkSuffix}`); | ||
} | ||
function toPrefix(value, wrapInBoolean) { | ||
const wrapped = wrap(value, wrapInBoolean); | ||
return wrapInBoolean || value instanceof Boolean ? `Boolean(${value}) = (${wrapped ? wrapped.valueOf() : value}) ` : ''; | ||
} | ||
function checkIsBooleans(wrapInBoolean, t) { | ||
function check(value, expected, wrapInBoolean, t) { | ||
return checkOkNotOk(Booleans.isBoolean, value, expected, wrapInBoolean, ' is a boolean', ' is NOT a boolean', t); | ||
function checkIsBooleans(t, wrapInBoolean) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Booleans.isBoolean, [wrap(value, wrapInBoolean)], expected, ' is a boolean', | ||
' is NOT a boolean', toPrefix(value, wrapInBoolean)); | ||
} | ||
// undefined | ||
check(undefined, wrapInBoolean, wrapInBoolean, t); | ||
check(undefined, wrapInBoolean); | ||
// null | ||
check(null, wrapInBoolean, wrapInBoolean, t); // '' ? | ||
check(null, wrapInBoolean); // '' ? | ||
// objects | ||
check({}, wrapInBoolean, wrapInBoolean, t); | ||
check({a:1,b:2}, wrapInBoolean, wrapInBoolean, t); | ||
check({}, wrapInBoolean); | ||
check({a: 1, b: 2}, wrapInBoolean); | ||
// booleans | ||
check(true, true, wrapInBoolean, t); | ||
check(false, true, wrapInBoolean, t); | ||
check(true, true); | ||
check(false, true); | ||
// arrays | ||
check([], wrapInBoolean, wrapInBoolean, t); | ||
check([1,2,3], wrapInBoolean, wrapInBoolean, t); | ||
check([], wrapInBoolean); | ||
check([1, 2, 3], wrapInBoolean); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.NEGATIVE_INFINITY, wrapInBoolean, wrapInBoolean, t); | ||
check(NaN, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.POSITIVE_INFINITY, wrapInBoolean); | ||
check(Number.NEGATIVE_INFINITY, wrapInBoolean); | ||
check(NaN, wrapInBoolean); | ||
// negative numbers | ||
check(Number.MIN_VALUE, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.MIN_SAFE_INTEGER, wrapInBoolean, wrapInBoolean, t); | ||
check(-123.456, wrapInBoolean, wrapInBoolean, t); | ||
check(-1, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.MIN_VALUE, wrapInBoolean); | ||
check(Number.MIN_SAFE_INTEGER, wrapInBoolean); | ||
check(-123.456, wrapInBoolean); | ||
check(-1, wrapInBoolean); | ||
// zero | ||
check(0, wrapInBoolean, wrapInBoolean, t); | ||
check(0, wrapInBoolean); | ||
// positive numbers | ||
check(1, wrapInBoolean, wrapInBoolean, t); | ||
check(123.456, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.MAX_SAFE_INTEGER, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.MAX_VALUE, wrapInBoolean, wrapInBoolean, t); | ||
check(1, wrapInBoolean); | ||
check(123.456, wrapInBoolean); | ||
check(Number.MAX_SAFE_INTEGER, wrapInBoolean); | ||
check(Number.MAX_VALUE, wrapInBoolean); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, wrapInBoolean, wrapInBoolean, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, wrapInBoolean, wrapInBoolean, t); | ||
check('-123.456', wrapInBoolean, wrapInBoolean, t); | ||
check('-1', wrapInBoolean, wrapInBoolean, t); | ||
check(`${Number.MIN_VALUE}`, wrapInBoolean); | ||
check(`${Number.MIN_SAFE_INTEGER}`, wrapInBoolean); | ||
check('-123.456', wrapInBoolean); | ||
check('-1', wrapInBoolean); | ||
check('0', wrapInBoolean, wrapInBoolean, t); | ||
check('0', wrapInBoolean); | ||
check('1', wrapInBoolean, wrapInBoolean, t); | ||
check('123.456', wrapInBoolean, wrapInBoolean, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, wrapInBoolean, wrapInBoolean, t); | ||
check(`${Number.MAX_VALUE}`, wrapInBoolean, wrapInBoolean, t); | ||
check('1', wrapInBoolean); | ||
check('123.456', wrapInBoolean); | ||
check(`${Number.MAX_SAFE_INTEGER}`, wrapInBoolean); | ||
check(`${Number.MAX_VALUE}`, wrapInBoolean); | ||
// strings containing booleans | ||
check('true', wrapInBoolean, wrapInBoolean, t); | ||
check('false', wrapInBoolean, wrapInBoolean, t); | ||
check('true', wrapInBoolean); | ||
check('false', wrapInBoolean); | ||
// strings not containing numbers | ||
check('', wrapInBoolean, wrapInBoolean, t); | ||
check('abc', wrapInBoolean, wrapInBoolean, t); | ||
check('ABC', wrapInBoolean, wrapInBoolean, t); | ||
check('', wrapInBoolean); | ||
check('abc', wrapInBoolean); | ||
check('ABC', wrapInBoolean); | ||
} | ||
function checkIsTrueOrFalses(wrapInBoolean, t) { | ||
function check(value, expected, wrapInBoolean, t) { | ||
return checkOkNotOk(Booleans.isTrueOrFalse, value, expected, wrapInBoolean, ' is true or false', ' is NOT true or false', t); | ||
function checkIsTrueOrFalses(t, wrapInBoolean) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Booleans.isTrueOrFalse, [wrap(value, wrapInBoolean)], expected, ' is true or false', | ||
' is NOT true or false', toPrefix(value, wrapInBoolean)); | ||
} | ||
// undefined | ||
check(undefined, wrapInBoolean, wrapInBoolean, t); | ||
check(undefined, wrapInBoolean); | ||
// null | ||
check(null, wrapInBoolean, wrapInBoolean, t); // '' ? | ||
check(null, wrapInBoolean); // '' ? | ||
// objects | ||
check({}, wrapInBoolean, wrapInBoolean, t); | ||
check({a:1,b:2}, wrapInBoolean, wrapInBoolean, t); | ||
check({}, wrapInBoolean); | ||
check({a: 1, b: 2}, wrapInBoolean); | ||
// booleans | ||
check(true, true, wrapInBoolean, t); | ||
check(false, true, wrapInBoolean, t); | ||
check(true, true); | ||
check(false, true); | ||
// arrays | ||
check([], wrapInBoolean, wrapInBoolean, t); | ||
check([1,2,3], wrapInBoolean, wrapInBoolean, t); | ||
check([], wrapInBoolean); | ||
check([1, 2, 3], wrapInBoolean); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.NEGATIVE_INFINITY, wrapInBoolean, wrapInBoolean, t); | ||
check(NaN, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.POSITIVE_INFINITY, wrapInBoolean); | ||
check(Number.NEGATIVE_INFINITY, wrapInBoolean); | ||
check(NaN, wrapInBoolean); | ||
// negative numbers | ||
check(Number.MIN_VALUE, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.MIN_SAFE_INTEGER, wrapInBoolean, wrapInBoolean, t); | ||
check(-123.456, wrapInBoolean, wrapInBoolean, t); | ||
check(-1, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.MIN_VALUE, wrapInBoolean); | ||
check(Number.MIN_SAFE_INTEGER, wrapInBoolean); | ||
check(-123.456, wrapInBoolean); | ||
check(-1, wrapInBoolean); | ||
// zero | ||
check(0, wrapInBoolean, wrapInBoolean, t); | ||
check(0, wrapInBoolean); | ||
// positive numbers | ||
check(1, wrapInBoolean, wrapInBoolean, t); | ||
check(123.456, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.MAX_SAFE_INTEGER, wrapInBoolean, wrapInBoolean, t); | ||
check(Number.MAX_VALUE, wrapInBoolean, wrapInBoolean, t); | ||
check(1, wrapInBoolean); | ||
check(123.456, wrapInBoolean); | ||
check(Number.MAX_SAFE_INTEGER, wrapInBoolean); | ||
check(Number.MAX_VALUE, wrapInBoolean); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, wrapInBoolean, wrapInBoolean, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, wrapInBoolean, wrapInBoolean, t); | ||
check('-123.456', wrapInBoolean, wrapInBoolean, t); | ||
check('-1', wrapInBoolean, wrapInBoolean, t); | ||
check(`${Number.MIN_VALUE}`, wrapInBoolean); | ||
check(`${Number.MIN_SAFE_INTEGER}`, wrapInBoolean); | ||
check('-123.456', wrapInBoolean); | ||
check('-1', wrapInBoolean); | ||
check('0', wrapInBoolean, wrapInBoolean, t); | ||
check('0', wrapInBoolean); | ||
check('1', wrapInBoolean, wrapInBoolean, t); | ||
check('123.456', wrapInBoolean, wrapInBoolean, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, wrapInBoolean, wrapInBoolean, t); | ||
check(`${Number.MAX_VALUE}`, wrapInBoolean, wrapInBoolean, t); | ||
check('1', wrapInBoolean); | ||
check('123.456', wrapInBoolean); | ||
check(`${Number.MAX_SAFE_INTEGER}`, wrapInBoolean); | ||
check(`${Number.MAX_VALUE}`, wrapInBoolean); | ||
// strings containing booleans | ||
check('true', wrapInBoolean, wrapInBoolean, t); | ||
check('false', wrapInBoolean, wrapInBoolean, t); | ||
check('true', wrapInBoolean); | ||
check('false', wrapInBoolean); | ||
// strings not containing numbers | ||
check('', wrapInBoolean, wrapInBoolean, t); | ||
check('abc', wrapInBoolean, wrapInBoolean, t); | ||
check('ABC', wrapInBoolean, wrapInBoolean, t); | ||
check('', wrapInBoolean); | ||
check('abc', wrapInBoolean); | ||
check('ABC', wrapInBoolean); | ||
} | ||
test('isTrueOrFalse on Booleans', t => { | ||
checkIsTrueOrFalses(true, t); | ||
checkIsTrueOrFalses(t, true); | ||
t.end(); | ||
@@ -159,3 +170,3 @@ }); | ||
test('isTrueOrFalse on booleans', t => { | ||
checkIsTrueOrFalses(false, t); | ||
checkIsTrueOrFalses(t, false); | ||
t.end(); | ||
@@ -165,3 +176,3 @@ }); | ||
test('isBoolean on Booleans', t => { | ||
checkIsBooleans(true, t); | ||
checkIsBooleans(t, true); | ||
t.end(); | ||
@@ -171,4 +182,4 @@ }); | ||
test('isBoolean on booleans', t => { | ||
checkIsBooleans(false, t); | ||
checkIsBooleans(t, false); | ||
t.end(); | ||
}); |
'use strict'; | ||
/** | ||
* Unit tests for core-functions/numbers.js | ||
* @author Byron du Preez | ||
*/ | ||
const test = require('tape'); | ||
@@ -7,280 +12,279 @@ | ||
function isNaN(value) { | ||
return (typeof value === 'number' && Number.isNaN(value)) || (value instanceof Number && isNaN(value.valueOf())); | ||
} | ||
const testing = require('./testing'); | ||
// const okNotOk = testing.okNotOk; | ||
const checkOkNotOk = testing.checkOkNotOk; | ||
// const checkMethodOkNotOk = testing.checkMethodOkNotOk; | ||
// const equal = testing.equal; | ||
const checkEqual = testing.checkEqual; | ||
// const checkMethodEqual = testing.checkMethodEqual; | ||
function stringify(value, v) { | ||
const val = value === +Infinity || value === -Infinity || isNaN(value) ? value : JSON.stringify(value); | ||
return v instanceof Number ? `Number(${val}) with valueOf (${v.valueOf()})` : val; | ||
function wrap(value, wrapInNumber) { | ||
return wrapInNumber && !(value instanceof Number) ? new Number(value) : value; | ||
} | ||
function checkOkNotOk(fn, value, expected, wrapInNumber, okSuffix, notOkSuffix, t) { | ||
const v = wrapInNumber && !(value instanceof Number) ? new Number(value) : value; | ||
if (expected) { | ||
t.ok(fn(v), `${stringify(value, v)}${okSuffix}`); | ||
} else { | ||
t.notOk(fn(v), `${stringify(value, v)}${notOkSuffix}`); | ||
} | ||
function toPrefix(value, wrapInNumber) { | ||
const wrapped = wrap(value, wrapInNumber); | ||
return wrapInNumber || value instanceof Number ? `Number(${value}) = (${wrapped ? wrapped.valueOf() : value}) ` : ''; | ||
} | ||
function checkIsNumber(wrapInNumber, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkOkNotOk(Numbers.isNumber, value, expected, wrapInString, ' is a number', ' is NOT a number', t); | ||
function checkIsNumber(t, wrapInNumber) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Numbers.isNumber, [wrap(value, wrapInNumber)], expected, ' is a number', ' is NOT a number', | ||
toPrefix(value, wrapInNumber)); | ||
} | ||
// undefined | ||
check(undefined, wrapInNumber, wrapInNumber, t); | ||
check(undefined, wrapInNumber); | ||
// null | ||
check(null, wrapInNumber, wrapInNumber, t); | ||
check(null, wrapInNumber); | ||
// objects | ||
check({}, wrapInNumber, wrapInNumber, t); | ||
check({a:1,b:2}, wrapInNumber, wrapInNumber, t); | ||
check({}, wrapInNumber); | ||
check({a: 1, b: 2}, wrapInNumber); | ||
// booleans | ||
check(true, wrapInNumber, wrapInNumber, t); // 1 when wrapped | ||
check(false, wrapInNumber, wrapInNumber, t); // 0 when wrapped | ||
check(true, wrapInNumber); // 1 when wrapped | ||
check(false, wrapInNumber); // 0 when wrapped | ||
// arrays | ||
check([], wrapInNumber, wrapInNumber, t); | ||
check([1,2,3], wrapInNumber, wrapInNumber, t); | ||
check([], wrapInNumber); | ||
check([1, 2, 3], wrapInNumber); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, true, wrapInNumber, t); | ||
check(Number.NEGATIVE_INFINITY, true, wrapInNumber, t); | ||
check(NaN, true, wrapInNumber, t); | ||
check(Number.POSITIVE_INFINITY, true); | ||
check(Number.NEGATIVE_INFINITY, true); | ||
check(NaN, true); | ||
// negative numbers | ||
check(Number.MIN_VALUE, true, wrapInNumber, t); | ||
check(Number.MIN_SAFE_INTEGER, true, wrapInNumber, t); | ||
check(-123.456, true, wrapInNumber, t); | ||
check(-1, true, wrapInNumber, t); | ||
check(Number.MIN_VALUE, true); | ||
check(Number.MIN_SAFE_INTEGER, true); | ||
check(-123.456, true); | ||
check(-1, true); | ||
// zero | ||
check(0, true, wrapInNumber, t); | ||
check(0, true); | ||
// positive numbers | ||
check(1, true, wrapInNumber, t); | ||
check(123.456, true, wrapInNumber, t); | ||
check(Number.MAX_SAFE_INTEGER, true, wrapInNumber, t); | ||
check(Number.MAX_VALUE, true, wrapInNumber, t); | ||
check(1, true); | ||
check(123.456, true); | ||
check(Number.MAX_SAFE_INTEGER, true); | ||
check(Number.MAX_VALUE, true); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, wrapInNumber, wrapInNumber, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, wrapInNumber, wrapInNumber, t); | ||
check('-123.456', wrapInNumber, wrapInNumber, t); | ||
check('-1', wrapInNumber, wrapInNumber, t); | ||
check(`${Number.MIN_VALUE}`, wrapInNumber); | ||
check(`${Number.MIN_SAFE_INTEGER}`, wrapInNumber); | ||
check('-123.456', wrapInNumber); | ||
check('-1', wrapInNumber); | ||
check('0', wrapInNumber, wrapInNumber, t); | ||
check('0', wrapInNumber); | ||
check('1', wrapInNumber, wrapInNumber, t); | ||
check('123.456', wrapInNumber, wrapInNumber, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, wrapInNumber, wrapInNumber, t); | ||
check(`${Number.MAX_VALUE}`, wrapInNumber, wrapInNumber, t); | ||
check('1', wrapInNumber); | ||
check('123.456', wrapInNumber); | ||
check(`${Number.MAX_SAFE_INTEGER}`, wrapInNumber); | ||
check(`${Number.MAX_VALUE}`, wrapInNumber); | ||
// strings not containing numbers | ||
check('', wrapInNumber, wrapInNumber, t); // 0 when wrapped | ||
check('a', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('abc', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('ABC', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('', wrapInNumber); // 0 when wrapped | ||
check('a', wrapInNumber); // NaN when wrapped | ||
check('abc', wrapInNumber); // NaN when wrapped | ||
check('ABC', wrapInNumber); // NaN when wrapped | ||
} | ||
function checkIsFiniteNumber(wrapInNumber, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkOkNotOk(Numbers.isFiniteNumber, value, expected, wrapInString, ' is a finite number', ' is NOT a finite number', t); | ||
function checkIsFiniteNumber(t, wrapInNumber) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Numbers.isFiniteNumber, [wrap(value, wrapInNumber)], expected, ' is a finite number', | ||
' is NOT a finite number', toPrefix(value, wrapInNumber)); | ||
} | ||
// undefined | ||
check(undefined, false, wrapInNumber, t); | ||
check(undefined, false); | ||
// null | ||
check(null, wrapInNumber, wrapInNumber, t); // null becomes 0 when wrapped | ||
check(null, wrapInNumber); // null becomes 0 when wrapped | ||
// objects | ||
check({}, false, wrapInNumber, t); // i.e. NaN | ||
check({a:1,b:2}, false, wrapInNumber, t); // i.e. NaN | ||
check({}, false); // i.e. NaN | ||
check({a: 1, b: 2}, false); // i.e. NaN | ||
// booleans | ||
check(true, wrapInNumber, wrapInNumber, t); // 1 when wrapped | ||
check(false, wrapInNumber, wrapInNumber, t); // 0 when wrapped | ||
check(true, wrapInNumber); // 1 when wrapped | ||
check(false, wrapInNumber); // 0 when wrapped | ||
check([], wrapInNumber, wrapInNumber, t); // i.e. 0 | ||
check([1,2,3], false, wrapInNumber, t); // i.e. NaN | ||
check([], wrapInNumber); // i.e. 0 | ||
check([1, 2, 3], false); // i.e. NaN | ||
check(Number.POSITIVE_INFINITY, false, wrapInNumber, t); | ||
check(Number.NEGATIVE_INFINITY, false, wrapInNumber, t); | ||
check(NaN, false, wrapInNumber, t); | ||
check(Number.POSITIVE_INFINITY, false); | ||
check(Number.NEGATIVE_INFINITY, false); | ||
check(NaN, false); | ||
// negative numbers | ||
check(Number.MIN_VALUE, true, wrapInNumber, t); | ||
check(Number.MIN_SAFE_INTEGER, true, wrapInNumber, t); | ||
check(-123.456, true, wrapInNumber, t); | ||
check(-1, true, wrapInNumber, t); | ||
check(Number.MIN_VALUE, true); | ||
check(Number.MIN_SAFE_INTEGER, true); | ||
check(-123.456, true); | ||
check(-1, true); | ||
// zero | ||
check(0, true, wrapInNumber, t); | ||
check(0, true); | ||
// positive numbers | ||
check(1, true, wrapInNumber, t); | ||
check(123.456, true, wrapInNumber, t); | ||
check(Number.MAX_SAFE_INTEGER, true, wrapInNumber, t); | ||
check(Number.MAX_VALUE, true, wrapInNumber, t); | ||
check(1, true); | ||
check(123.456, true); | ||
check(Number.MAX_SAFE_INTEGER, true); | ||
check(Number.MAX_VALUE, true); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, wrapInNumber, wrapInNumber, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, wrapInNumber, wrapInNumber, t); | ||
check('-123.456', wrapInNumber, wrapInNumber, t); | ||
check('-1', wrapInNumber, wrapInNumber, t); | ||
check(`${Number.MIN_VALUE}`, wrapInNumber); | ||
check(`${Number.MIN_SAFE_INTEGER}`, wrapInNumber); | ||
check('-123.456', wrapInNumber); | ||
check('-1', wrapInNumber); | ||
check('0', wrapInNumber, wrapInNumber, t); | ||
check('0', wrapInNumber); | ||
check('1', wrapInNumber, wrapInNumber, t); | ||
check('123.456', wrapInNumber, wrapInNumber, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, wrapInNumber, wrapInNumber, t); | ||
check(`${Number.MAX_VALUE}`, wrapInNumber, wrapInNumber, t); | ||
check('1', wrapInNumber); | ||
check('123.456', wrapInNumber); | ||
check(`${Number.MAX_SAFE_INTEGER}`, wrapInNumber); | ||
check(`${Number.MAX_VALUE}`, wrapInNumber); | ||
// strings not containing numbers | ||
check('abc', false, wrapInNumber, t); // NaN when wrapped | ||
check('', wrapInNumber, wrapInNumber, t); // 0 when wrapped | ||
check('abc', false); // NaN when wrapped | ||
check('', wrapInNumber); // 0 when wrapped | ||
} | ||
function checkIsSpecialNumber(wrapInNumber, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkOkNotOk(Numbers.isSpecialNumber, value, expected, wrapInString, ' is a special number', ' is NOT a special number', t); | ||
function checkIsSpecialNumber(t, wrapInNumber) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Numbers.isSpecialNumber, [wrap(value, wrapInNumber)], expected, ' is a special number', | ||
' is NOT a special number', toPrefix(value, wrapInNumber)); | ||
} | ||
// undefined | ||
check(undefined, wrapInNumber, wrapInNumber, t); // NaN wrapped | ||
check(undefined, wrapInNumber); // NaN wrapped | ||
// null | ||
check(null, false, wrapInNumber, t); // 0 wrapped | ||
check(null, false); // 0 wrapped | ||
// objects | ||
check({}, wrapInNumber, wrapInNumber, t); // NaN wrapped | ||
check({a:1,b:2}, wrapInNumber, wrapInNumber, t); // NaN wrapped | ||
check({}, wrapInNumber); // NaN wrapped | ||
check({a: 1, b: 2}, wrapInNumber); // NaN wrapped | ||
// booleans | ||
check(true, false, wrapInNumber, t); // 1 when wrapped | ||
check(false, false, wrapInNumber, t); // 0 when wrapped | ||
check(true, false); // 1 when wrapped | ||
check(false, false); // 0 when wrapped | ||
// arrays | ||
check([], false, wrapInNumber, t); // 0 when wrapped | ||
check([1,2,3], wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check([], false); // 0 when wrapped | ||
check([1, 2, 3], wrapInNumber); // NaN when wrapped | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, true, wrapInNumber, t); | ||
check(Number.NEGATIVE_INFINITY, true, wrapInNumber, t); | ||
check(NaN, true, wrapInNumber, t); | ||
check(Number.POSITIVE_INFINITY, true); | ||
check(Number.NEGATIVE_INFINITY, true); | ||
check(NaN, true); | ||
// negative numbers | ||
check(Number.MIN_VALUE, false, wrapInNumber, t); | ||
check(Number.MIN_SAFE_INTEGER, false, wrapInNumber, t); | ||
check(-123.456, false, wrapInNumber, t); | ||
check(-1, false, wrapInNumber, t); | ||
check(Number.MIN_VALUE, false); | ||
check(Number.MIN_SAFE_INTEGER, false); | ||
check(-123.456, false); | ||
check(-1, false); | ||
// zero | ||
check(0, false, wrapInNumber, t); | ||
check(0, false); | ||
// positive numbers | ||
check(1, false, wrapInNumber, t); | ||
check(123.456, false, wrapInNumber, t); | ||
check(Number.MAX_SAFE_INTEGER, false, wrapInNumber, t); | ||
check(Number.MAX_VALUE, false, wrapInNumber, t); | ||
check(1, false); | ||
check(123.456, false); | ||
check(Number.MAX_SAFE_INTEGER, false); | ||
check(Number.MAX_VALUE, false); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, false, wrapInNumber, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, false, wrapInNumber, t); | ||
check('-123.456', false, wrapInNumber, t); | ||
check('-1', false, wrapInNumber, t); | ||
check(`${Number.MIN_VALUE}`, false); | ||
check(`${Number.MIN_SAFE_INTEGER}`, false); | ||
check('-123.456', false); | ||
check('-1', false); | ||
check('0', false, wrapInNumber, t); | ||
check('0', false); | ||
check('1', false, wrapInNumber, t); | ||
check('123.456', false, wrapInNumber, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, false, wrapInNumber, t); | ||
check(`${Number.MAX_VALUE}`, false, wrapInNumber, t); | ||
check('1', false); | ||
check('123.456', false); | ||
check(`${Number.MAX_SAFE_INTEGER}`, false); | ||
check(`${Number.MAX_VALUE}`, false); | ||
// special case numbers in strings | ||
check(`${Number.POSITIVE_INFINITY}`, wrapInNumber, wrapInNumber, t); | ||
check(`${Number.NEGATIVE_INFINITY}`, wrapInNumber, wrapInNumber, t); | ||
check(`${NaN}`, wrapInNumber, wrapInNumber, t); | ||
check(`${Number.POSITIVE_INFINITY}`, wrapInNumber); | ||
check(`${Number.NEGATIVE_INFINITY}`, wrapInNumber); | ||
check(`${NaN}`, wrapInNumber); | ||
// strings not containing numbers | ||
check('', false, wrapInNumber, t); // 0 when wrapped | ||
check('a', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('abc', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('ABC', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('', false); // 0 when wrapped | ||
check('a', wrapInNumber); // NaN when wrapped | ||
check('abc', wrapInNumber); // NaN when wrapped | ||
check('ABC', wrapInNumber); // NaN when wrapped | ||
} | ||
function checkIsNaN(wrapInNumber, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkOkNotOk(Numbers.isNaN, value, expected, wrapInString, ' is NaN', ' is NOT NaN', t); | ||
function checkIsNaN(t, wrapInNumber) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Numbers.isNaN, [wrap(value, wrapInNumber)], expected, ' is NaN', ' is NOT NaN', | ||
toPrefix(value, wrapInNumber)); | ||
} | ||
// undefined | ||
check(undefined, wrapInNumber, wrapInNumber, t); // NaN wrapped | ||
check(undefined, wrapInNumber); // NaN wrapped | ||
// null | ||
check(null, false, wrapInNumber, t); // 0 wrapped | ||
check(null, false); // 0 wrapped | ||
// objects | ||
check({}, wrapInNumber, wrapInNumber, t); // NaN wrapped | ||
check({a:1,b:2}, wrapInNumber, wrapInNumber, t); // NaN wrapped | ||
check({}, wrapInNumber); // NaN wrapped | ||
check({a: 1, b: 2}, wrapInNumber); // NaN wrapped | ||
// booleans | ||
check(true, false, wrapInNumber, t); // 1 when wrapped | ||
check(false, false, wrapInNumber, t); // 0 when wrapped | ||
check(true, false); // 1 when wrapped | ||
check(false, false); // 0 when wrapped | ||
// arrays | ||
check([], false, wrapInNumber, t); // 0 when wrapped | ||
check([1,2,3], wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check([], false); // 0 when wrapped | ||
check([1, 2, 3], wrapInNumber); // NaN when wrapped | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, false, wrapInNumber, t); | ||
check(Number.NEGATIVE_INFINITY, false, wrapInNumber, t); | ||
check(NaN, true, wrapInNumber, t); | ||
check(Number.POSITIVE_INFINITY, false); | ||
check(Number.NEGATIVE_INFINITY, false); | ||
check(NaN, true); | ||
// negative numbers | ||
check(Number.MIN_VALUE, false, wrapInNumber, t); | ||
check(Number.MIN_SAFE_INTEGER, false, wrapInNumber, t); | ||
check(-123.456, false, wrapInNumber, t); | ||
check(-1, false, wrapInNumber, t); | ||
check(Number.MIN_VALUE, false); | ||
check(Number.MIN_SAFE_INTEGER, false); | ||
check(-123.456, false); | ||
check(-1, false); | ||
// zero | ||
check(0, false, wrapInNumber, t); | ||
check(0, false); | ||
// positive numbers | ||
check(1, false, wrapInNumber, t); | ||
check(123.456, false, wrapInNumber, t); | ||
check(Number.MAX_SAFE_INTEGER, false, wrapInNumber, t); | ||
check(Number.MAX_VALUE, false, wrapInNumber, t); | ||
check(1, false); | ||
check(123.456, false); | ||
check(Number.MAX_SAFE_INTEGER, false); | ||
check(Number.MAX_VALUE, false); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, false, wrapInNumber, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, false, wrapInNumber, t); | ||
check('-123.456', false, wrapInNumber, t); | ||
check('-1', false, wrapInNumber, t); | ||
check(`${Number.MIN_VALUE}`, false); | ||
check(`${Number.MIN_SAFE_INTEGER}`, false); | ||
check('-123.456', false); | ||
check('-1', false); | ||
check('0', false, wrapInNumber, t); | ||
check('0', false); | ||
check('1', false, wrapInNumber, t); | ||
check('123.456', false, wrapInNumber, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, false, wrapInNumber, t); | ||
check(`${Number.MAX_VALUE}`, false, wrapInNumber, t); | ||
check('1', false); | ||
check('123.456', false); | ||
check(`${Number.MAX_SAFE_INTEGER}`, false); | ||
check(`${Number.MAX_VALUE}`, false); | ||
// special case numbers in strings | ||
check(`${Number.POSITIVE_INFINITY}`, wrapInNumber, wrapInNumber, t); | ||
check(`${Number.NEGATIVE_INFINITY}`, wrapInNumber, wrapInNumber, t); | ||
check(`${NaN}`, wrapInNumber, wrapInNumber, t); | ||
check(`${Number.POSITIVE_INFINITY}`, wrapInNumber); | ||
check(`${Number.NEGATIVE_INFINITY}`, wrapInNumber); | ||
check(`${NaN}`, wrapInNumber); | ||
// strings not containing numbers | ||
check('', false, wrapInNumber, t); // 0 when wrapped | ||
check('a', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('abc', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('ABC', wrapInNumber, wrapInNumber, t); // NaN when wrapped | ||
check('', false); // 0 when wrapped | ||
check('a', wrapInNumber); // NaN when wrapped | ||
check('abc', wrapInNumber); // NaN when wrapped | ||
check('ABC', wrapInNumber); // NaN when wrapped | ||
} | ||
test('isNumber on Numbers', t => { | ||
checkIsNumber(true, t); | ||
checkIsNumber(t, true); | ||
t.end(); | ||
@@ -290,3 +294,3 @@ }); | ||
test('isNumber on numbers', t => { | ||
checkIsNumber(false, t); | ||
checkIsNumber(t, false); | ||
t.end(); | ||
@@ -296,3 +300,3 @@ }); | ||
test('isFiniteNumber on Numbers', t => { | ||
checkIsFiniteNumber(true, t); | ||
checkIsFiniteNumber(t, true); | ||
t.end(); | ||
@@ -302,3 +306,3 @@ }); | ||
test('isFiniteNumber on numbers', t => { | ||
checkIsFiniteNumber(false, t); | ||
checkIsFiniteNumber(t, false); | ||
t.end(); | ||
@@ -308,3 +312,3 @@ }); | ||
test('isSpecialNumber on numbers', t => { | ||
checkIsSpecialNumber(false, t); | ||
checkIsSpecialNumber(t, false); | ||
t.end(); | ||
@@ -314,3 +318,3 @@ }); | ||
test('isSpecialNumber on Numbers', t => { | ||
checkIsSpecialNumber(true, t); | ||
checkIsSpecialNumber(t, true); | ||
t.end(); | ||
@@ -320,3 +324,3 @@ }); | ||
test('isNaN on numbers', t => { | ||
checkIsNaN(false, t); | ||
checkIsNaN(t, false); | ||
t.end(); | ||
@@ -326,4 +330,4 @@ }); | ||
test('isNaN on Numbers', t => { | ||
checkIsNaN(false, t); | ||
checkIsNaN(t, false); | ||
t.end(); | ||
}); |
{ | ||
"name": "core-functions-tests", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"author": "Byron du Preez", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
'use strict'; | ||
/** | ||
* Unit tests for core-functions/strings.js | ||
* @author Byron du Preez | ||
*/ | ||
const test = require('tape'); | ||
const Strings = require('../strings'); | ||
const Numbers = require('../numbers'); | ||
function stringify(value, v) { | ||
const val = value === +Infinity || value === -Infinity || Numbers.isNaN(value) ? | ||
value : JSON.stringify(value); | ||
return v instanceof String ? `String(${val}) with valueOf ("${v.valueOf()}")` : val; | ||
} | ||
const testing = require('./testing'); | ||
// const okNotOk = testing.okNotOk; | ||
const checkOkNotOk = testing.checkOkNotOk; | ||
// const checkMethodOkNotOk = testing.checkMethodOkNotOk; | ||
// const equal = testing.equal; | ||
const checkEqual = testing.checkEqual; | ||
// const checkMethodEqual = testing.checkMethodEqual; | ||
function checkOkNotOk(fn, value, expected, wrapInString, okSuffix, notOkSuffix, t) { | ||
const v = wrapInString && !(value instanceof String) ? new String(value) : value; | ||
if (expected) { | ||
t.ok(fn(v), `${stringify(value, v)}${okSuffix}`); | ||
} else { | ||
t.notOk(fn(v), `${stringify(value, v)}${notOkSuffix}`); | ||
} | ||
function wrap(value, wrapInString) { | ||
return wrapInString && !(value instanceof String) ? new String(value) : value; | ||
} | ||
function checkEqual(fn, value, expected, wrapInString, equalSuffix, t) { | ||
const v = wrapInString && !(value instanceof String) ? new String(value) : value; | ||
const result = fn(v); | ||
if (Numbers.isNaN(result)) { | ||
if (Numbers.isNaN(expected)) { | ||
t.pass(`${stringify(value, v)} must be NaN`); | ||
} else { | ||
t.fail(`${stringify(value, v)} must NOT be NaN`) | ||
} | ||
} else if (result instanceof Object) { | ||
t.deepEqual(result, expected, `${stringify(value, v)}${equalSuffix}`); | ||
} else { | ||
t.equal(result, expected, `${stringify(value, v)}${equalSuffix}`); | ||
} | ||
function toPrefix(value, wrapInString) { | ||
const wrapped = wrap(value, wrapInString); | ||
return wrapInString || value instanceof String ? `String(${value}) = (${wrapped ? wrapped.valueOf() : value}) ` : ''; | ||
} | ||
function checkIsStrings(wrapInString, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkOkNotOk(Strings.isString, value, expected, wrapInString, ' is a string', ' is NOT a string', t); | ||
function checkIsString(t, wrapInString) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Strings.isString, [wrap(value, wrapInString)], expected, ' is a string', ' is NOT a string', | ||
toPrefix(value, wrapInString)); | ||
} | ||
// undefined | ||
check(undefined, wrapInString, wrapInString, t); | ||
check(undefined, wrapInString); | ||
// null | ||
check(null, wrapInString, wrapInString, t); // '' ? | ||
check(null, wrapInString); // '' ? | ||
// objects | ||
check({}, wrapInString, wrapInString, t); | ||
check({a: 1, b: 2}, wrapInString, wrapInString, t); | ||
check({}, wrapInString); | ||
check({a: 1, b: 2}, wrapInString); | ||
// booleans | ||
check(true, wrapInString, wrapInString, t); | ||
check(false, wrapInString, wrapInString, t); | ||
check(true, wrapInString); | ||
check(false, wrapInString); | ||
// arrays | ||
check([], wrapInString, wrapInString, t); | ||
check([1, 2, 3], wrapInString, wrapInString, t); | ||
check([], wrapInString); | ||
check([1, 2, 3], wrapInString); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, wrapInString, wrapInString, t); | ||
check(Number.NEGATIVE_INFINITY, wrapInString, wrapInString, t); | ||
check(NaN, wrapInString, wrapInString, t); | ||
check(Number.POSITIVE_INFINITY, wrapInString); | ||
check(Number.NEGATIVE_INFINITY, wrapInString); | ||
check(NaN, wrapInString); | ||
// negative numbers | ||
check(Number.MIN_VALUE, wrapInString, wrapInString, t); | ||
check(Number.MIN_SAFE_INTEGER, wrapInString, wrapInString, t); | ||
check(-123.456, wrapInString, wrapInString, t); | ||
check(-1, wrapInString, wrapInString, t); | ||
check(Number.MIN_VALUE, wrapInString); | ||
check(Number.MIN_SAFE_INTEGER, wrapInString); | ||
check(-123.456, wrapInString); | ||
check(-1, wrapInString); | ||
// zero | ||
check(0, wrapInString, wrapInString, t); | ||
check(0, wrapInString); | ||
// positive numbers | ||
check(1, wrapInString, wrapInString, t); | ||
check(123.456, wrapInString, wrapInString, t); | ||
check(Number.MAX_SAFE_INTEGER, wrapInString, wrapInString, t); | ||
check(Number.MAX_VALUE, wrapInString, wrapInString, t); | ||
check(1, wrapInString); | ||
check(123.456, wrapInString); | ||
check(Number.MAX_SAFE_INTEGER, wrapInString); | ||
check(Number.MAX_VALUE, wrapInString); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, true, wrapInString, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, true, wrapInString, t); | ||
check('-123.456', true, wrapInString, t); | ||
check('-1', true, wrapInString, t); | ||
check(`${Number.MIN_VALUE}`, true); | ||
check(`${Number.MIN_SAFE_INTEGER}`, true); | ||
check('-123.456', true); | ||
check('-1', true); | ||
check('0', true, wrapInString, t); | ||
check('0', true); | ||
check('1', true, wrapInString, t); | ||
check('123.456', true, wrapInString, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, true, wrapInString, t); | ||
check(`${Number.MAX_VALUE}`, true, wrapInString, t); | ||
check('1', true); | ||
check('123.456', true); | ||
check(`${Number.MAX_SAFE_INTEGER}`, true); | ||
check(`${Number.MAX_VALUE}`, true); | ||
// strings not containing numbers | ||
check('', true, wrapInString, t); | ||
check('a', true, wrapInString, t); | ||
check('abc', true, wrapInString, t); | ||
check('ABC', true, wrapInString, t); | ||
check('', true); | ||
check('a', true); | ||
check('abc', true); | ||
check('ABC', true); | ||
} | ||
function checkIsBlank(wrapInString, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkOkNotOk(Strings.isBlank, value, expected, wrapInString, ' is blank', ' is NOT blank', t); | ||
function checkIsBlank(t, wrapInString) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Strings.isBlank, [wrap(value, wrapInString)], expected, ' is blank', ' is NOT blank', | ||
toPrefix(value, wrapInString)); | ||
} | ||
// undefined | ||
check(undefined, !wrapInString, wrapInString, t); // blank ? | ||
check(undefined, !wrapInString); // blank ? | ||
// null | ||
check(null, !wrapInString, wrapInString, t); // blank ? | ||
check(null, !wrapInString); // blank ? | ||
// objects | ||
check({}, false, wrapInString, t); | ||
check({a: 1, b: 2}, false, wrapInString, t); | ||
check({}, false); | ||
check({a: 1, b: 2}, false); | ||
// booleans | ||
check(true, false, wrapInString, t); | ||
check(false, !wrapInString, wrapInString, t); // blank ? | ||
check(true, false); | ||
check(false, !wrapInString); // blank ? | ||
// arrays | ||
check([], wrapInString, wrapInString, t); // [] -> '' wrapped | ||
check([1, 2, 3], false, wrapInString, t); | ||
check([], wrapInString); // [] -> '' wrapped | ||
check([1, 2, 3], false); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, false, wrapInString, t); | ||
check(Number.NEGATIVE_INFINITY, false, wrapInString, t); | ||
check(NaN, !wrapInString, wrapInString, t); | ||
check(Number.POSITIVE_INFINITY, false); | ||
check(Number.NEGATIVE_INFINITY, false); | ||
check(NaN, !wrapInString); | ||
// negative numbers | ||
check(Number.MIN_VALUE, false, wrapInString, t); | ||
check(Number.MIN_SAFE_INTEGER, false, wrapInString, t); | ||
check(-123.456, false, wrapInString, t); | ||
check(-1, false, wrapInString, t); | ||
check(Number.MIN_VALUE, false); | ||
check(Number.MIN_SAFE_INTEGER, false); | ||
check(-123.456, false); | ||
check(-1, false); | ||
// zero | ||
check(0, !wrapInString, wrapInString, t); // not sure if this should return blank for 0 | ||
check(0, !wrapInString); // not sure if this should return blank for 0 | ||
// positive numbers | ||
check(1, false, wrapInString, t); | ||
check(123.456, false, wrapInString, t); | ||
check(Number.MAX_SAFE_INTEGER, false, wrapInString, t); | ||
check(Number.MAX_VALUE, false, wrapInString, t); | ||
check(1, false); | ||
check(123.456, false); | ||
check(Number.MAX_SAFE_INTEGER, false); | ||
check(Number.MAX_VALUE, false); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, false, wrapInString, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, false, wrapInString, t); | ||
check('-123.456', false, wrapInString, t); | ||
check('-1', false, wrapInString, t); | ||
check(`${Number.MIN_VALUE}`, false); | ||
check(`${Number.MIN_SAFE_INTEGER}`, false); | ||
check('-123.456', false); | ||
check('-1', false); | ||
check('0', false, wrapInString, t); | ||
check('0', false); | ||
check('1', false, wrapInString, t); | ||
check('123.456', false, wrapInString, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, false, wrapInString, t); | ||
check(`${Number.MAX_VALUE}`, false, wrapInString, t); | ||
check('1', false); | ||
check('123.456', false); | ||
check(`${Number.MAX_SAFE_INTEGER}`, false); | ||
check(`${Number.MAX_VALUE}`, false); | ||
// strings containing only whitespace | ||
check('', true, wrapInString, t); | ||
check(' ', true, wrapInString, t); | ||
check('\n', true, wrapInString, t); | ||
check('\r', true, wrapInString, t); | ||
check('\t', true, wrapInString, t); | ||
check(' ', true, wrapInString, t); | ||
check(' \n ', true, wrapInString, t); | ||
check(' \r ', true, wrapInString, t); | ||
check(' \t ', true, wrapInString, t); | ||
check(' \n \r \t \n \r \t ', true, wrapInString, t); | ||
check('', true); | ||
check(' ', true); | ||
check('\n', true); | ||
check('\r', true); | ||
check('\t', true); | ||
check(' ', true); | ||
check(' \n ', true); | ||
check(' \r ', true); | ||
check(' \t ', true); | ||
check(' \n \r \t \n \r \t ', true); | ||
// strings not containing numbers | ||
check('a', false, wrapInString, t); | ||
check('abc', false, wrapInString, t); | ||
check('ABC', false, wrapInString, t); | ||
check('a', false); | ||
check('abc', false); | ||
check('ABC', false); | ||
} | ||
function checkIsNotBlank(wrapInString, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkOkNotOk(Strings.isNotBlank, value, expected, wrapInString, ' is not blank', ' is blank', t); | ||
function checkIsNotBlank(t, wrapInString) { | ||
function check(value, expected) { | ||
return checkOkNotOk(t, Strings.isNotBlank, [wrap(value, wrapInString)], expected, ' is not blank', ' is blank', | ||
toPrefix(value, wrapInString)); | ||
} | ||
// undefined | ||
check(undefined, wrapInString, wrapInString, t); // blank ? | ||
check(undefined, wrapInString); // blank ? | ||
// null | ||
check(null, wrapInString, wrapInString, t); // blank ? | ||
check(null, wrapInString); // blank ? | ||
// objects | ||
check({}, true, wrapInString, t); | ||
check({a: 1, b: 2}, true, wrapInString, t); | ||
check({}, true); | ||
check({a: 1, b: 2}, true); | ||
// booleans | ||
check(true, true, wrapInString, t); | ||
check(false, wrapInString, wrapInString, t); // blank ? | ||
check(true, true); | ||
check(false, wrapInString); // blank ? | ||
// arrays | ||
check([], !wrapInString, wrapInString, t); // [] -> '' wrapped | ||
check([1, 2, 3], true, wrapInString, t); | ||
check([], !wrapInString); // [] -> '' wrapped | ||
check([1, 2, 3], true); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, true, wrapInString, t); | ||
check(Number.NEGATIVE_INFINITY, true, wrapInString, t); | ||
check(NaN, wrapInString, wrapInString, t); | ||
check(Number.POSITIVE_INFINITY, true); | ||
check(Number.NEGATIVE_INFINITY, true); | ||
check(NaN, wrapInString); | ||
// negative numbers | ||
check(Number.MIN_VALUE, true, wrapInString, t); | ||
check(Number.MIN_SAFE_INTEGER, true, wrapInString, t); | ||
check(-123.456, true, wrapInString, t); | ||
check(-1, true, wrapInString, t); | ||
check(Number.MIN_VALUE, true); | ||
check(Number.MIN_SAFE_INTEGER, true); | ||
check(-123.456, true); | ||
check(-1, true); | ||
// zero | ||
check(0, wrapInString, wrapInString, t); // not sure if this should return blank for 0 | ||
check(0, wrapInString); // not sure if this should return blank for 0 | ||
// positive numbers | ||
check(1, true, wrapInString, t); | ||
check(123.456, true, wrapInString, t); | ||
check(Number.MAX_SAFE_INTEGER, true, wrapInString, t); | ||
check(Number.MAX_VALUE, true, wrapInString, t); | ||
check(1, true); | ||
check(123.456, true); | ||
check(Number.MAX_SAFE_INTEGER, true); | ||
check(Number.MAX_VALUE, true); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, true, wrapInString, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, true, wrapInString, t); | ||
check('-123.456', true, wrapInString, t); | ||
check('-1', true, wrapInString, t); | ||
check(`${Number.MIN_VALUE}`, true); | ||
check(`${Number.MIN_SAFE_INTEGER}`, true); | ||
check('-123.456', true); | ||
check('-1', true); | ||
check('0', true, wrapInString, t); | ||
check('0', true); | ||
check('1', true, wrapInString, t); | ||
check('123.456', true, wrapInString, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, true, wrapInString, t); | ||
check(`${Number.MAX_VALUE}`, true, wrapInString, t); | ||
check('1', true); | ||
check('123.456', true); | ||
check(`${Number.MAX_SAFE_INTEGER}`, true); | ||
check(`${Number.MAX_VALUE}`, true); | ||
// strings containing only whitespace | ||
check('', false, wrapInString, t); | ||
check(' ', false, wrapInString, t); | ||
check('\n', false, wrapInString, t); | ||
check('\r', false, wrapInString, t); | ||
check('\t', false, wrapInString, t); | ||
check(' ', false, wrapInString, t); | ||
check(' \n ', false, wrapInString, t); | ||
check(' \r ', false, wrapInString, t); | ||
check(' \t ', false, wrapInString, t); | ||
check(' \n \r \t \n \r \t ', false, wrapInString, t); | ||
check('', false); | ||
check(' ', false); | ||
check('\n', false); | ||
check('\r', false); | ||
check('\t', false); | ||
check(' ', false); | ||
check(' \n ', false); | ||
check(' \r ', false); | ||
check(' \t ', false); | ||
check(' \n \r \t \n \r \t ', false); | ||
// strings not containing numbers | ||
check('a', true, wrapInString, t); | ||
check('abc', true, wrapInString, t); | ||
check('ABC', true, wrapInString, t); | ||
check('a', true); | ||
check('abc', true); | ||
check('ABC', true); | ||
} | ||
function checkStringify(wrapInString, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkEqual(Strings.stringify, value, expected, wrapInString, ` must be ${stringify(expected, undefined)}`, t); | ||
function checkStringify(t, wrapInString) { | ||
function check(value, expected) { | ||
return checkEqual(t, Strings.stringify, [wrap(value, wrapInString)], expected, toPrefix(value, wrapInString)); | ||
} | ||
// undefined | ||
check(undefined, 'undefined', wrapInString, t); | ||
check(undefined, 'undefined'); | ||
// null | ||
check(null, 'null', wrapInString, t); | ||
check(null, 'null'); | ||
// objects | ||
check({}, wrapInString ? '[object Object]' : '{}', wrapInString, t); | ||
check({a: 1, b: 2}, wrapInString ? '[object Object]' : `${JSON.stringify({a: 1, b: 2})}`, wrapInString, t); | ||
check({}, wrapInString ? '[object Object]' : '{}'); | ||
check({a: 1, b: 2}, wrapInString ? '[object Object]' : `${JSON.stringify({a: 1, b: 2})}`); | ||
// booleans | ||
check(true, 'true', wrapInString, t); | ||
check(false, 'false', wrapInString, t); | ||
check(true, 'true'); | ||
check(false, 'false'); | ||
// arrays | ||
check([], wrapInString ? '' : '[]', wrapInString, t); | ||
check([1, 2, 3], wrapInString ? '1,2,3' : `${JSON.stringify([1, 2, 3])}`, wrapInString, t); | ||
check([], wrapInString ? '' : '[]'); | ||
check([1, 2, 3], wrapInString ? '1,2,3' : '[1, 2, 3]'); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, `${Number.POSITIVE_INFINITY}`, wrapInString, t); | ||
check(Number.NEGATIVE_INFINITY, `${Number.NEGATIVE_INFINITY}`, wrapInString, t); | ||
check(NaN, 'NaN', wrapInString, t); | ||
check(Number.POSITIVE_INFINITY, `${Number.POSITIVE_INFINITY}`); | ||
check(Number.NEGATIVE_INFINITY, `${Number.NEGATIVE_INFINITY}`); | ||
check(NaN, 'NaN'); | ||
// negative numbers | ||
check(Number.MIN_VALUE, `${Number.MIN_VALUE}`, wrapInString, t); | ||
check(Number.MIN_SAFE_INTEGER, `${Number.MIN_SAFE_INTEGER}`, wrapInString, t); | ||
check(-123.456, '-123.456', wrapInString, t); | ||
check(-1, '-1', wrapInString, t); | ||
check(Number.MIN_VALUE, `${Number.MIN_VALUE}`); | ||
check(Number.MIN_SAFE_INTEGER, `${Number.MIN_SAFE_INTEGER}`); | ||
check(-123.456, '-123.456'); | ||
check(-1, '-1'); | ||
// zero | ||
check(0, '0', wrapInString, t); // not sure if this should return blank for 0 | ||
check(0, '0'); // not sure if this should return blank for 0 | ||
// positive numbers | ||
check(1, '1', wrapInString, t); | ||
check(123.456, '123.456', wrapInString, t); | ||
check(Number.MAX_SAFE_INTEGER, `${Number.MAX_SAFE_INTEGER}`, wrapInString, t); | ||
check(Number.MAX_VALUE, `${Number.MAX_VALUE}`, wrapInString, t); | ||
check(1, '1'); | ||
check(123.456, '123.456'); | ||
check(Number.MAX_SAFE_INTEGER, `${Number.MAX_SAFE_INTEGER}`); | ||
check(Number.MAX_VALUE, `${Number.MAX_VALUE}`); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, `${Number.MIN_VALUE}`, wrapInString, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, `${Number.MIN_SAFE_INTEGER}`, wrapInString, t); | ||
check('-123.456', '-123.456', wrapInString, t); | ||
check('-1', '-1', wrapInString, t); | ||
check(`${Number.MIN_VALUE}`, `${Number.MIN_VALUE}`); | ||
check(`${Number.MIN_SAFE_INTEGER}`, `${Number.MIN_SAFE_INTEGER}`); | ||
check('-123.456', '-123.456'); | ||
check('-1', '-1'); | ||
check('0', '0', wrapInString, t); | ||
check('0', '0'); | ||
check('1', '1', wrapInString, t); | ||
check('123.456', '123.456', wrapInString, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, `${Number.MAX_SAFE_INTEGER}`, wrapInString, t); | ||
check(`${Number.MAX_VALUE}`, `${Number.MAX_VALUE}`, wrapInString, t); | ||
check('1', '1'); | ||
check('123.456', '123.456'); | ||
check(`${Number.MAX_SAFE_INTEGER}`, `${Number.MAX_SAFE_INTEGER}`); | ||
check(`${Number.MAX_VALUE}`, `${Number.MAX_VALUE}`); | ||
// strings containing only whitespace | ||
check('', '', wrapInString, t); | ||
check(' ', ' ', wrapInString, t); | ||
check('\n', '\n', wrapInString, t); | ||
check('\r', '\r', wrapInString, t); | ||
check('\t', '\t', wrapInString, t); | ||
check(' ', ' ', wrapInString, t); | ||
check(' \n ', ' \n ', wrapInString, t); | ||
check(' \r ', ' \r ', wrapInString, t); | ||
check(' \t ', ' \t ', wrapInString, t); | ||
check(' \n \r \t \n \r \t ', ' \n \r \t \n \r \t ', wrapInString, t); | ||
check('', ''); | ||
check(' ', ' '); | ||
check('\n', '\n'); | ||
check('\r', '\r'); | ||
check('\t', '\t'); | ||
check(' ', ' '); | ||
check(' \n ', ' \n '); | ||
check(' \r ', ' \r '); | ||
check(' \t ', ' \t '); | ||
check(' \n \r \t \n \r \t ', ' \n \r \t \n \r \t '); | ||
// strings not containing numbers | ||
check('a', 'a', wrapInString, t); | ||
check('abc', 'abc', wrapInString, t); | ||
check('ABC', 'ABC', wrapInString, t); | ||
check('a', 'a'); | ||
check('abc', 'abc'); | ||
check('ABC', 'ABC'); | ||
} | ||
function checkTrim(wrapInString, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkEqual(Strings.trim, value, expected, wrapInString, ` must be ${stringify(expected, undefined)}`, t); | ||
function checkTrim(t, wrapInString) { | ||
function check(value, expected) { | ||
return checkEqual(t, Strings.trim, [wrap(value, wrapInString)], expected, toPrefix(value, wrapInString)); | ||
} | ||
// undefined | ||
check(undefined, wrapInString ? "undefined" : undefined, wrapInString, t); | ||
check(undefined, wrapInString ? "undefined" : undefined); | ||
// null | ||
check(null, wrapInString ? "null" : null, wrapInString, t); | ||
check(null, wrapInString ? "null" : null); | ||
// objects | ||
check({}, wrapInString ? "[object Object]" : {}, wrapInString, t); | ||
check({a: 1, b: 2}, wrapInString ? "[object Object]" : {a: 1, b: 2}, wrapInString, t); | ||
check({}, wrapInString ? "[object Object]" : {}); | ||
check({a: 1, b: 2}, wrapInString ? "[object Object]" : {a: 1, b: 2}); | ||
// booleans | ||
check(true, wrapInString ? "true" : true, wrapInString, t); | ||
check(false, wrapInString ? "false" : false, wrapInString, t); | ||
check(true, wrapInString ? "true" : true); | ||
check(false, wrapInString ? "false" : false); | ||
// arrays | ||
check([], wrapInString ? "" : [], wrapInString, t); | ||
check([1, 2, 3], wrapInString ? "1,2,3" : [1, 2, 3], wrapInString, t); | ||
check([], wrapInString ? "" : []); | ||
check([1, 2, 3], wrapInString ? "1,2,3" : [1, 2, 3]); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, wrapInString ? `${Number.POSITIVE_INFINITY}` : Number.POSITIVE_INFINITY, wrapInString, t); | ||
check(Number.NEGATIVE_INFINITY, wrapInString ? `${Number.NEGATIVE_INFINITY}` : Number.NEGATIVE_INFINITY, wrapInString, t); | ||
check(NaN, wrapInString ? 'NaN' : NaN, wrapInString, t); | ||
check(Number.POSITIVE_INFINITY, wrapInString ? `${Number.POSITIVE_INFINITY}` : Number.POSITIVE_INFINITY); | ||
check(Number.NEGATIVE_INFINITY, wrapInString ? `${Number.NEGATIVE_INFINITY}` : Number.NEGATIVE_INFINITY); | ||
check(NaN, wrapInString ? 'NaN' : NaN); | ||
// negative numbers | ||
check(Number.MIN_VALUE, wrapInString ? `${Number.MIN_VALUE}` : Number.MIN_VALUE, wrapInString, t); | ||
check(Number.MIN_SAFE_INTEGER, wrapInString ? `${Number.MIN_SAFE_INTEGER}` : Number.MIN_SAFE_INTEGER, wrapInString, t); | ||
check(-123.456, wrapInString ? '-123.456' : -123.456, wrapInString, t); | ||
check(-1, wrapInString ? '-1' : -1, wrapInString, t); | ||
check(Number.MIN_VALUE, wrapInString ? `${Number.MIN_VALUE}` : Number.MIN_VALUE); | ||
check(Number.MIN_SAFE_INTEGER, wrapInString ? `${Number.MIN_SAFE_INTEGER}` : Number.MIN_SAFE_INTEGER); | ||
check(-123.456, wrapInString ? '-123.456' : -123.456); | ||
check(-1, wrapInString ? '-1' : -1); | ||
// zero | ||
check(0, wrapInString ? '0' : 0, wrapInString, t); // not sure if this should return blank for 0 | ||
check(0, wrapInString ? '0' : 0); // not sure if this should return blank for 0 | ||
// positive numbers | ||
check(1, wrapInString ? '1' : 1, wrapInString, t); | ||
check(123.456, wrapInString ? '123.456' : 123.456, wrapInString, t); | ||
check(Number.MAX_SAFE_INTEGER, wrapInString ? `${Number.MAX_SAFE_INTEGER}` : Number.MAX_SAFE_INTEGER, wrapInString, t); | ||
check(Number.MAX_VALUE, wrapInString ? `${Number.MAX_VALUE}` : Number.MAX_VALUE, wrapInString, t); | ||
check(1, wrapInString ? '1' : 1); | ||
check(123.456, wrapInString ? '123.456' : 123.456); | ||
check(Number.MAX_SAFE_INTEGER, wrapInString ? `${Number.MAX_SAFE_INTEGER}` : Number.MAX_SAFE_INTEGER); | ||
check(Number.MAX_VALUE, wrapInString ? `${Number.MAX_VALUE}` : Number.MAX_VALUE); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, `${Number.MIN_VALUE}`, wrapInString, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, `${Number.MIN_SAFE_INTEGER}`, wrapInString, t); | ||
check('-123.456', '-123.456', wrapInString, t); | ||
check('-1', '-1', wrapInString, t); | ||
check(`${Number.MIN_VALUE}`, `${Number.MIN_VALUE}`); | ||
check(`${Number.MIN_SAFE_INTEGER}`, `${Number.MIN_SAFE_INTEGER}`); | ||
check('-123.456', '-123.456'); | ||
check('-1', '-1'); | ||
check('0', '0', wrapInString, t); | ||
check('0', '0'); | ||
check('1', '1', wrapInString, t); | ||
check('123.456', '123.456', wrapInString, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, `${Number.MAX_SAFE_INTEGER}`, wrapInString, t); | ||
check(`${Number.MAX_VALUE}`, `${Number.MAX_VALUE}`, wrapInString, t); | ||
check('1', '1'); | ||
check('123.456', '123.456'); | ||
check(`${Number.MAX_SAFE_INTEGER}`, `${Number.MAX_SAFE_INTEGER}`); | ||
check(`${Number.MAX_VALUE}`, `${Number.MAX_VALUE}`); | ||
// strings containing only whitespace | ||
check('', '', wrapInString, t); | ||
check(' ', '', wrapInString, t); | ||
check('\n', '', wrapInString, t); | ||
check('\r', '', wrapInString, t); | ||
check('\t', '', wrapInString, t); | ||
check(' ', '', wrapInString, t); | ||
check(' \n ', '', wrapInString, t); | ||
check(' \r ', '', wrapInString, t); | ||
check(' \t ', '', wrapInString, t); | ||
check(' \n \r \t \n \r \t ', '', wrapInString, t); | ||
check('', ''); | ||
check(' ', ''); | ||
check('\n', ''); | ||
check('\r', ''); | ||
check('\t', ''); | ||
check(' ', ''); | ||
check(' \n ', ''); | ||
check(' \r ', ''); | ||
check(' \t ', ''); | ||
check(' \n \r \t \n \r \t ', ''); | ||
// strings not containing numbers | ||
check('a', 'a', wrapInString, t); | ||
check('abc', 'abc', wrapInString, t); | ||
check('ABC', 'ABC', wrapInString, t); | ||
check(' ABC ', 'ABC', wrapInString, t); | ||
check(' A B C ', 'A B C', wrapInString, t); | ||
check('a', 'a'); | ||
check('abc', 'abc'); | ||
check('ABC', 'ABC'); | ||
check(' ABC ', 'ABC'); | ||
check(' A B C ', 'A B C'); | ||
} | ||
function checkTrimOrEmpty(wrapInString, t) { | ||
function check(value, expected, wrapInString, t) { | ||
return checkEqual(Strings.trimOrEmpty, value, expected, wrapInString, ` must be ${stringify(expected, undefined)}`, t); | ||
function checkTrimOrEmpty(t, wrapInString) { | ||
function check(value, expected) { | ||
return checkEqual(t, Strings.trimOrEmpty, [wrap(value, wrapInString)], expected, toPrefix(value, wrapInString)); | ||
} | ||
// undefined | ||
check(undefined, wrapInString ? "undefined" : '', wrapInString, t); | ||
check(undefined, wrapInString ? "undefined" : ''); | ||
// null | ||
check(null, wrapInString ? "null" : '', wrapInString, t); | ||
check(null, wrapInString ? "null" : ''); | ||
// objects | ||
check({}, wrapInString ? "[object Object]" : {}, wrapInString, t); | ||
check({a: 1, b: 2}, wrapInString ? "[object Object]" : {a: 1, b: 2}, wrapInString, t); | ||
check({}, wrapInString ? "[object Object]" : {}); | ||
check({a: 1, b: 2}, wrapInString ? "[object Object]" : {a: 1, b: 2}); | ||
// booleans | ||
check(true, wrapInString ? "true" : true, wrapInString, t); | ||
check(false, wrapInString ? "false" : false, wrapInString, t); | ||
check(true, wrapInString ? "true" : true); | ||
check(false, wrapInString ? "false" : false); | ||
// arrays | ||
check([], wrapInString ? "" : [], wrapInString, t); | ||
check([1, 2, 3], wrapInString ? "1,2,3" : [1, 2, 3], wrapInString, t); | ||
check([], wrapInString ? "" : []); | ||
check([1, 2, 3], wrapInString ? "1,2,3" : [1, 2, 3]); | ||
// special case numbers | ||
check(Number.POSITIVE_INFINITY, wrapInString ? `${Number.POSITIVE_INFINITY}` : Number.POSITIVE_INFINITY, wrapInString, t); | ||
check(Number.NEGATIVE_INFINITY, wrapInString ? `${Number.NEGATIVE_INFINITY}` : Number.NEGATIVE_INFINITY, wrapInString, t); | ||
check(NaN, wrapInString ? 'NaN' : NaN, wrapInString, t); | ||
check(Number.POSITIVE_INFINITY, wrapInString ? `${Number.POSITIVE_INFINITY}` : Number.POSITIVE_INFINITY); | ||
check(Number.NEGATIVE_INFINITY, wrapInString ? `${Number.NEGATIVE_INFINITY}` : Number.NEGATIVE_INFINITY); | ||
check(NaN, wrapInString ? 'NaN' : NaN); | ||
// negative numbers | ||
check(Number.MIN_VALUE, wrapInString ? `${Number.MIN_VALUE}` : Number.MIN_VALUE, wrapInString, t); | ||
check(Number.MIN_SAFE_INTEGER, wrapInString ? `${Number.MIN_SAFE_INTEGER}` : Number.MIN_SAFE_INTEGER, wrapInString, t); | ||
check(-123.456, wrapInString ? '-123.456' : -123.456, wrapInString, t); | ||
check(-1, wrapInString ? '-1' : -1, wrapInString, t); | ||
check(Number.MIN_VALUE, wrapInString ? `${Number.MIN_VALUE}` : Number.MIN_VALUE); | ||
check(Number.MIN_SAFE_INTEGER, wrapInString ? `${Number.MIN_SAFE_INTEGER}` : Number.MIN_SAFE_INTEGER); | ||
check(-123.456, wrapInString ? '-123.456' : -123.456); | ||
check(-1, wrapInString ? '-1' : -1); | ||
// zero | ||
check(0, wrapInString ? '0' : 0, wrapInString, t); // not sure if this should return blank for 0 | ||
check(0, wrapInString ? '0' : 0); // not sure if this should return blank for 0 | ||
// positive numbers | ||
check(1, wrapInString ? '1' : 1, wrapInString, t); | ||
check(123.456, wrapInString ? '123.456' : 123.456, wrapInString, t); | ||
check(Number.MAX_SAFE_INTEGER, wrapInString ? `${Number.MAX_SAFE_INTEGER}` : Number.MAX_SAFE_INTEGER, wrapInString, t); | ||
check(Number.MAX_VALUE, wrapInString ? `${Number.MAX_VALUE}` : Number.MAX_VALUE, wrapInString, t); | ||
check(1, wrapInString ? '1' : 1); | ||
check(123.456, wrapInString ? '123.456' : 123.456); | ||
check(Number.MAX_SAFE_INTEGER, wrapInString ? `${Number.MAX_SAFE_INTEGER}` : Number.MAX_SAFE_INTEGER); | ||
check(Number.MAX_VALUE, wrapInString ? `${Number.MAX_VALUE}` : Number.MAX_VALUE); | ||
// strings containing numbers | ||
check(`${Number.MIN_VALUE}`, `${Number.MIN_VALUE}`, wrapInString, t); | ||
check(`${Number.MIN_SAFE_INTEGER}`, `${Number.MIN_SAFE_INTEGER}`, wrapInString, t); | ||
check('-123.456', '-123.456', wrapInString, t); | ||
check('-1', '-1', wrapInString, t); | ||
check(`${Number.MIN_VALUE}`, `${Number.MIN_VALUE}`); | ||
check(`${Number.MIN_SAFE_INTEGER}`, `${Number.MIN_SAFE_INTEGER}`); | ||
check('-123.456', '-123.456'); | ||
check('-1', '-1'); | ||
check('0', '0', wrapInString, t); | ||
check('0', '0'); | ||
check('1', '1', wrapInString, t); | ||
check('123.456', '123.456', wrapInString, t); | ||
check(`${Number.MAX_SAFE_INTEGER}`, `${Number.MAX_SAFE_INTEGER}`, wrapInString, t); | ||
check(`${Number.MAX_VALUE}`, `${Number.MAX_VALUE}`, wrapInString, t); | ||
check('1', '1'); | ||
check('123.456', '123.456'); | ||
check(`${Number.MAX_SAFE_INTEGER}`, `${Number.MAX_SAFE_INTEGER}`); | ||
check(`${Number.MAX_VALUE}`, `${Number.MAX_VALUE}`); | ||
// strings containing only whitespace | ||
check('', '', wrapInString, t); | ||
check(' ', '', wrapInString, t); | ||
check('\n', '', wrapInString, t); | ||
check('\r', '', wrapInString, t); | ||
check('\t', '', wrapInString, t); | ||
check(' ', '', wrapInString, t); | ||
check(' \n ', '', wrapInString, t); | ||
check(' \r ', '', wrapInString, t); | ||
check(' \t ', '', wrapInString, t); | ||
check(' \n \r \t \n \r \t ', '', wrapInString, t); | ||
check('', ''); | ||
check(' ', ''); | ||
check('\n', ''); | ||
check('\r', ''); | ||
check('\t', ''); | ||
check(' ', ''); | ||
check(' \n ', ''); | ||
check(' \r ', ''); | ||
check(' \t ', ''); | ||
check(' \n \r \t \n \r \t ', ''); | ||
// strings not containing numbers | ||
check('a', 'a', wrapInString, t); | ||
check('abc', 'abc', wrapInString, t); | ||
check('ABC', 'ABC', wrapInString, t); | ||
check(' ABC ', 'ABC', wrapInString, t); | ||
check(' A B C ', 'A B C', wrapInString, t); | ||
check('a', 'a'); | ||
check('abc', 'abc'); | ||
check('ABC', 'ABC'); | ||
check(' ABC ', 'ABC'); | ||
check(' A B C ', 'A B C'); | ||
} | ||
test('isString on Strings', t => { | ||
checkIsStrings(true, t); | ||
checkIsString(t, true); | ||
t.end(); | ||
@@ -482,3 +473,3 @@ }); | ||
test('isString on strings', t => { | ||
checkIsStrings(false, t); | ||
checkIsString(t, false); | ||
t.end(); | ||
@@ -488,3 +479,3 @@ }); | ||
test('isBlank on Strings', t => { | ||
checkIsBlank(true, t); | ||
checkIsBlank(t, true); | ||
t.end(); | ||
@@ -494,3 +485,3 @@ }); | ||
test('isBlank on strings', t => { | ||
checkIsBlank(false, t); | ||
checkIsBlank(t, false); | ||
t.end(); | ||
@@ -500,3 +491,3 @@ }); | ||
test('isNotBlank on Strings', t => { | ||
checkIsNotBlank(true, t); | ||
checkIsNotBlank(t, true); | ||
t.end(); | ||
@@ -506,3 +497,3 @@ }); | ||
test('isNotBlank on strings', t => { | ||
checkIsNotBlank(false, t); | ||
checkIsNotBlank(t, false); | ||
t.end(); | ||
@@ -512,3 +503,3 @@ }); | ||
test('stringify on strings', t => { | ||
checkStringify(false, t); | ||
checkStringify(t, false); | ||
t.end(); | ||
@@ -518,3 +509,3 @@ }); | ||
test('stringify on Strings', t => { | ||
checkStringify(true, t); | ||
checkStringify(t, true); | ||
t.end(); | ||
@@ -524,3 +515,3 @@ }); | ||
test('trim on strings', t => { | ||
checkTrim(false, t); | ||
checkTrim(t, false); | ||
t.end(); | ||
@@ -530,3 +521,3 @@ }); | ||
test('trim on Strings', t => { | ||
checkTrim(true, t); | ||
checkTrim(t, true); | ||
t.end(); | ||
@@ -536,3 +527,3 @@ }); | ||
test('trimOrEmpty on strings', t => { | ||
checkTrimOrEmpty(false, t); | ||
checkTrimOrEmpty(t, false); | ||
t.end(); | ||
@@ -542,4 +533,4 @@ }); | ||
test('trimOrEmpty on Strings', t => { | ||
checkTrimOrEmpty(true, t); | ||
checkTrimOrEmpty(t, true); | ||
t.end(); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
145793
26
2936
129
1