Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove math.js depenadancies from the codebase #574

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 5 additions & 55 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
"bespoken-tools": "^2.6.7",
"dashbot": "^12.1.0",
"debug": "^4.3.4",
"expr-eval": "^2.0.2",
"firebase-admin": "^8.9.0",
"firebase-functions": "^3.3.0",
"glob": "^7.1.6",
"hirestime": "^4.0.0",
"lodash": "^4.17.21",
"mathjs": "^5.10.3",
"mustache": "^2.3.2",
"raven": "^2.6.4",
"uuid": "^9.0.0"
Expand Down
34 changes: 18 additions & 16 deletions functions/src/configurator/selectors/condition-selector.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
const math = require('mathjs');
const { debug, timer, warning } = require('../../utils/logger')('ia:selectors:condition-selector');
const equal = require('../../mathjs/equal');
const includes = require('../../mathjs/includes');
const Parser = require('expr-eval').Parser;
const parser = new Parser();
const util = require('util');

const { debug, timer, warning } = require('../../utils/logger')('ia:selectors:condition-selector');
parser.functions.equal = equal();
parser.functions.includes = includes();

function safieMathEval (condition, context) {
function safieEval (condition, context) {
try {
return math.eval(condition, context);
return parser.evaluate(condition, context);
} catch (error) {
debug('Get error from Math.js:', error && error.message);
debug('Get error from eval:', error && error.message);
return false;
}
}

/**
* Choose one which satisfies context.
* Or get default which doesn't have condition
* Choose one option that satisfies the context or get the default option that doesn't have a condition.
*
* @param options {Array.<Object>}
* @param context {Object}
* @returns {string}
* @returns {Object|null}
*/
function find (options, context) {
debug('select option by condition');
Expand All @@ -30,14 +34,12 @@ function find (options, context) {
const stopFindOptionTimer = timer.start('find option');
const option = options
.filter(({ condition }) => condition)
.find(
({ condition }) => {
const stopMathEvalTimer = timer.start('math.eval');
const res = safieMathEval(condition, context);
stopMathEvalTimer();
return res;
}
);
.find(({ condition }) => {
const stopEvalTimer = timer.start('eval');
const res = safieEval(condition, context);
stopEvalTimer();
return res;
});

stopFindOptionTimer();

Expand Down
10 changes: 4 additions & 6 deletions functions/src/mathjs/equal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const math = require('mathjs');

const { debug } = require('../utils/logger')('ia:mathjs:equal');

/**
* Support equal command:
*
Expand All @@ -10,7 +7,8 @@ const { debug } = require('../utils/logger')('ia:mathjs:equal');
*/
module.exports = () => {
debug('support');
math.import({
equal: (a, b) => a === b,
}, { override: true });
// Define the equal function
const equal = (a, b) => a === b;
// Export the equal function
return equal;
};
26 changes: 11 additions & 15 deletions functions/src/mathjs/includes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const math = require('mathjs');

const { debug } = require('../utils/logger')('ia:mathjs:equal');

const { debug } = require('../utils/logger')('ia:mathjs:includes');
/**
* Support includes command:
*
Expand All @@ -10,15 +7,14 @@ const { debug } = require('../utils/logger')('ia:mathjs:equal');
*/
module.exports = () => {
debug('support');
math.import({
includes: (a, b) => {
let array;
if (typeof a.toArray === 'function') {
array = a.toArray();
} else {
array = a;
}
return array.indexOf(b) >= 0;
},
}, { override: true });
const includes = (a, b) => {
let array;
if (typeof a.toArray === 'function') {
array = a.toArray();
} else {
array = a;
}
return array.indexOf(b) >= 0;
};
return includes;
};
19 changes: 8 additions & 11 deletions functions/src/mathjs/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/**
* All extensions for Math.js
*/

const builder = require('../extensions/builder');

const extensions = builder.build({ root: __dirname });
const includes = require('./includes');
const equal = require('./equal');
const Parser = require('expr-eval').Parser;
const parser = new Parser();

// FIXME: Need to improve this code to avoid duplications
module.exports = {
/**
* Get all extensions and apply patch
*/
parser, // Export the parser instance
patch: () => {
extensions.all().forEach(({ ext }) => ext());
parser.functions.equal = equal;
parser.functions.includes = includes;
},
};
10 changes: 6 additions & 4 deletions functions/src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const packageJSON = require('../package.json');
const appConfig = require('./config');
const env = require('./config/env');
const errors = require('./errors');
const mathjsExtensions = require('./mathjs');
const equal = require('../src/mathjs/equal');
const includes = require('../src/mathjs/includes');
const Parser = require('expr-eval').Parser;
const axiosProfile = require('./performance/axios');
const { debug, warning } = require('./utils/logger')('ia:axio:interceptions');

Expand All @@ -33,9 +35,9 @@ const errorHandler = (error) => {
module.exports = ({ platform }) => {
// turn-off escaping in MustacheJS
mustache.escape = v => v;

mathjsExtensions.patch();

const parser = new Parser();
parser.functions.equal = equal();
parser.functions.includes = includes();
const userAgent = mustache.render(
appConfig.request.userAgent,
Object.assign({}, packageJSON, { platform })
Expand Down
9 changes: 6 additions & 3 deletions functions/tests/actions/music-query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const rewire = require('rewire');
const sinon = require('sinon');

const action = rewire('../../src/actions/music-query');
const mathjsExtensions = require('../../src/mathjs');
const equal = require('../../src/mathjs/equal');
const includes = require('../../src/mathjs/includes');
const Parser = require('expr-eval').Parser;
const query = require('../../src/state/query');

const mockApp = require('../_utils/mocking/platforms/app');
const mockDialog = require('../_utils/mocking/dialog');
const mockAlbumsFeeder = require('../_utils/mocking/feeders/albums');
Expand Down Expand Up @@ -88,7 +89,9 @@ describe('actions / music query', () => {
describe('multiple slot schemes', () => {
beforeEach(() => {
handler = action.build(slotSchemeWithMultipleCases).handler;
mathjsExtensions.patch();
const parser = new Parser();
parser.functions.equal = equal();
parser.functions.includes = includes();
});

it('should get slot scheme without condition', () => {
Expand Down
53 changes: 27 additions & 26 deletions functions/tests/mathjs/patches.spec.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
const { expect } = require('chai');
const mathjsExtensions = require('../../src/mathjs');

const math = require('mathjs');
const equal = require('../../src/mathjs/equal');
const includes = require('../../src/mathjs/includes');
const Parser = require('expr-eval').Parser;
const parser = new Parser();

describe('mathjs', () => {
beforeEach(() => {
// TODO: for clear test we should have
// mathjsExtensions.unpatch();
mathjsExtensions.patch();
});

describe('includes', () => {
it('should be true when collection includes value', () => {
expect(math.eval('includes([1,2], 1)')).to.be.true;
expect(math.eval('includes(["a","b"], "a")')).to.be.true;
expect(math.eval('includes(options, "a")', {
options: ['a', 'b'],
})).to.be.true;
describe('equal', () => {
beforeEach(() => {
parser.functions.equal = equal();
parser.functions.includes = includes();
});

it('should be false when collection includes value', () => {
expect(math.eval('includes([1,2], 3)')).to.be.false;
expect(math.eval('includes(["a","b"], "c")')).to.be.false;
});
});
describe('includes', () => {
it('should be true when collection includes value', () => {
expect(parser.evaluate('includes([1,2], 1)')).to.be.true;
expect(parser.evaluate('includes(["a","b"], "a")')).to.be.true;
expect(parser.evaluate('includes(options, "a")', {
options: ['a', 'b'],
})).to.be.true;
});

describe('equal', () => {
it('should "abc" == "abc"', () => {
expect(math.eval('equal("abc", "abc")')).to.be.true;
it('should be false when collection includes value', () => {
expect(parser.evaluate('includes([1,2], 3)')).to.be.false;
expect(parser.evaluate('includes(["a","b"], "c")')).to.be.false;
});
});

it('should "abc" != "cba"', () => {
expect(math.eval('equal("abc", "cba")')).to.be.false;
describe('equal', () => {
it('should "abc" == "abc"', () => {
expect(parser.evaluate('equal("abc", "abc")')).to.be.true;
});
it('should "abc" != "cba"', () => {
expect(parser.evaluate('equal("abc", "cba")')).to.be.false;
});
});
});
});