Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
typeforce
Advanced tools
Readme
Another biased type checking solution for Javascript.
Exception messages may change between patch versions, as often the patch will change some behaviour that was unexpected and naturally it results in a different error message.
var typeforce = require('typeforce')
var element = { prop: 'foo' }
var elementNumber = { prop: 2 }
var array = [element, element, elementNumber]
// supported primitives 'Array', 'Boolean', 'Buffer', 'Number', 'Object', 'String'
typeforce('Array', array)
typeforce('Number', array)
// TypeError: Expected Number, got Array
// array types
typeforce(['Object'], array)
typeforce(typeforce.arrayOf('Object'), array)
// supports recursive type templating
typeforce({ prop: 'Number' }, elementNumber)
// maybe types
typeforce('?Number', 2)
typeforce('?Number', null)
typeforce(typeforce.maybe(typeforce.Number), 2)
typeforce(typeforce.maybe(typeforce.Number), null)
// sum types
typeforce(typeforce.anyOf('String', 'Number'), 2)
typeforce(typeforce.allOf({ x: typeforce.Number }, { y: typeforce.Number }), {
x: 1,
y: 2
})
// value types
typeforce(typeforce.value(3.14), 3.14)
// custom types
function LongString (value, strict) {
if (!typeforce.String(value)) return false
if (value.length !== 32) return false
return true
}
typeforce(LongString, '00000000000000000000000000000000')
// => OK!
typeforce(LongString, 'not long enough')
// TypeError: Expected LongString, got String 'not long enough'
Protips:
// use precompiled primitives for high performance
typeforce(typeforce.Array, array)
// or just precompile a template
var type = {
foo: 'Number',
bar: '?String'
}
var fastType = typeforce.compile(type)
// fastType => typeforce.object({
// foo: typeforce.Number,
// bar: typeforce.maybe(typeforce.String)
// })
// use strictness for recursive types to enforce whitelisting properties
typeforce({
x: 'Number'
}, { x: 1 }, true)
// OK!
typeforce({
x: 'Number'
}, { x: 1, y: 2 }, true)
// TypeError: Unexpected property 'y' of type Number
Protips (extended types):
typeforce(typeforce.tuple('String', 'Number'), ['foo', 1])
// OK!
typeforce(typeforce.tuple('Number', 'Number'), ['not a number', 1])
// TypeError: Expected property "0" of type Number, got String 'not a number'
typeforce(typeforce.map('Number'), {
'anyKeyIsOK': 1
})
// OK!
typeforce(typeforce.map('Number', typeforce.HexN(8)), {
'deadbeef': 1,
'ffff0000': 2
})
// OK!
function Foo () {
this.x = 2
}
typeforce(typeforce.quacksLike('Foo'), new Foo())
// OK!
// Note, any Foo will do
typeforce(typeforce.quacksLike('Foo'), new (function Foo() {}))
// OK!
Protips (no throw)
var typeforce = require('typeforce/nothrow')
var value = 'foobar'
if (typeforce(typeforce.Number, value)) {
// didn't throw!
console.log(`${value} is a number`) // never happens
} else {
console.log(`Oops, ${typeforce.error.message}`)
// prints 'Oops, Expected Number, got String foobar'
}
Protips (async)
var typeforce = require('typeforce/async')
typeforce(typeforce.Number, value, function (err) {
if (err) return console.log(`Oops, ${typeforce.error.message}`)
console.log(`${value} is a number`) // never happens
})
WARNING: Be very wary of using the quacksLike
type, as it relies on the Foo.name
property.
If that property is mangled by a transpiler, such as uglifyjs
, you will have a bad time.
FAQs
Another biased type checking solution for Javascript
The npm package typeforce receives a total of 153,625 weekly downloads. As such, typeforce popularity was classified as popular.
We found that typeforce 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.