Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
simplify arguments signature, use options object instead of named arg…
Browse files Browse the repository at this point in the history
…uments, resolves #4
  • Loading branch information
adamrenklint committed Mar 23, 2015
1 parent beddf1e commit 31b8f7c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 42 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ $ npm install --save dilla-expressions

```javascript
var expr = require('dilla-expressions');
var barsPerLoop = 2;
var beatsPerBar = 4;

var notes = [
['*.even.01']
];

var expanded = expr(notes, barsPerLoop, beatsPerBar);
var expanded = expr(notes, {
'barsPerLoop': 2,
'beatsPerBar': 4
});

expect(expanded.length).to.equal(4);
expect(expanded[0][0]).to.equal('1.2.01');
Expand All @@ -48,7 +49,9 @@ expect(expanded[3][0]).to.equal('2.4.01');
- Initial release with wildcard (```*```), ```even``` and ```odd``` expression operators
- **1.0.1**
- CHANGED: *events* are now called *notes* [dilla/8](https://github.com/adamrenklint/dilla/issues/8)
- **1.1.0**
- CHANGED: expects options object instead of barsPerLoop and beatsPerBar separately [#4](https://github.com/adamrenklint/dilla-expressions/issues/4)

## License

MIT © [Adam Renklint](http://adamrenklint.com)
MIT © [Adam Renklint](http://adamrenklint.com)
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ function makeExpressionFunction (expression) {
};
}

function expressions (notes, barsPerLoop, beatsPerBar) {
function expressions (notes, options) {

if (!notes) throw new Error('Invalid "notes" array');
if (!barsPerLoop || typeof barsPerLoop !== 'number') throw new Error('Invalid "barsPerLoop" argument');
if (!beatsPerBar || typeof beatsPerBar !== 'number') throw new Error('Invalid "barsPerLoop" argument');
if (!options || typeof options !== 'object') throw new Error('Invalid "options" object');
if (typeof options.beatsPerBar !== 'number' || options.beatsPerBar < 0) throw new Error('Invalid options: beatsPerBar is not a valid number');
if (typeof options.barsPerLoop !== 'number' || options.barsPerLoop < 0) throw new Error('Invalid options: barsPerLoop is not a valid number');

var possibles = getPossiblePositions(barsPerLoop, beatsPerBar);
var possibles = getPossiblePositions(options.barsPerLoop, options.beatsPerBar);
var all = [];

notes.forEach(function (event) {

var position = event[0];
if (isPlainPosition(position)) return all.push(event);

Expand All @@ -74,4 +75,4 @@ function expressions (notes, barsPerLoop, beatsPerBar) {
});
}

module.exports = expressions;
module.exports = expressions;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dilla-expressions",
"version": "1.0.1",
"version": "1.1.0",
"description": "expand expressions for repeating notes into flat positions",
"main": "index.js",
"scripts": {
Expand Down
104 changes: 74 additions & 30 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,57 @@ describe('when events is not defined', function () {
})
});

describe('when barsPerLoop is not defined', function () {

describe('when options is not defined', function () {
it('should throw an error', function () {
expect(function () {
expr([
['1.1.01', 1, {}],
['2.4.01', 2, {}]
]);
}).to.throw(Error);
})
});
});

describe('when beatsPerBar is not defined', function () {
it('should throw an error', function () {
expect(function () {
expr([
['1.1.01', 1, {}],
['2.4.01', 2, {}]
], 2);
}).to.throw('')
})
describe('when options is an object', function () {
describe('when options.barsPerLoop is not a number', function () {
it('should throw an error', function () {
expect(function () {
expr([
['1.1.01', 1, {}],
['2.4.01', 2, {}]
], {
'beatsPerBar': 4
});
}).to.throw(Error);
});
});
describe('when options.beatsPerBar is not a number', function () {
it('should throw an error', function () {
expect(function () {
expr([
['1.1.01', 1, {}],
['2.4.01', 2, {}]
], {
'barsPerLoop': 2,
'beatsPerBar': 'foo'
});
}).to.throw(Error);
});
});
});

var standardOptions = {
'beatsPerBar': 4,
'barsPerLoop': 2
};

describe('when no expression is used', function () {
it('should not change normal positions', function () {
var result = expr([
['1.1.01', 1, {}],
['2.4.01', 2, {}]
], 2, 4);
], standardOptions);
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('2.4.01');
Expand All @@ -49,7 +72,7 @@ describe('when using wildcard expression', function () {
it('should repeat any bar', function () {
var result = expr([
['*.1.01', 1, { 'freq': 440 }]
], 2, 4);
], standardOptions);
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('1.1.01');
expect(result[0][1]).to.equal(1);
Expand All @@ -62,7 +85,7 @@ describe('when using wildcard expression', function () {
it('should repeat any beat', function () {
var result = expr([
['1.*.01']
], 2, 4);
], standardOptions);
expect(result.length).to.equal(4);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.2.01');
Expand All @@ -73,7 +96,10 @@ describe('when using wildcard expression', function () {
it('should repeat any bar and beat', function () {
var result = expr([
['*.*.01']
], 4, 4);
], {
'beatsPerBar': 4,
'barsPerLoop': 4
});
expect(result.length).to.equal(16);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.2.01');
Expand All @@ -96,7 +122,10 @@ describe('when using wildcard expression', function () {
it('should repeat any tick', function () {
var result = expr([
['1.1.*']
], 1, 1);
], {
'beatsPerBar': 1,
'barsPerLoop': 1
});
expect(result.length).to.equal(96);
expect(result[0][0]).to.equal('1.1.01');
expect(result[95][0]).to.equal('1.1.96');
Expand All @@ -105,7 +134,7 @@ describe('when using wildcard expression', function () {
it('should repeat any bar, beat and tick', function () {
var result = expr([
['*.*.*']
], 2, 4);
], standardOptions);
expect(result.length).to.equal(768);
expect(result[0][0]).to.equal('1.1.01');
expect(result[96][0]).to.equal('1.2.01');
Expand All @@ -119,7 +148,10 @@ describe('when using even/odd expression', function () {
it('should repeat even bars', function () {
var result = expr([
['even.2.45']
], 4, 4);
], {
'beatsPerBar': 4,
'barsPerLoop': 4
});
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('2.2.45');
expect(result[1][0]).to.equal('4.2.45');
Expand All @@ -128,7 +160,10 @@ describe('when using even/odd expression', function () {
it('should repeat odd bars', function () {
var result = expr([
['odd.1.25']
], 8, 4);
], {
'beatsPerBar': 4,
'barsPerLoop': 8
});
expect(result.length).to.equal(4);
expect(result[0][0]).to.equal('1.1.25');
expect(result[1][0]).to.equal('3.1.25');
Expand All @@ -139,7 +174,7 @@ describe('when using even/odd expression', function () {
it('should repeat even beats', function () {
var result = expr([
['1.even.10']
], 2, 4);
], standardOptions);
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('1.2.10');
expect(result[1][0]).to.equal('1.4.10');
Expand All @@ -148,7 +183,7 @@ describe('when using even/odd expression', function () {
it('should repeat odd beats', function () {
var result = expr([
['1.odd.10']
], 2, 4);
], standardOptions);
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('1.1.10');
expect(result[1][0]).to.equal('1.3.10');
Expand All @@ -157,7 +192,7 @@ describe('when using even/odd expression', function () {
it('should repeat even bars and odd beats', function () {
var result = expr([
['even.odd.10']
], 2, 4);
], standardOptions);
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('2.1.10');
expect(result[1][0]).to.equal('2.3.10');
Expand All @@ -166,7 +201,10 @@ describe('when using even/odd expression', function () {
it('should repeat odd bars and even beats', function () {
var result = expr([
['odd.even.10']
], 4, 6);
], {
'beatsPerBar': 6,
'barsPerLoop': 4
});
expect(result.length).to.equal(6);
expect(result[0][0]).to.equal('1.2.10');
expect(result[1][0]).to.equal('1.4.10');
Expand All @@ -179,7 +217,10 @@ describe('when using even/odd expression', function () {
it('should repeat even ticks', function () {
var result = expr([
['1.1.even']
], 1, 1);
], {
'beatsPerBar': 1,
'barsPerLoop': 1
});
expect(result.length).to.equal(48);
expect(result[0][0]).to.equal('1.1.02');
expect(result[47][0]).to.equal('1.1.96');
Expand All @@ -188,7 +229,10 @@ describe('when using even/odd expression', function () {
it('should repeat odd ticks', function () {
var result = expr([
['1.1.odd']
], 1, 1);
], {
'beatsPerBar': 1,
'barsPerLoop': 1
});
expect(result.length).to.equal(48);
expect(result[0][0]).to.equal('1.1.01');
expect(result[47][0]).to.equal('1.1.95');
Expand All @@ -197,7 +241,7 @@ describe('when using even/odd expression', function () {
it('should repeat even bars, odd beats and ticks', function () {
var result = expr([
['even.odd.odd']
], 2, 4);
], standardOptions);
expect(result.length).to.equal(96);
expect(result[0][0]).to.equal('2.1.01');
expect(result[1][0]).to.equal('2.1.03');
Expand Down Expand Up @@ -286,7 +330,7 @@ describe('when using mixed expression', function () {
it('should repeat every bar and even beats', function () {
var result = expr([
['*.even.01']
], 2, 4);
], standardOptions);
expect(result.length).to.equal(4);
expect(result[0][0]).to.equal('1.2.01');
expect(result[1][0]).to.equal('1.4.01');
Expand All @@ -297,8 +341,8 @@ describe('when using mixed expression', function () {
it('should repeat every beat and odd ticks', function () {
var result = expr([
['1.*.odd']
], 2, 4);
], standardOptions);

expect(result.length).to.equal(192);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.1.03');
Expand All @@ -323,4 +367,4 @@ describe('when using mixed expression', function () {
// expect(result[6][0]).to.equal('1.2.61');
// expect(result[7][0]).to.equal('1.2.91');
// });
});
});

0 comments on commit 31b8f7c

Please sign in to comment.