-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnice-number.test.js
66 lines (55 loc) · 2.13 KB
/
nice-number.test.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
const { niceNumber } = require(`./nice-number`)
describe(`nice-number`, () => {
it(`should return 'null' for null`, () => {
expect(niceNumber(null)).toBe(`null`)
})
it(`should return 'null' for undefined`, () => {
expect(niceNumber(undefined)).toBe(`null`)
})
it(`should use exponential notation for values < 0.001`, () => {
expect(niceNumber(0.00091)).toBe(`9.1e-4`)
})
it(`should use 0.xxx notation for values < 1`, () => {
expect(niceNumber(0.001)).toBe(`0.001`)
expect(niceNumber(0.123)).toBe(`0.123`)
expect(niceNumber(0.700)).toBe(`0.700`)
})
it(`should use x.x notation for values < 10`, () => {
expect(niceNumber(5.437)).toBe(`5.4`)
expect(niceNumber(7)).toBe(`7.0`)
expect(niceNumber(9.9)).toBe(`9.9`)
})
it(`should use integer notation for values < 1,000`, () => {
expect(niceNumber(10)).toBe(`10`)
expect(niceNumber(100)).toBe(`100`)
expect(niceNumber(999)).toBe(`999`)
})
it(`should use K notation for values < 1,000,000`, () => {
expect(niceNumber(10000)).toBe(`10K`)
expect(niceNumber(100000)).toBe(`100K`)
expect(niceNumber(999000)).toBe(`999K`)
})
it(`should use M notation for values < 1,000,000,000`, () => {
expect(niceNumber(10000000)).toBe(`10M`)
expect(niceNumber(100000000)).toBe(`100M`)
expect(niceNumber(999000000)).toBe(`999M`)
})
it(`should use G notation for values < 1,000,000,000,000`, () => {
expect(niceNumber(10000000000)).toBe(`10G`)
expect(niceNumber(100000000000)).toBe(`100G`)
expect(niceNumber(999000000000)).toBe(`999G`)
})
it(`should use exponential notation for values >= 1,000,000,000,000`, () => {
expect(niceNumber(1000000000000)).toBe(`1.0e+12`)
})
it(`should handle negative numbers properly`, () => {
expect(niceNumber(-0.00091)).toBe(`-9.1e-4`)
expect(niceNumber(-0.123)).toBe(`-0.123`)
expect(niceNumber(-7)).toBe(`-7.0`)
expect(niceNumber(-100)).toBe(`-100`)
expect(niceNumber(-100000)).toBe(`-100K`)
expect(niceNumber(-100000000)).toBe(`-100M`)
expect(niceNumber(-100000000000)).toBe(`-100G`)
expect(niceNumber(-1000000000000)).toBe(`-1.0e+12`)
})
})