Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

human-format

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

human-format - npm Package Compare versions

Comparing version 0.1.4 to 0.2.0

93

index.js

@@ -21,23 +21,19 @@ // UMD: https://github.com/umdjs/umd/blob/master/returnExports.js

// https://www.npmjs.org/package/escape-regexp
var escapeRegexp = function (str) {
function escapeRegexp(str) {
return str.replace(/([.*+?=^!:${}()|[\]\/\\])/g, '\\$1');
};
}
var isArray = (function () {
var toString = Object.prototype.toString;
toString = toString.call.bind(toString);
var tag = toString([]);
return function (value) {
return (toString(value) === tag);
var isArray = (function (toString) {
var tag = toString.call([]);
return function isArray(value) {
return (toString.call(value) === tag);
};
})();
})(Object.prototype.toString);
var mergeDefaults = (function () {
var has = Object.prototype.hasOwnProperty;
has = has.call.bind(has);
return function (opts, defs) {
var mergeDefaults = (function (has) {
return function mergeDefaults(opts, defs) {
var key;
for (key in defs)
{
if (has(defs, key) && (opts[key] === undefined))
if (has.call(defs, key) && (opts[key] === undefined))
{

@@ -49,8 +45,17 @@ opts[key] = defs[key];

};
})();
})(Object.prototype.hasOwnProperty);
function round(f, n) {
if (!n) {
return Math.round(f);
}
var p = Math.pow(10, n);
return Math.round(f * p) / p;
}
//==================================================================
// Binary search to find the greatest index which has a value <=.
var findPrefix = function (list, value) {
function findPrefix(list, value) {
/* jshint bitwise: false */

@@ -74,3 +79,3 @@

return list[low];
};
}

@@ -81,3 +86,3 @@ //==================================================================

// (e.g. K/M/G and Ki/Mi/Gi).
var makePrefixes = function (prefixes, base, init) {
function humanFormat$makePrefixes(prefixes, base, init) {
init || (init = 0);

@@ -125,3 +130,3 @@

};
};
}

@@ -138,3 +143,3 @@ // FIXME: it makes little sense to have fractional prefixes for an

// the same unit to ease the comparison.
prefixes: makePrefixes(
prefixes: humanFormat$makePrefixes(
'y,z,a,f,p,n,µ,m,,k,M,G,T,P,E,Z,Y'.split(','),

@@ -146,3 +151,3 @@ 1e3, // Base.

var humanFormat = function (num, opts) {
function humanFormat$raw(num, opts) {
opts = mergeDefaults(opts || {}, defaults);

@@ -156,15 +161,47 @@

{
return '0'+ opts.unit;
return {
num: 0,
prefix: '',
unit: opts.unit
};
}
var prefix = findPrefix(opts.prefixes.list, num);
var prefix;
// if a prefix is given use that prefix
if(opts.prefix) {
var factor = opts.factor;
// if no factor is given then look it up
if(factor === undefined) {
factor = opts.prefixes.map[opts.prefix];
}
// if we found a factor use it
if(factor !== undefined) {
prefix = [opts.prefix, factor];
}
}
// if no prefix was provided search for the best prefix
if(!prefix) {
prefix = findPrefix(opts.prefixes.list, num);
}
// Rebases the number using the current prefix and rounds it with
// 2 decimals.
num = Math.round(num * 1e2 / prefix[1]) / 1e2;
num /= prefix[1];
return num + prefix[0] + opts.unit;
};
humanFormat.makePrefixes = makePrefixes;
humanFormat.parse = function (str, opts) {
return {
num: num,
prefix: prefix[0],
unit: opts.unit
};
}
function humanFormat(num, opts){
var info = humanFormat$raw(num, opts);
return round(info.num, 2) + info.prefix + info.unit;
}
humanFormat.raw = humanFormat$raw;
humanFormat.makePrefixes = humanFormat$makePrefixes;
humanFormat.parse = function humanFormat$parse(str, opts) {
var prefixes = mergeDefaults(opts || {}, defaults).prefixes;

@@ -171,0 +208,0 @@

@@ -14,11 +14,18 @@ 'use strict';

var data = [
[1e-25, '0.1yB'],
[1e-3, '1mB'],
[0, '0B'],
[1, '1B'],
[10, '10B'],
[1e12, '1TB'],
[1e28, '10000YB'],
[1e-25, '0.1yB', { num: 0.1, prefix: 'y', unit: 'B' }],
[1e-3, '1mB', { num: 1, prefix: 'm', unit: 'B' }],
[0, '0B', { num: 0, prefix: '', unit: 'B' }],
[1, '1B', { num: 1, prefix: '', unit: 'B' }],
[10, '10B', { num: 10, prefix: '', unit: 'B' }],
[1e12, '1TB', { num: 1, prefix: 'T', unit: 'B' }],
[1e28, '10000YB', { num: 10000, prefix: 'Y', unit: 'B' }],
];
function compareRaw(actual, expected) {
expect(actual).to.be.an('object');
expect(actual.num).to.be.closeTo(expected.num, 1e-3);
expect(actual.prefix).to.equal(expected.prefix);
expect(actual.unit).to.equal(expected.unit);
}
//====================================================================

@@ -38,2 +45,8 @@

expect(humanFormat(value)).to.equal('0B');
compareRaw(humanFormat.raw(value), {
num: 0,
prefix: '',
unit: 'B',
});
});

@@ -45,2 +58,3 @@ });

expect(humanFormat(datum[0])).to.equal(datum[1]);
compareRaw(humanFormat.raw(datum[0]), datum[2]);
});

@@ -51,2 +65,7 @@ });

expect(humanFormat(0, { unit: 'g' })).to.equal('0g');
compareRaw(humanFormat.raw(0, { unit: 'g' }), {
num: 0,
prefix: '',
unit: 'g',
});
});

@@ -61,3 +80,19 @@

expect(humanFormat(102400, { prefixes: prefixes })).to.equal('100kiB');
compareRaw(humanFormat.raw(102400, { prefixes: prefixes }), {
num: 100,
prefix: 'ki',
unit: 'B',
});
});
it('can force a prefix', function () {
expect(humanFormat(100, { unit: 'm', prefix: 'k' })).to.equal('0.1km');
compareRaw(humanFormat.raw(100, { unit: 'm', prefix: 'k' }), {
num: 0.1,
prefix: 'k',
unit: 'm',
});
});
});

@@ -64,0 +99,0 @@

{
"name": "human-format",
"version": "0.1.4",
"version": "0.2.0",
"description": "Converts a number to/from a human readable string: `1337` ↔ `1.34kB`",

@@ -5,0 +5,0 @@ "keywords": [

@@ -48,2 +48,14 @@ # human-format

// You can force a prefix to be used.
humanFormat(100, { unit: 'm', prefix: 'k' });
//=> 0.1km
// You can access the raw result.
humanFormat.raw(100, { unit: 'm', prefix: 'k' });
//=> {
// num: 0.09999999999999999, // Close value, not rounded.
// prefix: 'k',
// num: 'm',
// }
// You can also parses a human readable string.

@@ -65,4 +77,8 @@ humanFormat.parse('1.34kB');

Contributors:
- @sweetpi
## License
ISC © [Julien Fontanet](http://julien.isonoe.net)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc