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

Fix up regex, fails test for #79(!) #94

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

wadtech
Copy link
Collaborator

@wadtech wadtech commented Jan 25, 2016

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)

@wadtech
Copy link
Collaborator Author

wadtech commented Jan 25, 2016

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) {
Copy link
Owner

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.

@sideshowcoder
Copy link
Owner

Really like where this is going 👍

remove parseOption from scope
@sideshowcoder
Copy link
Owner

Ok so for the regex the problem is indeed that /g does not behave as expected... the fix for that would be something like

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 /g as one would expect, the match set contains { match: 'aaa', full: 'aaa' }

@wadtech
Copy link
Collaborator Author

wadtech commented Jan 25, 2016

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');
Copy link
Owner

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 :(

@sideshowcoder
Copy link
Owner

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants