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

Modernizing build & outputs #104

Merged
merged 2 commits into from
Oct 1, 2019
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/*
.idea/*
node_modules
.idea
*.tgz
6 changes: 1 addition & 5 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/node_modules/
/lib/filesize.min.js
/lib/filesize.min.js.map
/lib/filesize.es6.min.js
/lib/filesize.es6.min.js.map
/src/
/test/
.idea
Expand All @@ -11,4 +7,4 @@
.travis.yml
bower.json
composer.json
Gruntfile.js
tsconfig.json
101 changes: 0 additions & 101 deletions Gruntfile.js

This file was deleted.

5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ const size = filesize.partial({standard: "iec"});
size(265318); // "259.1 KiB"
```

## 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) 2019 Jason Mulligan
Licensed under the BSD-3 license.
5 changes: 5 additions & 0 deletions filesize.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function filesize(arg: string, descriptor: object): any;

export interface filesize {
partial: any;
}
137 changes: 137 additions & 0 deletions lib/filesize.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
'use strict';

const b = /^(b|B)$/,
symbol = {
iec: {
bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
},
jedec: {
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
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, descriptor = {}) {
if (isNaN(arg)) {
throw new TypeError("Invalid number");
}

let result = [],
val = 0,
bits = descriptor.bits === true,
unix = descriptor.unix === true,
base = descriptor.base || 2,
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2,
locale = descriptor.locale !== void 0 ? descriptor.locale : "",
localeOptions = descriptor.localeOptions || {},
separator = descriptor.separator !== void 0 ? descriptor.separator : "",
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ",
symbols = descriptor.symbols || {},
standard = base === 2 ? descriptor.standard || "jedec" : "jedec",
output = descriptor.output || "string",
full = descriptor.fullform === true,
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [],
e = descriptor.exponent !== void 0 ? descriptor.exponent : -1,
num = Number(arg),
neg = num < 0,
ceil = base > 2 ? 1000 : 1024;

// 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) {
e = 8;
}

if (output === "exponent") {
return e;
}

// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
result[1] = unix ? "" : 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++;
}
}

result[0] = Number(val.toFixed(e > 0 ? round : 0));

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

result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];

if (unix) {
result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];

if (b.test(result[1])) {
result[0] = Math.floor(result[0]);
result[1] = "";
}
}
}

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

// 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(".", separator);
}

// Returning Array, Object, or String (default)
if (output === "array") {
return result;
}

if (full) {
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
}

if (output === "object") {
return {value: result[0], symbol: result[1]};
}

return result.join(spacer);
}

// Partial application for functional programming
filesize.partial = opt => arg => filesize(arg, opt);

module.exports = filesize;
Loading