fast-type-check v1.3.6
Fast-type-check to the rescue
A nice, small and fast library for checking data types. Javascript is always a pain with type checking and I often end
up typing if (typeof myvar === 'string')
too many times.
No external dependencies.
Howto to get started
Install the module:
$ npm i fast-type-check --save
Howto use the module
Include it at the top of your script:
const tc = require('fast-type-check');
Test if value is a number:
if (tc.isNumber(123)) {
console.log('this is a number.');
}
if (tc.isNumber('123')) {
console.log('this is not a number.');
}
Test if value is an array of objects:
const obj = [{}, {}];
if (tc.isArrayOfObjects(obj)) {
console.log('This is an array of objects.')
// This is an array of objects.
}
Make uniq array:
const arr = [[1, 2], [1, 2], [3, 4, 5], [1, 2]];
console.log(tc.ensureUniqArray(arr));
// [[1, 2], [3, 4, 5]]
Works with arrays of objects too:
const arr = [{ foo: 1, bar: 2 }, { gomle: 3, foobar: 4 }, { foo: 1, bar: 2 }, { foo: 1, bar: 2 }];
console.log(tc.ensureUniqArray(arr));
// [{ foo: 1, bar: 2 }, { gomle: 3, foobar: 4 }]
Testing methods for datatypes
Check if variable is a specific type.
All methods returns true
or false
.
- isArray
- isObject
- isEmptyObject
- isString
- isDate
- isNumber
- isFunction
- isRegexp
- isBoolean
- isNull
- isUndefined
- isMongoObject
- isArrayOfObjects
- isArrayOfArrays
- isArrayOfStrings
- isArrayOfNumbers
- isArrayOfMongoObjects
- isEqual
- isEqualArrays
- isEqualObjects
- isInArray
Enforcing methods for datatypes
Always try to return the required datatype.
- ensureNumber alias asNumber
- ensureString alias asString
- ensureArray alias asArray
- ensureObject alias asObject
- ensureUniqArray alias asUniqArray
- ensureDate alias asDate
Other useful methods
- parseObject
- cleanObject
API Reference
FastTypeCheck
Kind: global class
- FastTypeCheck
- .getType(element) ⇒ string
- .isError(element) ⇒ boolean
- .isArray(element) ⇒ boolean
- .isObject(element) ⇒ boolean
- .isEmptyObject(element) ⇒ boolean
- .isString(element) ⇒ boolean
- .isDate(element) ⇒ boolean
- .isNumber(element) ⇒ boolean
- .isFunction(element) ⇒ boolean
- .isRegexp(element) ⇒ boolean
- .isBoolean(element) ⇒ boolean
- .isNull(element) ⇒ boolean
- .isUndefined(element) ⇒ boolean
- .isDefined(element) ⇒ boolean
- .isMongoObject(element) ⇒ boolean
- .isArrayOfObjects(element) ⇒ boolean
- .isArrayOfArrays(element) ⇒ boolean
- .isArrayOfStrings(element) ⇒ boolean
- .isArrayOfNumbers(element) ⇒ boolean
- .isArrayOfMongoObjects(element) ⇒ boolean
- .isEqual(a, b) ⇒ boolean
- .isEqualArrays(array1, array2) ⇒ boolean
- .isEqualObjects(object1, object2) ⇒ boolean
- .isInArray(array, element) ⇒ boolean
- .ensureNumber(input, useUndefined) ⇒ number
- .ensureString(input, useUndefined) ⇒ string
- .ensureArray(input, useUndefined) ⇒ array
- .ensureObject(input, useUndefined) ⇒ object
- .ensureUniqArray(input) ⇒ array
- .ensureDate(input) ⇒ date | null
- .asNumber()
- .asString()
- .asArray()
- .asObject()
- .asUniqArray()
- .asDate()
- .parseObject(object, key, [key], [key]) ⇒ *
- .checkNested(object, key, [key], [key]) ⇒ true | false
- .getNestedValue(object, path) ⇒ *
- .setNestedValue(object, path, value) ⇒ object
- .cleanObject(object) ⇒ object
- .dump(input) ⇒ string
FastTypeCheck.getType(element) ⇒ string
Get the real type of this element.
Kind: static method of FastTypeCheck
Returns: string - Prototype type as a string.
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isError(element) ⇒ boolean
Check if this is an error or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isArray(element) ⇒ boolean
Check if this is an array or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isObject(element) ⇒ boolean
Check if this is an object or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isEmptyObject(element) ⇒ boolean
Check if this is an empty object or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isString(element) ⇒ boolean
Check if this is a string or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isDate(element) ⇒ boolean
Check if this is a date or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isNumber(element) ⇒ boolean
Check if this is a number or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isFunction(element) ⇒ boolean
Check if this is a function or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isRegexp(element) ⇒ boolean
Check if this is a regular expression or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isBoolean(element) ⇒ boolean
Check if this is a boolean or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isNull(element) ⇒ boolean
Check if this is null or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isUndefined(element) ⇒ boolean
Check if this is undefined or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isDefined(element) ⇒ boolean
Check if this is defined or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isMongoObject(element) ⇒ boolean
Check if this a MongoDB object or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isArrayOfObjects(element) ⇒ boolean
Check if this a string or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isArrayOfArrays(element) ⇒ boolean
Check if this is an array of objects or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isArrayOfStrings(element) ⇒ boolean
Check if this is an array of strings or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isArrayOfNumbers(element) ⇒ boolean
Check if this is an array of numbers or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isArrayOfMongoObjects(element) ⇒ boolean
Check if this is an array of MongoDB objects or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
FastTypeCheck.isEqual(a, b) ⇒ boolean
isEqual uses Object.is() and determines whether two values are the same value. Two values are the same if one of the following holds:
- both undefined
- both null
- both true or both false
- both strings of the same length with the same characters in the same order
- both the same object (means both object have same reference)
- both numbers and
- both +0
- both -0
- both NaN
- or both non-zero and both not NaN and both have the same value
This is not the same as being equal according to the == operator. The == operator applies various coercions to both sides (if they are not the same Type) before testing for equality (resulting in such behavior as "" == false being true), but Object.is doesn't coerce either value.
This is also not the same as being equal according to the === operator. The === operator (and the == operator as well) treats the number values -0 and +0 as equal and treats Number.NaN as not equal to NaN.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
a | * | Element to check. |
b | * | Element to check. |
Example
Object.is('foo', 'foo'); // true
Object.is(window, window); // true
Object.is('foo', 'bar'); // false
Object.is([], []); // false
var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo); // true
Object.is(foo, bar); // false
Object.is(null, null); // true
// Special Cases
Object.is(0, -0); // false
Object.is(-0, -0); // true
Object.is(NaN, 0/0); // true
FastTypeCheck.isEqualArrays(array1, array2) ⇒ boolean
Check if these arrays are equal. Checking every value with isEqual.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
array1 | array | Array 1 to be checked. |
array2 | array | Array 2 to be compared to Array 1. |
FastTypeCheck.isEqualObjects(object1, object2) ⇒ boolean
Check if these objects are equal. Doing a deep equal.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
object1 | object | Object 1 to be checked. |
object2 | object | Object 2 to be compared to Object 1. |
FastTypeCheck.isInArray(array, element) ⇒ boolean
Check if element is part of array. Can be used on: - Array of objects - Array of arrays - Array of simple values.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
array | array | Array to check against. |
element | * | Element to check if exists inside array. |
FastTypeCheck.ensureNumber(input, useUndefined) ⇒ number
Ensure that input is a number. If input is: - a number. Returns this number. - an array. Returns 0. - a boolean and true. Returns 0. - a string. Trying to parse number. Returns the value if successful. If none of above is successful. Returns 0.
Kind: static method of FastTypeCheck
Param | Type | Default | Description |
---|---|---|---|
input | * | Input to be casted to number. | |
useUndefined | boolean | false | If not defined, return undefined. |
FastTypeCheck.ensureString(input, useUndefined) ⇒ string
Ensure that input is a string. If input is: - a string. Returns string. - an array, a number, a date or a boolean. Returns element casted to string. If none of above is successful. Returns ''.
Kind: static method of FastTypeCheck
Param | Type | Default | Description |
---|---|---|---|
input | * | Input to be casted to string. | |
useUndefined | boolean | false | If not defined, return undefined. |
FastTypeCheck.ensureArray(input, useUndefined) ⇒ array
Ensure that input is an array. If input is: - an array. Returns array. - a string, a number, a date, a boolean or null. Returns an array with input as the element. If none of above is successful. Returns [].
Kind: static method of FastTypeCheck
Param | Type | Default | Description |
---|---|---|---|
input | * | Input to be casted to Array. | |
useUndefined | boolean | false | If not defined, return undefined. |
FastTypeCheck.ensureObject(input, useUndefined) ⇒ object
Ensure that input is an object.
If input is:
- an object. Returns object.
- a non empty string. Returns an object { input: input }
.
- a number !== 0. Returns an object { input: input }
.
If none of above is successful. Returns {}.
Kind: static method of FastTypeCheck
Param | Type | Default | Description |
---|---|---|---|
input | * | Input to be casted to Object. | |
useUndefined | boolean | false | If not defined, return undefined. |
FastTypeCheck.ensureUniqArray(input) ⇒ array
Ensure that input array has uniq values. Removes duplicate values from array.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
input | array | Input array. |
FastTypeCheck.ensureDate(input) ⇒ date | null
Ensure that input is a date.
If input is:
- a date: Returns date.
- a string: Trying to parse date and returns date if successful.
- a number: Trying to figure out if this is an epoch. If successful, returns date.
If none of above is successful. Returns null
.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
input | array | Input to be casted to Date. |
FastTypeCheck.asNumber()
Kind: static method of FastTypeCheck
See: Identical to ensureNumber
FastTypeCheck.asString()
Kind: static method of FastTypeCheck
See: Identical to ensureString
FastTypeCheck.asArray()
Kind: static method of FastTypeCheck
See: Identical to ensureArray
FastTypeCheck.asObject()
Kind: static method of FastTypeCheck
See: Identical to ensureObject
FastTypeCheck.asUniqArray()
Kind: static method of FastTypeCheck
See: Identical to ensureUniqArray
FastTypeCheck.asDate()
Kind: static method of FastTypeCheck
See: Identical to ensureDate
FastTypeCheck.parseObject(object, key, key, key) ⇒ *
Get object deep value if it exists.
Kind: static method of FastTypeCheck
Returns: * - object value
Param | Type | Description |
---|---|---|
object | object | Data object to get the value from. |
key | string | Name of key on level 1. |
key | string | Name of key on level 2. |
key | string | Name of key on level n. |
Example
const tc = require('fast-type-check');
// Let's say you have object:
conat obj = {
foo: {
bar: 1
}
};
// 1. You want to get the value obj.foo.bar if it exists:
tc.parseObject(obj, 'foo', 'bar');
// -> returns 1
// 2. You want to get the value obj.foo.bar.gomle if it exists:
tc.parseObject(obj, 'foo', 'bar', 'gomle');
// -> returns null
FastTypeCheck.checkNested(object, key, key, key) ⇒ true | false
Check if object has deep value.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
object | object | Data object to get the value from. |
key | string | Name of key on level 1. |
key | string | Name of key on level 2. |
key | string | Name of key on level n. |
Example
const tc = require('fast-type-check');
// Let's say you have object:
conat obj = {
foo: {
bar: 1
}
};
// 1. You want to get the value obj.foo.bar if it exists:
tc.checkNested(obj, 'foo', 'bar');
// -> returns true
// 2. You want to get the value obj.foo.bar.gomle if it exists:
tc.checkNested(obj, 'foo', 'bar', 'gomle');
// -> returns false
FastTypeCheck.getNestedValue(object, path) ⇒ *
Get object deep value if it exists.
Kind: static method of FastTypeCheck
Returns: * - object value
Param | Type | Description |
---|---|---|
object | object | Data object to get the value from. |
path | string | Path to value in 'foo.bar.gomle' format. |
Example
const tc = require('fast-type-check');
// Let's say you have object:
conat obj = {
foo: {
bar: 1
}
};
// 1. You want to get the value obj.foo.bar if it exists:
tc.getNestedValue(obj, 'foo.bar');
// -> returns 1
// 2. You want to get the value obj.foo.bar.gomle if it exists:
tc.getNestedValue(obj, 'foo.bar.gomle');
// -> returns null
FastTypeCheck.setNestedValue(object, path, value) ⇒ object
Set object deep value.
Kind: static method of FastTypeCheck
Returns: object - Object with new value.
Todo
- Add example.
Param | Type | Description |
---|---|---|
object | object | Data object to get the value from. |
path | string | Path to value in 'foo.bar.gomle' format. |
value | * | Value to set. |
FastTypeCheck.cleanObject(object) ⇒ object
Remove empty key, values from an object.
Kind: static method of FastTypeCheck
Returns: object - Cleand object.
Todo
- Add example.
Param | Type | Description |
---|---|---|
object | object | Object to be cleaned. |
FastTypeCheck.dump(input) ⇒ string
Return input as string. Dumping deep objects and other data structures. Very handy for debug purposes.
Kind: static method of FastTypeCheck
Returns: string - Dumped object.
Param | Type |
---|---|
input | * |
Howto update API docs
$ npm run docs
Howto report issues
Use the Issue tracker
Versioning
For transparency and insight into the release cycle, releases will be numbered with the follow format:
<major>.<minor>.<patch>
And constructed with the following guidelines:
- Breaking backwards compatibility bumps the major
- New additions without breaking backwards compatibility bumps the minor
- Bug fixes and misc changes bump the patch
For more information on semantic versioning, please visit http://semver.org/.
Contributions and feedback:
We ❤️ contributions and feedback.
If you want to contribute, please check out the CONTRIBUTING.md file.
If you have any question or suggestion create an issue.
Bug reports should always be done with a new issue.
Other Resources
- AWS Basic setup with Cloudformation
- AWS Lambda boilerplate
- Automated AWS Lambda update
- AWS API Gateway setup with Cloudformation
- AWS IoT setup with Cloudformation