Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigInts and named exports #160

Merged
merged 10 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"es6": true,
"amd": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"globals": {
"describe": "readonly",
"it": "readonly",
"beforeEach": "readonly",
"BigInt": "readonly"
},
"rules": {
"arrow-parens": [2, "as-needed"],
"arrow-spacing": [2, {"before": true, "after": true}],
Expand Down Expand Up @@ -162,4 +172,4 @@
"wrap-regex": [2],
"yoda": [2, "never", { "exceptRange": true }]
}
}
}
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# filesize.js

[![build status](https://secure.travis-ci.org/avoidwork/filesize.js.svg)](http://travis-ci.org/avoidwork/filesize.js) [![downloads](https://img.shields.io/npm/dt/filesize.svg)](https://www.npmjs.com/package/filesize) [![CDNJS version](https://img.shields.io/cdnjs/v/filesize.svg)](https://cdnjs.com/libraries/filesize)
[![downloads](https://img.shields.io/npm/dt/filesize.svg)](https://www.npmjs.com/package/filesize) [![CDNJS version](https://img.shields.io/cdnjs/v/filesize.svg)](https://cdnjs.com/libraries/filesize)

filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string.

```javascript
import {filesize} from "filesize";
filesize(265318, {base: 2, standard: "jedec"}); // "259.1 KB"
```

## Optional settings

`filesize()` accepts an optional descriptor Object as a second argument, so you can customize the output.
Expand Down Expand Up @@ -58,21 +63,17 @@ _*(object)*_ Dictionary of IEC/JEDEC symbols to replace for localization, defaul


## Partial Application
`filesize.partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
`partial()` takes the second parameter of `filesize()` and returns a new function with the configuration applied
upon execution. This can be used to reduce `Object` creation if you call `filesize()` without caching the `descriptor`
in lexical scope.

```javascript
const size = filesize.partial({base: 2, standard: "jedec"});
import {partial} from "filesize";
const size = partial({base: 2, standard: "jedec"});

size(265318); // "259.1 KB"
```

## How can I load filesize.js?
filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (```npm install filesize```), or using a script tag.

An ES6 version is bundled with an npm install, but requires you load it with the full path, e.g. `require(path.join(__dirname, 'node_modules', 'filesize', 'lib', 'filesize.es6.js'))`.

## License
Copyright (c) 2022 Jason Mulligan
Licensed under the BSD-3 license.
237 changes: 237 additions & 0 deletions dist/filesize.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/**
* filesize
*
* @copyright 2022 Jason Mulligan <[email protected]>
* @license BSD-3-Clause
* @version 10.0.0
*/
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const ARRAY = "array";
const BIT = "bit";
const BITS = "bits";
const BYTE = "byte";
const BYTES = "bytes";
const EMPTY = "";
const EXPONENT = "exponent";
const FUNCTION = "function";
const IEC = "iec";
const INVALID_NUMBER = "Invalid number";
const INVALID_ROUND = "Invalid rounding method";
const JEDEC = "jedec";
const OBJECT = "object";
const PERIOD = ".";
const ROUND = "round";
const S = "s";
const SI_KBIT = "kbit";
const SI_KBYTE = "kB";
const SPACE = " ";
const STRING = "string";
const ZERO = "0";
const STRINGS = {
symbol: {
iec: {
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
},
jedec: {
bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
}
},
fullform: {
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
}
};

function filesize (arg, {
bits = false,
pad = false,
base = -1,
round = 2,
locale = EMPTY,
localeOptions = {},
separator = EMPTY,
spacer = SPACE,
symbols = {},
standard = EMPTY,
output = STRING,
fullform = false,
fullforms = [],
exponent = -1,
roundingMethod = ROUND,
precision = 0
} = {}) {
let e = exponent,
num = Number(arg),
result = [],
val = 0,
u = EMPTY;

// Sync base & standard
if (base === -1 && standard.length === 0) {
base = 10;
standard = JEDEC;
} else if (base === -1 && standard.length > 0) {
standard = standard === IEC ? IEC : JEDEC;
base = standard === IEC ? 2 : 10;
} else {
base = base === 2 ? 2 : 10;
standard = base === 10 ? JEDEC : standard === JEDEC ? JEDEC : IEC;
}

const ceil = base === 10 ? 1000 : 1024,
full = fullform === true,
neg = num < 0,
roundingFunc = Math[roundingMethod];

if (typeof arg !== "bigint" && isNaN(arg)) {
throw new TypeError(INVALID_NUMBER);
}

if (typeof roundingFunc !== FUNCTION) {
throw new TypeError(INVALID_ROUND);
}

// Flipping a negative number to determine the size
if (neg) {
num = -num;
}

// Determining the exponent
if (e === -1 || isNaN(e)) {
e = Math.floor(Math.log(num) / Math.log(ceil));

if (e < 0) {
e = 0;
}
}

// Exceeding supported length, time to reduce & multiply
if (e > 8) {
if (precision > 0) {
precision += 8 - e;
}

e = 8;
}

if (output === EXPONENT) {
return e;
}

// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
u = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];
} else {
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));

if (bits) {
val = val * 8;

if (val >= ceil && e < 8) {
val = val / ceil;
e++;
}
}

const p = Math.pow(10, e > 0 ? round : 0);
result[0] = roundingFunc(val * p) / p;

if (result[0] === ceil && e < 8 && exponent === -1) {
result[0] = 1;
e++;
}

u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];
}

// Decorating a 'diff'
if (neg) {
result[0] = -result[0];
}

// Setting optional precision
if (precision > 0) {
result[0] = result[0].toPrecision(precision);
}

// Applying custom symbol
result[1] = symbols[result[1]] || result[1];

if (locale === true) {
result[0] = result[0].toLocaleString();
} else if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale, localeOptions);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(PERIOD, separator);
}

if (pad && Number.isInteger(result[0]) === false && round > 0) {
const x = separator || PERIOD,
tmp = result[0].toString().split(x),
s = tmp[1] || EMPTY,
l = s.length,
n = round - l;

result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;
}

if (full) {
result[1] = fullforms[e] ? fullforms[e] : STRINGS.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);
}

// Returning Array, Object, or String (default)
return output === ARRAY ? result : output === OBJECT ? {
value: result[0],
symbol: result[1],
exponent: e,
unit: u
} : result.join(spacer);
}

// Partial application for functional programming
function partial ({
bits = false,
pad = false,
base = -1,
round = 2,
locale = EMPTY,
localeOptions = {},
separator = EMPTY,
spacer = SPACE,
symbols = {},
standard = EMPTY,
output = STRING,
fullform = false,
fullforms = [],
exponent = -1,
roundingMethod = ROUND,
precision = 0
} = {}) {
return arg => filesize(arg, {
bits,
pad,
base,
round,
locale,
localeOptions,
separator,
spacer,
symbols,
standard,
output,
fullform,
fullforms,
exponent,
roundingMethod,
precision
});
}

exports.filesize = filesize;
exports.partial = partial;
Loading