Skip to content

Commit

Permalink
Adds is.[not].primitive To is (#15)
Browse files Browse the repository at this point in the history
Adds `is.primitive` and `is.not.primitive` to `is`

The following results from `is.getType` will return `true` for
`is.primitive`. Everything else returns `false`.

* 'boolean',
* 'null',
* 'undefined',
* 'number',
* 'bigint',
* 'string',
* 'symbol'
  • Loading branch information
losandes authored Sep 11, 2019
1 parent e81fcf4 commit 3130dcf
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
26 changes: 18 additions & 8 deletions dist/blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
number: undefined,
nullOrWhitespace: undefined,
decimal: undefined,
primitive: undefined,
not: {
defined: undefined,
nullOrUndefined: undefined,
Expand All @@ -666,16 +667,17 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
regexp: undefined,
number: undefined,
nullOrWhitespace: undefined,
decimal: undefined
decimal: undefined,
primitive: undefined
}
/**
* Produces the printed type (i.e. [object Object], [object Function]),
* removes everything except for the type, and returns the lowered form.
* (i.e. boolean, number, string, function, asyncfunction, promise, array,
* date, regexp, object)
*/

};
var primitives = ['boolean', 'null', 'undefined', 'number', 'bigint', 'string', 'symbol'];
/**
* Produces the printed type (i.e. [object Object], [object Function]),
* removes everything except for the type, and returns the lowered form.
* (i.e. boolean, number, string, function, asyncfunction, promise, array,
* date, regexp, object)
*/

is.getType = function (obj) {
return Object.prototype.toString.call(obj).replace(/(^\[object )|(\]$)/g, '').toLowerCase();
Expand Down Expand Up @@ -825,6 +827,14 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return is.decimal(val, places) === false;
};

is.primitive = function (input) {
return primitives.indexOf(is.getType(input)) > -1;
};

is.not.primitive = function (input) {
return is.primitive(input) === false;
};

return is;
}
};
Expand Down
6 changes: 3 additions & 3 deletions dist/blueprint.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polyn/blueprint",
"version": "2.4.2",
"version": "2.5.0",
"description": "An easy to use, flexible, and powerful validation library for nodejs and browsers",
"main": "index.js",
"types": "index.d.ts",
Expand Down
22 changes: 21 additions & 1 deletion src/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
number: undefined,
nullOrWhitespace: undefined,
decimal: undefined,
primitive: undefined,
not: {
defined: undefined,
nullOrUndefined: undefined,
Expand All @@ -37,10 +38,21 @@ module.exports = {
regexp: undefined,
number: undefined,
nullOrWhitespace: undefined,
decimal: undefined
decimal: undefined,
primitive: undefined
}
}

const primitives = [
'boolean',
'null',
'undefined',
'number',
'bigint',
'string',
'symbol'
]

/**
* Produces the printed type (i.e. [object Object], [object Function]),
* removes everything except for the type, and returns the lowered form.
Expand Down Expand Up @@ -203,6 +215,14 @@ module.exports = {
return is.decimal(val, places) === false
}

is.primitive = function (input) {
return primitives.indexOf(is.getType(input)) > -1
}

is.not.primitive = function (input) {
return is.primitive(input) === false
}

return is
}
}
53 changes: 49 additions & 4 deletions src/is.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ const expect = require('chai').expect
module.exports = (test) => {
const { is } = test.sut

const makeDescription = (func, name, value) => {
try {
return `${func}.${name}(${value})`
} catch (e) {
// ignore - symbols can't be coerced to strings
return `${func}.${name}(probably a symbol)`
}
}

const assert = (name, values) => {
values.is.forEach((value) => {
expect(is[name](value), `is.${name}(${value})`).to.equal(true)
expect(is.not[name](value), `is.not.${name}(${value})`).to.equal(false)
expect(is[name](value), makeDescription('is', name, value)).to.equal(true)
expect(is.not[name](value), makeDescription('is.not', name, value)).to.equal(false)
})

values.isNot.forEach((value) => {
expect(is[name](value), `is.${name}(${value})`).to.equal(false)
expect(is.not[name](value), `is.not.${name}(${value})`).to.equal(true)
expect(is[name](value), makeDescription('is', name, value)).to.equal(false)
expect(is.not[name](value), makeDescription('is.not', name, value)).to.equal(true)
})
}

Expand Down Expand Up @@ -349,6 +358,42 @@ module.exports = (test) => {
expect(is.not.decimal(-42.42, 3)).to.equal(true)
expect(is.not.decimal(42.4242424242, 12)).to.equal(true)
expect(is.not.decimal(-42.4242424242, 12)).to.equal(true)
},
// primitive
'when `primitive` is executed': {
'it should return the expected responses': () => {
assert('primitive', {
is: [
true,
false,
'string',
null,
undefined,
0,
1,
42.4,
Infinity,
1n,
BigInt(Number.MAX_SAFE_INTEGER + Number.MAX_SAFE_INTEGER), // eslint-disable-line no-undef
Symbol(true),
Symbol(false),
Symbol('string'),
Symbol(null),
Symbol(undefined),
Symbol(0),
Symbol(1),
Symbol(42.4),
Symbol(1n)
],
isNot: [
{},
{ foo: 'bar' },
function () {},
() => {},
new Date()
]
})
}
}
})
}

0 comments on commit 3130dcf

Please sign in to comment.