
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
contracts.js
Advanced tools
Contracts.js is a contract library for JavaScript that allows you to specify invariants between parts of your code and have them checked at runtime for violations.
It is used in the CoffeeScript dialect contracts.coffee but can also be used directly in normal JavaScript programs if you don't want to or can't use CoffeeScript.
This library is possible because of and requires Proxies which is a new feature of JavaScript that is currently only implemented in Firefox 4+ and bleeding edge V8 (not yet in Chrome but they are in Node.js (0.5.8+) with the --harmony_proxies
flag).
To use include these files:
This adds a Contracts
object to the global scope that has two properties Contracts.contracts
(which contains some prebuilt contracts to use) and Contracts.combinators
(which contains utility functions to build new contracts).
We can now wrap a function in a contract like so:
var C = Contracts.contracts,
K = Contracts.combinators,
id = K.guard(
K.fun(C.Num, C.Num),
function(x) { return x; });
id = id.use();
id("foo"); // contract violation!
If you would like to load all of the contracts and combinators into the global scope, just include src/autoload.js
.
More documentation and rational can be found at the sister project contracts.coffee.
Guards a value with a contract
Contracts.combinators.guard :: (Contract, Any, Str?, Str?) -> { use: () -> Any }
Contracts.combinators.guard(contract, value [, server[, client]])
Returns an object with a .use
function that must be called before the contracted value can be used (this is done to correctly setup "module" names when they are not supplied to guard
).
Creates a contract that checks first-order values (i.e. not functions or objects).
Contracts.combinators.check :: ((Any) -> Bool, Str) -> Contract
Contracts.combinators.check(predicate, name)
This is used to build contracts that get applied to values via the guard
function. The guard
function handles calling the predicate supplied to check
at the appropriate time.
An example of a contract to check for numbers:
Contracts.combinators.check(function(x) {
return typeof(x) === 'number';
}, 'Number')
Contracts.combinators.fun :: (Contract or [...Contract],
((Any) -> Contract) or Contract,
{
callOnly: Bool
newOnly: Bool
pre: (Any) -> Bool
post: (Any) -> Bool
this: {...}
}) -> Contract
Contracts.combinators.fun(domain, range, options)
new
is used with the functionnew
is not used with the functionthis
objectDependent function contracts (where the result depends on the argument values) are handled by using a function as the range
. When the function returns its argument values are first passed to the range
function which should return a contract. This contract is then used to check the original function's result.
As a contrived example:
Contracts.combinators.fun(Str, function(x) {
if(x === 42) {
return Contracts.contracts.Num;
} else {
return Contracts.contracts.Str;
}
})
If the function contracted is called with 42
then its result must be a Num
otherwise it must be a Str
.
Note that arguments are potentially mutable (they might be one value at the beginning of the function and different when the function returns) so keep that in mind when using dependent contracts.
Contracts.combinators.object :: ({ ... },
{
extensible: Bool
sealed: Bool
frozen: Bool
invariant: (Any) -> Bool
}) -> Contract
Contracts.combinators.object(object, options)
Object contracts are built with an object that maps properties to objects. Example:
Contracts.combinators.object({
foo: Str,
bar: Num
})
In this case the contracted object must have both the foo
and bar
properties (if missing, a contract violation is thrown at contract application time) and these properties must abide by their respective contracts (which are checked each time the property is changed).
Object invariants can be checked with the invariant option. Whenever any property is changed the invariant function is called with a reference to the object. If the invariant returns false a contract violation is thrown.
FAQs
A contract library for JavaScript
The npm package contracts.js receives a total of 1 weekly downloads. As such, contracts.js popularity was classified as not popular.
We found that contracts.js 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.