paypal-invoice
Version:
PayPal Invoicing for Node.js
1 lines • 20.8 kB
JavaScript
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.Invoice=a()}}(function(){var a;return function b(a,c,d){function e(g,h){if(!c[g]){if(!a[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};a[g][0].call(j.exports,function(b){var c=a[g][1][b];return e(c?c:b)},j,j.exports,b,a,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){b.exports=a("./lib/invoice")},{"./lib/invoice":4}],2:[function(a,b){"use strict";var c=function(a){var b=this;a=a||{},"firstName lastName businessName phoneNumber faxNumber website customValue".split(" ").forEach(function(c){a[c]&&(b[c]=a[c])}),a.address&&(b.address={},"country line1 line2 city state postalCode".split(" ").forEach(function(c){b.address[c]=a.address[c]}))};b.exports=c},{}],3:[function(a,b){"use strict";var c=a("bignumber.js"),d={USD:{symbol:"$",decimals:2,round:c.ROUND_HALF_UP,iso4217:840},GBP:{symbol:"£",decimals:2,round:c.ROUND_HALF_UP,iso4217:826},AUD:{symbol:"$",decimals:2,round:c.ROUND_HALF_UP,iso4217:36}};b.exports={round:function(a,b){return new c(b.round(d[a].decimals,d[a].round))},isSupported:function(a){return d.hasOwnProperty(a)},Number:c,newNumber:function(a){return new c(a)},toCents:function(a,b){var e=d[a].decimals;return new c(10).pow(e).times(b)},properties:d}},{"bignumber.js":6}],4:[function(a,b){"use strict";function c(a,b){return a===b?0:b>a?-1:1}var d=a("./currency"),e=d.newNumber,f=a("./contactInfo"),g=a("./totals"),h=e(0),i=(e(1),function(a){a&&"string"!=typeof a?this.initWithDetails(a):this.initWithCurrency(a)});i.STATUS={CANCELLED:"cancelled",CANCELED:"cancelled",DRAFT:"draft",PAYMENT_PENDING:"payment_pending"},i.Number=d.newNumber,i.Currencies=d.properties,i.Item=function(a,b,c,d){if(!a)throw new Error("You must specify a quantity when adding an item.");this.quantity=e(a),this.unitPrice=e(b||0),this.itemId=c,this.detailId=d||null},i.Item.prototype={totalForInvoice:function(a){var b=this.quantity.times(this.unitPrice||h);if(this.discountPercentage){var c=d.round(a.currencyCode,b.times(this.discountPercentage));b=b.minus(c)}else this.discountAmount&&(b=b.minus(this.discountAmount));return d.round(a.currencyCode,b)}},i.prototype={initWithCurrency:function(a){if(this.currencyCode=a||"USD",!d.isSupported(this.currencyCode))throw new Error("Unsupported currency: "+this.currencyCode);this.payerEmail="noreply@here.paypal.com",this.paymentTerms="DueOnReceipt",this.taxInclusive=!1,this.taxCalculatedAfterDiscount=!0,this.merchantInfo=new f,this.billingInfo=new f,this.shippingInfo=new f,this.items=[],this.status=i.STATUS.DRAFT},initWithDetails:function(a){if(this.currencyCode=a.currencyCode,!d.isSupported(this.currencyCode))throw new Error("Unsupported currency: "+this.currencyCode);this.taxInclusive=a.taxInclusive?!0:!1,this.taxCalculatedAfterDiscount=a.taxCalculatedAfterDiscount?!0:!1;var b=this;"status payerEmail number invoiceId paymentTerms referrerCode receiptDetails merchantMemo shippingAmount shippingTaxName customAmountName customAmountValue discountAmount discountPercentage gratuityAmount".split(" ").forEach(function(c){a[c]&&(b[c]=a[c])}),a.shippingTaxRate&&(this.shippingTaxRate=e(e(a.shippingTaxRate).dividedBy(100))),this.readMetadata(a),this.readItems(a.items)},readMetadata:function(a){this.merchantInfo=new f(a.merchantInfo),this.billingInfo=new f,this.shippingInfo=new f},readItems:function(a){this.items=[];var b=this.items;a&&a.forEach(function(a){var c=new i.Item(a.quantity,a.unitPrice,a.itemId||"ItemId"+(new Date).getTime());c.name=a.name,c.description=a.description,a.taxRate&&(c.taxName=a.taxName,c.taxRate=e(a.taxRate)),c.imageUrl=a.imageUrl,a.discountAmount&&(c.discountAmount=a.discountAmount),a.discountPercentage&&(c.discountPercentage=a.discountPercentage),b.push(c)})},calculate:function(){return new g(this)},findItem:function(a,b){"string"!=typeof a&&(b=a.detailId,a=a.itemId);for(var c=0,d=this.items.length;d>c;c++){var e=this.items[c];if(e.itemId===a&&e.detailId===b)return e}return null},addItem:function(a){var b=this.findItem(a);return b?(b.quantity=b.quantity.plus(a.quantity),b):(!a.taxRate||a.taxRate instanceof d.Number||(a.taxRate=e(a.taxRate)),this.items.push(a),a)},_sortedInvoiceItems:function(){var a=this.items.slice(0);return a.sort(function(a,b){return a.taxRate?b.taxRate?b.taxRate.equals(a.taxRate)?c(a.name,b.name):-1*a.taxRate.comparedTo(b.taxRate):-1:b.taxRate?1:c(a.name,b.name)}),a}},b.exports=i},{"./contactInfo":2,"./currency":3,"./totals":5}],5:[function(a,b){"use strict";function c(a){return a&&!g.equals(a)}function d(a){return a||g}var e=a("./currency"),f=e.newNumber,g=f(0),h=f(1),i=function(a){this.invoice=a,this.shippingTax=g,this.itemSubTotal=g,this.itemTax=g,this.discountTotal=g,this.refundTotal=g;var b=a._sortedInvoiceItems();this.itemSubTotal=this.calculateItemSubtotals(b),a.taxCalculatedAfterDiscount||a.taxInclusive?(this.discountTotal=this.calculatePreTaxDiscountForItemSubtotal(this.itemSubTotal),this.taxes=a.taxCalculatedAfterDiscount?this.calculateTaxesForItemsWithPreTaxDiscountTotal(b,this.discountTotal,this.itemSubTotal):this.calculateTaxesForItemsWithDiscountAfterTax(b),this.shippingTax=this.calculateShippingTax(this.taxes),this.itemTax=this.calculateTotalTaxFromTaxes(this.taxes),this.itemTax=f(this.itemTax.minus(this.shippingTax)),this.taxes=this.generateRoundedTaxDetailsFromTaxes(this.taxes)):(this.taxes=this.calculateTaxesForItemsWithDiscountAfterTax(b),this.shippingTax=this.calculateShippingTax(this.taxes),this.itemTax=this.calculateTotalTaxFromTaxes(this.taxes),this.itemTax=f(this.itemTax.minus(this.shippingTax)),this.taxes=this.generateRoundedTaxDetailsFromTaxes(this.taxes),this.discountTotal=this.calculateDiscountWithItemSubTotal(this.itemSubTotal,this.itemTax)),this.setDerivedTotals()};i.prototype={_round:function(a){return e.round(this.invoice.currencyCode,a)},toCents:function(a){return e.toCents(this.invoice.currencyCode,a)},generateRoundedTaxDetailsFromTaxes:function(a){var b={};for(var c in a)b[c]=this._round(a[c]);return b},calculateTotalTaxFromTaxes:function(a){var b=g;for(var c in a)b=b.plus(a[c]);return b},calculateTaxesForItemsWithPreTaxDiscountTotal:function(a,b,d){var e={},f=this;return a.forEach(function(a){var i=f.calculateItemDiscount(a,b,d),j=g;if(c(a.unitPrice)&&(j=f.calculateItemPrice(a)),c(a.taxRate)){var k,l=[a.taxName," (",a.taxRate.toString(),"%)"].join("");if(f.invoice.taxInclusive){var m=j.minus(i);k=f._round(m.dividedBy(h.plus(a.taxRate.dividedBy(100))))}else k=f._round(j.minus(i).times(a.taxRate.dividedBy(100)));k&&!g.equals(k)&&(e[l]=(e[l]||g).plus(k))}}),e},calculateItemDiscount:function(a,b,d){if(c(b)){var e=this.calculateItemPrice(a);if(c(d))return this.discountPercentage&&!g.equals(this.discountPercentage)?e.times(this.discountPercentage):e.dividedBy(d).times(b)}return g},calculateTaxesForItemsWithDiscountAfterTax:function(a){var b={},d=this;return a.forEach(function(a){var f=g;if(c(a.unitPrice)&&(f=e.round(d.invoice.currencyCode,a.quantity.times(a.unitPrice))),c(a.taxRate)){var i,j=[a.taxName," (",a.taxRate.toString(),"%)"].join("");if(d.invoice.taxInclusive){var k=f.dividedBy(h.plus(a.taxRate.dividedBy(100)));i=d._round(f.minus(k))}else i=d._round(f.times(a.taxRate.dividedBy(100)));i&&!g.equals(i)&&(b[j]=(b[j]||g).plus(i))}}),b},calculateShippingTax:function(a){if(this.shippingTaxRate&&this.shippingAmount&&!g.equals(this.shippingTaxRate)&&!g.equals(this.shippingAmount)){var b=[this.shippingTaxName," (",this.shippingTaxRate.toString(),"%)"].join(""),d=this.shippingAmount.times(this.shippingTaxRate.dividedBy(100));return c(d)&&(a[b]=(a[b]||g).plus(d)),d}return g},calculateItemSubtotals:function(a){for(var b=g,c=0,d=a.length;d>c;c++)b=b.plus(this.calculateItemPrice(a[c]));return f(b)},calculateDiscountWithItemSubTotal:function(a,b){return c(this.invoice.discountAmount)?f(this.discountAmount):c(this.invoice.discountPercentage)?e.round(this.currencyCode,a.plus(b).times(this.discountPercentage)):g},calculatePreTaxDiscountForItemSubtotal:function(a){return c(this.invoice.discountAmount)?f(this.invoice.discountAmount):c(this.invoice.discountPercentage)?e.round(this.currencyCode,f(this.invoice.discountPercentage).times(a)):g},calculateItemPrice:function(a){var b=a.quantity.times(a.unitPrice||g);if(a.discountPercentage){var c=e.round(this.invoice.currencyCode,b.times(a.discountPercentage));b=b.minus(c)}else a.discountAmount&&(b=b.minus(a.discountAmount));return this._round(b)},setDerivedTotals:function(){this.total=d(this.itemSubTotal).plus(d(this.invoice.gratuityAmount)).plus(d(this.invoice.shippingAmount)),this.invoice.taxInclusive||(this.total=this.total.plus(d(this.itemTax)).plus(d(this.shippingTax))),c(this.discountTotal)&&(this.total=this.total.minus(this.discountTotal)),this.total=f(this.total)},toJSON:function(){var a={};for(var b in this)this.hasOwnProperty(b)&&"invoice"!==b&&(a[b]=this[b]);return a}},b.exports=i},{"./currency":3}],6:[function(b,c){!function(b){"use strict";function d(a,b){var c,g,h,i,l,m,p=this;if(!(p instanceof d))return new d(a,b);if(a instanceof d){if(x=0,b===c)return p.s=a.s,p.e=a.e,void(p.c=(a=a.c)?a.slice():a);a+=""}if("string"!=typeof a&&(a=(h="number"==typeof a||"[object Number]"==Object.prototype.toString.call(a))&&0===a&&0>1/a?"-0":a+""),m=a,b===c&&y.test(a))p.s="-"==a.charAt(0)?(a=a.slice(1),-1):1;else{if(10==b)return j(a,n,o);if(a=z.call(a).replace(/^\+(?!-)/,""),p.s="-"==a.charAt(0)?(a=a.replace(/^-(?!-)/,""),-1):1,null!=b?b!=(0|b)&&t||(k=!(b>=2&&65>b))?(e(b,2),l=y.test(a)):(i="["+w.slice(0,b=0|b)+"]+",a=a.replace(/\.$/,"").replace(/^\./,"0."),(l=new RegExp("^"+i+"(?:\\."+i+")?$",37>b?"i":"").test(a))?(h&&(a.replace(/^0\.0*|\./,"").length>15&&e(m,0),h=!h),a=f(a,10,b,p.s)):"Infinity"!=a&&"NaN"!=a&&(e(m,1,b),a="NaN")):l=y.test(a),!l)return p.c=p.e=null,"Infinity"!=a&&("NaN"!=a&&e(m,3),p.s=null),void(x=0)}for((c=a.indexOf("."))>-1&&(a=a.replace(".","")),(g=a.search(/e/i))>0?(0>c&&(c=g),c+=+a.slice(g+1),a=a.substring(0,g)):0>c&&(c=a.length),g=0;"0"==a.charAt(g);g++);if(b=a.length,h&&b>15&&a.slice(g).length>15&&e(m,0),x=0,(c-=g+1)>s)p.c=p.e=null;else if(g==b||r>c)p.c=[p.e=0];else{for(;"0"==a.charAt(--b););for(p.e=c,p.c=[],c=0;b>=g;p.c[c++]=+a.charAt(g++));}}function e(a,b,c,d,e,f){if(t){var g,h=["new BigNumber","cmp","div","eq","gt","gte","lt","lte","minus","mod","plus","times","toFr"][x?0>x?-x:x:0>1/x?1:0]+"()",i=k?" out of range":" not a"+(e?" non-zero":"n")+" integer";throw i=([h+" number type has more than 15 significant digits",h+" not a base "+c+" number",h+" base"+i,h+" not a number"][b]||c+"() "+b+(f?" not a boolean or binary digit":i+(d?" or not ["+(k?" negative, positive":" integer, integer")+" ]":"")))+": "+a,k=x=0,g=new Error(i),g.name="BigNumber Error",g}}function f(a,b,c,e){function f(a,d){var e,f,g=0,h=a.length,i=[0];for(d=d||c;h>g;g++){for(f=i.length,e=0;f>e;i[e]*=d,e++);for(i[0]+=w.indexOf(a.charAt(g)),e=0;e<i.length;e++)i[e]>b-1&&(null==i[e+1]&&(i[e+1]=0),i[e+1]+=i[e]/b^0,i[e]%=b)}return i.reverse()}function h(a){for(var b=0,c=a.length,d="";c>b;d+=w.charAt(a[b++]));return d}var i,j,k,l,m,n;if(37>c&&(a=a.toLowerCase()),(i=a.indexOf("."))>-1)if(i=a.length-i-1,j=f(new d(c).pow(i).toF(),10),l=a.split("."),k=f(l[1]),l=f(l[0]),n=g(k,j,k.length-j.length,e,b,1&l[l.length-1]),m=n.c,i=n.e){for(;++i;m.unshift(0));a=h(l)+"."+h(m)}else m[0]?l[i=l.length-1]<b-1?(++l[i],a=h(l)):a=new d(h(l),b).plus(A).toS(b):a=h(l);else a=h(f(a));return a}function g(a,b,c,e,f,g){var h,j,k,l,m,o=b.slice(),p=h=b.length,q=a.length,t=a.slice(0,h),u=t.length,v=new d(A),w=v.c=[],x=0,y=n+(v.e=c)+1;for(v.s=e,e=0>y?0:y;u++<h;t.push(0));o.unshift(0);do{for(k=0;f>k;k++){if(h!=(u=t.length))l=h>u?1:-1;else for(m=-1,l=0;++m<h;)if(b[m]!=t[m]){l=b[m]>t[m]?1:-1;break}if(!(0>l))break;for(j=u==h?b:o;u;){if(t[--u]<j[u]){for(m=u;m&&!t[--m];t[m]=f-1);--t[m],t[u]+=f}t[u]-=j[u]}for(;!t[0];t.shift());}w[x++]=l?k:++k,t[0]&&l?t[u]=a[p]||0:t=[a[p]]}while((p++<q||null!=t[0])&&e--);return w[0]||1==x||(--v.e,w.shift()),x>y&&i(v,n,f,g,null!=t[0]),v.e>s?v.c=v.e=null:v.e<r&&(v.c=[v.e=0]),v}function h(a,b,c){var e=b-(a=new d(a)).e,f=a.c;if(!f)return a.toS();for(f.length>++b&&i(a,e,10),e=0==f[0]?e+1:c?b:a.e+e+1;f.length<e;f.push(0));return e=a.e,1==c||2==c&&(--b<e||p>=e)?(a.s<0&&f[0]?"-":"")+(f.length>1?(f.splice(1,0,"."),f.join("")):f[0])+(0>e?"e":"e+")+e:a.toS()}function i(a,b,c,d,e){var f=a.c,g=a.s<0,h=c/2,i=a.e+b+1,j=f[i],k=e||0>i||null!=f[i+1];if(e=4>o?(null!=j||k)&&(0==o||2==o&&!g||3==o&&g):j>h||j==h&&(4==o||k||6==o&&(1&f[i-1]||!b&&d)||7==o&&!g||8==o&&g),1>i||!f[0])return f.length=0,f.push(0),e?(f[0]=1,a.e=-b):a.e=0,a;if(f.length=i--,e)for(--c;++f[i]>c;)f[i]=0,i--||(++a.e,f.unshift(1));for(i=f.length;!f[--i];f.pop());return a}function j(a,b,c){var e=o;return o=c,a=new d(a),a.c&&i(a,b,10),o=e,a}var k,l=1e9,m=1e6,n=20,o=4,p=-7,q=21,r=-l,s=l,t=!0,u=parseInt,v=d.prototype,w="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_",x=0,y=/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,z=String.prototype.trim||function(){return this.replace(/^\s+|\s+$/g,"")},A=d(1);d.ROUND_UP=0,d.ROUND_DOWN=1,d.ROUND_CEIL=2,d.ROUND_FLOOR=3,d.ROUND_HALF_UP=4,d.ROUND_HALF_DOWN=5,d.ROUND_HALF_EVEN=6,d.ROUND_HALF_CEIL=7,d.ROUND_HALF_FLOOR=8,d.config=function(){var a,b,c=0,d={},f=arguments,g=f[0],h="config",i=function(a,b,c){return!((k=b>a||a>c)||u(a)!=a&&0!==a)},j=g&&"object"==typeof g?function(){return g.hasOwnProperty(b)?null!=(a=g[b]):void 0}:function(){return f.length>c?null!=(a=f[c++]):void 0};return j(b="DECIMAL_PLACES")&&(i(a,0,l)?n=0|a:e(a,b,h)),d[b]=n,j(b="ROUNDING_MODE")&&(i(a,0,8)?o=0|a:e(a,b,h)),d[b]=o,j(b="EXPONENTIAL_AT")&&(i(a,-l,l)?p=-(q=~~(0>a?-a:+a)):!k&&a&&i(a[0],-l,0)&&i(a[1],0,l)?(p=~~a[0],q=~~a[1]):e(a,b,h,1)),d[b]=[p,q],j(b="RANGE")&&(i(a,-l,l)&&~~a?r=-(s=~~(0>a?-a:+a)):!k&&a&&i(a[0],-l,-1)&&i(a[1],1,l)?(r=~~a[0],s=~~a[1]):e(a,b,h,1,1)),d[b]=[r,s],j(b="ERRORS")&&(a===!!a||1===a||0===a?(k=x=0,u=(t=!!a)?parseInt:parseFloat):e(a,b,h,0,0,1)),d[b]=t,d},v.abs=v.absoluteValue=function(){var a=new d(this);return a.s<0&&(a.s=1),a},v.ceil=function(){return j(this,0,2)},v.comparedTo=v.cmp=function(a,b){var c,e=this,f=e.c,g=(x=-x,a=new d(a,b)).c,h=e.s,i=a.s,j=e.e,k=a.e;if(!h||!i)return null;if(c=f&&!f[0],b=g&&!g[0],c||b)return c?b?0:-i:h;if(h!=i)return h;if(c=0>h,b=j==k,!f||!g)return b?0:!f^c?1:-1;if(!b)return j>k^c?1:-1;for(h=-1,i=(j=f.length)<(k=g.length)?j:k;++h<i;)if(f[h]!=g[h])return f[h]>g[h]^c?1:-1;return j==k?0:j>k^c?1:-1},v.decimalPlaces=v.dp=function(){var a;return this.c?(a=this.c.length-this.e-1,0>a?0:a):null},v.dividedBy=v.div=function(a,b){var c=this.c,e=this.e,f=this.s,h=(x=2,a=new d(a,b)).c,i=a.e,j=a.s,k=f==j?1:-1;return(e||c&&c[0])&&(i||h&&h[0])?g(c,h,e-i,k,10):new d(f&&j&&(c?!h||c[0]!=h[0]:h)?c&&0==c[0]||!h?0*k:k/0:0/0)},v.equals=v.eq=function(a,b){return x=3,0===this.cmp(a,b)},v.floor=function(){return j(this,0,3)},v.greaterThan=v.gt=function(a,b){return x=4,this.cmp(a,b)>0},v.greaterThanOrEqualTo=v.gte=function(a,b){return x=5,1==(b=this.cmp(a,b))||0===b},v.isFinite=v.isF=function(){return!!this.c},v.isNaN=function(){return!this.s},v.isNegative=v.isNeg=function(){return this.s<0},v.isZero=v.isZ=function(){return!!this.c&&0==this.c[0]},v.lessThan=v.lt=function(a,b){return x=6,this.cmp(a,b)<0},v.lessThanOrEqualTo=v.lte=function(a,b){return x=7,-1==(b=this.cmp(a,b))||0===b},v.minus=function(a,b){var c,e,f,g,h=this,i=h.s;if(b=(x=8,a=new d(a,b)).s,!i||!b)return new d(0/0);if(i!=b)return a.s=-b,h.plus(a);var j=h.c,k=h.e,l=a.c,m=a.e;if(!k||!m){if(!j||!l)return j?(a.s=-b,a):new d(l?h:0/0);if(!j[0]||!l[0])return l[0]?(a.s=-b,a):new d(j[0]?h:3==o?-0:0)}if(j=j.slice(),i=k-m){for(c=(g=0>i)?(i=-i,j):(m=k,l),c.reverse(),b=i;b--;c.push(0));c.reverse()}else for(f=((g=j.length<l.length)?j:l).length,i=b=0;f>b;b++)if(j[b]!=l[b]){g=j[b]<l[b];break}if(g&&(c=j,j=l,l=c,a.s=-a.s),(b=-((f=j.length)-l.length))>0)for(;b--;j[f++]=0);for(b=l.length;b>i;){if(j[--b]<l[b]){for(e=b;e&&!j[--e];j[e]=9);--j[e],j[b]+=10}j[b]-=l[b]}for(;0==j[--f];j.pop());for(;0==j[0];j.shift(),--m);return(r>m||!j[0])&&(j[0]||(a.s=3==o?-1:1),j=[m=0]),a.c=j,a.e=m,a},v.modulo=v.mod=function(a,b){var c=this,e=c.c,f=(x=9,a=new d(a,b)).c,g=c.s,h=a.s;return b=!g||!h||f&&!f[0],b||e&&!e[0]?new d(b?0/0:c):(c.s=a.s=1,b=1==a.cmp(c),c.s=g,a.s=h,b?new d(c):(g=n,h=o,n=0,o=1,c=c.div(a),n=g,o=h,this.minus(c.times(a))))},v.negated=v.neg=function(){var a=new d(this);return a.s=-a.s||null,a},v.plus=function(a,b){var c,e=this,f=e.s;if(b=(x=10,a=new d(a,b)).s,!f||!b)return new d(0/0);if(f!=b)return a.s=-b,e.minus(a);var g=e.e,h=e.c,i=a.e,j=a.c;if(!g||!i){if(!h||!j)return new d(f/0);if(!h[0]||!j[0])return j[0]?a:new d(h[0]?e:0*f)}if(h=h.slice(),f=g-i){for(c=f>0?(i=g,j):(f=-f,h),c.reverse();f--;c.push(0));c.reverse()}for(h.length-j.length<0&&(c=j,j=h,h=c),f=j.length,b=0;f;b=(h[--f]=h[f]+j[f]+b)/10^0,h[f]%=10);for(b&&(h.unshift(b),++i>s&&(h=i=null)),f=h.length;0==h[--f];h.pop());return a.c=h,a.e=i,a},v.toPower=v.pow=function(a){var b=0*a==0?0|a:a,c=new d(this),f=new d(A);if(((k=-m>a||a>m)&&(b=1*a/0)||u(a)!=a&&0!==a&&!(b=0/0))&&!e(a,"exponent","pow")||!b)return new d(Math.pow(c.toS(),b));for(b=0>b?-b:b;1&b&&(f=f.times(c)),b>>=1,b;)c=c.times(c);return 0>a?A.div(f):f},v.round=function(a,b){return a=null==a||((k=0>a||a>l)||u(a)!=a)&&!e(a,"decimal places","round")?0:0|a,b=null==b||((k=0>b||b>8)||u(b)!=b&&0!==b)&&!e(b,"mode","round")?o:0|b,j(this,a,b)},v.squareRoot=v.sqrt=function(){var a,b,c,e,f=this,g=f.c,h=f.s,j=f.e,k=n,l=o,m=new d("0.5");if(1!==h||!g||!g[0])return new d(!h||0>h&&(!g||g[0])?0/0:g?f:1/0);for(h=Math.sqrt(f.toS()),o=1,0==h||h==1/0?(a=g.join(""),a.length+j&1||(a+="0"),b=new d(Math.sqrt(a)+""),b.c||(b.c=[1]),b.e=((j+1)/2|0)-(0>j||1&j)):b=new d(a=h.toString()),c=b.e,h=c+(n+=4),3>h&&(h=0),j=h;;)if(e=b,b=m.times(e.plus(f.div(e))),e.c.slice(0,h).join("")===b.c.slice(0,h).join("")){if(g=b.c,h-=a&&b.e<c,9!=g[h]||9!=g[h-1]||9!=g[h-2]||!(9==g[h-3]||a&&4==g[h-3])){if(!(g[j]||g[j-1]||g[j-2]||g[j-3]&&5!=g[j-3]||(g.length>j-2&&(g.length=j-2),b.times(b).eq(f)))){for(;g.length<j-3;)g.push(0);g[j-3]++}return o=l,i(b,n=k,10),b}if(a&&9==g[h-3]&&(e=b.round(k,0),e.times(e).eq(f)))return o=l,n=k,e;n+=4,h+=4,a=""}},v.times=function(a,b){var c,e=this,f=e.c,g=(x=11,a=new d(a,b)).c,h=e.e,i=a.e,j=e.s;if(a.s=j==(b=a.s)?1:-1,!((h||f&&f[0])&&(i||g&&g[0])))return new d(!j||!b||f&&!f[0]&&!g||g&&!g[0]&&!f?0/0:f&&g?0*a.s:a.s/0);for(a.e=h+i,(j=f.length)<(b=g.length)&&(c=f,f=g,g=c,i=j,j=b,b=i),i=j+b,c=[];i--;c.push(0));for(h=b-1;h>-1;h--){for(b=0,i=j+h;i>h;b=c[i]+g[h]*f[i-h-1]+b,c[i--]=b%10|0,b=b/10|0);b&&(c[i]=(c[i]+b)%10)}for(b&&++a.e,!c[0]&&c.shift(),i=c.length;!c[--i];c.pop());return a.c=a.e>s?a.e=null:a.e<r?[a.e=0]:c,a},v.toExponential=v.toE=function(a){return h(this,(null==a||((k=0>a||a>l)||u(a)!=a&&0!==a)&&!e(a,"decimal places","toE"))&&this.c?this.c.length-1:0|a,1)},v.toFixed=v.toF=function(a){var b,c,d,f=this;return null==a||((k=0>a||a>l)||u(a)!=a&&0!==a)&&!e(a,"decimal places","toF")||(d=f.e+(0|a)),b=p,a=q,p=-(q=1/0),d==c?c=f.toS():(c=h(f,d),f.s<0&&f.c&&(f.c[0]?c.indexOf("-")<0&&(c="-"+c):c=c.replace(/^-/,""))),p=b,q=a,c},v.toFraction=v.toFr=function(a){var b,c,f,g,h,i,j,l=g=new d(A),m=f=new d("0"),p=this,q=p.c,r=s,u=n,v=o,w=new d(A);if(!q)return p.toS();for(j=w.e=q.length-p.e-1,(null==a||(!(x=12,i=new d(a)).s||(k=i.cmp(l)<0||!i.c)||t&&i.e<i.c.length-1)&&!e(a,"max denominator","toFr")||(a=i).cmp(w)>0)&&(a=j>0?w:l),s=1/0,i=new d(q.join("")),n=0,o=1;b=i.div(w),h=g.plus(b.times(m)),1!=h.cmp(a);)g=m,m=h,l=f.plus(b.times(h=l)),f=h,w=i.minus(b.times(h=w)),i=h;return h=a.minus(g).div(m),f=f.plus(h.times(l)),g=g.plus(h.times(m)),f.s=l.s=p.s,n=2*j,o=v,c=l.div(m).minus(p).abs().cmp(f.div(g).minus(p).abs())<1?[l.toS(),m.toS()]:[f.toS(),g.toS()],s=r,n=u,c},v.toPrecision=v.toP=function(a){return null==a||((k=1>a||a>l)||u(a)!=a)&&!e(a,"precision","toP")?this.toS():h(this,0|--a,2)},v.toString=v.toS=function(a){var b,c,d,g=this,i=g.e;if(null===i)c=g.s?"Infinity":"NaN";else{if(a===b&&(p>=i||i>=q))return h(g,g.c.length-1,1);if(c=g.c.join(""),0>i){for(;++i;c="0"+c);c="0."+c}else if(d=c.length,i>0)if(++i>d)for(i-=d;i--;c+="0");else d>i&&(c=c.slice(0,i)+"."+c.slice(i));else if(b=c.charAt(0),d>1)c=b+"."+c.slice(1);else if("0"==b)return b;if(null!=a)if((k=!(a>=2&&65>a))||a!=(0|a)&&t)e(a,"base","toS");else if(c=f(c,0|a,10,g.s),"0"==c)return c}return g.s<0?"-"+c:c},v.toNumber=v.toN=function(){var a=this;return+a||(a.s?0*a.s:0/0)},v.valueOf=v.toJSON=function(){return this.toS()},"undefined"!=typeof c&&c.exports?c.exports=d:"function"==typeof a&&a.amd?a(function(){return d}):b.BigNumber=d}(this)},{}]},{},[1])(1)});