diff --git a/package.json b/package.json index 92001a4..608a81d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "qc-to_num", - "version": "1.0.1", + "version": "1.0.2", "author": { "name": "Danny Hurlburt", "url": "https://github.com/dhurlburtusa" diff --git a/src/index.test.ts b/src/index.test.ts index 12d6a15..ecf1210 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -17,6 +17,656 @@ describe('qc-to_num', () => { expect(toNum(arguments)).toBe(arguments); }); + it('called with inconvertible input should return default value', function () { + let def: any; + + def = []; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = false; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = true; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = new Date(); + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = new Error('Help!'); + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = function () {}; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = arguments; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = 123; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = -123; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = Number.MAX_VALUE; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = Number.MIN_VALUE; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = Number.MAX_SAFE_INTEGER; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = Number.MIN_SAFE_INTEGER; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = NaN; + expect(toNum([], def)).toEqual(NaN); + expect(toNum([], { def })).toEqual(NaN); + expect(toNum(['not empty'], def)).toEqual(NaN); + expect(toNum(['not empty'], { def })).toEqual(NaN); + expect(toNum(true, def)).toEqual(NaN); + expect(toNum(true, { def })).toEqual(NaN); + expect(toNum(false, def)).toEqual(NaN); + expect(toNum(false, { def })).toEqual(NaN); + expect(toNum(new Error('Help!'), def)).toEqual(NaN); + expect(toNum(new Error('Help!'), { def })).toEqual(NaN); + expect(toNum(function () {}, def)).toEqual(NaN); + expect(toNum(function () {}, { def })).toEqual(NaN); + expect(toNum(arguments, def)).toEqual(NaN); + expect(toNum(arguments, { def })).toEqual(NaN); + expect(toNum(NaN, def)).toEqual(NaN); + expect(toNum(NaN, { def })).toEqual(NaN); + expect(toNum(Number.NaN, def)).toEqual(NaN); + expect(toNum(Number.NaN, { def })).toEqual(NaN); + expect(toNum({}, def)).toEqual(NaN); + expect(toNum({}, { def })).toEqual(NaN); + expect(toNum({ prop: 'not empty' }, def)).toEqual(NaN); + expect(toNum({ prop: 'not empty' }, { def })).toEqual(NaN); + expect(toNum(/regexp/, def)).toEqual(NaN); + expect(toNum(/regexp/, { def })).toEqual(NaN); + expect(toNum('', def)).toEqual(NaN); + expect(toNum('', { def })).toEqual(NaN); + expect(toNum('not empty', def)).toEqual(NaN); + expect(toNum('not empty', { def })).toEqual(NaN); + expect(toNum(null, def)).toEqual(NaN); + expect(toNum(null, { def })).toEqual(NaN); + expect(toNum(undefined, def)).toEqual(NaN); + expect(toNum(undefined, { def })).toEqual(NaN); + + def = {}; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = /def/; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = ''; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = 'not empty'; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + + def = null; + expect(toNum([], def)).toBe(def); + expect(toNum([], { def })).toBe(def); + expect(toNum(['not empty'], def)).toBe(def); + expect(toNum(['not empty'], { def })).toBe(def); + expect(toNum(true, def)).toBe(def); + expect(toNum(true, { def })).toBe(def); + expect(toNum(false, def)).toBe(def); + expect(toNum(false, { def })).toBe(def); + expect(toNum(new Error('Help!'), def)).toBe(def); + expect(toNum(new Error('Help!'), { def })).toBe(def); + expect(toNum(function () {}, def)).toBe(def); + expect(toNum(function () {}, { def })).toBe(def); + expect(toNum(arguments, def)).toBe(def); + expect(toNum(arguments, { def })).toBe(def); + expect(toNum(NaN, def)).toBe(def); + expect(toNum(NaN, { def })).toBe(def); + expect(toNum(Number.NaN, def)).toBe(def); + expect(toNum(Number.NaN, { def })).toBe(def); + expect(toNum({}, def)).toBe(def); + expect(toNum({}, { def })).toBe(def); + expect(toNum({ prop: 'not empty' }, def)).toBe(def); + expect(toNum({ prop: 'not empty' }, { def })).toBe(def); + expect(toNum(/regexp/, def)).toBe(def); + expect(toNum(/regexp/, { def })).toBe(def); + expect(toNum('', def)).toBe(def); + expect(toNum('', { def })).toBe(def); + expect(toNum('not empty', def)).toBe(def); + expect(toNum('not empty', { def })).toBe(def); + expect(toNum(null, def)).toBe(def); + expect(toNum(null, { def })).toBe(def); + expect(toNum(undefined, def)).toBe(def); + expect(toNum(undefined, { def })).toBe(def); + }); + it('called with `arguments` and default value should return default value', function () { const input = arguments; let toValue: any; diff --git a/src/index.ts b/src/index.ts index b39b779..bc2b825 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,7 +66,7 @@ * @param {*=} input - The value to be converted to a JavaScript number * primitive. This may also be an object with a custom `valueOf` method that * returns a number or parsible string. - * @param {*=|{ def=: *}} [def=undefined] - The default value to return if + * @param {*=|{ def: *}} [def=undefined] - The default value to return if * unable to convert. This is allowed to be of any data type. This may also * be an object with a `def` property. To return an object as a default value, * then wrap it in an object with a `def` property set to the object that is to @@ -77,7 +77,7 @@ * default value if unable to convert. Note: a value of type number is not * always returned when the default value is returned. */ -function toNum(input?: any, def?: any | { def?: any }): any { +function toNum(input?: any, def?: any | { def: any }): any { let coercedInput: any, output: any; if (typeof input == 'number' && input === input) { @@ -91,7 +91,7 @@ function toNum(input?: any, def?: any | { def?: any }): any { if (output === undefined || output != output) { // Resolve default value: - if (typeof def == 'object' && def !== null) { + if (typeof def == 'object' && def !== null && def.hasOwnProperty('def')) { def = def.def; } else {