Socket
Socket
Sign inDemoInstall

intertype

Package Overview
Dependencies
Maintainers
1
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

intertype - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

13

lib/cataloguing.js

@@ -161,2 +161,7 @@ // Generated by CoffeeScript 2.3.2

//-----------------------------------------------------------------------------------------------------------
this.has_key = function(x, key) {
return this.has_keys(x, key);
};
//-----------------------------------------------------------------------------------------------------------
this.has_only_keys = function(x, ...P) {

@@ -169,12 +174,4 @@ var keys, probes;

//-----------------------------------------------------------------------------------------------------------
this.has_key = function(x, key) {
if (x == null) {
return false;
}
return x[key] !== void 0;
};
}).call(this);
//# sourceMappingURL=cataloguing.js.map

@@ -115,2 +115,45 @@ // Generated by CoffeeScript 2.3.2

//.........................................................................................................
this.declare('empty', function(x) {
return (this.size_of(x)) === 0;
});
this.declare('singular', function(x) {
return (this.size_of(x)) === 1;
});
this.declare('nonempty', function(x) {
return (this.size_of(x)) > 0;
});
this.declare('plural', function(x) {
return (this.size_of(x)) > 1;
});
this.declare('nonempty_text', function(x) {
return (this.isa.text(x)) && (this.isa.nonempty(x));
});
this.declare('nonempty_list', function(x) {
return (this.isa.list(x)) && (this.isa.nonempty(x));
});
this.declare('nonempty_object', function(x) {
return (this.isa.object(x)) && (this.isa.nonempty(x));
});
this.declare('nonempty_set', function(x) {
return (this.isa.set(x)) && (this.isa.nonempty(x));
});
this.declare('nonempty_map', function(x) {
return (this.isa.map(x)) && (this.isa.nonempty(x));
});
this.declare('empty_text', function(x) {
return (this.isa.text(x)) && (this.isa.empty(x));
});
this.declare('empty_list', function(x) {
return (this.isa.list(x)) && (this.isa.empty(x));
});
this.declare('empty_object', function(x) {
return (this.isa.object(x)) && (this.isa.empty(x));
});
this.declare('empty_set', function(x) {
return (this.isa.set(x)) && (this.isa.empty(x));
});
this.declare('empty_map', function(x) {
return (this.isa.map(x)) && (this.isa.empty(x));
});
//.........................................................................................................
this.declare('buffer', {

@@ -117,0 +160,0 @@ size: 'length'

@@ -57,5 +57,4 @@ // Generated by CoffeeScript 2.3.2

//---------------------------------------------------------------------------------------------------------
constructor(instance_name) {
constructor(target = null) {
super();
this.instance_name = instance_name;
this.specs = {};

@@ -65,2 +64,5 @@ this.isa = Multimix.get_keymethod_proxy(this, isa);

declarations.declare_types.apply(this);
if (target != null) {
this.export(target);
}
}

@@ -67,0 +69,0 @@

@@ -102,3 +102,3 @@ // Generated by CoffeeScript 2.3.2

intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export());
//.........................................................................................................

@@ -128,3 +128,3 @@ probes_and_matchers = [["isa( 'callable', 'xxx' )", false, null], ["isa( 'callable', function () {} )", true, null], ["isa( 'callable', async function () { await 42 } )", true, null], ["isa( 'callable', function* () { yield 42 } )", true, null], ["isa( 'callable', ( function* () { yield 42 } )() )", false, null], ["isa( 'date', new Date() )", true, null], ["isa( 'date', true )", false, null], ["isa( 'date', 'helo' )", false, null], ["isa( 'date', 2 )", false, null], ["isa( 'date', Date.now() )", false, null], ["isa( 'finite', 123 )", true, null], ["isa( 'global', global )", true, null], ["isa( 'integer', 123 )", true, null], ["isa( 'integer', 42 )", true, null], ["isa( 'number', 123 )", true, null], ["isa( 'number', 42 )", true, null], ["isa( 'number', NaN )", false, null], ["isa( 'number', NaN )", false, null], ["isa( 'safeinteger', 123 )", true, null], ["isa( 'text', 'x' )", true, null], ["isa( 'text', NaN )", false, null], ["isa.even( 42 )", true, null], ["isa.finite( 123 )", true, null], ["isa.integer( 123 )", true, null], ["isa.integer( 42 )", true, null], ["isa.multiple_of( 42, 2 )", true, null], ["isa.multiple_of( 5, 2.5 )", true, null], ["isa.multiple_of( 5, 2 )", false, null], ["isa.number( 123 )", true, null], ["isa.safeinteger( 123 )", true, null], ["isa[ 'multiple_of' ]( 42, 2 )", true, null], ["isa.weakmap( new WeakMap() )", true, null], ["isa.map( new Map() )", true, null], ["isa.set( new Set() )", true, null], ["isa.date( new Date() )", true, null], ["isa.error( new Error() )", true, null], ["isa.list( [] )", true, null], ["isa.boolean( true )", true, null], ["isa.boolean( false )", true, null], ["isa.function( ( () => {} ) )", true, null], ["isa.asyncfunction( ( async () => { await f() } ) )", true, null], ["isa.null( null )", true, null], ["isa.text( 'helo' )", true, null], ["isa.undefined( undefined )", true, null], ["isa.global( global )", true, null], ["isa.regex( /^xxx$/g )", true, null], ["isa.object( {} )", true, null], ["isa.nan( NaN )", true, null], ["isa.infinity( 1 / 0 )", true, null], ["isa.infinity( -1 / 0 )", true, null], ["isa.number( 12345 )", true, null], ["isa.buffer( new Buffer( 'xyz' ) )", true, null], ["isa.uint8array( new Buffer( 'xyz' ) )", true, null]];

intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export());
//.........................................................................................................

@@ -154,3 +154,3 @@ probes_and_matchers = [["type_of( new WeakMap() )", 'weakmap', null], ["type_of( new Map() )", 'map', null], ["type_of( new Set() )", 'set', null], ["type_of( new Date() )", 'date', null], ["type_of( new Error() )", 'error', null], ["type_of( [] )", 'list', null], ["type_of( true )", 'boolean', null], ["type_of( false )", 'boolean', null], ["type_of( ( () => {} ) )", 'function', null], ["type_of( ( async () => { await f() } ) )", 'asyncfunction', null], ["type_of( null )", 'null', null], ["type_of( 'helo' )", 'text', null], ["type_of( undefined )", 'undefined', null], ["type_of( global )", 'global', null], ["type_of( /^xxx$/g )", 'regex', null], ["type_of( {} )", 'object', null], ["type_of( NaN )", 'nan', null], ["type_of( 1 / 0 )", 'infinity', null], ["type_of( -1 / 0 )", 'infinity', null], ["type_of( 12345 )", 'number', null], ["type_of( 'xxx' )", 'text', null], ["type_of( function () {} )", 'function', null], ["type_of( async function () { await 42 } )", 'asyncfunction', null], ["type_of( function* () { yield 42 } )", 'generatorfunction', null], ["type_of( ( function* () { yield 42 } )() )", 'generator', null], ["type_of( 123 )", 'number', null], ["type_of( 42 )", 'number', null], ["type_of( [] )", 'list', null], ["type_of( global )", 'global', null], ["type_of( new Date() )", 'date', null], ["type_of( {} )", 'object', null], ["type_of( new Buffer( 'helo' ) )", 'buffer', null], ["type_of( new ArrayBuffer( 42 ) )", 'arraybuffer', null], ["type_of( new Int8Array( 5 ) )", 'int8array', null], ["type_of( new Uint8Array( 5 ) )", 'uint8array', null], ["type_of( new Uint8ClampedArray( 5 ) )", 'uint8clampedarray', null], ["type_of( new Int16Array( 5 ) )", 'int16array', null], ["type_of( new Uint16Array( 5 ) )", 'uint16array', null], ["type_of( new Int32Array( 5 ) )", 'int32array', null], ["type_of( new Uint32Array( 5 ) )", 'uint32array', null], ["type_of( new Float32Array( 5 ) )", 'float32array', null], ["type_of( new Float64Array( 5 ) )", 'float64array', null]];

intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export());
//.........................................................................................................

