added alias handling in Actions(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-08-04 03:58:24 +03:00
parent 0a0ac752b9
commit 0d1a788410
2 changed files with 96 additions and 21 deletions

View File

@ -305,7 +305,30 @@ function(txt){
// parse arguments... // parse arguments...
var args = JSON.parse('['+( var args = JSON.parse('['+(
((c[1] || '') ((c[1] || '')
.match(/"[^"]*"|'[^']*'|\{[^\}]*\}|\[[^\]]*\]|\d+|\d+\.\d*|null/gm) .match(RegExp([
// strings...
'"[^"]*"',
"'[^']*'",
'`[^`]*`',
// objects...
// XXX hack-ish...
'\\{[^\\}]*\\}',
// lists...
// XXX hack-ish...
'\\[[^\]]*\]',
// numbers...
'\\d+\\.\\d+|\\d+',
// identifiers...
// XXX not JSON compatible...
//'[a-zA-Z$@#_][a-zA-Z0-9$@#_]*',
// null...
'null'
].join('|'), 'gm'))
|| []) || [])
.join(','))+']') .join(','))+']')
@ -321,6 +344,21 @@ function(txt){
} }
// XXX make this stricter...
var isStringAction =
module.isStringAction =
function(txt){
try{
var parsed = typeof(txt) == typeof('str')
&& (this.parseStringAction || parseStringAction)(txt)
return parsed
&& /[a-zA-Z_][a-zA-Z0-9_]*/.test(parsed.action)
} catch(e){
return false
}
}
/*********************************************************************/ /*********************************************************************/
@ -664,7 +702,6 @@ function Alias(alias, doc, ldoc, attrs, target){
doc = (!doc && parsed) ? parsed.doc : doc doc = (!doc && parsed) ? parsed.doc : doc
// XXX would be good to pre-parse the target...
var meth = Action(alias, var meth = Action(alias,
doc, doc,
null, null,
@ -737,17 +774,42 @@ module.MetaActions = {
return this.actions return this.actions
.filter(function(n){ return that[n] instanceof Alias }) }, .filter(function(n){ return that[n] instanceof Alias }) },
// XXX move this to the right spot... // XXX move this to the right spot...
parseStringAction: parseStringAction, parseStringAction: parseStringAction,
isStringAction: isStringAction,
// Set/remove action alias...
//
// Set alias...
// .alias(alias, code)
// -> action-set
//
// Remove alias...
// .alias(alias, null)
// -> action-set
//
// code should be compatible with .parseStringAction(..)
//
// NOTE: this does not check if it will override anything, so it is
// possible to override/delete an action/method/attribute with
// this...
//
// XXX should this prevent overriding stuff??? // XXX should this prevent overriding stuff???
// XXX move to a better spot... // XXX move to a better spot...
alias: Action('alias', function(alias, target){ alias: Action('alias', function(alias, target){
var parsed = typeof(target) == typeof('str') ? // remove alias...
this.parseStringAction(target) if((target === false || target === null)
: target && this[alias] instanceof Alias){
this[alias] = Alias(alias, parsed) delete this[alias]
// set alias...
} else {
var parsed = typeof(target) == typeof('str') ?
this.parseStringAction(target)
: target
this[alias] = Alias(alias, parsed)
}
}), }),
@ -1746,6 +1808,15 @@ function Actions(a, b){
var proto = b == null ? b : a var proto = b == null ? b : a
obj = obj || new ActionSet() obj = obj || new ActionSet()
if(proto != null){
obj.__proto__ = proto
// XXX is this the right way to go???
if(obj.config != null && proto.config != null){
obj.config.__proto__ = proto.config
}
}
// NOTE: this is intentionally done only for own attributes... // NOTE: this is intentionally done only for own attributes...
Object.keys(obj).forEach(function(k){ Object.keys(obj).forEach(function(k){
// NOTE: we are not getting the attrs directly (vars = obj[k]) // NOTE: we are not getting the attrs directly (vars = obj[k])
@ -1761,25 +1832,29 @@ function Actions(a, b){
// from node console instanceof tests fail... // from node console instanceof tests fail...
//|| !(arg instanceof Array) //|| !(arg instanceof Array)
|| arg.constructor.name != 'Array' || arg.constructor.name != 'Array'
// and arrays the last element of which is not a function... // skip arrays where the last element of which is not a function...
|| typeof(arg[arg.length-1]) != 'function'){
//|| !(arg[arg.length-1] instanceof Function)){ //|| !(arg[arg.length-1] instanceof Function)){
// XXX EXPERIMENTAL...
// XXX for this to work we need obj to be an
// instance of ActionSet...
// ...need a way to check this yet not require obj
// to strictly be anything special, the problem is
// that the string action syntax is defined in the
// action-set, thus we need to know about it here...
|| !(arg[arg.length-1] instanceof Function
|| (typeof(arg[arg.length-1]) == typeof('str')
// XXX should this be stricter???
&& (obj.isStringAction || isStringAction)(arg[arg.length-1]))) ){
//*/
return return
} }
// create a new action... // create a new action/alias...
var a = obj[k] = new Action(k, arg) var a = obj[k] = arg[arg.length-1] instanceof Function ?
(new Action(k, arg))
: (new Alias(k, arg))
}) })
if(proto != null){
obj.__proto__ = proto
// XXX is this the right way to go???
if(obj.config != null && proto.config != null){
obj.config.__proto__ = proto.config
}
}
return obj return obj
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-actions", "name": "ig-actions",
"version": "3.4.0", "version": "3.5.0",
"description": "", "description": "",
"main": "actions.js", "main": "actions.js",
"scripts": { "scripts": {