-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.js
executable file
·6 lines (6 loc) · 2.79 KB
/
value.js
1
2
3
4
5
6
function Value(c){function u(a){if(a)switch(typeof a){case "object":v(a);break;case "string":a in j.formatters&&v(j.formatters[a]);break;case "function":p=a}}function v(a){k(a.separator)&&(l=a.separator);k(a.symbol)&&(m=a.symbol);k(a.position)&&a.position.length&&(q=a.position);k(a.nill)&&(i=a.nill);typeof a.format_thousands==="boolean"&&(r=a.format_thousands);switch(q){case "before":w=m+l;break;default:x=l+m}p=j.format}function d(a){return!isNaN(parseFloat(a))}function k(a){return typeof a==="string"}
if(!(this instanceof Value))return new Value(c);var b=this,c=c||{},f=d(c.value)&&c.value||0,e=d(c.min)&&c.min||(f>=0?0:f),g=d(c.max)&&c.max||Number.MAX_VALUE,h=d(c.nudge)&&Math.abs(c.nudge)||1,n=d(c.plus_nudge)&&Math.abs(c.plus_nudge)||h,o=d(c.minus_nudge)&&-1*Math.abs(c.minus_nudge)||-1*Math.abs(h),s=typeof c.frozen==="boolean"?c.frozen:!1,l="",m="",q="after",i="",r=!1,w="",x="",t=[],y=0,j={format:function(a){var a=a||f,b;if(r){var c=Math.floor(a);b=a-c;var d=c+"",e=d.length%3||3,c=d.substr(0,e),
d=d.substr(e),d=d.replace(/(\d{3})/g,",$1");b&&(d+=(b+"").substr(1));b=c+d}else b=a+"";b=w+b+x;a===0&&i&&(b=i);return b},unformat:function(a){a+="";return a===i?0:a.replace(/[^0-9\.]/gi,"")},formatters:{quantity:{symbol:"",format_thousands:!0},money:{symbol:"$",position:"before",format_thousands:!0},percent:{symbol:"%"}}},p=j.format,z=j.unformat;u(c.format);c.unformat&&(z=c.unformat);b.format=function(a){return a?(u(a),b):{separator:l,symbol:m,position:q,nill:i,format_thousands:r}};b.setNudge=function(a){return d(a)?
(h=Math.abs(a),n=Math.abs(h),o=-1*Math.abs(h),b):h};b.plusNudge=function(a){return d(a)?(n=Math.abs(a),b):n};b.minusNudge=function(a){return d(a)?(o=-1*Math.abs(a),b):o};b.nudge=function(a,c){b.set(f+a,c);return b};b.plus=function(a,c){var e=d(a)?a:n;b.set(f+e,typeof a==="function"?a:c);return b};b.minus=function(a,c){var e=d(a)?a:o;b.set(f+e,typeof a==="function"?a:c);return b};b.get=function(){return p(f)};b.set=function(a,c){k(a)&&(a=z(a,j.unformat));d(a)&&(a=parseFloat(a));if(!isNaN(a))if(a>=
e&&a<=g){if(a!==f&&(y=a-f,s||(f=a),(c&&c.call(b,b))!==!1))for(var h=0,i=t.length;h<i;h++)t[h].call(b,b)}else a<e&&b.set(e),a>g&&b.set(g);return b};this.value=function(){return f};this.max=function(a){return d(a)?(g=a,f>=e&&f<=g||b.set(g),e>g&&b.min(g),b):g};this.min=function(a){return d(a)?(e=a,f>=e&&f<=g||b.set(e),e>g&&b.max(e),b):e};this.delta=function(){return y};this.freeze=function(){s=!0;return b};this.defrost=function(){s=!1;return b};this.subscribe=function(a){t.push(a);return b};this.derive=
function(){for(var a=Array.prototype.slice.apply(arguments,[0,arguments.length-1]),c=arguments[arguments.length-1],d=function(){b.set(c.apply(b,a))},e=0,f=arguments.length-1;e<f;e++){var g=arguments[e];if(!(g instanceof Value))return!1;g.subscribe(d)}d();return b};this.toString=this.get;this.valueOf=this.value};