@@ -195,3 +195,3 @@ probes_and_matchers = [["validate( 'callable', 'xxx' )", false, 'not a valid callable'], ["validate( 'callable', ( function* () { yield 42 } )() )", false, 'not a valid callable'], ["validate( 'date', true )", false, 'not a valid date'], ["validate( 'date', 'helo' )", false, 'not a valid date'], ["validate( 'date', 2 )", false, 'not a valid date'], ["validate( 'date', Date.now() )", false, 'not a valid date'], ["validate( 'number', NaN )", false, 'not a valid number'], ["validate( 'number', NaN )", false, 'not a valid number'], ["validate( 'text', NaN )", false, 'not a valid text'], ["validate.multiple_of( 5, 2 )", false, 'not a valid multiple_of'], ["validate( 'callable', function () {} )", true, null], ["validate( 'callable', async function () { await 42 } )", true, null], ["validate( 'callable', function* () { yield 42 } )", true, null], ["validate( 'date', new Date() )", true, null], ["validate( 'finite', 123 )", true, null], ["validate( 'global', global )", true, null], ["validate( 'integer', 123 )", true, null], ["validate( 'integer', 42 )", true, null], ["validate( 'number', 123 )", true, null], ["validate( 'number', 42 )", true, null], ["validate( 'safeinteger', 123 )", true, null], ["validate( 'text', 'x' )", true, null], ["validate.even( 42 )", true, null], ["validate.finite( 123 )", true, null], ["validate.integer( 123 )", true, null], ["validate.integer( 42 )", true, null], ["validate.multiple_of( 42, 2 )", true, null], ["validate.multiple_of( 5, 2.5 )", true, null], ["validate.number( 123 )", true, null], ["validate.safeinteger( 123 )", true, null], ["validate[ 'multiple_of' ]( 42, 2 )", true, null], ["validate.weakmap( new WeakMap() )", true, null], ["validate.map( new Map() )", true, null], ["validate.set( new Set() )", true, null], ["validate.date( new Date() )", true, null], ["validate.error( new Error() )", true, null], ["validate.list( [] )", true, null], ["validate.boolean( true )", true, null], ["validate.boolean( false )", true, null], ["validate.function( ( () => {} ) )", true, null], ["validate.asyncfunction( ( async () => { await f() } ) )", true, null], ["validate.null( null )", true, null], ["validate.text( 'helo' )", true, null], ["validate.undefined( undefined )", true, null], ["validate.global( global )", true, null], ["validate.regex( /^xxx$/g )", true, null], ["validate.object( {} )", true, null], ["validate.nan( NaN )", true, null], ["validate.infinity( 1 / 0 )", true, null], ["validate.infinity( -1 / 0 )", true, null], ["validate.number( 12345 )", true, null], ["validate.buffer( new Buffer( 'xyz' ) )", true, null], ["validate.uint8array( new Buffer( 'xyz' ) )", true, null]];

intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export());
//.........................................................................................................

@@ -229,3 +229,3 @@ probes_and_matchers = [[123, ["count", "finite", "frozen", "integer", "nonnegative", "notunset", "number", "numeric", "odd", "positive", "safeinteger", "sealed", "truthy"], null], [124, ["count", "even", "finite", "frozen", "integer", "nonnegative", "notunset", "number", "numeric", "positive", "safeinteger", "sealed", "truthy"], null], [0, ["count", "even", "falsy", "finite", "frozen", "integer", "nonnegative", "nonpositive", "notunset", "number", "numeric", "safeinteger", "sealed", "zero"], null], [true, ["boolean", "frozen", "notunset", "odd", "sealed", "truthy"], null], [null, ["falsy", "frozen", "null", "odd", "sealed", "unset"], null], [void 0, ["falsy", "frozen", "odd", "sealed", "undefined", "unset"], null], [{}, ["extensible", "notunset", "object", "odd", "truthy"], null], [[], ["extensible", "list", "notunset", "odd", "truthy"], null]];

intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export());
//.........................................................................................................

@@ -354,2 +354,20 @@ probes_and_matchers = [

//-----------------------------------------------------------------------------------------------------------
this["export to target"] = function(T, done) {
var intertype, return_value, target;
//.........................................................................................................
target = {};
intertype = new Intertype;
return_value = intertype.export(target);
T.ok(return_value === target);
target.declare('sometype', function(x) {
return (this.isa.text(x)) && (x.startsWith(':'));
});
debug('µ44333', target);
debug('µ44333', target.isa.sometype('sometext'));
debug('µ44333', target.isa.sometype(':sometext'));
done();
return null;
};
later = function() {};

@@ -385,3 +403,3 @@

intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export());
urge(size_of('𣁬𡉜𠑹𠅁', 'codepoints'));

