forked from elm/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basics.js
118 lines (103 loc) · 2.45 KB
/
Basics.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
Elm.Native.Basics = {};
Elm.Native.Basics.make = function(elm) {
elm.Native = elm.Native || {};
elm.Native.Basics = elm.Native.Basics || {};
if (elm.Native.Basics.values) return elm.Native.Basics.values;
var Utils = Elm.Native.Utils.make(elm);
function div(a, b) {
return (a/b)|0;
}
function rem(a, b) {
return a % b;
}
function mod(a, b) {
if (b === 0) {
throw new Error("Cannot perform mod 0. Division by zero error.");
}
var r = a % b;
var m = a === 0 ? 0 : (b > 0 ? (a >= 0 ? r : r+b) : -mod(-a,-b));
return m === b ? 0 : m;
}
function logBase(base, n) {
return Math.log(n) / Math.log(base);
}
function negate(n) {
return -n;
}
function abs(n) {
return n < 0 ? -n : n;
}
function min(a, b) {
return Utils.cmp(a,b) < 0 ? a : b;
}
function max(a, b) {
return Utils.cmp(a,b) > 0 ? a : b;
}
function clamp(lo, hi, n) {
return Utils.cmp(n,lo) < 0 ? lo : Utils.cmp(n,hi) > 0 ? hi : n;
}
function xor(a, b) {
return a !== b;
}
function not(b) {
return !b;
}
function isInfinite(n) {
return n === Infinity || n === -Infinity
}
function truncate(n) {
return n|0;
}
function degrees(d) {
return d * Math.PI / 180;
}
function turns(t) {
return 2 * Math.PI * t;
}
function fromPolar(point) {
var r = point._0;
var t = point._1;
return Utils.Tuple2(r * Math.cos(t), r * Math.sin(t));
}
function toPolar(point) {
var x = point._0;
var y = point._1;
return Utils.Tuple2(Math.sqrt(x * x + y * y), Math.atan2(y,x));
}
var basics = {
div: F2(div),
rem: F2(rem),
mod: F2(mod),
pi: Math.PI,
e: Math.E,
cos: Math.cos,
sin: Math.sin,
tan: Math.tan,
acos: Math.acos,
asin: Math.asin,
atan: Math.atan,
atan2: F2(Math.atan2),
degrees: degrees,
turns: turns,
fromPolar: fromPolar,
toPolar: toPolar,
sqrt: Math.sqrt,
logBase: F2(logBase),
negate: negate,
abs: abs,
min: F2(min),
max: F2(max),
clamp: F3(clamp),
compare: Utils.compare,
xor: F2(xor),
not: not,
truncate: truncate,
ceiling: Math.ceil,
floor: Math.floor,
round: Math.round,
toFloat: function(x) { return x; },
isNaN: isNaN,
isInfinite: isInfinite
};
return elm.Native.Basics.values = basics;
};