
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
fast-type-check
Advanced tools
A nice, small and fast library for checking data types. Javascript is always a pain with type checking.
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.
Install the module:
$ npm i fast-type-check --save
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 }]
Check if variable is a specific type.
All methods returns true
or false
.
Always try to return the required datatype.
Kind: global class
string
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
number
string
array
object
array
date
| null
*
true
| false
*
object
object
string
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. |
boolean
Check if this is an error or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is an array or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is an object or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is an empty object or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is a string or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is a date or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is a number or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is a function or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is a regular expression or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is a boolean or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is null or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is undefined or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is defined or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this a MongoDB object or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this a string or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is an array of objects or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is an array of strings or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is an array of numbers or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
boolean
Check if this is an array of MongoDB objects or not.
Kind: static method of FastTypeCheck
Param | Type | Description |
---|---|---|
element | * | Element to check. |
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:
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
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
Kind: static method of FastTypeCheck
See: Identical to ensureNumber
Kind: static method of FastTypeCheck
See: Identical to ensureString
Kind: static method of FastTypeCheck
See: Identical to ensureArray
Kind: static method of FastTypeCheck
See: Identical to ensureObject
Kind: static method of FastTypeCheck
See: Identical to ensureUniqArray
Kind: static method of FastTypeCheck
See: Identical to ensureDate
*
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
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
*
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
object
Set object deep value.
Kind: static method of FastTypeCheck
Returns: object
- Object with new value.
Todo
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. |
object
Remove empty key, values from an object.
Kind: static method of FastTypeCheck
Returns: object
- Cleand object.
Todo
Param | Type | Description |
---|---|---|
object | object | Object to be cleaned. |
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 | * |
$ npm run docs
Use the Issue tracker
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:
For more information on semantic versioning, please visit http://semver.org/.
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.
FAQs
A nice, small and fast library for checking data types. Javascript is always a pain with type checking.
We found that fast-type-check demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.