1 | ;
|
2 |
|
3 | var utils = require('./utils');
|
4 |
|
5 | /**
|
6 | * Return the product of `a` plus `b`.
|
7 | *
|
8 | * ```js
|
9 | * <%= add(1, 2) %>
|
10 | * //=> '3'
|
11 | * ```
|
12 | *
|
13 | * @param {Number} `a`
|
14 | * @param {Number} `b`
|
15 | * @api public
|
16 | */
|
17 |
|
18 | exports.add = function add(a, b) {
|
19 | return a + b;
|
20 | };
|
21 |
|
22 | /**
|
23 | * Subtract `b` from `a`.
|
24 | *
|
25 | * ```js
|
26 | * <%= subtract(5, 2) %>
|
27 | * //=> '3'
|
28 | * ```
|
29 | *
|
30 | * @param {Number} `a`
|
31 | * @param {Number} `b`
|
32 | * @api public
|
33 | */
|
34 |
|
35 | exports.subtract = function subtract(a, b) {
|
36 | return a - b;
|
37 | };
|
38 |
|
39 | /**
|
40 | * Divide `a` (the numerator) by `b` (the divisor).
|
41 | *
|
42 | * ```js
|
43 | * <%= divide(10, 2) %>
|
44 | * //=> '5'
|
45 | * ```
|
46 | *
|
47 | * @param {Number} `a` the numerator.
|
48 | * @param {Number} `b` the divisor.
|
49 | * @return {Number} The quotient of `a` divided by `b`.
|
50 | * @api public
|
51 | */
|
52 |
|
53 | exports.divide = function divide(a, b) {
|
54 | return (+a) / (+b);
|
55 | };
|
56 |
|
57 | /**
|
58 | * Multiply `a` by `b`.
|
59 | *
|
60 | * ```js
|
61 | * <%= divide(10, 2) %>
|
62 | * //=> '5'
|
63 | * ```
|
64 | *
|
65 | * @param {Number} `a`
|
66 | * @param {Number} `b`
|
67 | * @return {Number} The product of `a` times `b`.
|
68 | * @api public
|
69 | */
|
70 |
|
71 | exports.multiply = function multiply(a, b) {
|
72 | return (+a) * (+b);
|
73 | };
|
74 |
|
75 | /**
|
76 | * Returns the largest integer less than or equal to the
|
77 | * given `number`.
|
78 | *
|
79 | * ```js
|
80 | * <%= floor(10.6) %>
|
81 | * //=> '10'
|
82 | * ```
|
83 | *
|
84 | * @param {Number} `number`
|
85 | * @return {Number}
|
86 | * @api public
|
87 | */
|
88 |
|
89 | exports.floor = function floor(n) {
|
90 | return Math.floor(+n);
|
91 | };
|
92 |
|
93 | /**
|
94 | * Returns the smallest integer greater than or equal to the
|
95 | * given `number`.
|
96 | *
|
97 | * ```js
|
98 | * <%= ceil(10.1) %>
|
99 | * //=> '11'
|
100 | * ```
|
101 | *
|
102 | * @param {Number} `number`
|
103 | * @return {Number}
|
104 | * @api public
|
105 | */
|
106 |
|
107 | exports.ceil = function ceil(n) {
|
108 | return Math.ceil(+n);
|
109 | };
|
110 |
|
111 | /**
|
112 | * Returns the value of the given `number` rounded to the
|
113 | * nearest integer.
|
114 | *
|
115 | * ```js
|
116 | * <%= round(10.1) %>
|
117 | * //=> '10'
|
118 | *
|
119 | * <%= round(10.5) %>
|
120 | * //=> '11'
|
121 | * ```
|
122 | *
|
123 | * @param {Number} `number`
|
124 | * @return {Number}
|
125 | * @api public
|
126 | */
|
127 |
|
128 | exports.round = function round(n) {
|
129 | return Math.round(+n);
|
130 | };
|
131 |
|
132 | /**
|
133 | * Returns the sum of all numbers in the given array.
|
134 | *
|
135 | * ```js
|
136 | * <%= sum([1, 2, 3, 4, 5]) %>
|
137 | * //=> '15'
|
138 | * ```
|
139 | *
|
140 | * @param {Number} `number`
|
141 | * @return {Number}
|
142 | * @api public
|
143 | */
|
144 |
|
145 | exports.sum = function sum() {
|
146 | var args = utils.flatten([].concat.apply([], arguments));
|
147 | var len = args.length;
|
148 | var idx = -1;
|
149 | var num = 0;
|
150 |
|
151 | while (++idx < len) {
|
152 | if (!utils.isNumber(args[idx])) {
|
153 | continue;
|
154 | }
|
155 | num += (+args[idx]);
|
156 | }
|
157 | return num;
|
158 | };
|