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

Commit

Permalink
modulus operator, resolves #1
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrenklint committed Mar 23, 2015
1 parent 5ab5ce7 commit 6ded86c
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 131 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ expect(expanded[3][0]).to.equal('2.4.01');
- ```*```
- ```even```
- ```odd```
- ```% (modulus)```

### Examples

#### Modulus

```js
> "1.%3.%30"
= "1.1.01", "1.1.31", "1.1.61", "1.1.91", "1.4.01", "1.4.31", "1.4.61", "1.4.91"

> "1.1.5%20"
= "1.1.05", "1.1.25", "1.1.45", "1.1.65", "1.1.85"
```

## Custom matchers

Expand All @@ -44,9 +57,8 @@ It is possible to add a custom *matcher callback*, a function which gets execute
The stack of matchers will continue to execute until a position has been accepted or until the stack ends.

```js
expr.addMatcher(function (position, fragments) {
if (position === '1.1.45') return true;
if (fragments[0] === 1 && fragments[2] === 96) return true;
expr.addMatcher(function (exprFragments, posFragments) {
if (posFragments[2] === 45) return true;
return false;
});
```
Expand All @@ -66,6 +78,7 @@ expr.addMatcher(function (position, fragments) {
- **1.1.0**
- 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)

## License

Expand Down
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getPossiblePositions (barsPerLoop, beatsPerBar) {
function getFragments (position) {
return position.split('.').map(function (fragment) {
var fragmentNumber = parseInt(fragment, 10);
if (!isNaN(fragmentNumber)) return fragmentNumber;
if (fragment.match(/^\d+$/) && !isNaN(fragmentNumber)) return fragmentNumber;
return fragment;
});
}
Expand All @@ -35,6 +35,7 @@ function addMatcher (matcher) {

function makeExpressionFunction (expression) {
var exprFragments = getFragments(expression);
// console.log(exprFragments, expression)
return function expressionFn (position) {
var positionFragments = getFragments(position);
var valid = true;
Expand All @@ -44,9 +45,16 @@ function makeExpressionFunction (expression) {
if (exprFragment === 'odd' && positionFragments[index] % 2 === 1) return;
if (exprFragment === '*') return;

// if (typeof exprFragment === 'string' && exprFragment.indexOf('%') >= 0) {
// console.log('deal with modulus', exprFragment, positionFragments[index])
// }
// 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;
}
// position is invalid, break out early
// console.log('>', position, positionFragments);
valid = false;
Expand All @@ -57,7 +65,7 @@ function makeExpressionFunction (expression) {
var matcher;
while (!valid && _matchers.length) {
matcher = _matchers.shift();
if (matcher(position, positionFragments)) {
if (matcher(exprFragments, positionFragments)) {
valid = true;
}
}
Expand Down
Loading

0 comments on commit 6ded86c

Please sign in to comment.