I already did some tests with it working using an object mapping to values, but it doesn’t work well with nested expressions.
With nested expressions, you can’t really pre-cache objects and it will execute all the possible branches code before doing the resolution leading to performance issue.
With nested expressions you can have functions for some branches and strings for some. :v
Don't know about performance, but it is usually very compact, and you just need one helper:
Not really so compact if you transpile it to ES5.
Here is an example of a complete solution (if I don't miss a thing ?).
The translation :
Updated: {minutes, plural,
easteregg {never}
=0 {just now}
=1 {one minute ago}
other {
{minutes} minutes ago by {gender, select,
male {male}
female {female}
other {other}
}
}
}
The global functions
// Global SELECTfunctiongetBranchData(branch,prop,params,getPluralCategory,onMissingVariable,key,language){vardata=branch.hasOwnProperty(prop)?branch[prop]:branch.other;// fallback to 'others'returntypeofdata==="function"?data(params,getPluralCategory,onMissingVariable,key,language):data;}// Global PLURALfunctiongetBranchPluralData(branch,prop,params,getPluralCategory,onMissingVariable,key,language,){varcategory=getPluralCategory&&getPluralCategory(params[prop]);vardata;if(branch.hasOwnProperty(prop)){// direct assignmentdata=branch[prop];}elseif(category&&branch.hasOwnProperty(category)){// category check (easter egg)data=branch[category];}else{// default to otherdata=branch.other;}returntypeofdata==="function"?data(params,getPluralCategory,onMissingVariable,key,language):data;}// Global InterpolationfunctionhandleInterpolation(params,prop,onMissingVariable,key,language){return!params.hasOwnProperty(prop)?onMissingVariable(prop,key,language):typeofparams[prop]==="number"?params[prop]:params[prop]||"";}
The generated function demo:
functionfunctionGenerator(){// Closure to avoid re-defining branches at each callvarbranchA={0:"just now",1:"one minute ago",other:function(params,getPluralCategory,onMissingVariable,key,language){return(handleInterpolation(params,"minutes",onMissingVariable,key,language)+" minutes ago by "+getBranchData(branchB,params.gender,params,getPluralCategory,onMissingVariable,key,language));}};varbranchB={male:"male",female:"female",other:"other"};returnfunction(params,getPluralCategory,onMissingVariable,key,language){return("Updated: "+getBranchPluralData(branchA,params.minutes,params,getPluralCategory,onMissingVariable,key,language));};}varfn=functionGenerator();fn({minutes:5,gender:"male"});
I'll probably do a branch to see if it's a good candidate.
Just saw your comment about SELECT optimization.
I already did some tests with it working using an object mapping to values, but it doesn’t work well with nested expressions.
With nested expressions, you can’t really pre-cache objects and it will execute all the possible branches code before doing the resolution leading to performance issue.
With nested expressions you can have functions for some branches and strings for some. :v
Don't know about performance, but it is usually very compact, and you just need one helper:
Not really so compact if you transpile it to ES5.
Here is an example of a complete solution (if I don't miss a thing ?).
The translation :
The global functions
The generated function demo:
I'll probably do a branch to see if it's a good candidate.
should probably be an object, passed by reference instead of individually through arguments?