Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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 1.0.2 to 2.0.0

8

lib/cataloguing.js

@@ -168,4 +168,12 @@ // Generated by CoffeeScript 2.3.2

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

121

lib/declarations.js

@@ -66,5 +66,2 @@ // Generated by CoffeeScript 2.3.2

});
this.declare('global', (x) => {
return (js_type_of(x)) === 'global';
});
this.declare('callable', (x) => {

@@ -124,17 +121,57 @@ var ref;

});
this.declare('list', {
this.declare('arraybuffer', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'array';
return (js_type_of(x)) === 'arraybuffer';
});
this.declare('object', {
this.declare('int8array', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'object';
return (js_type_of(x)) === 'int8array';
});
this.declare('text', {
this.declare('uint8array', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'string';
return (js_type_of(x)) === 'uint8array';
});
this.declare('uint8clampedarray', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'uint8clampedarray';
});
this.declare('int16array', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'int16array';
});
this.declare('uint16array', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'uint16array';
});
this.declare('int32array', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'int32array';
});
this.declare('uint32array', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'uint32array';
});
this.declare('float32array', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'float32array';
});
this.declare('float64array', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'float64array';
});
this.declare('list', {
size: 'length'
}, (x) => {
return (js_type_of(x)) === 'array';
});
this.declare('set', {

@@ -153,27 +190,53 @@ size: 'size'

});
return this.declare('weakset', function(x) {
this.declare('weakset', function(x) {
return (js_type_of(x)) === 'weakset';
});
this.declare('error', function(x) {
return (js_type_of(x)) === 'error';
});
this.declare('regex', function(x) {
return (js_type_of(x)) === 'regexp';
});
//.........................................................................................................
this.declare('object', {
tests: (x) => {
return (js_type_of(x)) === 'object';
},
size: (...xP) => {
return (this.keys_of(...xP)).length;
}
});
//.........................................................................................................
this.declare('global', {
tests: (x) => {
return (js_type_of(x)) === 'global';
},
size: (...xP) => {
return (this.all_keys_of(...xP)).length;
}
});
//.........................................................................................................
return this.declare('text', {
tests: (x) => {
return (js_type_of(x)) === 'string';
},
size: function(x, selector = 'codeunits') {
var ref;
switch (selector) {
case 'codepoints':
return (Array.from(x)).length;
case 'codeunits':
return x.length;
case 'bytes':
return Buffer.byteLength(x, (ref = typeof settings !== "undefined" && settings !== null ? settings['encoding'] : void 0) != null ? ref : 'utf-8');
default:
throw new Error(`unknown counting selector ${rpr(selector)}`);
}
}
});
};
//-----------------------------------------------------------------------------------------------------------
// list: 'length'
// # arguments: 'length'
// buffer: 'length'
// set: 'size'
// map: 'size'
// #.........................................................................................................
// global: ( x ) => ( @all_keys_of x ).length
// pod: ( x ) => ( @keys_of x ).length
// #.........................................................................................................
// text: ( x, selector = 'codeunits' ) ->
// switch selector
// when 'codepoints' then return ( Array.from x ).length
// when 'codeunits' then return x.length
// when 'bytes' then return Buffer.byteLength x, ( settings?[ 'encoding' ] ? 'utf-8' )
// else throw new Error "unknown counting selector #{rpr selector}"
/* not supported until we figure out how to do it in strict mode: */
// @declare 'arguments', ( x ) -> ( js_type_of x ) is 'arguments'
// @declare 'boundfunction', { supertype: 'callable', }, ( x ) => ( ( @supertype_of x ) is 'callable' ) and ( not Object.hasOwnProperty x, 'prototype' )
// @declare 'boundfunction', ( x ) => ( @isa 'callable', x ) and ( not Object.hasOwnProperty x, 'prototype' )
// Array.isArray

@@ -180,0 +243,0 @@ // ArrayBuffer.isView

@@ -50,3 +50,11 @@ // Generated by CoffeeScript 2.3.2

//-----------------------------------------------------------------------------------------------------------
this._check_spec = function(type, ...xP) {
this._satisfies_all_aspects = function(type, ...xP) {
if ((this._get_unsatisfied_aspect(type, ...xP)) == null) {
return true;
}
return false;
};
//-----------------------------------------------------------------------------------------------------------
this._get_unsatisfied_aspect = function(type, ...xP) {
var aspect, ref, spec, test;

@@ -61,6 +69,6 @@ if ((spec = this.specs[type]) == null) {

if (!test.apply(this, xP)) {
return false;
return aspect;
}
}
return true;
return null;
};

@@ -70,12 +78,14 @@

this.type_of = function(...xP) {
var R, i, len, ref, subtype;
/* TAINT this should be generalized for all Intertype types that split up / rename a JS type: */
var R, x;
switch (R = js_type_of(...xP)) {
case 'uint8array':
if (Buffer.isBuffer(...xP)) {
R = 'buffer';
}
break;
case 'number':
ref = ['nan', 'infinity'];
/* TAINT this should be generalized for all Intertype types that split up / rename a JS type: */
for (i = 0, len = ref.length; i < len; i++) {
subtype = ref[i];
if (this._check_spec(subtype, ...xP)) {
return subtype;
}
[x] = xP;
if (!Number.isFinite(x)) {
R = (Number.isNaN(x)) ? 'nan' : 'infinity';
}

@@ -92,3 +102,3 @@ break;

}
if (this.specs[R] == null) {
if (!(R in this.specs)) {
/* Refuse to answer question in case type found is not in specs: */

@@ -153,4 +163,13 @@ // debug 'µ33332', R, ( k for k of @specs )

//.........................................................................................................
if ((T = js_type_of(spec.tests)) !== 'object') {
throw new Error(`µ7115 expected an object for spec.tests, got a ${T}`);
switch ((T = js_type_of(spec.tests))) {
case 'function':
spec.tests = {
main: spec.tests
};
break;
case 'object':
null;
break;
default:
throw new Error(`µ7115 expected an object for spec.tests, got a ${T}`);
}

@@ -238,3 +257,3 @@ //.........................................................................................................

// @validate[ type ] = ( P... ) => @validate type, P...
spec.size_of = this._sizeof_method_from_spec(type, spec);
spec.size = this._sizeof_method_from_spec(type, spec);
//.........................................................................................................

@@ -241,0 +260,0 @@ return null;

// Generated by CoffeeScript 2.3.2
(function() {
//...........................................................................................................
var CND, _xrpr, inspect;
'use strict';
var CND, _xrpr, inspect, rpr;
//-----------------------------------------------------------------------------------------------------------
CND = require('cnd');
rpr = CND.rpr.bind(CND);
({inspect} = require('util'));

@@ -9,0 +12,0 @@

// Generated by CoffeeScript 2.3.2
(function() {
'use strict';
var CND, Multimix, alert, assign, badge, debug, declarations, flatten, help, info, isa, jr, js_type_of, rpr, urge, warn, whisper, xrpr;
var CND, Multimix, alert, assign, badge, debug, declarations, flatten, get_rprs_of_tprs, help, info, isa, jr, js_type_of, rpr, urge, validate, warn, whisper, xrpr;

@@ -31,3 +31,3 @@ //###########################################################################################################

//...........................................................................................................
({assign, jr, flatten, xrpr, js_type_of} = require('./helpers'));
({assign, jr, flatten, xrpr, get_rprs_of_tprs, js_type_of} = require('./helpers'));

@@ -39,21 +39,16 @@ //...........................................................................................................

isa = function(type, ...xP) {
if ((this.type_of(...xP)) === type) {
// debug 'µ33444', type, xP
// debug 'µ33444', ( k for k of @ )
return true;
}
return this._check_spec(type, ...xP);
return this._satisfies_all_aspects(type, ...xP);
};
//-----------------------------------------------------------------------------------------------------------
this._validate = function(type, ...xP) {};
//-----------------------------------------------------------------------------------------------------------
this.validate = new Proxy(this._validate, {
get: function(target, type) {
return (...P) => {
return target(type, ...P);
};
validate = function(type, ...xP) {
var P, aspect, message, rpr_of_tprs, srpr_of_tprs, x;
if ((aspect = this._get_unsatisfied_aspect(type, ...xP)) == null) {
return true;
}
});
[x, ...P] = xP;
({rpr_of_tprs, srpr_of_tprs} = get_rprs_of_tprs(P));
message = aspect === 'main' ? `µ3093 not a valid ${type}: ${xrpr(x)}${srpr_of_tprs}` : `µ3093 not a valid ${type} (violates ${rpr(aspect)}): ${xrpr(x)}${srpr_of_tprs}`;
throw new Error(message);
};

@@ -69,3 +64,3 @@ //===========================================================================================================

this.isa = Multimix.get_keymethod_proxy(this, isa);
// @validate = Multimix.get_keymethod_proxy @, validate
this.validate = Multimix.get_keymethod_proxy(this, validate);
declarations.declare_types.apply(this);

@@ -72,0 +67,0 @@ }

// Generated by CoffeeScript 2.3.2
(function() {
'use strict';
var CND, INTERTYPE, Intertype, alert, assign, badge, debug, echo, flatten, help, info, jr, js_type_of, log, njs_path, praise, rpr, test, urge, warn, whisper, xrpr;
var CND, INTERTYPE, Intertype, alert, assign, badge, debug, echo, flatten, help, info, jr, js_type_of, later, log, njs_path, praise, rpr, test, urge, warn, whisper, xrpr,
indexOf = [].indexOf;

@@ -48,82 +49,3 @@ //###########################################################################################################

//-----------------------------------------------------------------------------------------------------------
this["test type_of"] = function(T) {
T.eq(isa(new WeakMap()), 'weakmap');
T.eq(isa(new Map()), 'map');
T.eq(isa(new Set()), 'set');
T.eq(isa(new Date()), 'date');
T.eq(isa(new Error()), 'error');
T.eq(isa([]), 'list');
T.eq(isa(true), 'boolean');
T.eq(isa(false), 'boolean');
T.eq(isa((function() {})), 'function');
T.eq(isa((function*() {
return (yield 123);
})), 'generatorfunction');
T.eq(isa((function*() {
return (yield 123);
})()), 'generator');
T.eq(isa((async function() {
return (await f());
})), 'asyncfunction');
T.eq(isa(null), 'null');
T.eq(isa('helo'), 'text');
T.eq(isa(void 0), 'undefined');
T.eq(isa(arguments), 'arguments');
T.eq(isa(global), 'global');
T.eq(isa(/^xxx$/g), 'regex');
T.eq(isa({}), 'pod');
T.eq(isa(0/0), 'nan');
T.eq(isa(1 / 0), 'infinity');
T.eq(isa(-1 / 0), 'infinity');
T.eq(isa(12345), 'number');
T.eq(isa(new Buffer('helo')), 'buffer');
T.eq(isa(new ArrayBuffer(42)), 'arraybuffer');
//.........................................................................................................
T.eq(isa(new Int8Array(5)), 'int8array');
T.eq(isa(new Uint8Array(5)), 'uint8array');
T.eq(isa(new Uint8ClampedArray(5)), 'uint8clampedarray');
T.eq(isa(new Int16Array(5)), 'int16array');
T.eq(isa(new Uint16Array(5)), 'uint16array');
T.eq(isa(new Int32Array(5)), 'int32array');
T.eq(isa(new Uint32Array(5)), 'uint32array');
T.eq(isa(new Float32Array(5)), 'float32array');
T.eq(isa(new Float64Array(5)), 'float64array');
//.........................................................................................................
return null;
};
//-----------------------------------------------------------------------------------------------------------
this["test size_of"] = function(T) {
// debug ( new Buffer '𣁬', ), ( '𣁬'.codePointAt 0 ).toString 16
// debug ( new Buffer '𡉜', ), ( '𡉜'.codePointAt 0 ).toString 16
// debug ( new Buffer '𠑹', ), ( '𠑹'.codePointAt 0 ).toString 16
// debug ( new Buffer '𠅁', ), ( '𠅁'.codePointAt 0 ).toString 16
T.eq(isa.size_of([1, 2, 3, 4]), 4);
T.eq(isa.size_of(new Buffer([1, 2, 3, 4])), 4);
T.eq(isa.size_of('𣁬𡉜𠑹𠅁'), 2 * (Array.from('𣁬𡉜𠑹𠅁')).length);
T.eq(isa.size_of('𣁬𡉜𠑹𠅁', 'codepoints'), (Array.from('𣁬𡉜𠑹𠅁')).length);
T.eq(isa.size_of('𣁬𡉜𠑹𠅁', 'codeunits'), 2 * (Array.from('𣁬𡉜𠑹𠅁')).length);
T.eq(isa.size_of('𣁬𡉜𠑹𠅁', 'bytes'), (new Buffer('𣁬𡉜𠑹𠅁', 'utf-8')).length);
T.eq(isa.size_of('abcdefghijklmnopqrstuvwxyz'), 26);
T.eq(isa.size_of('abcdefghijklmnopqrstuvwxyz', 'codepoints'), 26);
T.eq(isa.size_of('abcdefghijklmnopqrstuvwxyz', 'codeunits'), 26);
T.eq(isa.size_of('abcdefghijklmnopqrstuvwxyz', 'bytes'), 26);
T.eq(isa.size_of('ä'), 1);
T.eq(isa.size_of('ä', 'codepoints'), 1);
T.eq(isa.size_of('ä', 'codeunits'), 1);
T.eq(isa.size_of('ä', 'bytes'), 2);
T.eq(isa.size_of(new Map([['foo', 42], ['bar', 108]])), 2);
T.eq(isa.size_of(new Set(['foo', 42, 'bar', 108])), 4);
return T.eq(isa.size_of({
'foo': 42,
'bar': 108,
'baz': 3
}), 3);
};
/* TAINT re-implement types object, pod */
// T.eq ( isa.size_of { '~isa': 'XYZ/yadda', 'foo': 42, 'bar': 108, 'baz': 3, } ), 4
//-----------------------------------------------------------------------------------------------------------
this["_demo 1"] = function(T) {
this["_prototype keys"] = function(T) {
var isa, x, y;

@@ -165,38 +87,46 @@ isa = this;

info('µ9', isa.keys_of(me));
info('µ9', jr(isa.values_of(isa.keys_of(me))));
// info 'µ10', jr ( k for k of me )
// info 'µ11', jr Object.keys me
// info 'µ12', isa.values_of isa.all_own_keys_of true
// info 'µ13', isa.values_of isa.all_own_keys_of undefined
// info 'µ14', isa.values_of isa.all_own_keys_of null
return info('µ9', jr(isa.values_of(isa.keys_of(me))));
};
// debug '' + rpr Object.create null
// debug isa.values_of isa.all_keys_of Object::
urge(CND.type_of((function() {})));
urge(CND.type_of((function*() {
return (yield 4);
})));
urge(CND.type_of((function*() {
return (yield 4);
})()));
urge(CND.type_of((async function() {
return (await f());
})));
urge(CND.isa((function() {}), 'function'));
urge(CND.isa((function*() {
return (yield 4);
}), 'function'));
urge(CND.isa((function*() {
return (yield 4);
})(), 'function'));
return urge(CND.isa((async function() {
return (await f());
}), 'function'));
// info 'µ10', jr ( k for k of me )
// info 'µ11', jr Object.keys me
// info 'µ12', isa.values_of isa.all_own_keys_of true
// info 'µ13', isa.values_of isa.all_own_keys_of undefined
// info 'µ14', isa.values_of isa.all_own_keys_of null
// debug '' + rpr Object.create null
// debug isa.values_of isa.all_keys_of Object::
//-----------------------------------------------------------------------------------------------------------
this["isa"] = async function(T, done) {
var all_keys_of, declare, error, i, intertype, isa, len, matcher, probe, probes_and_matchers, size_of, type_of, types_of, validate;
intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
//.........................................................................................................
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]];
//.........................................................................................................
for (i = 0, len = probes_and_matchers.length; i < len; i++) {
[probe, matcher, error] = probes_and_matchers[i];
await T.perform(probe, matcher, error, function() {
return new Promise(function(resolve, reject) {
var result;
result = eval(probe);
// log jr [ probe, result, ]
// resolve result
resolve(result);
return null;
});
});
}
done();
return null;
};
//-----------------------------------------------------------------------------------------------------------
this["multiple tests"] = async function(T, done) {
var error, i, len, matcher, probe, probes_and_matchers;
this["type_of"] = async function(T, done) {
var all_keys_of, declare, error, i, intertype, isa, len, matcher, probe, probes_and_matchers, size_of, type_of, types_of, validate;
intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
//.........................................................................................................
probes_and_matchers = [["isa.number( 42 )", true, null], ["isa.finite_number( 42 )", true, null], ["isa.infinity( Infinity )", true, null], ["isa.infinity( 42 )", false, null], ["isa.integer( 42 )", true, null], ["isa.integer( 42.1 )", false, null], ["isa.count( 42 )", true, null], ["isa.count( -42 )", false, null], ["isa.count( 42.1 )", false, null], ["isa.callable( 42.1 )", false, null], ["isa.extends( 'function', 'callable' )", true, null], ["isa.extends( 'safe_integer', 'integer' )", true, null], ["isa.extends( 'safe_integer', 'number' )", true, null], ["isa.type_of( 42 )", "number", null], ["isa.type_of( 42.1 )", "number", null], ["isa.supertype_of( 42 )", "number", null], ["isa.supertype_of( 42.1 )", "number", null], ["isa.multiple_of( 33, 3 )", true, null], ["isa.multiple_of( 33, 11 )", true, null], ["isa.multiple_of( 5, 2.5 )", true, null], ["isa.multiple_of( 5, 2.6 )", false, null], ["isa.even( Infinity )", false, null], ["isa.odd( Infinity )", false, null], ["isa.callable( () => {} )", true, null], ["isa.type_of( () => {} )", "function", null], ["isa.type_of( async () => { return await f() } )", "asyncfunction", null], ["isa.supertype_of( () => {} )", "callable", null], ["isa.supertype_of( async () => { return await f(); } )", "callable", null], ["isa.callable( function() {} )", true, null], ["isa.type_of( function() {} )", "function", null], ["isa.type_of( async function() { return await f(); } )", "asyncfunction", null], ["isa.supertype_of( function() {} )", "callable", null], ["isa.supertype_of( async function() { return await f(); } )", "callable", null], ["isa.keys_of( { line: 42, ch: 33, } )", ["line", "ch"], null], ["isa.keys_of( { line: 42, } )", ["line"], null], ["isa.keys_of( { line: 42, ch: undefined, } )", ["line"], null], ["isa.has_keys( { line: 42, ch: 33, }, [ 'line', ] )", true, null], ["isa.has_keys( { line: 42, ch: undefined, }, [ 'line', 'ch', ] )", false, null], ["isa.has_keys( { line: 42, ch: 33, }, [ 'line', 'ch', ] )", true, null], ["isa.has_keys( { line: 42, ch: 33, }, [ 'line', 'ch', 'other', ] )", false, null], ["isa.has_only_keys( { line: 42, ch: 33, }, [ 'line', ] )", false, null], ["isa.has_only_keys( { line: 42, ch: undefined, }, [ 'line', 'ch', ] )", false, null], ["isa.has_only_keys( { line: 42, ch: 33, }, [ 'line', 'ch', ] )", true, null], ["isa.has_only_keys( { line: 42, ch: 33, }, [ 'line', 'ch', 'other', ] )", false, null]];
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]];
//.........................................................................................................

@@ -221,72 +151,206 @@ for (i = 0, len = probes_and_matchers.length; i < len; i++) {

//-----------------------------------------------------------------------------------------------------------
this["_demo 2"] = function(T, done) {
var all_keys_of, declare, intertype, isa, size_of, type_of, types_of, validate;
this["validate"] = async function(T, done) {
var all_keys_of, declare, error, i, intertype, isa, len, matcher, probe, probes_and_matchers, size_of, type_of, types_of, validate;
intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
T.eq(isa('callable', 'xxx'), false);
T.eq(isa('callable', (function() {})), true);
T.eq(isa('callable', (function() {}).bind(this)), true);
T.eq(isa('callable', (async function() {
return (await 42);
})), true);
T.eq(isa('callable', (function*() {
return (yield 42);
})), true);
T.eq(isa('callable', (function*() {
return (yield 42);
})()), false);
T.eq(isa('date', new Date()), true);
T.eq(isa('date', Date.now()), false);
T.eq(isa('finite', 123), true);
T.eq(isa('global', global), true);
T.eq(isa('integer', 123), true);
T.eq(isa('integer', 42), true);
T.eq(isa('number', 123), true);
T.eq(isa('number', 42), true);
T.eq(isa('number', 0/0), false);
T.eq(isa('number', 0/0), false);
T.eq(isa('safeinteger', 123), true);
T.eq(isa('text', 'x'), true);
T.eq(isa('text', 0/0), false);
T.eq(isa.even(42), true);
T.eq(isa.finite(123), true);
T.eq(isa.integer(123), true);
T.eq(isa.integer(42), true);
T.eq(isa.multiple_of(42, 2), true);
T.eq(isa.number(123), true);
T.eq(isa.safeinteger(123), true);
T.eq(isa['multiple_of'](42, 2), true);
T.eq(type_of('xxx'), 'text');
T.eq(type_of((function() {})), 'function');
T.eq(type_of((function() {}).bind(this)), 'function');
T.eq(type_of((async function() {
return (await 42);
})), 'asyncfunction');
T.eq(type_of((function*() {
return (yield 42);
})), 'generatorfunction');
T.eq(type_of((function*() {
return (yield 42);
})()), 'generator');
T.eq(type_of(123), 'number');
T.eq(type_of(42), 'number');
T.eq(type_of([]), 'list');
T.eq(type_of(global), 'global');
T.eq(type_of(new Date()), 'date');
T.eq(type_of({}), 'object');
info('µ01-39', xrpr(types_of(123)));
info('µ01-40', xrpr(types_of(124)));
info('µ01-41', xrpr(types_of(0)));
info('µ01-42', xrpr(types_of(true)));
info('µ01-43', xrpr(types_of(null)));
info('µ01-44', xrpr(types_of(void 0)));
info('µ01-45', xrpr(types_of({})));
info('µ01-46', xrpr(types_of([])));
info('µ01-47', xrpr(all_keys_of([null])));
// info 'µ01-37', xrpr type_of Buffer.from [ 1, 2, 3, ]
return done();
//.........................................................................................................
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]];
//.........................................................................................................
for (i = 0, len = probes_and_matchers.length; i < len; i++) {
[probe, matcher, error] = probes_and_matchers[i];
await T.perform(probe, matcher, error, function() {
return new Promise(function(resolve, reject) {
var result;
result = eval(probe);
// log jr [ probe, result, ]
// resolve result
resolve(result);
return null;
});
});
}
done();
return null;
};
//-----------------------------------------------------------------------------------------------------------
this["types_of"] = async function(T, done) {
var all_keys_of, declare, error, i, intersection_of, intertype, isa, len, matcher, probe, probes_and_matchers, size_of, type_of, types_of, validate;
intersection_of = function(a, b) {
var x;
return ((function() {
var i, len, results;
results = [];
for (i = 0, len = a.length; i < len; i++) {
x = a[i];
if (indexOf.call(b, x) >= 0) {
results.push(x);
}
}
return results;
})()).sort();
};
//.........................................................................................................
intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
//.........................................................................................................
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]];
//.........................................................................................................
// debug intersection_of [ 1, 2, 3, ], [ 'a', 3, 1, ]
for (i = 0, len = probes_and_matchers.length; i < len; i++) {
[probe, matcher, error] = probes_and_matchers[i];
await T.perform(probe, matcher, error, function() {
return new Promise(function(resolve, reject) {
var result;
result = intersection_of(matcher, types_of(probe));
// log jr [ probe, result, ]
// resolve result
resolve(result);
return null;
});
});
}
done();
return null;
};
//-----------------------------------------------------------------------------------------------------------
this["size_of"] = async function(T, done) {
var all_keys_of, declare, error, i, intertype, isa, len, matcher, probe, probes_and_matchers, size_of, type_of, types_of, validate;
// debug ( new Buffer '𣁬', ), ( '𣁬'.codePointAt 0 ).toString 16
// debug ( new Buffer '𡉜', ), ( '𡉜'.codePointAt 0 ).toString 16
// debug ( new Buffer '𠑹', ), ( '𠑹'.codePointAt 0 ).toString 16
// debug ( new Buffer '𠅁', ), ( '𠅁'.codePointAt 0 ).toString 16
/* TAINT re-implement types object, pod */
// T.eq ( isa.size_of { '~isa': 'XYZ/yadda', 'foo': 42, 'bar': 108, 'baz': 3, } ), 4
//.........................................................................................................
intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
//.........................................................................................................
probes_and_matchers = [
[[[1,
2,
3,
4]],
4,
null],
[[new Buffer([1,
2,
3,
4])],
4,
null],
[['𣁬𡉜𠑹𠅁'],
2 * (Array.from('𣁬𡉜𠑹𠅁')).length,
null],
[['𣁬𡉜𠑹𠅁',
'codepoints'],
(Array.from('𣁬𡉜𠑹𠅁')).length,
null],
[['𣁬𡉜𠑹𠅁',
'codeunits'],
2 * (Array.from('𣁬𡉜𠑹𠅁')).length,
null],
[['𣁬𡉜𠑹𠅁',
'bytes'],
(new Buffer('𣁬𡉜𠑹𠅁',
'utf-8')).length,
null],
[['abcdefghijklmnopqrstuvwxyz'],
26,
null],
[['abcdefghijklmnopqrstuvwxyz',
'codepoints'],
26,
null],
[['abcdefghijklmnopqrstuvwxyz',
'codeunits'],
26,
null],
[['abcdefghijklmnopqrstuvwxyz',
'bytes'],
26,
null],
[['ä'],
1,
null],
[['ä',
'codepoints'],
1,
null],
[['ä',
'codeunits'],
1,
null],
[['ä',
'bytes'],
2,
null],
[[new Map([['foo',
42],
['bar',
108]])],
2,
null],
[[new Set(['foo',
42,
'bar',
108])],
4,
null],
[
[
{
'foo': 42,
'bar': 108,
'baz': 3
}
],
3,
null
],
[
[
{
'foo': null,
'bar': 108,
'baz': 3
}
],
3,
null
],
[
[
{
'foo': void 0,
'bar': 108,
'baz': 3
}
],
2,
null
]
];
//.........................................................................................................
for (i = 0, len = probes_and_matchers.length; i < len; i++) {
[probe, matcher, error] = probes_and_matchers[i];
// debug 'µ22900', probe
await T.perform(probe, matcher, error, function() {
return new Promise(function(resolve, reject) {
var result;
result = size_of(...probe);
resolve(result);
return null;
});
});
}
done();
return null;
};
later = function() {};
/*
* info size_of 'xxx'
info 'µ01-47', xrpr all_keys_of [ null, ]
X = {}

@@ -309,13 +373,202 @@ X.x = true

info Y.spec.spec_of_Y
*/
*/
//###########################################################################################################
if (module.parent == null) {
// test @
// test @[ "multiple tests" ]
// test @[ "nasty error message, tamed" ]
test(this["_demo 2"]);
test(this);
(function() { // test @[ "validate" ]
// do -> debug ( require '../helpers' ).js_type_of arguments
var all_keys_of, declare, error, i, intertype, isa, len, nr, result, size_of, tests, type_of, types_of, validate;
intertype = new Intertype;
({isa, validate, type_of, types_of, size_of, declare, all_keys_of} = intertype.export_methods());
urge(size_of('𣁬𡉜𠑹𠅁', 'codepoints'));
intertype.declare('point', {
size: 2,
tests: {
'? is an object': function(x) {
return this.isa.object(x);
},
'?.x is set': function(x) {
return this.has_key(x, 'x');
},
'?.y is set': function(x) {
return this.has_key(x, 'y');
},
'?.x is a number': function(x) {
return this.isa.number(x.x);
},
'?.y is a number': function(x) {
return this.isa.number(x.x);
}
}
});
intertype.declare('vector', {
size: 2,
tests: {
'? is a list': function(x) {
return this.isa.list(x);
},
'size of ? is 2': function(x) {
return (this.size_of(x)) === 2;
},
'?[ 0 ] is a number': function(x) {
return this.isa.number(x[0]);
},
'?[ 1 ] is a number': function(x) {
return this.isa.number(x[1]);
}
}
});
info(isa.point(42));
info(isa.point({
x: 42,
y: 108
}));
info(isa.point({
x: 2e308,
y: 108
}));
tests = [
[
1,
(function() {
return validate.number(42);
})
],
[
2,
(function() {
return validate.integer(42);
})
],
[
3,
(function() {
return validate.even(42);
})
],
[
4,
(function() {
return validate.number(42.5);
})
],
[
5,
(function() {
return validate.integer(42.5);
})
],
[
6,
(function() {
return validate.even(42.5);
})
],
[
7,
(function() {
return validate.point(42);
})
],
[
8,
(function() {
return validate.point({
x: 42,
y: 108
});
})
],
[
9,
(function() {
return validate.point({
y: 108
});
})
],
[
10,
(function() {
return validate.point({
x: 2e308,
y: 108
});
})
],
[
11,
(function() {
return validate.vector(null);
})
],
[
12,
(function() {
return validate.vector([2]);
})
],
[
13,
(function() {
return validate.vector([2,
3]);
})
],
[
14,
(function() {
return validate.regex([2,
3]);
})
],
[
15,
(function() {
return validate.regex(/x/);
})
],
[
16,
(function() {
return validate.regex(/^x$/g);
})
],
[
17,
(function() {
return isa.regex(/x/);
})
],
[
18,
(function() {
return isa.regex(/^x$/g);
})
]
];
for (i = 0, len = tests.length; i < len; i++) {
[nr, test] = tests[i];
try {
result = test();
} catch (error1) {
error = error1;
warn(nr, error.message);
// throw error
continue;
}
info(nr, result);
}
help(isa.number(42));
help(isa.number(new Number(42)));
help(types_of(42));
return help(types_of(new Number(42)));
})();
}
// help validate.multiple_of 3, 4
}).call(this);
//# sourceMappingURL=main.test.js.map
{
"name": "intertype",
"version": "1.0.2",
"version": "2.0.0",
"description": "A JavaScript typechecker",

@@ -5,0 +5,0 @@ "main": "lib/main.js",

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

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