kitten-format
Advanced tools
Comparing version 1.10.0 to 1.11.0
@@ -202,6 +202,6 @@ (function (global, factory) { | ||
if (locale$$1.isCurrencyFirst === true) { | ||
res = locale$$1.currencySymbol + res; | ||
res = (options.unitPrefix || '') + locale$$1.currencySymbol + res; | ||
} | ||
else { | ||
res += ' ' + locale$$1.currencySymbol; | ||
res += ' ' + (options.unitPrefix || '') + locale$$1.currencySymbol; | ||
} | ||
@@ -319,3 +319,3 @@ } | ||
if (!_unit) { | ||
if (_unit === null || _unit === undefined) { | ||
return value; | ||
@@ -492,2 +492,29 @@ } | ||
/** | ||
* Average a currency | ||
* @param {Number} value | ||
* @param {Object} options | ||
* @returns {String} | ||
*/ | ||
function averageCurrency (value, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
options.unit = ''; | ||
options.maxPower = 3; | ||
const average = averageN(value, options); | ||
let parameters = formatC(value, options); | ||
if (parameters == null || typeof parameters !== 'object') { | ||
return parameters; | ||
} | ||
parameters.unitPrefix = average.unit; | ||
parameters.style = 'currency'; | ||
return format(parameters.locale, average.value, parameters); | ||
} | ||
const kittenFormat = {}; | ||
@@ -513,2 +540,4 @@ | ||
kittenFormat.convertCurrency = convC; | ||
kittenFormat.averageC = averageCurrency; | ||
kittenFormat.averageCurrency = averageCurrency; | ||
@@ -515,0 +544,0 @@ return kittenFormat; |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e=e||self).kittenFormat=r()}(this,function(){"use strict";function e(e){return null==e||"string"!=typeof e?e:e.toLowerCase()}var r={EUR:"€",GBP:"£",CHF:"CHF",USD:"$",AED:"AED",SAR:"SAR",XPF:"XPF"},t={locale:"fr-FR",currency:"EUR",currencySymbol:r.EUR,precision:2,unitPrefixes:{15:{default:"P",g:"GT"},12:{default:"T",g:"MT"},9:{default:"G",g:"kT"},6:{default:"M",g:"T"},3:"k",0:"","-3":"m","-6":"μ","-9":"n"},thousandSeparator:" ",decimalSeparator:","},n={default:t};function i(e){e&&(n.default=e)}function o(e){return n[e]?n[e]:n.default}function u(e,r,t){e=o(e);let n=(r+="").split("."),i=n[0],u=n[1]||"",l=e.thousandSeparator||" ",a=0,c="";"0"!==u[u.length-1]&&!0!==t.shouldNotRound&&(u=function(e,r){var t=Math.pow(10,r);return Math.round(e*t)/t}(Number("0."+u,10),null!=t.maximumFractionDigits?t.maximumFractionDigits:e.precision)+"",1===Number(u)&&(i=Number(i)+1+""),u=u.slice(2));for(let e=i.length-1;e>=0;e--)c=i[e]+c,3===++a&&e-1>=0&&(c=l+c,a=0);if(null!=t.minimumFractionDigits)for(u+="";u.length<t.minimumFractionDigits;u+="0");return!0===t.shouldNotRound&&(u=u.slice(0,null!=t.maximumFractionDigits?t.maximumFractionDigits:u.length)),u.length&&(c+=e.decimalSeparator+u),"currency"===t.style&&(!0===e.isCurrencyFirst?c=e.currencySymbol+c:c+=" "+e.currencySymbol),c}function l(e,r){let t=function(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=o((r=r||{}).locale),n=r.precision;r.shouldNotRound||null!=n||(n=t.precision);return{locale:r.locale||t.locale,precision:n}}(e,r);return null==t||"object"!=typeof t?t:((r=r||{}).maximumFractionDigits=t.precision,u(t.locale,e,r))}function a(e,r){let t=function(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=null===(r=r||{}).power||void 0===r.power?0:r.power,n=r.unit;if(!n)return e;var i=e,u=0,l=null;if(e>=1){var a=(e+"").split(".")[0],c=a.length;u=3*Math.trunc(c/3),null!==r.maxPower&&void 0!==r.maxPower&&u+t>r.maxPower&&(l=r.maxPower-t),i=e*Math.pow(10,-(null!==l?l:u))}i<1&&(i*=Math.pow(10,3),-0===(t-=3)&&(t=0));var s=o(r.locale).unitPrefixes[(null!==l?l:u)+t];void 0===s?s="10^"+(u+t)+n:"string"!=typeof s?s=s[n]||s.default:s+=n;return{value:i,unit:s}}(e,r);return null==t||"object"!=typeof t?t:(r=r||{},(e=l(t.value,r))+" "+t.unit)}function c(e,r){let t=function(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=o((r=r||{}).locale),n=r.shouldNotRound?r.precision:r.precision||t.precision,i=r.currency||r.source||t.currency,u=r.locale||t.locale;r.target&&r.rates&&r.rates[r.target]&&(r.source=i,i=r.target,e=s(e,r));return{locale:u,currency:i,precision:n,value:e,shouldNotRound:r.shouldNotRound}}(e,r);return null==t||"object"!=typeof t?t:(t.precision<2&&(t.precision=2),t.style="currency",t.minimumFractionDigits=t.precision,null==t.minimumFractionDigits&&(t.minimumFractionDigits=t.locale.precision),t.maximumFractionDigits=t.minimumFractionDigits,u(t.locale,t.value,t))}function s(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=o((r=r||{}).locale),n=r.source||t.currency,i=r.target,u=r.rates;return!i||!u||u&&!u[n]||u&&!u[i]?e:e/u[n]*u[i]}const f={};return f.setOptions=i,f.setOption=function(e,i){null!=e&&("currency"===e&&(n.default.currencySymbol=r[i]||t.currencySymbol),n.default[e]=i)},f.locale=function(e){e&&(n[e.locale]=e,i(n[e.locale]))},f.lowerCase=e,f.upperCase=function(e){return null==e||"string"!=typeof e?e:e.toUpperCase()},f.upperCaseFirstChar=function(r){return null!=r&&"string"==typeof r&&r.length?r[0].toUpperCase()+e(r.slice(1,r.length)):r},f.formatN=l,f.formatNumber=l,f.percent=function(e,r){let t=function(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=100*e;return r&&r.isAlreadyPercentageNumber&&(t=e),{value:t}}(e,r);return null==t||"object"!=typeof t?t:l(t.value,r)+"%"},f.averageN=a,f.averageNumber=a,f.formatC=c,f.formatCurrency=c,f.convC=s,f.convertCurrency=s,f}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e=e||self).kittenFormat=r()}(this,function(){"use strict";function e(e){return null==e||"string"!=typeof e?e:e.toLowerCase()}var r={EUR:"€",GBP:"£",CHF:"CHF",USD:"$",AED:"AED",SAR:"SAR",XPF:"XPF"},t={locale:"fr-FR",currency:"EUR",currencySymbol:r.EUR,precision:2,unitPrefixes:{15:{default:"P",g:"GT"},12:{default:"T",g:"MT"},9:{default:"G",g:"kT"},6:{default:"M",g:"T"},3:"k",0:"","-3":"m","-6":"μ","-9":"n"},thousandSeparator:" ",decimalSeparator:","},n={default:t};function i(e){e&&(n.default=e)}function u(e){return n[e]?n[e]:n.default}function o(e,r,t){e=u(e);let n=(r+="").split("."),i=n[0],o=n[1]||"",l=e.thousandSeparator||" ",a=0,c="";"0"!==o[o.length-1]&&!0!==t.shouldNotRound&&(o=function(e,r){var t=Math.pow(10,r);return Math.round(e*t)/t}(Number("0."+o,10),null!=t.maximumFractionDigits?t.maximumFractionDigits:e.precision)+"",1===Number(o)&&(i=Number(i)+1+""),o=o.slice(2));for(let e=i.length-1;e>=0;e--)c=i[e]+c,3===++a&&e-1>=0&&(c=l+c,a=0);if(null!=t.minimumFractionDigits)for(o+="";o.length<t.minimumFractionDigits;o+="0");return!0===t.shouldNotRound&&(o=o.slice(0,null!=t.maximumFractionDigits?t.maximumFractionDigits:o.length)),o.length&&(c+=e.decimalSeparator+o),"currency"===t.style&&(!0===e.isCurrencyFirst?c=(t.unitPrefix||"")+e.currencySymbol+c:c+=" "+(t.unitPrefix||"")+e.currencySymbol),c}function l(e,r){let t=function(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=u((r=r||{}).locale),n=r.precision;r.shouldNotRound||null!=n||(n=t.precision);return{locale:r.locale||t.locale,precision:n}}(e,r);return null==t||"object"!=typeof t?t:((r=r||{}).maximumFractionDigits=t.precision,o(t.locale,e,r))}function a(e,r){let t=c(e,r);return null==t||"object"!=typeof t?t:(r=r||{},(e=l(t.value,r))+" "+t.unit)}function c(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=null===(r=r||{}).power||void 0===r.power?0:r.power,n=r.unit;if(null==n)return e;var i=e,o=0,l=null;if(e>=1){var a=(e+"").split(".")[0].length;o=3*Math.trunc(a/3),null!==r.maxPower&&void 0!==r.maxPower&&o+t>r.maxPower&&(l=r.maxPower-t),i=e*Math.pow(10,-(null!==l?l:o))}i<1&&(i*=Math.pow(10,3),-0===(t-=3)&&(t=0));var c=u(r.locale).unitPrefixes[(null!==l?l:o)+t];return void 0===c?c="10^"+(o+t)+n:"string"!=typeof c?c=c[n]||c.default:c+=n,{value:i,unit:c}}function s(e,r){let t=f(e,r);return null==t||"object"!=typeof t?t:(t.precision<2&&(t.precision=2),t.style="currency",t.minimumFractionDigits=t.precision,null==t.minimumFractionDigits&&(t.minimumFractionDigits=t.locale.precision),t.maximumFractionDigits=t.minimumFractionDigits,o(t.locale,t.value,t))}function f(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=u((r=r||{}).locale),n=r.shouldNotRound?r.precision:r.precision||t.precision,i=r.currency||r.source||t.currency,o=r.locale||t.locale;return r.target&&r.rates&&r.rates[r.target]&&(r.source=i,i=r.target,e=m(e,r)),{locale:o,currency:i,precision:n,value:e,shouldNotRound:r.shouldNotRound}}function m(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=u((r=r||{}).locale),n=r.source||t.currency,i=r.target,o=r.rates;return!i||!o||o&&!o[n]||o&&!o[i]?e:e/o[n]*o[i]}function p(e,r){r||(r={}),r.unit="",r.maxPower=3;const t=c(e,r);let n=f(e,r);return null==n||"object"!=typeof n?n:(n.unitPrefix=t.unit,n.style="currency",o(n.locale,t.value,n))}const y={};return y.setOptions=i,y.setOption=function(e,i){null!=e&&("currency"===e&&(n.default.currencySymbol=r[i]||t.currencySymbol),n.default[e]=i)},y.locale=function(e){e&&(n[e.locale]=e,i(n[e.locale]))},y.lowerCase=e,y.upperCase=function(e){return null==e||"string"!=typeof e?e:e.toUpperCase()},y.upperCaseFirstChar=function(r){return null!=r&&"string"==typeof r&&r.length?r[0].toUpperCase()+e(r.slice(1,r.length)):r},y.formatN=l,y.formatNumber=l,y.percent=function(e,r){let t=function(e,r){if(null==e)return e;if("string"==typeof e&&isNaN(e))return"-";var t=100*e;return r&&r.isAlreadyPercentageNumber&&(t=e),{value:t}}(e,r);return null==t||"object"!=typeof t?t:l(t.value,r)+"%"},y.averageN=a,y.averageNumber=a,y.formatC=s,y.formatCurrency=s,y.convC=m,y.convertCurrency=m,y.averageC=p,y.averageCurrency=p,y}); |
@@ -198,6 +198,6 @@ 'use strict'; | ||
if (locale$$1.isCurrencyFirst === true) { | ||
res = locale$$1.currencySymbol + res; | ||
res = (options.unitPrefix || '') + locale$$1.currencySymbol + res; | ||
} | ||
else { | ||
res += ' ' + locale$$1.currencySymbol; | ||
res += ' ' + (options.unitPrefix || '') + locale$$1.currencySymbol; | ||
} | ||
@@ -315,3 +315,3 @@ } | ||
if (!_unit) { | ||
if (_unit === null || _unit === undefined) { | ||
return value; | ||
@@ -488,2 +488,29 @@ } | ||
/** | ||
* Average a currency | ||
* @param {Number} value | ||
* @param {Object} options | ||
* @returns {String} | ||
*/ | ||
function averageCurrency (value, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
options.unit = ''; | ||
options.maxPower = 3; | ||
const average = averageN(value, options); | ||
let parameters = formatC(value, options); | ||
if (parameters == null || typeof parameters !== 'object') { | ||
return parameters; | ||
} | ||
parameters.unitPrefix = average.unit; | ||
parameters.style = 'currency'; | ||
return format(parameters.locale, average.value, parameters); | ||
} | ||
const kittenFormat = {}; | ||
@@ -509,3 +536,5 @@ | ||
kittenFormat.convertCurrency = convC; | ||
kittenFormat.averageC = averageCurrency; | ||
kittenFormat.averageCurrency = averageCurrency; | ||
module.exports = kittenFormat; |
@@ -216,2 +216,22 @@ # kitten-format | ||
### kittenFormat.averageC (kittenFormat.averageCurrency) | ||
Average a currency to default or specified locale. | ||
It can only average to € or k€. | ||
```js | ||
kittenFormat.averageC(1234, { | ||
locale : 'fr-FR' | ||
}); | ||
// res: '1,2 k€' | ||
// options | ||
{ | ||
locale : String, | ||
power : Int // current unit | ||
} | ||
``` | ||
### Strings | ||
@@ -218,0 +238,0 @@ |
@@ -8,3 +8,3 @@ import { | ||
import { formatNumber, percentNumber, averageNumber } from './lib/number'; | ||
import { formatCurrency, convC } from './lib/currency'; | ||
import { formatCurrency, convC, averageCurrency } from './lib/currency'; | ||
@@ -31,3 +31,5 @@ const kittenFormat = {}; | ||
kittenFormat.convertCurrency = convC; | ||
kittenFormat.averageC = averageCurrency; | ||
kittenFormat.averageCurrency = averageCurrency; | ||
export default kittenFormat; |
import { getLocale } from './options'; | ||
import { format } from './formatters'; | ||
import { averageN } from './number'; | ||
@@ -101,1 +102,28 @@ /** | ||
} | ||
/** | ||
* Average a currency | ||
* @param {Number} value | ||
* @param {Object} options | ||
* @returns {String} | ||
*/ | ||
export function averageCurrency (value, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
options.unit = ''; | ||
options.maxPower = 3; | ||
const average = averageN(value, options); | ||
let parameters = formatC(value, options); | ||
if (parameters == null || typeof parameters !== 'object') { | ||
return parameters; | ||
} | ||
parameters.unitPrefix = average.unit; | ||
parameters.style = 'currency'; | ||
return format(parameters.locale, average.value, parameters); | ||
} |
@@ -60,6 +60,6 @@ import { getLocale } from './options'; | ||
if (locale.isCurrencyFirst === true) { | ||
res = locale.currencySymbol + res; | ||
res = (options.unitPrefix || '') + locale.currencySymbol + res; | ||
} | ||
else { | ||
res += ' ' + locale.currencySymbol; | ||
res += ' ' + (options.unitPrefix || '') + locale.currencySymbol; | ||
} | ||
@@ -66,0 +66,0 @@ } |
@@ -110,3 +110,3 @@ import { getLocale } from './options'; | ||
if (!_unit) { | ||
if (_unit === null || _unit === undefined) { | ||
return value; | ||
@@ -113,0 +113,0 @@ } |
{ | ||
"name": "kitten-format", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"description": "Fast formatters for browsers", | ||
@@ -5,0 +5,0 @@ "main": "build/kittenFormat.server.js", |
@@ -216,2 +216,22 @@ # kitten-format | ||
### kittenFormat.averageC (kittenFormat.averageCurrency) | ||
Average a currency to default or specified locale. | ||
It can only average to € or k€. | ||
```js | ||
kittenFormat.averageC(1234, { | ||
locale : 'fr-FR' | ||
}); | ||
// res: '1,2 k€' | ||
// options | ||
{ | ||
locale : String, | ||
power : Int // current unit | ||
} | ||
``` | ||
### Strings | ||
@@ -218,0 +238,0 @@ |
@@ -324,2 +324,67 @@ const should = require('should'); | ||
describe('averageC', () => { | ||
it ('should be defined', () => { | ||
should(kittenFormat.averageC).be.a.Function(); | ||
should(kittenFormat.averageCurrency).be.a.Function(); | ||
}); | ||
it('should not average a number if no value is given', () => { | ||
should(kittenFormat.averageC()).eql(undefined); | ||
}); | ||
it('should not average a number if null value is given', () => { | ||
should(kittenFormat.averageC(null)).eql(null); | ||
}); | ||
it('should not average a number if no unit is given', () => { | ||
should(kittenFormat.averageC(100)).eql('100 €'); | ||
}); | ||
it('should not average a string (NaN)', () => { | ||
should(kittenFormat.averageC('123a')).eql('-'); | ||
}); | ||
it('should average a string', () => { | ||
should(kittenFormat.averageC('123')).eql('123 €'); | ||
}); | ||
it('should average without precising the source power', () => { | ||
should(kittenFormat.averageC(1001.24, { | ||
precision : 1 | ||
})).eql('1 k€'); | ||
}); | ||
it('should average 10^0', () => { | ||
should(kittenFormat.averageC(1001.24, { | ||
power : 0, | ||
precision : 1 | ||
})).eql('1 k€'); | ||
}); | ||
it('should average if it is < 1', () => { | ||
should(kittenFormat.averageC(0.243, { | ||
power : 3, | ||
precision : 1 | ||
})).eql('243 €'); | ||
}); | ||
it('should format for another locale', () => { | ||
should(kittenFormat.averageC(101.24, { | ||
locale : 'en-GB', | ||
power : 0, | ||
unit : 'g', | ||
precision : 1 | ||
})).eql('£101.24'); | ||
}); | ||
it('should format for another locale 10^3', () => { | ||
should(kittenFormat.averageC(1010.24, { | ||
locale : 'en-GB', | ||
power : 0, | ||
unit : 'g', | ||
precision : 1 | ||
})).eql('k£1.01'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
109616
2532
267