@@ -2,74 +2,117 @@ import { Num } from '../number';
2
2
3
3
jest . mock ( '../../config/service' ) ;
4
4
5
- describe ( 'Number Helpers ' , ( ) => {
5
+ describe ( 'Numbers Helper ' , ( ) => {
6
6
beforeEach ( async ( ) => { } ) ;
7
7
8
- it ( 'abbreviates the passed number to abbreviated format ' , ( ) => {
9
- const num = 1000 ;
10
- expect ( Num . abbreviate ( num ) ) . toStrictEqual ( '1K ') ;
8
+ it ( 'should abbrevate with en locale to 1 decimal point precision ' , ( ) => {
9
+ const number = 12345 ;
10
+ expect ( Num . abbreviate ( number ) ) . toBe ( '12.3K ') ;
11
11
} ) ;
12
12
13
- it ( 'abbreviates the passed number to a abbreviated format, but with precision' , ( ) => {
14
- const num = 1200 ;
15
- expect ( Num . abbreviate ( num , { precision : 2 } ) ) . toStrictEqual ( '1.2K' ) ;
13
+ it ( 'should abbrevate with en locale to 3 decimal precision' , ( ) => {
14
+ const number = 12345 ;
15
+ const options = { precision : 3 , locale : 'en' } ;
16
+ expect ( Num . abbreviate ( number , options ) ) . toBe ( '12.345K' ) ;
16
17
} ) ;
17
18
18
- it ( 'abbreviates the passed number to a abbreviated format, but with different locale' , ( ) => {
19
- const num = 1200 ;
20
- expect ( Num . abbreviate ( num , { locale : 'hi' } ) ) . toStrictEqual ( '1.2 हज़ार' ) ;
19
+ it ( 'should abbrevate with en-IN locale to 3 decimal precision' , ( ) => {
20
+ const number = 12345 ;
21
+ const options = { precision : 3 , locale : 'en-IN' } ;
22
+ expect ( Num . abbreviate ( number , options ) ) . toBe ( '12.345K' ) ;
21
23
} ) ;
22
24
23
- it ( 'should conver the number to indian currency format' , ( ) => {
24
- const num = 12300 ;
25
- expect ( Num . currency ( num , { currency : 'INR' } ) ) . toStrictEqual ( '₹12,300.00' ) ;
25
+ it ( 'should return number itself' , ( ) => {
26
+ const number = 12345 ;
27
+ const min = 12300 ;
28
+ const max = 12400 ;
29
+ expect ( Num . clamp ( number , min , max ) ) . toBe ( number ) ;
26
30
} ) ;
27
31
28
- it ( 'should conver the number to dollar currency format' , ( ) => {
29
- const num = 12300 ;
30
- expect ( Num . currency ( num , { currency : 'USD' } ) ) . toStrictEqual ( '$12,300.00' ) ;
32
+ it ( 'should return minimum number' , ( ) => {
33
+ const number = 12345 ;
34
+ const min = 12350 ;
35
+ const max = 12400 ;
36
+ expect ( Num . clamp ( number , min , max ) ) . toBe ( min ) ;
31
37
} ) ;
32
38
33
- it ( 'should convert the number to file size representation' , ( ) => {
34
- const samples = { 1000 : '1KB' , 1024 : '1KB' , [ 1024 * 1024 * 1.5 ] : '1.57MB' } ;
35
- expect ( Num . fileSize ( 1000 ) ) . toStrictEqual ( '1KB' ) ;
36
- expect ( Num . fileSize ( 1024 ) ) . toStrictEqual ( '1KB' ) ;
37
- expect ( Num . fileSize ( 1024 * 1024 * 1.5 , { precision : 2 } ) ) . toStrictEqual (
38
- '1.57MB' ,
39
- ) ;
39
+ it ( 'should return maximum number' , ( ) => {
40
+ const number = 12345 ;
41
+ const min = 12300 ;
42
+ const max = 12340 ;
43
+ expect ( Num . clamp ( number , min , max ) ) . toBe ( max ) ;
40
44
} ) ;
41
45
42
- it ( 'should convert the number to human readable format' , ( ) => {
43
- expect ( Num . forHumans ( 100 ) ) . toStrictEqual ( '100' ) ;
44
- expect ( Num . forHumans ( 1200 ) ) . toStrictEqual ( '1.2 thousand' ) ;
46
+ it ( 'should return number in currency style in INR' , ( ) => {
47
+ const number = 12345 ;
48
+ const options = { currency : 'INR' , locale : 'en' } ;
49
+ expect ( Num . currency ( number , options ) ) . toBe ( '₹12,345.00' ) ;
45
50
} ) ;
46
51
47
- it ( 'should convert the number to human readable format, with precision' , ( ) => {
48
- expect ( Num . forHumans ( 1230 , { precision : 2 } ) ) . toStrictEqual (
49
- '1.23 thousand' ,
50
- ) ;
52
+ it ( 'should return number in currency style in USD' , ( ) => {
53
+ const number = 12345 ;
54
+ const options = { currency : 'USD' , locale : 'en' } ;
55
+ expect ( Num . currency ( number , options ) ) . toBe ( '$12,345.00' ) ;
56
+ } ) ;
57
+
58
+ it ( 'should return number in file size format' , ( ) => {
59
+ const number = 12345 ;
60
+ expect ( Num . fileSize ( number ) ) . toBe ( '12.3KB' ) ;
61
+ } ) ;
62
+
63
+ it ( 'should return number in file size format with precision 3' , ( ) => {
64
+ const number = 123456789 ;
65
+ const options = { precision : 3 } ;
66
+ expect ( Num . fileSize ( number , options ) ) . toBe ( '123.457MB' ) ;
67
+ } ) ;
68
+
69
+ it ( 'should return number in humanize form with precision 1' , ( ) => {
70
+ const number = 12345 ;
71
+ const options = { precision : 1 , locale : 'en' } ;
72
+ expect ( Num . forHumans ( number , options ) ) . toBe ( '12.3 thousand' ) ;
51
73
} ) ;
52
74
53
- it ( 'should convert the number to human readable format, with locale' , ( ) => {
54
- expect ( Num . forHumans ( 1200 , { locale : 'fr' } ) ) . toStrictEqual ( '1,2 millier' ) ;
75
+ it ( 'should return number in humanize form with precision 3' , ( ) => {
76
+ const number = 123456789 ;
77
+ const options = { precision : 3 , locale : 'en' } ;
78
+ expect ( Num . forHumans ( number , options ) ) . toBe ( '123.457 million' ) ;
55
79
} ) ;
56
80
57
- it ( 'should format the number to the given locale string ' , ( ) => {
58
- expect ( Num . format ( 1000 ) ) . toStrictEqual ( '1,000' ) ;
59
- expect ( Num . format ( 1000 , { locale : 'fr ' } ) ) . toStrictEqual ( '1 000' ) ;
60
- expect ( Num . format ( 1200 ) ) . toStrictEqual ( '1,200 ') ;
81
+ it ( 'should return number in number system format with precision 1(default) ' , ( ) => {
82
+ const number = 12345.78 ;
83
+ const options = { locale : 'en ' } ;
84
+ expect ( Num . format ( number , options ) ) . toBe ( '12,345.8 ') ;
61
85
} ) ;
62
86
63
- it ( 'converts the given number to the ordinal format' , ( ) => {
64
- expect ( Num . ordinal ( 1 ) ) . toStrictEqual ( '1st' ) ;
65
- expect ( Num . ordinal ( 2 ) ) . toStrictEqual ( '2nd' ) ;
66
- expect ( Num . ordinal ( 3 ) ) . toStrictEqual ( '3rd' ) ;
67
- expect ( Num . ordinal ( 20 ) ) . toStrictEqual ( '20th' ) ;
87
+ it ( 'should return number in percents when passed as decimal portion with precision 1(default)' , ( ) => {
88
+ const number = 17.8 ;
89
+ const options = { locale : 'en' } ;
90
+ expect ( Num . percentage ( number , options ) ) . toBe ( '17.8%' ) ;
68
91
} ) ;
69
92
70
- it ( 'converts the number to a percentage format with support for precision and locale config' , ( ) => {
71
- expect ( Num . percentage ( 10 ) ) . toStrictEqual ( '10.0%' ) ;
72
- expect ( Num . percentage ( 10 , { locale : 'fr' } ) ) . toStrictEqual ( '10,0 %' ) ;
73
- expect ( Num . percentage ( 10.123 , { precision : 2 } ) ) . toStrictEqual ( '10.12%' ) ;
93
+ it ( 'should return number in ordinal format' , ( ) => {
94
+ const number = 231 ;
95
+ expect ( Num . ordinal ( number ) ) . toBe ( '231st' ) ;
96
+ } ) ;
97
+
98
+ it ( 'should return number in ordinal format' , ( ) => {
99
+ const number = 12345 ;
100
+ expect ( Num . ordinal ( number ) ) . toBe ( '12345th' ) ;
101
+ } ) ;
102
+
103
+ it ( 'should return number in english words' , ( ) => {
104
+ const number = 12345 ;
105
+ expect ( Num . spell ( number ) ) . toBe (
106
+ 'twelve thousand three hundred and forty five only' ,
107
+ ) ;
108
+ } ) ;
109
+
110
+ it ( 'should return false' , ( ) => {
111
+ const number = '12345' ;
112
+ expect ( Num . isInteger ( number ) ) . toBe ( false ) ;
113
+ } ) ;
114
+ it ( 'should return true' , ( ) => {
115
+ const number = 12345 ;
116
+ expect ( Num . isInteger ( number ) ) . toBe ( true ) ;
74
117
} ) ;
75
118
} ) ;
0 commit comments