-
Notifications
You must be signed in to change notification settings - Fork 46
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
Fix up regex, fails test for #79(!) #94
base: master
Are you sure you want to change the base?
Conversation
I've pushed a quick refactor to clear up the responsibility of some of the line parsing code. Unfortunately my original change breaks the spec for #79 but I'm not sure why yet. |
* _parseOptions takes a single line from a file and extracts | ||
* JSON data if possible. Returns an object. | ||
*/ | ||
Canned.prototype._parseOptions = function(line) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does not need to be on the prototype as it is only used inside the file as far as I can see.
Really like where this is going 👍 |
remove parseOption from scope
Ok so for the regex the problem is indeed that var string = "aaacaaa\naaa\nbbb\nccc\nbaaabaaa"
var re = new RegExp(/aaa/g)
var res = string.split("\n").reduce(function(acc, el) {
el.replace(re, function(match, _index, full){
acc.push({ match: match, full: full })
});
return acc
}, [])
console.log(res)
// => [ { match: 'aaa', full: 'aaacaaa' }, { match: 'aaa', full: 'aaacaaa' },
// { match: 'aaa', full: 'aaa' }, { match: 'aaa', full: 'baaabaaa' },
// { match: 'aaa', full: 'baaabaaa' } ] This now works correctly with the |
I pushed another big commit. I moved the request params and return options parsing into their own modules that are ludicrously similar. Probably best to have a shared parser that deals with all the front matter (to steal a term from jekyll) and it can return the comprehensive list of options per entry in the file. Hopefully I'm barking up the right tree. It's very much WIP, but I'd like to hear your feedback. |
@@ -0,0 +1,33 @@ | |||
var cannedUtils = require('./utils'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use underscore _
in the filename -
is a pain sometimes :(
NICE! I really like the approach, left some comments but mostly style, I try to keep this consistent if possible. Also I think the api would be nicer if the request and response parser would basically work like this var parseRequest = require("./request_parser")
var options = parseRequset(lines) |
This change fixes a couple of problems:
The regexes as they stood were both matching per character, which makes for some interesting matches such as: http://rubular.com/r/uZpZRyhmpN
Changing the character group to a non-captured OR group makes the results: http://rubular.com/r/XBXkjlu54P
It also removes the /g option from the regexp. Since the
.exec
method is called only once per line it was causing weird results because/g
causes it to return falsy unexpectedly. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Finding_successive_matches)