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

add support for paths wrapped in single-quotes along with tests #228

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 58 additions & 0 deletions spec/rivets/keypath_parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe("Rivets.KeypathParser", function() {
var Rivets = rivets._,
interfaces = ['.', ':', ',', ';'],
parse = Rivets.KeypathParser.parse;

describe("parse()", function() {
it("uses the root interface for the first path", function() {
var path = keypath = "foo",
root = ":",
tokens = parse(keypath, interfaces, root);
expect(tokens.length).toEqual(1);
expect(tokens[0]).toEqual({interface: root, path: path});
});

it("supports single-char interfaces", function() {
var keypath = "foo.bar:baz,wut",
root = ".",
tokens = parse(keypath, interfaces, root);
expect(tokens.length).toEqual(4);
expect(tokens[0]).toEqual({interface: root, path: "foo"});
expect(tokens[1]).toEqual({interface: ".", path: "bar"});
expect(tokens[2]).toEqual({interface: ":", path: "baz"});
expect(tokens[3]).toEqual({interface: ",", path: "wut"});

keypath = "0.1.2.3.4.5.6";
tokens = parse(keypath, interfaces, root);
for (var i = 0; i < tokens.length; i ++) {
expect(tokens[i]).toEqual({interface: ".", path: i.toString()});
}
});

it("supports paths wrapped in single-quotes", function() {
var keypath = "foo.'bar:baz',wut",
root = ".",
tokens = parse(keypath, interfaces, root);
expect(tokens.length).toEqual(3);
expect(tokens[0]).toEqual({interface: root, path: "foo"});
expect(tokens[1]).toEqual({interface: ".", path: "bar:baz"});
expect(tokens[2]).toEqual({interface: ",", path: "wut"});

keypath = "'0'.'1.0'.'2.1.0'.'3.2.1.0'";
tokens = parse(keypath, interfaces, root);
var path;
for (var i = 0; i < tokens.length; i ++) {
path = i.toString();
for (var j = i - 1; j >= 0; j--) {
path += "." + j;
}
expect(tokens[i]).toEqual({interface: ".", path: path});
}

keypath = "'obj'";
tokens = parse(keypath, interfaces, root);
expect(tokens.length).toEqual(1);
expect(tokens[0]).toEqual({interface: root, path: "obj"});
});
});
});
22 changes: 14 additions & 8 deletions src/parsers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ class Rivets.KeypathParser
# Parses the keypath and returns a set of adapter interface + path tokens.
@parse: (keypath, interfaces, root) ->
tokens = []
current = {interface: root, path: ''}

for index, char of keypath
if char in interfaces
tokens.push current
current = {interface: char, path: ''}
else
current.path += char
# construct regex from interfaces
splitChars = interfaces.join('')
splits = '[' + splitChars + ']'
word = '[^' + splitChars + "']"
path = "(?:'(.+?)'|(" + word + '+))'
firstPathRegex = new RegExp('^' + path)
subsequentPathRegex = new RegExp("(" + splits + ")" + path, 'g')

match = firstPathRegex.exec(keypath)
if match
tokens.push({interface: root, path: match[1] || match[2]})

while (match = subsequentPathRegex.exec(keypath)) != null
tokens.push({interface: match[1], path: match[2] || match[3]})

tokens.push current
tokens

# Rivets.TextTemplateParser
Expand Down