-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.spec.js
More file actions
168 lines (144 loc) · 4.85 KB
/
index.spec.js
File metadata and controls
168 lines (144 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
var assert = require("assert");
var mocha = require("tap").mocha;
var describe = mocha.describe;
var it = mocha.it;
var humanFormat = require("./");
// ===================================================================
var data = [
[1e-25, "0.1 y", { value: 0.1, prefix: "y" }],
[1e-3, "1 m", { value: 1, prefix: "m" }],
[0, "0", { value: 0, prefix: "" }],
[1, "1", { value: 1, prefix: "" }],
[10, "10", { value: 10, prefix: "" }],
[1e12, "1 T", { value: 1, prefix: "T" }],
[1e28, "10000 Y", { value: 10000, prefix: "Y" }],
];
data.forEach(function (datum) {
var num = datum[0];
if (num !== 0) {
var raw = datum[2];
data.push([-num, "-" + datum[1], { __proto__: raw, value: -raw.value }]);
}
});
function assertCloseTo(actual, expected, digit) {
assert(Math.abs(expected - actual) < Math.pow(10, -(digit || 2)) / 2);
}
function compareRaw(actual, expected) {
assert.strictEqual(typeof actual, "object");
assertCloseTo(actual.value, expected.value, 3);
assert.strictEqual(actual.prefix, expected.prefix);
assert.strictEqual(actual.unit, expected.unit);
}
// ===================================================================
describe("humanFormat()", function () {
it("returns throws for an invalid number", function () {
[undefined, null, NaN, true, false, "a string", [], {}].forEach(function (
value
) {
assert.throws(function () {
humanFormat(value);
}, TypeError);
});
});
it("should convert number to human readable string", function () {
data.forEach(function (datum) {
assert.strictEqual(humanFormat(datum[0]), datum[1]);
compareRaw(humanFormat.raw(datum[0]), datum[2]);
});
});
it("can use custom units", function () {
assert.strictEqual(humanFormat(0, { unit: "g" }), "0 g");
});
it("can use custom separators", function () {
assert.strictEqual(humanFormat(1337, { separator: " - " }), "1.34 - k");
});
describe("with scale opts", function () {
it("should use this custom scale", function () {
var scale = humanFormat.Scale.create(",ki,Mi,Gi".split(","), 1024, 0);
assert.strictEqual(humanFormat(102400, { scale: scale }), "100 ki");
compareRaw(humanFormat.raw(102400, { scale: scale }), {
value: 100,
prefix: "ki",
});
});
it("throws of unknown scale", function () {
assert.throws(
function () {
humanFormat(102400, { scale: "foo" });
},
{ message: "missing scale" }
);
});
});
describe("with prefix opts", function () {
it("should use this prefix", function () {
assert.strictEqual(
humanFormat(100, { unit: "m", prefix: "k" }),
"0.1 km"
);
compareRaw(humanFormat.raw(100, { unit: "m", prefix: "k" }), {
value: 0.1,
prefix: "k",
});
});
it("throws of unknown prefix", function () {
assert.throws(
function () {
humanFormat(102400, { prefix: "foo" });
},
{ message: "invalid prefix" }
);
});
});
describe("with maxDecimals opts", function () {
it("should round to decimal digits", function () {
assert.strictEqual(
humanFormat(2358, { maxDecimals: 1, prefix: "k" }),
"2.4 k"
);
assert.strictEqual(humanFormat(111111111, { maxDecimals: 1 }), "111.1 M");
assert.strictEqual(humanFormat(1e9, { maxDecimals: 0 }), "1 G");
});
it("should change the unit if necessary", function () {
assert.strictEqual(humanFormat(999.9, { maxDecimals: 0 }), "1 k");
});
it("with auto ", function () {
assert.strictEqual(
humanFormat(1181.1111, { maxDecimals: "auto" }),
"1.2 k"
);
assert.strictEqual(
humanFormat(11911.1111, { maxDecimals: "auto" }),
"12 k"
);
assert.strictEqual(humanFormat(1.0, { maxDecimals: "auto" }), "1");
assert.strictEqual(humanFormat(-5.36, { maxDecimals: "auto" }), "-5.4");
assert.strictEqual(humanFormat(-15.36, { maxDecimals: "auto" }), "-15");
});
});
describe("with decimals opt", function () {
it("forces a fixed number of decimals", function () {
assert.strictEqual(humanFormat(1, { decimals: 2 }), "1.00");
assert.strictEqual(humanFormat(1.11111, { decimals: 2 }), "1.11");
});
it("takes precedence over maxDecimals", function () {
assert.strictEqual(
humanFormat(1.1111, { decimals: 2, maxDecimals: 0 }),
"1.11"
);
});
});
});
describe("humanFormat.parse()", function () {
var parse = humanFormat.parse;
it("should convert human readable string to number", function () {
data.forEach(function (datum) {
assertCloseTo(parse(datum[1]), datum[0], 3);
// compareRaw(parse.raw(datum[1]), datum[2])
});
});
it("handle as gracefully as possible incorrect case", function () {
assert.strictEqual(parse("1g"), 1e9);
});
});