forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.js
130 lines (115 loc) · 4.56 KB
/
number.js
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
'use strict';
require('mocha');
var assert = require('assert');
var hbs = require('handlebars').create();
var helpers = require('..');
helpers.number({ handlebars: hbs });
describe('number', function () {
describe('bytes', function () {
it('should format a number', function () {
assert.equal(hbs.compile('{{bytes num}}')({ num: 13661855 }), '13.66 MB');
assert.equal(hbs.compile('{{bytes num}}')({ num: 825399 }), '825.4 kB');
assert.equal(hbs.compile('{{bytes num}}')({ num: 1396 }), '1.4 kB');
assert.equal(hbs.compile('{{bytes num}}')({ num: 0 }), '0 B');
assert.equal(hbs.compile('{{bytes num}}')({ num: 1 }), '1 B');
assert.equal(hbs.compile('{{bytes num}}')({ num: 2 }), '2 B');
});
it('should return "0 B" when an invalid value is passed', function () {
assert.equal(hbs.compile('{{bytes num}}')({ num: {} }), '0 B');
});
it('should return string length when a string is passed', function () {
assert.equal(hbs.compile('{{bytes num}}')({ num: 'foo' }), '3 B');
assert.equal(hbs.compile('{{bytes num}}')({ num: 'foobar' }), '6 B');
});
});
describe('phoneNumber', function () {
it('Format a phone number.', function () {
var fn = hbs.compile('{{phoneNumber value}}');
assert.equal(fn({ value: '8005551212' }), '(800) 555-1212');
});
});
describe('toFixed', function () {
it('should return the value rounded to the nearest integer.', function () {
var fn = hbs.compile('{{toFixed value}}');
assert.equal(fn({ value: 5.53231 }), '6');
});
it('should return the value rounded exactly n digits after the decimal place.', function () {
var fn = hbs.compile('{{toFixed value 3}}');
assert.equal(fn({ value: 5.53231 }), '5.532');
});
});
describe('toPrecision', function () {
it('Returns the number in fixed-point or exponential notation rounded to n significant digits.', function () {
var fn = hbs.compile('{{toPrecision value}}');
assert.equal(fn({ value: 555.322 }), '6e+2');
});
it('should return the value rounded exactly n digits after the decimal place.', function () {
var fn = hbs.compile('{{toPrecision value 4}}');
assert.equal(fn({ value: 555.322 }), '555.3');
});
});
describe('toExponential', function () {
it('should return the number in fixed-point or exponential notation rounded to n significant digits.', function () {
var fn = hbs.compile('{{toExponential value}}');
assert.equal(fn({ value: 5 }), '5e+0');
});
it('should return the number in fixed-point or exponential notation rounded to exactly n significant digits.', function () {
var fn = hbs.compile('{{toExponential value 5}}');
assert.equal(fn({ value: 5 }), '5.00000e+0');
});
});
describe('toInt', function () {
it('should return an integer.', function () {
var fn = hbs.compile('{{toInt value}}');
assert.equal(fn({ value: '3cc' }), '3');
});
});
describe('toFloat', function () {
it('should return a floating point number.', function () {
var fn = hbs.compile('{{toFloat value}}');
assert.equal(fn({ value: '3.1cc' }), '3.1');
});
});
describe('addCommas', function () {
it('should add commas to a number.', function () {
var fn = hbs.compile('{{addCommas value}}');
assert.equal(fn({ value: 2222222 }), '2,222,222');
});
});
describe('toAbbr', function () {
it('should abbreviate the given number.', function () {
var fn = hbs.compile('{{toAbbr number}}');
assert.equal(fn({ number: 123456789 }), '123.46m');
});
it('should abbreviate a number with to the given decimal.', function () {
var fn = hbs.compile('{{toAbbr number 3}}');
assert.equal(fn({ number: 123456789 }), '123.457m');
});
it('should round up to the next increment', function () {
var fn = hbs.compile('{{toAbbr number}}');
assert.equal(fn({ number: 999 }), '1k');
});
it('should abbreviate a number based on a number and include decimal.', function () {
assert.equal(
hbs.compile('{{toAbbr number 0}}')({ number: 9999999 }),
'10m'
);
assert.equal(
hbs.compile('{{toAbbr number}}')({ number: 1000000000 }),
'1b'
);
assert.equal(
hbs.compile('{{toAbbr number}}')({ number: 1000000000000 }),
'1t'
);
assert.equal(
hbs.compile('{{toAbbr number}}')({ number: 1000000000000000 }),
'1q'
);
assert.equal(
hbs.compile('{{toAbbr number}}')({ number: 99393999393 }),
'99.39b'
);
});
});
});