Skip to content

Commit

Permalink
merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Aug 14, 2012
2 parents 337b137 + c6cbf05 commit edb7672
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,14 @@
# Changelog

## 0.0.3

* Match arrays ending with ... to anything matching the prefix
* Match arrays beginning with ... to anything matching the suffix

## 0.0.2

* Add comments with //

## 0.0.1

* Extract JSONExp from [LiterAPI][]
Expand Down
29 changes: 28 additions & 1 deletion lib/index.js
Expand Up @@ -54,6 +54,15 @@ var specials = function(obj) {
if (obj.hasOwnProperty(specials.any)) opts.any = obj[specials.any]
if (obj.hasOwnProperty(specials.many)) opts.many = obj[specials.many]

if (obj instanceof Array) {
var last = obj[obj.length - 1]
, first = obj[0]
if (first && first.hasOwnProperty(specials.many))
opts.suffix = first[specials.many]
if (last && last.hasOwnProperty(specials.many))
opts.prefix = last[specials.many]
}

return opts
}

Expand All @@ -79,7 +88,8 @@ function escapeString(match, contents) {
JSONExp.preprocess = function(src) {
var output = src
.replace(/"(.*?[^\\])"/g, escapeString)
.replace(/\*\*/g, '{ "' + specials.many + '": true }')
.replace(/\.\.\.\s*\]/g, '{ "' + specials.many + '": true }]')
.replace(/\[\s*\.\.\./g, '[{ "' + specials.many + '": true }')
.replace(/\*/g, '{ "' + specials.any + '": true }')
.replace(/\.\.\./g, '"' + specials.glob + '": true')
.replace(captureRE, '{ "' + specials.capture + '": "$1" }')
Expand Down Expand Up @@ -134,6 +144,23 @@ JSONExp.prototype._match = function(pattern, given, options, path) {
match[specials(pattern).capture] = given
} else if (specials(pattern).any) {
// anything will do
} else if (given instanceof Array) {
// ignore arrays
assert.ok(pattern, "Expected an Array at " + path + ", got null")
assert.ok(pattern instanceof Array, "Expected an Array at " + path + ", got " + pattern.constructor)
var length = pattern.length, o = 0, i = 0
if (specials(pattern).prefix) {
length--
} else if (specials(pattern).suffix) {
o = given.length - pattern.length
i = 1
} else {
assert.equal(given.length, pattern.length, "Arrays are not the same length at " + path)
}
for (; i < length; i++) {
var m = this._match(pattern[i], given[i + o], options, path + '/' + i)
mergeCaptures(match, m)
}
} else {
test(typeof pattern, typeof given)

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jsonexp",
"version": "0.0.1",
"version": "0.0.3",
"description": "RegExp for JSON - powerful, expressive pattern matching for data structures",
"main": "lib/",
"directories": {
Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Expand Up @@ -49,6 +49,40 @@ vows.describe('JSONExp')
.addBatch(test(
'{ "foo": "http://bar" }', { foo: "http://bar" }
))
.addBatch(test(
'[1, 2]', [1, 2],
{ '[1]': null
, '{"0": 1}': null
, '[1, 2]': {}
, '[2, 1]': null
, '[1, 2, 3]': null
}
))
.addBatch(test(
'[...]', [{ "$JSONExp MANY": true }],
{ '[]': {}
, '["a"]': {}
, '["a", "b"]': {}
, '["a", "b", "c"]': {}
}
))
.addBatch(test(
'["a", "b", ...]', ["a", "b", { "$JSONExp MANY": true }],
{ '["a"]': null
, '["a", "b"]': {}
, '["a", "b", "c"]': {}
, '["c", "a", "b"]': null
}
))
.addBatch(test(
'[..., 1, 0]', [{ "$JSONExp MANY": true }, 1, 0],
{ '[1]': null
, '[1, 0]': {}
, '[2, 1, 0]': {}
, '[5, 4, 3, 2, 1, 0]': {}
, '[1, 0, 2]': null
}
))
.addBatch(test(
'{ "foo": <date: 1 month ago> }', {
foo: Date.create('1 month ago').format('{yyyy}-{MM}-{dd}T{hh}:{mm}:{ss}{zzzz}')
Expand Down

0 comments on commit edb7672

Please sign in to comment.