File tree 7 files changed +71
-0
lines changed
7 files changed +71
-0
lines changed Original file line number Diff line number Diff line change
1
+ * text eol =lf
Original file line number Diff line number Diff line change
1
+ . *
2
+ * .log
3
+ spec.js
Original file line number Diff line number Diff line change
1
+ package-lock = false
2
+ access = public
Original file line number Diff line number Diff line change
1
+ # percent [ ![ ] ( https://img.shields.io/npm/v/@does/percent.svg )] ( https://www.npmjs.com/package/@does/percent ) [ ![ ] ( https://img.shields.io/badge/source--000000.svg?logo=github&style=social )] ( https://github.com/omrilotan/mono/tree/master/packages/percent )
2
+
3
+ ## A round percentage of a number from the whole
4
+
5
+ ```
6
+ const percent = require('@does/percent');
7
+
8
+ // percent(part, whole):
9
+
10
+ percent(150, 200) // 75
11
+ percent(200, 150) // 133
12
+ ```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Calculate one part's percentage of the whole
3
+ * @param {Number } part
4
+ * @param {Number } whole
5
+ * @return {Number }
6
+ */
7
+ module . exports = ( part , whole ) => {
8
+ if ( part === 0 || whole === 0 ) {
9
+ return part === whole ? 0 : part > whole ? 100 : 0 ;
10
+ }
11
+
12
+ return Math . round ( ( part / whole ) * 100 ) ;
13
+ } ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " @does/percent" ,
3
+ "version" : " 0.0.0" ,
4
+ "description" : " Calculate one part's percentage of the whole" ,
5
+ "keywords" : [
6
+ " percent" ,
7
+ " percentage" ,
8
+ " calculate"
9
+ ],
10
+ "author" : " omrilotan" ,
11
+ "license" : " MIT" ,
12
+ "repository" : {
13
+ "type" : " git" ,
14
+ "url" : " git+https://github.com/omrilotan/mono.git"
15
+ },
16
+ "homepage" : " https://omrilotan.com/mono/percent/"
17
+ }
Original file line number Diff line number Diff line change
1
+ const percent = require ( '.' ) ;
2
+
3
+ describe ( 'percent' , ( ) => {
4
+ it ( 'Should calculate the percentage' , ( ) => {
5
+ expect ( percent ( 150 , 200 ) ) . to . equal ( 75 ) ;
6
+ expect ( percent ( 250 , 200 ) ) . to . equal ( 125 ) ;
7
+ } ) ;
8
+ it ( 'Should return 0 when part is 0' , ( ) => {
9
+ expect ( percent ( 0 , 200 ) ) . to . equal ( 0 ) ;
10
+ } ) ;
11
+ it ( 'Should return 1000 when whole is 0' , ( ) => {
12
+ expect ( percent ( 200 , 0 ) ) . to . equal ( 100 ) ;
13
+ } ) ;
14
+ it ( 'Should return 0 when both are 0' , ( ) => {
15
+ expect ( percent ( 0 , 0 ) ) . to . equal ( 0 ) ;
16
+ } ) ;
17
+ it ( 'Should return a whole number' , ( ) => {
18
+ expect ( percent ( 201 , 200 ) ) . to . equal ( 100 ) ;
19
+ } ) ;
20
+ it ( 'Should round correctly' , ( ) => {
21
+ expect ( percent ( 234183 , 234194 ) ) . to . equal ( 100 ) ;
22
+ } ) ;
23
+ } ) ;
You canβt perform that action at this time.
0 commit comments