playing with patterns...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-08-04 01:55:58 +03:00
parent 9fd9844336
commit b55b5ba70c

62
diff.js
View File

@ -286,37 +286,71 @@ object.makeConstructor('LogicType',
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Singletons... // XXX rename -- this makes a constructor/instance combination...
var makeSingletonPattern = function(name, check){ var makeCIPattern = function(name, check, init){
return new (object.makeConstructor( var o = Object.assign(Object.create(LogicTypePrototype), {
name, __cmp__: check,
Object.assign(new LogicType(), { __cmp__: check })))() } })
init
&& (o.__init__ = init)
return object.makeConstructor(name, o, o)
}
// Singleton...
var ANY = var ANY =
module.ANY = module.ANY =
makeSingletonPattern('ANY', makeCIPattern('ANY',
function(obj, cmp){ return true }) function(obj, cmp){ return true })()
// XXX should support regexp as arg... // STRING
// STRING(string)
// STRING(regexp)
// STRING(func)
// -> pattern
//
var STRING = var STRING =
module.STRING = module.STRING =
makeSingletonPattern('STRING', makeCIPattern('STRING',
function(obj, cmp){ function(obj, cmp){
return obj === STRING || typeof(obj) == typeof('str') }) return obj === STRING
|| (typeof(obj) == typeof('str')
&& (this.value instanceof RegExp ?
this.value.test(obj)
: typeof(this.value) == typeof('str') ?
this.value == obj
: this.value instanceof Function ?
this.value(obj)
// pattern...
: this.value != null ?
cmp(this.value, obj)
: true )) },
function(value){ this.value = value })
// NUMBER
// NUMBER(n)
// NUMBER(min, max)
// NUMBER(func)
// -> pattern
// XXX support range checking, ... // XXX support range checking, ...
var NUMBER = var NUMBER =
module.NUMBER = module.NUMBER =
makeSingletonPattern('NUMBER', makeCIPattern('NUMBER',
function(obj, cmp){ function(obj, cmp){
return obj === NUMBER || typeof(obj) == typeof(123) }) // XXX do the .value test....
return obj === NUMBER || typeof(obj) == typeof(123) },
function(...value){ this.value = value })
// ARRAY
// ARRAY(length)
// -> pattern
// XXX support length, types, ... // XXX support length, types, ...
var ARRAY = var ARRAY =
module.ARRAY = module.ARRAY =
makeSingletonPattern('ARRAY', makeCIPattern('ARRAY',
function(obj, cmp){ function(obj, cmp){
return obj === ARRAY || obj instanceof Array }) // XXX do the .value test....
return obj === ARRAY || obj instanceof Array },
function(...value){ this.value = value })
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -