Permalink
Browse files

fix modulus expression with mod 1

  • Loading branch information...
adamrenklint committed Apr 14, 2015
1 parent 584d377 commit 889be0251a9837c062abc8452328759627582903
Showing with 85 additions and 73 deletions.
  1. +2 −0 README.md
  2. +8 −6 index.js
  3. +1 −1 package.json
  4. +74 −66 test/index.test.js
View
@@ -91,6 +91,8 @@ expr.addMatcher(function (exprFragments, posFragments) {
- CHANGED: expects options object instead of barsPerLoop and beatsPerBar separately [#4](https://github.com/adamrenklint/dilla-expressions/issues/4)
- NEW: possible to add custom matcher callback [#3](https://github.com/adamrenklint/dilla-expressions/issues/3)
- NEW: modulus operator [#1](https://github.com/adamrenklint/dilla-expressions/issues/1)
- **1.1.1**
- FIXED: ```1.2%1.01``` expression would incorrectly match ```1.1.01```
## License
View
@@ -45,18 +45,20 @@ function makeExpressionFunction (expression) {
if (exprFragment === 'odd' && positionFragments[index] % 2 === 1) return;
if (exprFragment === '*') return;
// console.log('foo', exprFragment, typeof exprFragment);
if (typeof exprFragment === 'string' && exprFragment.indexOf('%') >= 0) {
// console.log(exprFragment)
var nums = exprFragment.split('%');
var offset = parseInt(nums[0] || 1, 10);
var mod = parseInt(nums[1], 10);
var res = (positionFragments[index] - offset) % mod;
if (!res) return;
var value = positionFragments[index] - offset;
if (mod === 1) {
if (value >= 0) return;
}
else {
var res = value % mod;
if (!res) return;
}
}
// position is invalid, break out early
// console.log('>', position, positionFragments);
valid = false;
return true;
});
View
@@ -1,6 +1,6 @@
{
"name": "dilla-expressions",
"version": "1.1.0",
"version": "1.1.2",
"description": "expand expressions for repeating notes into flat positions",
"main": "index.js",
"scripts": {
View
@@ -250,83 +250,91 @@ describe('when using even/odd expression', function () {
});
});
describe('when using modulus expression', function () {
describe('when no starting point is defined', function () {
it('should start from 1 and modulate every {n} ticks', function () {
var result = expr([
['1.1.%30']
], {
'beatsPerBar': 1,
'barsPerLoop': 1
});
expect(result.length).to.equal(4);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.1.31');
expect(result[2][0]).to.equal('1.1.61');
expect(result[3][0]).to.equal('1.1.91');
describe('when no starting point is defined', function () {
it('should start from 1 and modulate every {n} ticks', function () {
var result = expr([
['1.1.%30']
], {
'beatsPerBar': 1,
'barsPerLoop': 1
});
expect(result.length).to.equal(4);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.1.31');
expect(result[2][0]).to.equal('1.1.61');
expect(result[3][0]).to.equal('1.1.91');
});
it('should start from 1 and modulate every {n} beats', function () {
var result = expr([
['1.%3.01']
], {
'beatsPerBar': 4,
'barsPerLoop': 1
});
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.4.01');
it('should start from 1 and modulate every {n} beats', function () {
var result = expr([
['1.%3.01']
], {
'beatsPerBar': 4,
'barsPerLoop': 1
});
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.4.01');
});
it('should start from 1 and modulate every {n} beats and ticks', function () {
var result = expr([
['1.%3.%30']
], {
'beatsPerBar': 4,
'barsPerLoop': 1
});
expect(result.length).to.equal(8);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.1.31');
expect(result[2][0]).to.equal('1.1.61');
expect(result[3][0]).to.equal('1.1.91');
expect(result[4][0]).to.equal('1.4.01');
expect(result[5][0]).to.equal('1.4.31');
expect(result[6][0]).to.equal('1.4.61');
expect(result[7][0]).to.equal('1.4.91');
it('should start from 1 and modulate every {n} beats and ticks', function () {
var result = expr([
['1.%3.%30']
], {
'beatsPerBar': 4,
'barsPerLoop': 1
});
expect(result.length).to.equal(8);
expect(result[0][0]).to.equal('1.1.01');
expect(result[1][0]).to.equal('1.1.31');
expect(result[2][0]).to.equal('1.1.61');
expect(result[3][0]).to.equal('1.1.91');
expect(result[4][0]).to.equal('1.4.01');
expect(result[5][0]).to.equal('1.4.31');
expect(result[6][0]).to.equal('1.4.61');
expect(result[7][0]).to.equal('1.4.91');
});
});
describe('when a starting point is defined', function () {
describe('when a starting point is defined', function () {
it('should start from it and modulate every {n} ticks', function () {
var result = expr([
['1.1.5%20']
], {
'beatsPerBar': 1,
'barsPerLoop': 1
});
expect(result.length).to.equal(5);
expect(result[0][0]).to.equal('1.1.05');
expect(result[1][0]).to.equal('1.1.25');
expect(result[2][0]).to.equal('1.1.45');
expect(result[3][0]).to.equal('1.1.65');
expect(result[4][0]).to.equal('1.1.85');
it('should start from it and modulate every {n} ticks', function () {
var result = expr([
['1.1.5%20']
], {
'beatsPerBar': 1,
'barsPerLoop': 1
});
expect(result.length).to.equal(5);
expect(result[0][0]).to.equal('1.1.05');
expect(result[1][0]).to.equal('1.1.25');
expect(result[2][0]).to.equal('1.1.45');
expect(result[3][0]).to.equal('1.1.65');
expect(result[4][0]).to.equal('1.1.85');
});
it('should start from it and modulate every {n} beats', function () {
var result = expr([
['1.2%3.01']
], {
'beatsPerBar': 10,
'barsPerLoop': 1
});
expect(result.length).to.equal(3);
expect(result[0][0]).to.equal('1.2.01');
expect(result[1][0]).to.equal('1.5.01');
expect(result[2][0]).to.equal('1.8.01');
it('should start from it and modulate every {n} beats', function () {
var result = expr([
['1.2%2.01']
], {
'beatsPerBar': 4,
'barsPerLoop': 1
});
expect(result.length).to.equal(2);
expect(result[0][0]).to.equal('1.2.01');
expect(result[1][0]).to.equal('1.4.01');
});
it('should work with modulus 1, i.e. each after offset', function () {
var result = expr([
['1.2%1.01']
], {
'beatsPerBar': 4,
'barsPerLoop': 1
});
expect(result.length).to.equal(3);
expect(result[0][0]).to.equal('1.2.01');
expect(result[1][0]).to.equal('1.3.01');
expect(result[2][0]).to.equal('1.4.01');
});
});

0 comments on commit 889be02

Please sign in to comment.