Permalink
Browse files

Handle mismatches

  • Loading branch information...
1 parent 7bf0bb6 commit 7d22a37e9fdb1a3bac4947b368a9d7c56701b517 @laktek committed Oct 4, 2012
Showing with 12 additions and 3 deletions.
  1. +1 −1 Readme.md
  2. +7 −1 extract_values.js
  3. +4 −1 tests.js
View
@@ -27,7 +27,7 @@ This is a simple helper to extract values from a string based on a pattern.
* Then `require` in your project.
```javascript
- var extractValues = require("extractValues");
+ var extractValues = require("extract-values");
```
* For client-side use, simply copy and paste the function (`extract_values.js`) in to your source.
View
@@ -18,7 +18,13 @@ var extractValues = function(str, pattern, options) {
});
};
- var matches = str.match(pattern_regex).splice(1);
+ var matches = str.match(pattern_regex)
+
+ if (!matches) {
+ return null;
+ }
+
+ matches = matches.splice(1);
var output = {};
for (var i=0; i < tokens.length; i++) {
output[tokens[i].replace( new RegExp( delimeters[0] + "|" + delimeters[1], "g"), "")] = matches[i];
View
@@ -16,7 +16,10 @@ var cases = [
[["red blue green", "{first} {second} {third}", { whitespace: 1 }], {"first": "red", "second": "blue", "third": "green" }],
- [["from 4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimeters: ["`", "`"] }], {"from": "4th October", "to": "10th October" }]
+ [["from 4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimeters: ["`", "`"] }], {"from": "4th October", "to": "10th October" }],
+
+ [["4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimeters: ["`", "`"] }], null]
+
]
for (var i = 0; i < cases.length; i++) {

0 comments on commit 7d22a37

Please sign in to comment.