
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
typesnitch
Advanced tools
Have you ever wondered why typeof []
returns 'object'
? I did, and that is why I started the development of typesnitch
. Now snitch.type([])
will return 'Array'
– isn't that something? Basically, all standard JS prototypes are supported and returned as a string
value. Furthermore, the Number
prototype is more differentiated (see examples below).
If you find any bugs or have suggestions feel free to help and fork the package.
type
: Returns the prototype of the valueunveil
: Tries to convert the value, or 'unveil' it, e.g., numbers in disguise :-)unveilType
: combination of type
and unveil
isType
: Check if a value has a specific typeHave a look at the tests for more usage information.
You can use type()
to get the prototype
of a given input value. The method has a second parameter that can be used to get a more detailed prototype. The detailed
parameter is set to true
per default.
With unveil()
a type conversion will be tried. At the moment it only works with integers, strings, objects and flat arrays that include number or string values. This will, hopefully, change in the future.
const snitch = require('typesnitch')
// revealing the prototype of the input
const x = '[1, 2, 3]'
snitch.type(x) // 'String'
// trying to convert the input with unveil
const y = snitch.unveil(x)
snitch.type(y) // 'Array'
Using unveil()
at objects
is tricky, and will improve in the future. Here is what you can do at the moment:
const z = '{a: 1, 1: "b"}'
snitch.unveil(z) // { a: 1, "1": "b" }
// you can do this
snitch.unveil('{a: 1, 1: "b", c: [1, 2, 3]}')
// returns: { '1': 'b', a: 1, c: [ 1, 2, 3 ] }
// but, you can't do this at the moment
snitch.unveil('{a: 1, 1: "b", c: [1, 2, 3], d: {e: 1, f: 2}}')
// returns: '{a: 1, 1: "b", c: [1, 2, 3], d: {e: 1, f: 2}'
You can use typesnitch
for type checking like so:
const { type, unveil, unveilType, isType } = require('typesnitch')
const x = [1, 2, 3]
const y = '[1,2,3]'
type(x) === type(y)
// false
type(x) === type(unveil(y))
// true
// or use the combination
type(x) === unveilType(y)
// true
const z = "hello, world"
isType(z, 'string')
// true
snitch.type(1, true) // 'Integer'
snitch.type(1, false) // 'Number'
snitch.type(1.1) // 'Float'
snitch.type(Number.Nan) // 'NaN'
snitch.type(1 / 'a') // 'NaN'
snitch.type(1 / 'a', false) // 'Number'
snitch.type(1 / 0) // 'Infinity'
snitch.type(-1 / 0) // '-Infinity'
snitch.type('hello world') // 'String'
snitch.type([1, 2, 3]) // 'Array'
snitch.type({ a: 1, b: 2 }) // 'Object'
...
convert
: Convert data to strings
, numbers
, arrays
, or objects
snitch.convert.toString(1)
// '1'
snitch.convert.toNumber('1')
// 1
snitch.convert.toArray('hello; world', { delimiter: ';' })
// ['hello', 'world']
const x = { a: 1, b: 2 }
snitch.convert.toArray(x, { objectKeys: false })
// [1, 2]
snitch.convert.toArray(x, { objectKeys: true })
// ['a', 'b']
snitch.convert.toObject(['a', 'b'])
// {'0': 'a', '1': 'b'}
detect
: Helper functions for single type detectionsnitch.detect.isString('1') // true
snitch.detect.isNumber(1) // true
snitch.detect.isInteger(1) // true
snitch.detect.isInteger(1.1) // false
snitch.detect.isFloat(1.1) // true
snitch.detect.isFloat(1) // false
...
FAQs
A Simple Type Sniffer for JS
We found that typesnitch 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.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.