Skip to content

Commit

Permalink
Add support for matching some keys of an object against corresponding…
Browse files Browse the repository at this point in the history
… rules
  • Loading branch information
Page- committed Jun 11, 2016
1 parent aa4c834 commit bff277a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* 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,
eg `@{ foo: FooRule }`
* Added support for using `_form` (`[]`) on an object input,
Expand Down
6 changes: 4 additions & 2 deletions lib/ometajs/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,14 @@
_many1: function(x) {
return this._many(x, x.call(this));
},
_object: function(x) {
_object: function(x, matchAllInput) {
var v = this.anything();
this._pred(typeof v === 'object');
var ruleKeys = Object.keys(x),
result = {};
this._pred(ruleKeys.length === Object.keys(v).length);
if(matchAllInput) {
this._pred(ruleKeys.length === Object.keys(v).length);
}
for(var i = 0; i < ruleKeys.length; i++) {
var key = ruleKeys[i];
this._pred(v.hasOwnProperty(key));
Expand Down
5 changes: 3 additions & 2 deletions src/bs-ometa-compiler.ometajs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export ometa BSOMetaParser {
| ( keyword('undefined') | keyword('nil')
| keyword('true') | keyword('false') ):x -> [#App, #exactly, x]
| spaces (seqString | tokenString | string | number)
| "@{" objectRules:x "}" -> [#Object, x]
| "@{" objectRules:x "}" -> [#Object, x, true]
| "%{" objectRules:x "}" -> [#Object, x, false]
| "[" expr:x "]" -> [#Form, x]
| "<" expr:x ">" -> [#ConsBy, x]
| "@<" expr:x ">" -> [#IdxConsBy, x]
Expand Down Expand Up @@ -111,7 +112,7 @@ export ometa BSOMetaTranslator {
]
-> [JSON.stringify(key), x].join(':')
)+:o
] -> ['this._object({', o.join(','), '})'] .join(''),
] (true|false):matchAllInput -> ['this._object({', o.join(','), '},', matchAllInput, ')'] .join(''),
Form transFn:x -> ['this._form(', x, ')'] .join(''),
ConsBy transFn:x -> ['this._consumedBy(', x, ')'] .join(''),
IdxConsBy transFn:x -> ['this._idxConsumedBy(', x, ')'] .join(''),
Expand Down
9 changes: 9 additions & 0 deletions test/files/simple.ometajs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ export ometa Simple {
@{ 'simple': true }
-> 'should fail'
| -> 'ok'
,
ObjectRulesMatchPartial1 =
%{ 'simple': true }
-> 'ok'
,
ObjectRulesMatchPartial2 =
%{ 'simple': true, 'simpler': true }
-> 'should fail'
| -> 'ok'
}
4 changes: 3 additions & 1 deletion test/unit/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ var testMatches = {
FormObject: { 'simple': true },
ObjectRules: { 'simple': true },
ObjectRulesMatchAll1: { 'simple': true },
ObjectRulesMatchAll2: { 'simple': true, 'simpler': true }
ObjectRulesMatchAll2: { 'simple': true, 'simpler': true },
ObjectRulesMatchPartial1: { 'simple': true, 'simpler': true },
ObjectRulesMatchPartial2: { 'simple': true }
};

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

0 comments on commit bff277a

Please sign in to comment.