Skip to content

Commit

Permalink
Merge pull request #88 from haensl/77
Browse files Browse the repository at this point in the history
Add a profile for comparison statements.
  • Loading branch information
haensl authored Oct 22, 2019
2 parents 9eacc61 + 995ed7f commit 8a2cd8d
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 114 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2.1.0
* [#77: Add a profile for comparison statements.](https://github.com/haensl/js-profiler/issues/77)

### 2.0.0
* [#86: Add better descriptions to profiles and profile tests.](https://github.com/haensl/js-profiler/issues/86)
* [#69: Fix outdated docs.](https://github.com/haensl/js-profiler/issues/69)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const expect = require('chai').expect;
const comparisonStatements = require('./');

describe('Comparison statements', () => {
let result;
let data;

describe('data: 0', () => {
beforeEach(() => {
data = 0;
});

comparisonStatements.functions.forEach((fn) => {
describe(`${fn.description}`, () => {
beforeEach(() => {
result = fn.f(data);
});

it('returns 0', () => {
expect(result).to.equal(0);
});
});
});
});

describe('data: 2', () => {
beforeEach(() => {
data = 2;
});

comparisonStatements.functions.forEach((fn) => {
describe(`${fn.description}`, () => {
beforeEach(() => {
result = fn.f(data);
});

it('returns 0', () => {
expect(result).to.equal(1);
});
});
});
});
});
103 changes: 103 additions & 0 deletions lib/profiles/comparison-statements/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const unique = require('../../support/array').unique;

const compIf = {
description: 'if statement',
codeSample: 'if (d > 0) { return d / 2; }',
keywords: [
'if',
'comparison',
'statement',
'else',
'else if',
'branching',
'control structure',
'control flow',
'flow'
],
f: (d) => {
if (d === 0) {
return d;
} else if (d > 0) {
return d / 2;
} else {
return 0;
}
}
};

const compSwitch = {
description: 'switch statement',
codeSample: 'switch (d) { case 0: return d; default: return d / 2; }',
keywords: [
'switch',
'comparison',
'statement',
'break',
'branching',
'control structure',
'control flow',
'flow'
],
f: (d) => {
switch (d) {
case 0:
return d;
default:
return d / 2;
}
}
};

const compTernary = {
description: 'ternary expression',
keywords: [
'ternary',
'expression',
'comparison',
'statement',
'branching',
'control structure',
'control flow',
'flow'
],
codeSample: 'd > 0 ? d / 2 : d',
f: (d) => d > 0 ? d / 2 : d
};

const compAnd = {
description: 'and-or, && ||',
keywords: [
'and',
'or',
'comparison',
'statement',
'expression',
'branching',
'control structure',
'control flow',
'flow'
],
codeSample: '(d > 0 && d / 2) || d',
f: (d) => (d > 0 && d / 2) || d
};

const functions = [
compIf,
compSwitch,
compTernary,
compAnd
];

module.exports = {
name: 'comparison statements',
description: {
long: 'Comparison statements: conditionally branching in a function based on simple comparisons.',
short: 'Comparison statements: if vs. switch vs. ternary vs. logical.'
},
functions,
keywords: unique(
functions.map((fn) => fn.keywords)
.reduce((keywords, fnKeywords) => [...keywords, ...fnKeywords])
).sort(),
testDataType: 'number'
};
Loading

0 comments on commit 8a2cd8d

Please sign in to comment.