Skip to content

Commit

Permalink
Added support for optionally matching keys of an object
Browse files Browse the repository at this point in the history
  • Loading branch information
Page- committed Jun 11, 2016
1 parent bff277a commit 1fb436e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Added support for optionally matching keys of an object, eg `@{ foo?: true }`
* Added support for matching some keys of an object against corresponding rules,
eg `%{ foo: FooRule }`
* Added support for matching all keys of an object against corresponding rules,
Expand Down
19 changes: 15 additions & 4 deletions lib/ometajs/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,23 @@
var ruleKeys = Object.keys(x),
result = {};
if(matchAllInput) {
this._pred(ruleKeys.length === Object.keys(v).length);
var inputLength = Object.keys(v).length;
this._pred(ruleKeys.length >= inputLength);
}
for(var i = 0; i < ruleKeys.length; i++) {
var key = ruleKeys[i];
this._pred(v.hasOwnProperty(key));
result[key] = this._applyWithArgs('applyFn', x[key], v[key]);
var key = ruleKeys[i],
exists = v.hasOwnProperty(key),
ruleFn = x[key][0],
required = x[key][1];
if(required) {
this._pred(exists);
} else if(!exists) {
continue;
}
result[key] = this._applyWithArgs('applyFn', ruleFn, v[key]);
}
if(matchAllInput) {
this._pred(Object.keys(result).length === inputLength);
}
return v;
},
Expand Down
7 changes: 6 additions & 1 deletion src/bs-ometa-compiler.ometajs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ export ometa BSOMetaParser {
objectRules = {{}}:o
(objectRule:x ',' -> x)*:a
objectRule:x -> a.concat([x]),
objectRule = BSJSParser.jsonPropName:n ":" expr:x -> [n, x],
objectRule = BSJSParser.jsonPropName:n
( "?" -> false
| -> true
):required ":" expr:x -> [n, x, required],
param = ":" name:n -> n,
ruleName = name
| spaces tsString,
Expand Down Expand Up @@ -109,6 +112,8 @@ export ometa BSOMetaTranslator {
Lookahead transFn:x -> ['this._lookahead(', x, ')'] .join(''),
Object [ ( [ string:key
transFn:x
(true|false):required
{['[', x, ',', required, ']'].join('')}:x
]
-> [JSON.stringify(key), x].join(':')
)+:o
Expand Down
4 changes: 4 additions & 0 deletions test/files/simple.ometajs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ export ometa Simple {
%{ 'simple': true, 'simpler': true }
-> 'should fail'
| -> 'ok'
,
ObjectRulesMatchOptional =
@{ 'simple': true, 'simpler'?: true }
-> 'ok'
}
3 changes: 2 additions & 1 deletion test/unit/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var testMatches = {
ObjectRulesMatchAll1: { 'simple': true },
ObjectRulesMatchAll2: { 'simple': true, 'simpler': true },
ObjectRulesMatchPartial1: { 'simple': true, 'simpler': true },
ObjectRulesMatchPartial2: { 'simple': true }
ObjectRulesMatchPartial2: { 'simple': true },
ObjectRulesMatchOptional: { 'simple': true }
};

var runTests = function(test, grammar) {
Expand Down

0 comments on commit 1fb436e

Please sign in to comment.