build-function
Version:
The way to describe and build simple functions using JSON
3 lines (2 loc) • 8.65 kB
JavaScript
var r,n;r=this,n=function(r,n){"use strict";function t(r){return function(){return r}}var e=Object.prototype.hasOwnProperty,u="0$_";function i(r,n){var t={parent:r};if(n)for(var u in n)e.call(n,u)&&f(t,u,n[u]);return t}function o(r,n,t){for(var i=u+n,o=r;o;){if(e.call(o,i))return{env:o,id:i};if(t)return;o=o.parent}}function f(r,n,t){r[u+n]=t}function a(r){return new Error(r)}function c(r){return function(){for(var n=arguments.length,t=new Array(n),e=0;e<n;e++)t[e]=arguments[e];return a(r.replace(/\$(\d+)/g,(function(r,n){return t[n]})))}}var l,v=c('"$0" is not a valid $1'),p=c('"$0" is not a valid $1 type'),s=c('"$0" can\'t be found in this environment'),h=c('"$0" is required in a "$1" expression'),d=c('"$0" is required in a "$1" statement');n&&(l=function(r){for(var t=arguments.length,e=new Array(t>1?t-1:0),u=1;u<t;u++)e[u-1]=arguments[u];return e.reduce((function(r,t){return r+n.MD5(t)}),n.sha1(r))});var w=Array.isArray;function y(r){return"object"==typeof r}var g={rest:function(r){return function(n){for(var t=[],e=n.length,u=r;u<e;u++)t.push(n[u]);return t}},param:function(r){return function(n){return n[r]}}},m={"||":function(r){var n=r.length;return function(t){for(var e,u=0;u<n&&!(e=r[u](t));u++);return e}},"&&":function(r){var n=r.length;return function(t){for(var e,u=0;u<n&&(e=r[u](t));u++);return e}},"**":function(r){var n=r.pop();return function(t){for(var e=n(t),u=r.length-1;u>=0;)e=Math.pow(r[u](t),e),u--;return e}}},b={"+":function(r,n){return r+n},"-":function(r,n){return r-n},"*":function(r,n){return r*n},"/":function(r,n){return r/n},"%":function(r,n){return r%n},"&":function(r,n){return r&n},"|":function(r,n){return r|n},"^":function(r,n){return r^n},"<<":function(r,n){return r<<n},">>":function(r,n){return r>>n},">>>":function(r,n){return r>>>n},"==":function(r,n){return r==n},"===":function(r,n){return r===n},"!=":function(r,n){return r!=n},"!==":function(r,n){return r!==n},">":function(r,n){return r>n},"<":function(r,n){return r<n},">=":function(r,n){return r>=n},"<=":function(r,n){return r<=n}},x={"!":function(r){return!r},"!!":function(r){return!!r},"~":function(r){return~r}},$={literal:function(r){if(!e.call(r,"value"))throw h("value","literal");var n=r.value,u=typeof n;if("string"===u||"number"===u||"boolean"===u)return t(n);var i=JSON.stringify(n);return function(){return JSON.parse(i)}},get:function(r,n,t){if(!e.call(r,"id"))throw h("id","get");if("string"!=typeof r.id)throw a('A "get" expression "id" must be a string');var u=r.id;return function(r){var n=o(r,u);if(n)return n.env[n.id];if(!t)throw s(u)}},set:function(r,n){if(!e.call(r,"id"))throw h("id","set");if(!e.call(r,"value"))throw h("value","set");if("string"!=typeof r.id)throw a('A "set" expression "id" must be a string');var t=r.id,u=O(r.value,n);return function(r){var n=o(r,t);if(!n)throw s(t);return n.env[n.id]=u(r)}},ternary:function(r,n){if(!e.call(r,"condition"))throw h("condition","ternary");if(!e.call(r,"then"))throw h("then","ternary");if(!e.call(r,"otherwise"))throw h("otherwise","ternary");var t=O(r.condition,n),u=O(r.then,n),i=O(r.otherwise,n);return function(r){return t(r)?u(r):i(r)}},oper:function(r,n){if(!e.call(r,"oper"))throw h("oper","oper");if(!e.call(r,"exp"))throw h("exp","oper");var t=r.exp,u=r.oper;if(t.length<2)throw a("not enought operands");var i=O(t,n),o=m[u];if(o)return o(i);var f=b[u];if(!f)throw p(u,"operation");var c=i.shift(),l=i.length;return function(r){for(var n=c(r),t=0;t<l;t++)n=f(n,i[t](r));return n}},trans:function(r,n){if(!e.call(r,"oper"))throw h("oper","trans");if(!e.call(r,"exp"))throw h("exp","trans");if("typeof"===r.oper){var t=O(r.exp,n,!0);return function(r){return typeof t(r)}}var u=x[r.oper];if(!u)throw p(r.oper,"transform operation");var i=O(r.exp,n);return function(r){return u(i(r))}},func:function(r,n){return j(r,n)},call:function(r,n){if(!e.call(r,"func"))throw h("func","call");var t=r.args,u=O(r.func,n),i=t&&function(r,n){function t(r){if("spread"===r.type){var t=O(r.exp,n);return function(r,n){return n.push.apply(n,t(r)),n}}var e=O(r,n);return function(r,n){return n.push(e(r)),n}}function e(){return function(r,n){for(var t=0;t<o;t++)f[t](r,n);return n}}var u=n.spread||(n.spread={});function i(r){if(!l)return t(r);var n=l(r,r.type),e=u[n];return e||(u[n]=t(r))}if(!w(r))return i(r);var o=r.length;if(!o)return null;var f=r.map(i);if(1===o)return f[0];if(!l)return e();var a=l(r,r.length),c=u[a];return c||(u[a]=e())}(t,n);return function(r){var n=u(r);return i?n.apply(null,i(r,[])):n()}}},A={declare:function(r,n){if(!e.call(r,"set"))throw d("set","declare");var u=E(r.set,n);return u?function(r){u(r)}:t()},let:function(r,n){if(!e.call(r,"declare"))throw d("declare","let");var u=E(r.declare,n);return u?function(r){u(r)}:t()},if:function(r,n,u){if(!e.call(r,"condition"))throw d("condition","if");var o=r.then,f=r.otherwise,a=O(r.condition,n),c=o&&k(o,n,u),l=f&&k(f,n,u);return c||l?function(r){var n=a(r)?c:l;if(n)return n(i(r))}:t()},for:function(r,n){if(!e.call(r,"target"))throw d("target","for");var u=r.body,o=u&&k(u,n,!0);if(!o)return t();var f=r.index,a=r.value,c=O(r.target,n);return function(r){for(var n=c(r),t=n.length,e=0;e<t;){var u={};f&&(u[f]=e),a&&(u[a]=n[e]);var l=o(i(r,u));if(l){if("break"===l)return;return l}e++}}},break:function(r,n,e){if(!e)throw a('"break" is not allowed outside loops');return t(r.type)},return:function(r,n){if(!e.call(r,"value"))throw d("value","return");var t=r.value,u=r.type,i=O(t,n);return function(r){return{type:u,value:i(r)}}},try:function(r,n){var e=r.body,u=r.error,o=r.catch,f=e&&k(e,n),a=f&&o&&k(o,n);return f?function(r){try{var n=f(i(r));if(n&&"throw"===n.type)throw n.msg;return n}catch(n){if(a){var t={};return u&&(t[u]=""+(n.message||n)),a(i(r,t))}}}:t()},throw:function(r,n){if(!e.call(r,"msg"))throw d("msg","throw");var t=r.type,u=r.msg,i=y(u)&&O(u,n);return function(r){return{type:t,msg:i?i(r):""+u}}}};function j(r,n,e){var u=r.params,o=r.body,f=u&&function(r,n){function t(r){return y(r)?r:{id:r,type:"param"}}function e(r,n){var t=r.type,e=r.id,u=g[t];if(!u)throw p(t,"parameter");var i=u(n);return function(r,n){return n[e]=i(r),n}}function u(){return function(r,n){for(var t=0;t<f;t++)s[t](r,n);return n}}var i=n.param||(n.param={});function o(r,n){var t=r.id;if("string"!=typeof t)throw v(t,"parameter id");if("arguments"===t)throw a('"arguments" can\'t be used as parameter id');if(!l)return e(r,n);var u=l(r,r.type,n),o=i[u];return o||(i[u]=e(r,n))}if(!w(r))return o(t(r),0);var f=r.length;if(!f)return null;var c=r.map(t),s=c.map(o);if(1===f)return s[0];if(!l)return u();var h=l(c,c.length),d=i[h];return d||(i[h]=u())}(u,n),c=o&&k(o,n);return function(r){if(!c)return t();var n=function(){for(var r={},n=arguments.length,t=new Array(n),e=0;e<n;e++)t[e]=arguments[e];f&&(r=f(t,r)),r.arguments=t;var o=c(i(u,r));if(o){if("throw"===o.type)throw o.msg;return o.value}},u=r;if(e){var o={};o[e]=n,u=i(u,o)}return n}}function E(r,n){function t(r){var n=y(r)?{id:r.id,value:r.value}:{id:r};return e.call(n,"value")&&void 0===n.value&&delete n.value,n}function u(r){var t,e=r.id,u=r.value;return u&&(t=O(u,n)),function(r){if(o(r,e,!0))throw a('"'+e+'" has already been declared in this environment');f(r,e,t&&t(r))}}function i(){return function(r){for(var n=0;n<p;n++)h[n](r)}}var c=n.let||(n.let={});function v(r){if(!l)return u(r);var n=l(r,r.id),t=c[n];return t||(c[n]=u(r))}if(!w(r))return v(t(r));var p=r.length;if(!p)return null;var s=r.map(t),h=s.map(v);if(1===p)return h[0];if(!l)return i();var d=l(s,s.length),g=c[d];return g||(c[d]=i())}function O(r,n,t){function e(r){var e=r.type,u=$[e];if(!u)throw p(e,"expression");return u(r,n,t)}var u=n.exp||(n.exp={});function i(r){if(!r||!y(r))throw v(r,"expression");if(!l)return e(r);var n=l(r,r.type),t=u[n];return t||(u[n]=e(r))}return w(r)?r.map(i):i(r)}function k(r,n,e){function u(r){var t=A[r.type];if(t)return t(r,n,e);var u=O(r,n);return function(r){u(r)}}function i(){return function(r){for(var n=0;n<a;n++){var t=(0,c[n])(r);if(t)return t}}}var o=n.step||(n.step={});function f(r){if(!r||!y(r))throw v(r,"step");if(!l)return u(r);var n=l(r,r.type),t=o[n];return t||(o[n]=u(r))}if(!w(r))return f(r);var a=r.length;if(!a)return t();var c=r.map(f);if(1===a)return c[0];if(!l)return i();var p=l(r,r.length),s=o[p];return s||(o[p]=i())}r.build=function(r,n){return j(r,{},r.name)(n||i(null))},r.compileExp=O,r.compileStep=k,r.createEnv=i,r.findInEnv=o,r.setInEnv=f},"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("object-hash")):"function"==typeof define&&define.amd?define(["exports","object-hash"],n):n((r=r||self).buildFunc={},r.objectHash);
//# sourceMappingURL=build.umd.min.js.map