Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS objects are an unordered collection of properties, match-when overcomes it #6

Merged
merged 1 commit into from
Dec 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions match.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ function match(/* args... */){
matchers.push(when.unserialize(key, obj[key]));
}

// since JS objects are unordered we need to reorder what for..in give us even if the order was already right
// because it depends on the JS engine implementation. See #2
matchers.sort(function(a, b){
return a.position < b.position ? -1 : 1;
});

if(Object.getOwnPropertySymbols(obj).indexOf(_catchAllSymbol) === -1){
throw new MissingCatchAllPattern();
}
Expand All @@ -51,21 +57,34 @@ function match(/* args... */){
return args.length === 2 ? calculateResult(args[0]) : calculateResult;
}


function when(props){
if(props === undefined){
return _catchAllSymbol;
}

if(props instanceof RegExp){
return JSON.stringify([_patternREGEXP.toString(), props.toString()]);
return _serialize([_patternREGEXP.toString(), props.toString()]);
}

return JSON.stringify(props);
return _serialize(props);
}

when.__uid = 0;

// Any -> String
function _serialize(mixed){
return JSON.stringify([when.__uid++, mixed]);
}

// String -> [Number, Any]
function _unserialize(str){
return JSON.parse(str);
}

function _true(){return true;}

// mixed -> String
// Any -> String
function _match(props){

if(Array.isArray(props)){
Expand Down Expand Up @@ -124,23 +143,39 @@ function _match(props){

// mixed -> String
when.or = function(/* args... */){
return JSON.stringify([_patternOR.toString(), Array.prototype.slice.call(arguments)]);
return _serialize([_patternOR.toString(), Array.prototype.slice.call(arguments)]);
};

// mixed -> String
// upcoming...
when.and = function(/* args... */){
return JSON.stringify([_patternAND.toString(), Array.prototype.slice.call(arguments)]);
return _serialize([_patternAND.toString(), Array.prototype.slice.call(arguments)]);
};

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

when.unserialize = function(props, value){
when.unserialize = function(serializedKey, value){

if(serializedKey === _catchAllSymbol){
return {
match: _true,
call: value,
position: Infinity
};
}


// const {position, matcherConfiguration} = _unserialize(serializedKey);
const deserialized = _unserialize(serializedKey);
const matcherConfiguration = deserialized[1];
const position = deserialized[0];

return {
match: props === _catchAllSymbol ? _true : _match(JSON.parse(props)),
call: value
match: _match(matcherConfiguration),
call: value,
position: position
};
}

Expand Down
10 changes: 10 additions & 0 deletions match.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ const match = require('./match').match;
// var match = require('./');
const t = require('chai').assert;

describe('when multiple values can be hit', () => {
it("doesn't hit the first one", () => {
t.strictEqual(match({
[when.range(0, 43)]: 42,
[when(42)]: 72,
[when()]: 'never should be hit',
})(42), 42);
});
});

describe('match', () => {
const input = [{protocol: 'HTTP', i:10}, {protocol: 'AMQP', i:11}, {protocol: 'AMQP', i:5}, {protocol: 'WAT', i:3}];

Expand Down