Skip to content

Commit

Permalink
feat(range) inclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Gilliland committed Dec 29, 2015
1 parent 88ee496 commit b1f7cdd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ const protocols = repositories.map(match({
}))
```


##### Regular Expressions

match-when supports [regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) as well:
Expand All @@ -114,6 +113,18 @@ match-when supports [regular expressions](https://developer.mozilla.org/en-US/do
// ['hey.com', 'wat']
```

##### Range

```js
[12, 42, 99, 101].map(match({
[when.range(0, 41)]: '< answer',
[when.range(43, 100)]: '> answer',
[when(42)]: 'answer',
[when()]: '< 0, or > 100'
}));

// ['< answer', 'answer', '> answer', '< 0, or > 100']
```

### Supported patterns:

Expand All @@ -129,7 +140,6 @@ match-when supports [regular expressions](https://developer.mozilla.org/en-US/do
I will accept PR with their associated tests for the following features:


- support `range(x, y)`
- define and implement some syntax to support wildcards
- try and maybe support `match(input, {patterns...})` syntax instead of `match({patterns...})(input)`?

Expand Down
13 changes: 13 additions & 0 deletions match.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const _patternOR = Symbol('match.pattern.OR');
const _patternORStr = _patternOR.toString(); // dirty hack
const _patternAND = Symbol('match.pattern.AND');
const _patternANDStr = _patternAND.toString(); // dirty hack
const _patternRANGE = Symbol('match.pattern.RANGE');
const _patternRANGEStr = _patternRANGE.toString(); // dirty hack

const _patternREGEXP = Symbol('match.pattern.REGEXP');
const _patternREGEXPStr = _patternREGEXP.toString(); // dirty hack
Expand Down Expand Up @@ -77,6 +79,13 @@ function _match(props){
};
}

if(props[0] === _patternRANGEStr){
props.shift();
return function(input){
return props[0] <= input && input <= props[1];
};
}

if(props[0] === _patternREGEXPStr){
const res = EXTRACT_PATTERN_AND_FLAGS.exec(props[1]);
return _matching.bind(null, new RegExp(res[1], res[2]));
Expand Down Expand Up @@ -120,6 +129,10 @@ when.and = function(/* args... */){
return JSON.stringify([_patternAND.toString(), Array.prototype.slice.call(arguments)]);
};

when.range = function(start, end){
return JSON.stringify([_patternRANGE.toString(), start, end]);
};

when.unserialize = function(props, value){
return {
match: props === _catchAllSymbol ? _true : _match(JSON.parse(props)),
Expand Down
58 changes: 58 additions & 0 deletions match.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,64 @@ describe('match', () => {
})
});

describe('when.range', () => {
const rangeStart = 0,
rangeEnd = 5;

beforeEach(function () {
this.withinRange = match({
[when.range(rangeStart, rangeEnd)]: true,
[when()]: false
});
});

describe('given a value within the range', function () {
it('should match', function () {
t.isTrue(this.withinRange(rangeStart+1));
});
});

describe('given a value at the lower bound', function () {
it('should match', function () {
t.isTrue(this.withinRange(rangeStart));
});
});

describe('given a value at the upper bound', function () {
it('should match', function () {
t.isTrue(this.withinRange(rangeEnd));
});
});

describe('given a value above the upper bound', function () {
it('should not match', function () {
t.isFalse(this.withinRange(rangeEnd+1));
});
});

describe('given a value below the lower bound', function () {
it('should not match', function () {
t.isFalse(this.withinRange(rangeStart-1));
});
});

describe('the example in the docs', function () {
it('works correctly', function () {
var result = [12, 42, 99, 101].map(match({
[when.range(0, 41)]: '< answer',
[when.range(43, 100)]: '> answer',
[when(42)]: 'answer',
[when()]: '< 0, or > 100'
}));

var expected = ['< answer', 'answer', '> answer', '< 0, or > 100']

t.deepEqual(result, expected);
});
});

});

describe('yielding', () => {
it('should also be able to yield primitive values', () => {
const output = input.map(match({
Expand Down

0 comments on commit b1f7cdd

Please sign in to comment.