Skip to content

Commit

Permalink
Add deep, so deep pattern matching...
Browse files Browse the repository at this point in the history
that you can see Adele rolling in it.
  • Loading branch information
greenboxal committed Jun 17, 2015
1 parent 488860f commit 84200c4
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 77 deletions.
129 changes: 52 additions & 77 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,54 @@ var quoteRegex = function(str) {
return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};

var parsePath = function(path) {
var regex = '', acc = '';

var end = function() {
regex += quoteRegex(acc);
acc = '';
};

for (var i = 0; i < path.length; i++) {
var last = path[i - 1];
var c = path[i];
var next = path[i + 1];

if (c == '*') {
end();

regex += '[^.]*';
} else if (c == '$') {
end();

regex += '^';
} else if (c == '.') {
end();

if (next == '.') {
i++;

regex += '.*\\.';
} else if (last != '$') {
regex += '\\.';
}
} else if (c == '[') {
end();

regex += '\\.';
} else if (c == ']') {
} else {
acc += c;
}
}

if (acc != '') {
end();
}

return new RegExp(regex + '$');
};

var LivePatch = function(fn) {
if (!(this instanceof LivePatch)) {
return new LivePatch(fn);
Expand Down Expand Up @@ -58,29 +106,6 @@ var LivePatch = function(fn) {

util.inherits(LivePatch, stream.Transform);

LivePatch.prototype._matches = function(match, m) {
if (match.path.length != m.path.length) {
return false;
}

for (var i = 0; i < m.path.length; i++) {
var test = m.path[i];
var node = match.path[i];

if (test instanceof RegExp) {
if (!test.test(node)) {
return false;
}
} else {
if (node !== test) {
return false;
}
}
}

return true;
};

LivePatch.prototype._match = function(type, value) {
var keys = this.stack.map(function(k) {
return k.key;
Expand All @@ -98,7 +123,7 @@ LivePatch.prototype._match = function(type, value) {
for (var i = 0; i < this.matches.length; i++) {
var m = this.matches[i];

if (this._matches(match, m)) {
if (m.path.test(match.path.slice(1).join('.'))) {
result = m.fn(match);
break;
}
Expand Down Expand Up @@ -267,61 +292,11 @@ LivePatch.prototype.remove = function(path) {
});
};

LivePatch.prototype.rewrite = function(pathStr, fn) {
var path = [];
var currentLevel = 0, currentCheck = '', acc = '', regex = false, raw = false;

var flush = function() {
if (!raw) {
if (regex) {
currentCheck += quoteRegex(acc);
} else {
currentCheck += acc;
}
}

acc = '';
};

var end = function() {
flush();

if (regex) {
path.push(new RegExp('^' + currentCheck + '$'));
} else {
path.push(currentCheck);
}

currentCheck = '';
regex = false;
raw = false;
};

for (var i = 0; i < pathStr.length; i++) {
var c = pathStr[i];

if (c == '.' || c == '[') {
end();
} else if (c == '*') {
flush();

if (!regex) {
currentCheck = quoteRegex(currentCheck);
}

currentCheck += '.*';
regex = true;
} else if (c == '$') {
currentCheck = null;
raw = true;
} else if (c == ']') {
} else {
acc += c;
}
LivePatch.prototype.rewrite = function(path, fn) {
if (path.constructor == String) {
path = parsePath(path);
}

end();

this.matches.push({
path: path,
fn: fn
Expand Down
82 changes: 82 additions & 0 deletions test/rewrite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,87 @@

describe('Rewrite', function() {
describe('when using deep paths', function() {
var output = null;
var object = {
safe: true,
arr: [{
metadata_123: 'wtf',
safe: true,
deep: {
so: {
deep: {
that: {
i: {
can: [{
see: {
adele: {
rolling: {
in: {
it: {
metadata_test: 123
}
}
}
}
}
}]
}
}
}
}
}
}, {
metadata_456: 'bbq',
safe: true
}]
};

before(function(done) {
patchJson(object, function() {
this.rename('$..metadata_*', 'metadata');
}, function(err, data) {
output = data;
done(err);
});
});

it('should change the correct values', function() {
expect(output).to.eql({
safe: true,
arr: [{
metadata: 'wtf',
safe: true,
deep: {
so: {
deep: {
that: {
i: {
can: [{
see: {
adele: {
rolling: {
in: {
it: {
metadata: 123
}
}
}
}
}
}]
}
}
}
}
}
}, {
metadata: 'bbq',
safe: true
}]
});
});
});

describe('when renaming partially', function() {
var output = null;
var object = {
Expand Down

0 comments on commit 84200c4

Please sign in to comment.