Skip to content

Commit

Permalink
Introduce new filesize dependency
Browse files Browse the repository at this point in the history
FREEBIE
  • Loading branch information
scottnonnenberg committed May 9, 2017
1 parent 699549e commit 2a04fa0
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bower.json
Expand Up @@ -21,7 +21,8 @@
"emojidata": "https://github.com/iamcal/emoji-data.git",
"autosize": "~3.0.6",
"webaudiorecorder": "https://github.com/higuma/web-audio-recorder-js.git",
"mp3lameencoder": "https://github.com/higuma/mp3-lame-encoder-js.git"
"mp3lameencoder": "https://github.com/higuma/mp3-lame-encoder-js.git",
"filesize": "https://github.com/avoidwork/filesize.js.git"
},
"devDependencies": {
"mocha": "~2.0.1",
Expand Down Expand Up @@ -99,6 +100,9 @@
],
"mp3lameencoder": [
"lib/Mp3LameEncoder.js"
],
"filesize": [
"lib/filesize.js"
]
},
"concat": {
Expand All @@ -119,7 +123,8 @@
"blueimp-load-image",
"blueimp-canvas-to-blob",
"emojijs",
"autosize"
"autosize",
"filesize"
],
"libtextsecure": [
"long",
Expand Down
167 changes: 167 additions & 0 deletions components/filesize/lib/filesize.js
@@ -0,0 +1,167 @@
"use strict";

/**
* filesize
*
* @copyright 2017 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 3.5.9
*/
(function (global) {
var 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"]
};

/**
* filesize
*
* @method filesize
* @param {Mixed} arg String, Int or Float to transform
* @param {Object} descriptor [Optional] Flags
* @return {String} Readable file size String
*/
function filesize(arg) {
var descriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var result = [],
val = 0,
e = void 0,
base = void 0,
bits = void 0,
ceil = void 0,
full = void 0,
fullforms = void 0,
neg = void 0,
num = void 0,
output = void 0,
round = void 0,
unix = void 0,
spacer = void 0,
standard = void 0,
symbols = void 0;

if (isNaN(arg)) {
throw new Error("Invalid arguments");
}

bits = descriptor.bits === true;
unix = descriptor.unix === true;
base = descriptor.base || 2;
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
symbols = descriptor.symbols || descriptor.suffixes || {};
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 !== undefined ? 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;
}

// 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));
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];

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

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

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

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

return result.join(spacer);
}

// Partial application for functional programming
filesize.partial = function (opt) {
return function (arg) {
return filesize(arg, opt);
};
};

// CommonJS, AMD, script tag
if (typeof exports !== "undefined") {
module.exports = filesize;
} else if (typeof define === "function" && define.amd) {
define(function () {
return filesize;
});
} else {
global.filesize = filesize;
}
})(typeof window !== "undefined" ? window : global);
168 changes: 168 additions & 0 deletions js/components.js
Expand Up @@ -39227,6 +39227,174 @@ JSON.stringify(result);

module.exports = autosize;
});
"use strict";

/**
* filesize
*
* @copyright 2017 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 3.5.9
*/
(function (global) {
var 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"]
};

/**
* filesize
*
* @method filesize
* @param {Mixed} arg String, Int or Float to transform
* @param {Object} descriptor [Optional] Flags
* @return {String} Readable file size String
*/
function filesize(arg) {
var descriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var result = [],
val = 0,
e = void 0,
base = void 0,
bits = void 0,
ceil = void 0,
full = void 0,
fullforms = void 0,
neg = void 0,
num = void 0,
output = void 0,
round = void 0,
unix = void 0,
spacer = void 0,
standard = void 0,
symbols = void 0;

if (isNaN(arg)) {
throw new Error("Invalid arguments");
}

bits = descriptor.bits === true;
unix = descriptor.unix === true;
base = descriptor.base || 2;
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
symbols = descriptor.symbols || descriptor.suffixes || {};
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 !== undefined ? 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;
}

// 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));
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];

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

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

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

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

return result.join(spacer);
}

// Partial application for functional programming
filesize.partial = function (opt) {
return function (arg) {
return filesize(arg, opt);
};
};

// CommonJS, AMD, script tag
if (typeof exports !== "undefined") {
module.exports = filesize;
} else if (typeof define === "function" && define.amd) {
define(function () {
return filesize;
});
} else {
global.filesize = filesize;
}
})(typeof window !== "undefined" ? window : global);

(function(window) {
// internal: same as jQuery.extend(true, args...)
var extend = function() {
Expand Down

0 comments on commit 2a04fa0

Please sign in to comment.