Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "async-nmbr", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
const fetch = require('node-fetch'); | ||
const API_URL = 'https://nmbr.dev'; | ||
const OPERATORS = { | ||
'+': 'add', | ||
'-': 'subtract', | ||
'*': 'multiply', | ||
'/': 'divide', | ||
'%': 'remainder', | ||
'**': 'exponentiation' | ||
}; | ||
const createCallPromise = (operator, a, b) => { | ||
return new Promise((resolve, reject) => { | ||
fetch(`https://nmbr.dev/${operator}/${a}/${b}`) | ||
fetch(`${API_URL}/${operator}/${a}/${b}`) | ||
.then(res => res.json()) | ||
@@ -10,3 +20,4 @@ .then(({ result, error }) => { | ||
resolve(result); | ||
}); | ||
}) | ||
.catch(err => reject(err)); | ||
}); | ||
@@ -16,21 +27,8 @@ }; | ||
module.exports = Nmbr => { | ||
Nmbr.prototype[Symbol.for('+')] = function(b) { | ||
const a = this.valueOf(); | ||
return createCallPromise('add', a, b); | ||
}; | ||
Nmbr.prototype[Symbol.for('-')] = function(b) { | ||
const a = this.valueOf(); | ||
return createCallPromise('subtract', a, b); | ||
}; | ||
Nmbr.prototype[Symbol.for('*')] = function(b) { | ||
const a = this.valueOf(); | ||
return createCallPromise('multiply', a, b); | ||
}; | ||
Nmbr.prototype[Symbol.for('/')] = function(b) { | ||
const a = this.valueOf(); | ||
return createCallPromise('divide', a, b); | ||
}; | ||
for (const [key, value] of Object.entries(OPERATORS)) { | ||
Nmbr.prototype[Symbol.for(key)] = function(b) { | ||
const a = this.valueOf(); | ||
return createCallPromise(value, a, b); | ||
}; | ||
} | ||
}; |
require('./')(Number); | ||
(async () => { | ||
console.log('2 + 1 ='); | ||
console.time('async addition'); | ||
console.log(await (2 + 1)); | ||
console.timeEnd('async addition'); | ||
const TESTS = [ | ||
{ op: '+', a: 2, b: 1, exp: 3 }, | ||
{ op: '-', a: 2, b: 2, exp: 0 }, | ||
{ op: '*', a: 2, b: 2, exp: 4 }, | ||
{ op: '/', a: 4, b: 2, exp: 2 }, | ||
{ op: '**', a: 2, b: 2, exp: 4 }, | ||
{ op: '%', a: 5, b: 2, exp: 1 } | ||
]; | ||
console.log('\n'); | ||
const OPERATORS = { | ||
'+': async (a, b) => await (a + b), | ||
'-': async (a, b) => await (a - b), | ||
'*': async (a, b) => await (a * b), | ||
'/': async (a, b) => await (a / b), | ||
'**': async (a, b) => await (a ** b), | ||
'%': async (a, b) => await (a % b) | ||
}; | ||
console.log('2 - 2 ='); | ||
console.time('async substraction'); | ||
console.log(await (2 - 2)); | ||
console.timeEnd('async substraction'); | ||
(async () => { | ||
let errors = []; | ||
console.log('\n'); | ||
for (const test of TESTS) { | ||
const { op, a, b, exp } = test; | ||
const timeKey = `async ${op}`; | ||
console.log(`${a} ${op} ${b} =`); | ||
console.time(timeKey); | ||
const res = await OPERATORS[op](a, b); | ||
console.timeEnd(timeKey); | ||
if (res === exp) { | ||
console.log(`Success! Result of operator '${op}' matched expected value '${exp}'`); | ||
} else { | ||
console.error(`Error! Result '${res}' of operator '${op}' did not match expected value '${exp}'`); | ||
errors.push(op); | ||
} | ||
} | ||
console.log('2 * 2 ='); | ||
console.time('async multiplication'); | ||
console.log(await (2 * 2)); | ||
console.timeEnd('async multiplication'); | ||
console.log('\n'); | ||
console.log('4 / 2 ='); | ||
console.time('async division'); | ||
console.log(await (4 / 2)); | ||
console.timeEnd('async division'); | ||
console.log('\n'); | ||
console.time('async Pi multiplication'); | ||
console.log(await (Math.PI / 3)); | ||
console.timeEnd('async Pi multiplication'); | ||
if (errors.length === 0) { | ||
process.exit(0); | ||
} else { | ||
console.log(`Errored operators: ${JSON.stringify(errors)}`); | ||
process.exit(1); | ||
} | ||
})(); |
4012
69