-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewtonmath.js
112 lines (93 loc) · 3.7 KB
/
newtonmath.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
(function () {
'use strict';
// Newton API endpoints
const ENDPOINTS = ['simplify', 'factor', 'derive', 'integrate', 'zeroes',
'tangent', 'area', 'cos', 'sin', 'tan', 'arccos',
'arcsin', 'arctan', 'abs', 'log'];
let root = this,
prevNewtonMath = root.NewtonMath,
core = {},
NewtonMath = {};
// Dependencies
if (typeof root.XMLHttpRequest === 'undefined') {
core.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
} else {
core.XMLHttpRequest = root.XMLHttpRequest;
}
// Core functions
function createPromise (operation, expression) {
return new Promise(function (resolve, reject) {
let base = 'https://newton.now.sh/',
url = base + operation + '/' + encodeURIComponent(expression),
xhr = new core.XMLHttpRequest();
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.responseText);
} else {
reject({status: this.status, msg: xhr.statusText});
}
};
xhr.onerror = function () {
reject({status: this.status, msg: xhr.statusText});
};
xhr.open("GET", url);
xhr.send();
});
}
function sendRequest (operation, expression, callback) {
createPromise(operation, expression)
.then(response => callback(handleResponse(JSON.parse(response))))
.catch(error => printError('HTTP status', error.status, error));
}
function handleResponse (response) {
// Was the expression valid?
if (response.hasOwnProperty('error')) {
printError(response['error']);
} else {
let result = response['result'];
// Some of the strings returned can be parsed to integers or floats
try {
return JSON.parse(result);
} catch (e) {
// If the result is NaN, return NaN instead of a string
if (result.constructor == String && result.toLowerCase() == 'nan') {
return NaN;
} else {
return result;
}
}
}
}
function printError () {
console.error('NewtonMath error:', ...arguments);
}
// Expose the module
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = NewtonMath;
}
exports.NewtonMath = NewtonMath;
} else {
root.NewtonMath = NewtonMath;
}
// Allow module global to be renamed to remove conflicts with other vars
// of the same name (same concept as jQuery method of the same name)
NewtonMath.noConflict = function () {
root.NewtonMath = prevNewtonMath;
return NewtonMath;
};
// Instantiate basic endpoint functionality
ENDPOINTS.forEach(endpoint => {
core[endpoint] = (expression, callback) => sendRequest(endpoint, expression, callback);
NewtonMath[endpoint] = core[endpoint];
});
// Extend parameterised endpoint functionality
NewtonMath.log = (exp, a2, a3) =>
core.log(a3 ? a2 + '|' + exp : exp, a3 ? a3 : a2);
NewtonMath.tangent = (exp, a2, a3) =>
core.tangent(a3 ? a2 + '|' + exp : exp, a3 ? a3 : a2);
NewtonMath.area = (exp, a2, a3, a4) =>
core.area(a4 ? a2 + ':' + a3 + '|' + exp : exp, a4 ? a4 : a2);
// Expose NewtonMath object
return NewtonMath;
}).call(this);