From aacf5cbe29539584e33842d4dd13b0b3c40d3176 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 17 Dec 2017 03:58:46 +0300 Subject: [PATCH] added alias argument support... Signed-off-by: Alex A. Naanou --- actions.js | 56 +++++++++++++++++++++++++++++++++++++++++++++------- package.json | 2 +- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/actions.js b/actions.js index 3b12461..497379c 100755 --- a/actions.js +++ b/actions.js @@ -383,13 +383,23 @@ function(txt, context){ // identifiers... '[a-zA-Z$@#_][a-zA-Z0-9$@#_]*', + // rest args... + '\\.\\.\\.', + // null... - 'null' + 'null', ].join('|'), 'gm')) || []) .map(function(e){ - return /^[a-zA-Z$@#_][a-zA-Z0-9$@#_]*$/.test(e) ? - context[e] + // argument placeholder... + return /^\.\.\.$/.test(e) ? + parseStringAction.ALLARGS + : /^\$[a-zA-Z0-9$@#_]*$/.test(e) ? + new StringActionArgument(e.slice(1)) + // context idetifiers... + // NOTE: keep this last as it is the most general... + : /^[a-zA-Z$@#_][a-zA-Z0-9$@#_]*$/.test(e) ? + context[e] : JSON.parse(e) }) return { @@ -403,6 +413,18 @@ function(txt, context){ } } +StringActionArgument = +parseStringAction.StringActionArgument = +object.makeConstructor( + 'StringActionArgument', + { + __init__: function(value){ + this.value = value + }, + valueOf: function(){ return this.value }, + }) +parseStringAction.ALLARGS = new StringActionArgument('...') + // XXX make this stricter... var isStringAction = @@ -813,16 +835,36 @@ function Alias(alias, doc, ldoc, attrs, target){ if(target == ''){ return } + var that = this + var in_args = [].slice.call(arguments) + // parse the target... // XXX should we cache here??? var action = parsed || this.parseStringAction(target) + + // handle args... + var rest var args = action.arguments.slice() + // merge args... + .map(function(arg, i){ + return arg instanceof that.parseStringAction.StringActionArgument ? + (arg === that.parseStringAction.ALLARGS ? + (function(){ + rest = i + return arg + })() + : in_args[parseInt(arg.value)]) + : arg }) + rest != null + && args.splice(rest, 1, ...in_args) - // XXX merge args... - // XXX + if(this[action.action] instanceof Function){ + // call the alias... + return this[action.action].apply(this, args) + } - // call the alias... - return this[action.action].apply(this, args) + // error... + console.error(`${alias}: alias to unknown action: ${action.action}`) } func.toString = function(){ return meth.alias.code || meth.alias } diff --git a/package.json b/package.json index 8d11c3e..1509890 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-actions", - "version": "3.10.0", + "version": "3.11.0", "description": "", "main": "actions.js", "scripts": {