@@ -388,0 +406,0 @@ intertype.declare('point', {

{
"name": "intertype",
"version": "2.0.0",
"version": "2.1.0",
"description": "A JavaScript typechecker",

@@ -21,3 +21,3 @@ "main": "lib/main.js",

"cnd": "^4.5.0",
"multimix": "^1.0.0"
"multimix": "^2.0.0"
},

@@ -24,0 +24,0 @@ "devDependencies": {

@@ -8,5 +8,64 @@

### Declaring New Types
## Concepts
* what is a type
* fundamental types vs. domain types
* `isa`
* `validate`
* `type_of`
* `types_of`
* Types are defined using an ordered set of (one or more) named boolean test functions known as 'aspects'.
In order for a value `x` to be of type `T`, all aspects—when called in their defined order with `x` (and
possibly other arguments, see below)—have to return `true`. Aspect satisfication tests are done in a lazy
fashion, so that no tests are performed after one has failed. Likewise for type validation, the difference
being that the first failing aspect will cause an error to thrown that quotes the aspect's name.
* Types may be parametrized. For example, there's a 'partial' type `multiple_of` which needs a module (a
number to be a multiple of) as extra parameters; thus, we can test `isa.multiple_of 121, 11`.
* In InterType, a 'type' is, on the one hand, essentially an ordered set of aspects; on the other hand,
since within the context of a given InterType instance, each type corresponds to exactly one type name (a
nonempty text), a 'type' can be identified with a string. Thus, the type of, say, `[]` *is* `'list'` (i.e.
the string that spells its name).
Conversely, any list of functions that **1)** can be called with a value as first arguments (possibly
plus a number of extra parameters), that **2)** never throws an error and **3)** always returns
a Boolean value can be regarded as a list of aspects, hence defining a (possibly empty) set of values.
## Usage
[WIP]
One usage pattern for InterType is to make it so that one (sub-) project gets a module—call it `types`—that
is dedicated to type declarations; `require`ing that `types` module then makes type checking and type
validation methods available. Say we have:
```coffee
# in module `types.coffee`
# instantiate InterType instance, export its methods to `module.exports` in one go:
intertype = new ( require 'intertype' ) module.exports
# now you can call methods of InterType instance as *module* methods:
@declare 'mytype', ( x ) -> ( @isa number ) and ( x > 12 ) and ( x <= 42 )
```
In another module:
```coffee
# now use the declared types:
{ isa, type_of, validate, } = require './types'
console.log isa.integer 100 # true
console.log isa.mytype 20 # true
console.log isa.mytype 100 # false
console.log type_of 20 # 'number'
console.log validate.mytype 20 # true
console.log validate.mytype 100 # throws "not a valid mytype"
```
## Declaring New Types
`intertype.declare()` allows to add new type specifications to `intertype.specs`. It may be called with one

@@ -42,3 +101,3 @@ to three arguments. The three argument types are:

* `intertype.declare type, spec`—This form works like the above, except that, if `spec.type` is set, it must
equal the `type` argument. It is primarily implemented for syntactical reasons (see examples).
equal the `type` argument. It is primarily implemented for syntactical reasons (see examples).

@@ -55,2 +114,17 @@ * `intertype.declare type, test`—This form is handy for declaring types without any further details: you

## To Do
* [x] Allow to pass in target object at instantiation, so e.g. `new intertype @` will cause all InterType
methods to become available on target as `@isa()`, `@validate` and so on.
* [x] Rename `export_modules()` to `export()`, allow target object (e.g. `module.exports`) to be passed in.
* [ ] Add types `empty`, `nonempty`, ...
* [ ] Implement method to iterate over type names, specs.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc