forked from buxlabs/pure-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.js
261 lines (213 loc) · 4.12 KB
/
math.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
function abs (number) {
return Math.abs(number)
}
function acos (number) {
return Math.acos(number)
}
function acosh (number) {
return Math.acosh(number)
}
function asin (number) {
return Math.asin(number)
}
function asinh (number) {
return Math.asinh(number)
}
function atan (number) {
return Math.atan(number)
}
function atan2 (number1, number2) {
return Math.atan2(number1, number2)
}
function atanh (number) {
return Math.atanh(number)
}
function cbrt (number) {
return Math.cbrt(number)
}
function ceil (number) {
return Math.ceil(number)
}
function cos (number) {
return Math.cos(number)
}
function cosh (number) {
return Math.cosh(number)
}
function exp (number) {
return Math.exp(number)
}
function floor (number) {
return Math.floor(number)
}
function log (number) {
return Math.log(number)
}
function max (array) {
return Math.max.apply(this, array)
}
function min (array) {
return Math.min.apply(this, array)
}
function pow (number1, number2) {
return Math.pow(number1, number2)
}
function random () {
return Math.random()
}
function round (number) {
return Math.round(number)
}
function sin (number) {
return Math.sin(number)
}
function sinh (number) {
return Math.sinh(number)
}
function sqrt (number) {
return Math.sqrt(number)
}
function tan (number) {
return Math.tan(number)
}
function tanh (number) {
return Math.tanh(number)
}
function trunc (number) {
return Math.trunc(number)
}
function square (number) {
return number * number
}
function radians (number, precision = 2) {
return Number(((number * Math.PI) / 180).toFixed(precision))
}
function degrees (number, precision = 2) {
return Number(((180 / Math.PI) * number).toFixed(precision))
}
function factorial (number) {
return number < 2 ? 1 : number * factorial(number - 1)
}
function add (number1, number2) {
return number1 + number2
}
function subtract (number1, number2) {
return number1 - number2
}
function multiply (number1, number2) {
return number1 * number2
}
function divide (number1, number2) {
return number1 / number2
}
function modulo (number1, number2) {
return number1 % number2
}
function increment (number) {
return ++number
}
function decrement (number) {
return --number
}
function int (string) {
return parseInt(string)
}
function float (value) {
value = value.toString()
value = value.replace(/\s/g, '')
value = value.replace(/,/g, '.')
return parseFloat(value)
}
function clamp (number, min, max) {
if (number <= min) return min
if (number >= max) return max
return number
}
function percentage (number) {
return number * 100 + '%'
}
function fixed (number1, digits = 0) {
return number1.toFixed(digits)
}
function monetize (number, {
digits = 2,
separator = ',',
symbol = 'zł',
ending = true,
space = true,
hyphen = ' ',
size = 3
} = {}) {
let fixed = number.toFixed(digits)
let integer = fixed.substr(0, fixed.lastIndexOf('.'))
const decimal = fixed.substr(fixed.lastIndexOf('.')).replace('.', separator)
space = space ? ' ' : ''
if (integer.length > size) {
integer = integer.split('')
for (let i = integer.length - size - 1; i >= 0; i -= size) {
integer.splice(i + 1, 0, hyphen)
}
integer = integer.join('').trim()
}
fixed = integer + decimal
return ending ? fixed + space + symbol : symbol + space + fixed
}
function cube (number) {
return (number) ** 3
}
function feet (number, precision = 2, decimal = true) {
const result = Number(((number) * (1 / 12)).toFixed(precision))
return decimal ? result : `${result}′`
}
function inches (number, precision = 2, decimal = true) {
const result = Number((number * 12).toFixed(precision))
return decimal ? result : `${result}″`
}
module.exports = {
abs,
acos,
acosh,
asin,
asinh,
atan,
atan2,
atanh,
cbrt,
ceil,
cos,
cosh,
exp,
floor,
log,
max,
min,
pow,
random,
round,
sin,
sinh,
sqrt,
tan,
tanh,
trunc,
square,
factorial,
add,
subtract,
multiply,
divide,
modulo,
increment,
decrement,
int,
float,
clamp,
percentage,
fixed,
monetize,
cube,
feet,
inches,
radians,
degrees
}