refactoring and cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-04-22 04:32:44 +03:00
parent dd4629c670
commit e4a1139be3
2 changed files with 122 additions and 129 deletions

View File

@ -338,7 +338,6 @@ module.UNDEFINED = ASIS(undefined)
var normalizeTabs = function(str){ var normalizeTabs = function(str){
str = str.split(/\n/g) str = str.split(/\n/g)
// get min number of leading tabs... // get min number of leading tabs...
var i = str.length == 2 && /^\t/.test(str[1]) ? var i = str.length == 2 && /^\t/.test(str[1]) ?
str[1].split(/^(\t+)/)[1].length - 1 str[1].split(/^(\t+)/)[1].length - 1
@ -352,7 +351,6 @@ var normalizeTabs = function(str){
return /^\t+/.test(l) ? return /^\t+/.test(l) ?
l.split(/^(\t+)/)[1].length l.split(/^(\t+)/)[1].length
: 0})) : 0}))
return (str[0] +'\n' return (str[0] +'\n'
+ str + str
.slice(1) .slice(1)
@ -361,8 +359,7 @@ var normalizeTabs = function(str){
// replace tabs... // replace tabs...
.replace(/\t/g, ' ')) .replace(/\t/g, ' '))
// remove leading and trailing whitespace... // remove leading and trailing whitespace...
.trim() .trim() }
}
var doWithRootAction = var doWithRootAction =
@ -374,9 +371,7 @@ function(func){
|| MetaActions.getHandlerList) || MetaActions.getHandlerList)
.apply(this, args) .apply(this, args)
return func.apply(this, [handlers.pop()].concat(args)) return func.apply(this, [handlers.pop()].concat(args)) } }
}
}
// //
@ -417,9 +412,16 @@ function(func){
// NOTE: identifiers are resolved as attributes of the context... // NOTE: identifiers are resolved as attributes of the context...
// XXX this is the same as ImageGrid's keyboard.parseActionCall(..), reuse // XXX this is the same as ImageGrid's keyboard.parseActionCall(..), reuse
// in a logical manner... // in a logical manner...
// placeholders...
var __Atom
var __Argument
var parseStringAction = var parseStringAction =
module.parseStringAction = module.parseStringAction =
function(txt){ Object.assign(
// parser...
function(txt){
// split off the doc... // split off the doc...
var c = txt.split('--') var c = txt.split('--')
var doc = (c[1] || '').trim() var doc = (c[1] || '').trim()
@ -481,29 +483,22 @@ function(txt){
code: txt, code: txt,
} }
} },{
// atoms...
parseStringAction.Identifier = Atom: (__Atom = object.Constructor('Atom', {
object.Constructor(
'Identifier',
{
__init__: function(value){ __init__: function(value){
this.value = value this.value = value },
}, valueOf: function(){
valueOf: function(){ return this.value }, return this.value },
}) })),
parseStringAction.Argument = Identifier: object.Constructor('Identifier',
object.Constructor( Object.create(__Atom.prototype)),
'Argument', Argument: (__Argument = object.Constructor('Argument',
{ Object.create(__Atom.prototype))),
__init__: function(value){ ALLARGS: new __Argument('...'),
this.value = value
},
valueOf: function(){ return this.value },
})
parseStringAction.ALLARGS = new parseStringAction.Argument('...')
parseStringAction.resolveArgs = function(context, action_args, call_args){ // general API...
resolveArgs: function(context, action_args, call_args){
var that = this var that = this
var rest var rest
var args = [].slice.call(action_args) var args = [].slice.call(action_args)
@ -522,12 +517,10 @@ parseStringAction.resolveArgs = function(context, action_args, call_args){
: arg }) : arg })
rest != null rest != null
&& args.splice(rest, 1, ...call_args) && args.splice(rest, 1, ...call_args)
return args },
return args // XXX should this break if action does not exist???
} callAction: function(context, action, ...args){
// XXX should this break if action does not exist???
parseStringAction.callAction = function(context, action, ...args){
action = typeof(action) == typeof('str') ? action = typeof(action) == typeof('str') ?
this(action) this(action)
: action : action
@ -536,25 +529,25 @@ parseStringAction.callAction = function(context, action, ...args){
context[action.action] context[action.action]
.apply(context, this.resolveArgs(context, action.arguments, args)) .apply(context, this.resolveArgs(context, action.arguments, args))
// action not found or is not callable... (XXX) // action not found or is not callable... (XXX)
: undefined : undefined },
} applyAction: function(context, action, args){
parseStringAction.applyAction = function(context, action, args){ return this.callAction(context, action, ...args) },
return this.callAction(context, action, ...args) }
// XXX make this stricter...
// XXX make this stricter... isStringAction: function(txt){
var isStringAction =
module.isStringAction =
function(txt){
try{ try{
var parsed = typeof(txt) == typeof('str') var parsed = typeof(txt) == typeof('str')
&& (this.parseStringAction || parseStringAction)(txt) && (this.parseStringAction || parseStringAction)(txt)
return parsed return parsed
&& /[a-zA-Z_][a-zA-Z0-9_]*/.test(parsed.action) && /[a-zA-Z_][a-zA-Z0-9_]*/.test(parsed.action)
} catch(e){ } catch(e){
return false return false } },
} })
}
// shorthand...
var isStringAction =
module.isStringAction =
parseStringAction.isStringAction

View File

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