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

Bring back temperature, percentage and ratio conversion #41

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion utils/coreConv.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ const convert = (mode, value, oldUnit, newUnit) => {
return convertRatio(value, oldUnit, newUnit);
case 'p':
return convertPercent(value, oldUnit, newUnit);
default:
''
}
};

Expand All @@ -110,7 +112,6 @@ const convertPercent = (percent, _, ofValue) => {
return (percent / 100) * ofValue;
};


/**
* This is a function to perform weight conversion
* @param {Number} value - Value on which conversion is to be performed
Expand Down
24 changes: 24 additions & 0 deletions utils/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const textForOperators = {
cross: '*'
};

/** @const {string} */
const temperatureUnits = coreConv.temperatureUnits.join('|');

/** @const {string} */
const currencyUnits = Object.keys(coreConv.currencyUnits).join('|');

Expand All @@ -38,9 +41,18 @@ const generateRegExpForUnits = units =>
'm'
);

/** @const {object} */
const temperatureRegExp = generateRegExpForUnits(temperatureUnits);

/** @const {object} */
const currencyRegExp = generateRegExpForUnits(currencyUnits);

/** @const {object} */
const percentageRegExp = new RegExp(/(\d+)\s*(%\s*of)\s*(\d+)/, 'm');

/** @const {object} */
const ratioRegExp = new RegExp(/(\d+\/\d+)\s*of\s*(\d+)/, 'm');

/**
* This function filters the given value with
* filter conditions : null, undefined, empty or to
Expand Down Expand Up @@ -85,10 +97,22 @@ const evaluate = exp => {
exp = exp.replace(operatorRegExp, textForOperators[operator]);
});

if (temperatureRegExp.test(exp)) {
return parseExp(exp, temperatureRegExp, 't');
}

if (currencyRegExp.test(exp.toUpperCase())) {
return parseExp(exp.toUpperCase(), currencyRegExp, 'c');
}

if (percentageRegExp.test(exp)) {
return parseExp(exp, percentageRegExp, 'p');
}

if (ratioRegExp.test(exp)) {
return parseExp(exp, ratioRegExp, 'r');
}

return mathJs.evaluate(exp);
};

Expand Down
38 changes: 38 additions & 0 deletions utils/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const assert = require('assert');
const { describe, it } = require('mocha');

const main = require('./main');

describe('evaluate', () => {
it('should correctly evaluate a comment', () => {
assert.strictEqual(main.evaluate('#500g to kg'), '');
});

it('should correctly evaluate weight conversion', () => {
assert.strictEqual(main.evaluate('500g to kg'), '0.5 kg');
});

it('should correctly evaluate length conversion', () => {
assert.strictEqual(main.evaluate('500cm to m'), '0.5 m');
});

it('should correctly evaluate temperature conversion', () => {
assert.strictEqual(main.evaluate('100c to f'), '212');
});

it('should correctly evaluate currency conversion', () => {
assert.strictEqual(main.evaluate('1 usd to eur'), '0.91');
});

it('should correctly evaluate a percentage', () => {
assert.strictEqual(main.evaluate('30% of 1'), '0.3');
});

it('should correctly evaluate a ratio', () => {
assert.strictEqual(main.evaluate('1/2 of 3'), '1.5');
});

it('returns empty string when the expression cannot be parsed', () => {
assert.strictEqual(main.evaluate('unparsable'), 'sadf');
});
});