
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.
insist-types
Advanced tools
Insist on types! Make your code more readable by explicitly requiring type for function parameters.
npm install insist-types
Supports both Node and the browser. On the browser, exported as window.insist
.
var insist = require('insist-types');
var foo = function (arg1, arg2) {
insist.args(arguments, String, Boolean);
// ...
};
foo('hi', true); // works!
foo('hi', 'true'); // throws error
Insist can understand multiple types, optional arguments, nullable types, typed arrays, and classes.
Just put the desired types in an array.
var insist = require('insist-types');
var foo = function (arg1, arg2) {
insist.args(arguments, [String, Boolean, Object], String);
// ...
};
foo('hi', 'hello'); // works!
foo(true, 'hello'); // works!
foo({}, 'hello'); // works!
foo(1, 'hello'); // throws error
insist.args
always returns an array of the arguments and shifts each argument into the proper
index when dealing with optional types.
var insist = require('insist-types');
var foo = function (arg1, optionalArg2, arg3) {
var args = insist.args(arguments, Number, insist.optional(Object), Function);
// args will be [{number}, {object or null}, {function}]
};
foo(1, function () {}); // works!
foo(2, null, function () {}); // works!
foo(3, {}) // throws error
foo(4) // throws error
var insist = require('insist-types');
var foo = function (arg1) {
insist.args(arguments, insist.nullable(Function));
// ...
};
foo(null); // works!
foo(function () {}); // works!
foo(); // throws error
foo('wrong') // throws error
Both insist.optional
and insist.nullable
really just augment the type. In the above example, it would have worked to use [Function, null]
instead of insist.nullable(Function)
. If you want to include undefined
make sure you use insist.optional
instead of insist.nullable
.
var insist = require('insist-types');
var foo = function (arg1) {
insist.args(arguments, insist.arrayOf(Number));
// ...
};
foo([]); // works!
foo([1]); // works!
foo([1, 2]); // works!
foo([1, '2']); // throws error
You can also nest arrays.
var insist = require('insist-types');
var foo = function (arg1) {
insist.args(arguments, insist.arrayOf([Number, insist.arrayOf(Number)));
// ...
};
foo([]); // works!
foo([1]); // works!
foo([1, [2, 3]]); // works!
foo([1, [2, 3, [4]]]); // throws error
To handle the last case (foo([1, [2, 3, [4]]]);
), you can just use an array instead.
var insist = require('insist-types');
var foo = function (arg1) {
insist.args(arguments, insist.arrayOf([Number, Array]));
// ...
};
foo([]); // works!
foo([1]); // works!
foo([1, [2, 3]]); // works!
foo([1, [2, 3, [4]]]); // works!
var insist = require('insist-types');
function Bar() {};
var foo = function (arg1) {
insist.args(arguments, Bar);
// ...
};
foo(new Bar()); // works!
foo({}); // throws error
Sublasses work too!
var insist = require('insist-types');
var util = require('util');
var events = require('events');
function Foo() {
events.EventEmitter.call(this);
};
util.inherits(Foo, events.EventEmitter);
var fn = function (arg1) {
insist.args(arguments, events.EventEmitter);
// ...
};
fn(new Foo()); // works!
Sometimes it's handy to have a definition that'll take anything.
var insist = require('insist-types');
foo = function (arg1) {
insist.args(arguments, insist.anything());
// ...
};
foo(null); // works!
foo(true); // works!
foo("true"); // works!
foo(1); // works!
foo(["true"]); // works!
foo({}); // works!
foo(); // throws error
I've debated a lot about whether null
and undefined
should be include in insist.anything
. Currently, null
is acceptable and undefined
is not because I've found this to be the most useful anything
. Open an issue if you have some input!
It can be valuable to assert that a parameter is actually a type.
var insist = require('insist-types');
foo = function (arg1) {
insist.args(arguments, insist.type());
// ...
};
foo(String); // works!
foo(null); // works!
foo(undefined); // works!
foo(insist.arrayOf(Number)); // works!
foo("bar"); // throws error
var insist = require('insist-types');
Colors = {
RED: "red",
GREEN: "green",
BLUE: "blue"
};
foo = function (arg1) {
insist.args(arguments, insist.enum(Colors));
// ...
};
foo(Colors.GREEN); // works!
foo("red"); // works!
foo("yellow"); // throws error
foo(Colors); // throws error
You can set options on insist.
insist({isDisabled: true});
// or
var insist = require('insist')({isDisabled: true});
When set to true
, all asserts are set to noops, except for insist.args
, which checks for optional expected types (so as to not break argument shifting). In Node, when NODE_ENV
is set to production
, isDisabled is set to true by default, unless INSIST_IN_PROD
is set to true
.
It's recommended that insist is removed from production code, especially client-side JavaScript, for both size and efficiency. The Remover class makes this simple. It is worth noting, before removing a reference, the reference is checked to make sure that it is not being used to shift arguments. If it is, the reference will not be removed.
var insist = require('insist-types');
var source = fs.readFileSync('source/file.js').toString();
var remover = new insist.Remover();
var newSource remover.removeInsist(source);
fs.writeFileSync('source/out.js', newSource);
Currently, there is only one option for the remover: aliases. This option allows for when insist methods of been aliased to something else in a codebase.
var insist = require('insist-types');
var source = fs.readFileSync('source/file.js').toString();
var remover = new insist.Remover({
aliases: {
args: 'assertArgs',
ofType: 'assertOfType'
}
});
var newSource remover.removeInsist(source);
fs.writeFileSync('source/out.js', newSource);
The remover is also available through the CLI.
usage: insist-types [-h] [-v] -i [DIR|FILE [DIR|FILE ...]] [-a STRING]
[-t STRING]
Removes insist-types from matching files.
Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
-i [DIR|FILE [DIR|FILE ...]], --include [DIR|FILE [DIR|FILE ...]]
Adds the directory or file to be included in the
processing. At least one required.
-a STRING, --argsAlias STRING
Specifies the alias for insist.args. Defaults to
'insist.args'.
-t STRING, --ofTypeAlias STRING
Specifies the alias for insist.ofType. Defaults to
'insist.ofType'.
insist.args(arguments, types...) // asserts the type of an arguments object and returns the shifted arguments
insist.ofType(value, type) // asserts the type of a value
insist.isType(type) // asserts that the supplied type is actually a type
insist.isValidType(type) // returns true|false for whether the type is actually a type
insist.isOptionalType(type) // returns true|flase for whether the type is an optional type
insist.isOfType(value, type) // returns true|false for whether the value is of the type
insist.getNameForType(type) // returns the name of the type
insist.getNameForValue(value) // returns the name of the value (ex. String, Anonymous function)
insist.arrayOf(type) // used for creating an array type
insist.nullable(type) // used for creating a nullable type
insist.optional() // used for creating an optional type
insist.anything() // used for a type that can be anything
insist.type() // used for a type that expects a type
insist.enum(SomeEnumObject) // used for creating an enum type
insist.Remover // used to remove references from source
insist.Remover.prototype.removeInsist(source) // removes references
FAQs
Insist on types!
The npm package insist-types receives a total of 362 weekly downloads. As such, insist-types popularity was classified as not popular.
We found that insist-types 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.