UNPKG

2.46 kBJavaScriptView Raw
1"use strict";exports.__esModule=true;exports.default=prettyBytes;/*
2MIT License
3
4Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
8The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
10THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11*/const UNITS=['B','kB','MB','GB','TB','PB','EB','ZB','YB'];/*
12Formats the given number using `Number#toLocaleString`.
13- If locale is a string, the value is expected to be a locale-key (for example: `de`).
14- If locale is true, the system default locale is used for translation.
15- If no value for locale is specified, the number is returned unmodified.
16*/const toLocaleString=(number,locale)=>{let result=number;if(typeof locale==='string'){result=number.toLocaleString(locale);}else if(locale===true){result=number.toLocaleString();}return result;};function prettyBytes(number,options){if(!Number.isFinite(number)){throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);}options=Object.assign({},options);if(options.signed&&number===0){return' 0 B';}const isNegative=number<0;const prefix=isNegative?'-':options.signed?'+':'';if(isNegative){number=-number;}if(number<1){const numberString=toLocaleString(number,options.locale);return prefix+numberString+' B';}const exponent=Math.min(Math.floor(Math.log10(number)/3),UNITS.length-1);number=Number((number/Math.pow(1000,exponent)).toPrecision(3));const numberString=toLocaleString(number,options.locale);const unit=UNITS[exponent];return prefix+numberString+' '+unit;}
17//# sourceMappingURL=pretty-bytes.js.map
\No newline at end of file