Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
clarified unit test procedure in Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mcandre committed May 2, 2013
1 parent 85bb50d commit 5deb4d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
40 changes: 24 additions & 16 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ This is a simple helper to extract values from a string based on a pattern.

```javascript

extractValues("/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html")
>> { "year": "2012", "month": "08", "day": "12", "title": "test" }
extractValues("/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html")
>> { "year": "2012", "month": "08", "day": "12", "title": "test" }

extractValues("John Doe <john@example.com> (http://example.com)", "{name} <{email}> ({url})")
>> {"name": "John Doe", "email": "john@example.com", "url": "http://example.com" }
extractValues("John Doe <john@example.com> (http://example.com)", "{name} <{email}> ({url})")
>> {"name": "John Doe", "email": "john@example.com", "url": "http://example.com" }

extractValues("from 4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimiters: ["`", "`"] })
>> {"from": "4th October", "to": "10th October" }
extractValues("from 4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimiters: ["`", "`"] })
>> {"from": "4th October", "to": "10th October" }

extractValues("Convert 1500 Grams to Kilograms", "convert {quantity} {from_unit} to {to_unit}", { lowercase: true })
>> {"quantity": "1500", "from_unit": "grams", "to_unit": "kilograms" }]
extractValues("Convert 1500 Grams to Kilograms", "convert {quantity} {from_unit} to {to_unit}", { lowercase: true })
>> {"quantity": "1500", "from_unit": "grams", "to_unit": "kilograms" }]

```

Expand All @@ -25,13 +25,13 @@ This is a simple helper to extract values from a string based on a pattern.
#### Install as a NPM package

```
npm install extract-values
npm install extract-values
```

* Then `require` in your project.
```javascript
var extractValues = require("extract-values");
var extractValues = require("extract-values");
```

#### Use with web apps (in Browser)
Expand All @@ -40,16 +40,24 @@ This is a simple helper to extract values from a string based on a pattern.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src='extract_values.js'></script>
<script type="text/javascript">
var parsedDate = extractValues("/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html")
//{ "year": "2012", "month": "08", "day": "12", "title": "test" }
</script>
<script type="text/javascript" src='extract_values.js'></script>
<script type="text/javascript">
var parsedDate = extractValues("/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html")
//{ "year": "2012", "month": "08", "day": "12", "title": "test" }
</script>
</head>
<body></body>
</html>
```

### Unit Tests

Run `node tests.js`.

```$ node tests.js
14 tests pass
```

### Options

**whitespace** - normalizes the whitespace in the input string, so it can be aligned with the given pattern. You can define the number of continous whitespaces to contain in the string. Making it zero (0) will remove all whitespaces.
Expand Down
30 changes: 16 additions & 14 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@ var extract_values = require("./extract_values.js");
var assert = require("assert");

var cases = [
[["a:b,c:d", "a:{a},c:{c}"], { "a": "b", "c": "d" }],
[["a:b,c:d", "a:{a},c:{c}"], { "a": "b", "c": "d" }],

[["/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html"], { "year": "2012", "month": "08", "day": "12", "title": "test" }],
[["/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html"], { "year": "2012", "month": "08", "day": "12", "title": "test" }],

[["Content-Type: text/html; charset=utf-8", "Content-Type: {mime}; charset={charset}"], {"mime": "text/html", "charset": "utf-8" }],
[["Content-Type: text/html; charset=utf-8", "Content-Type: {mime}; charset={charset}"], {"mime": "text/html", "charset": "utf-8" }],

[["/assets/images/logo.jpg", "{dirpath}/{basename}.{extension}"], { "dirpath": "/assets/images", "basename": "logo", "extension": "jpg" }],
[["/assets/images/logo.jpg", "{dirpath}/{basename}.{extension}"], { "dirpath": "/assets/images", "basename": "logo", "extension": "jpg" }],

[["some long file name.html.mustache", "{name}.{output_extension}.{template_extension}"], { "name": "some long file name", "output_extension": "html", "template_extension": "mustache" }],
[["some long file name.html.mustache", "{name}.{output_extension}.{template_extension}"], { "name": "some long file name", "output_extension": "html", "template_extension": "mustache" }],

[["Lakshan Perera <lakshan@web2media.net> (http://laktek.com)", "{name} <{email}> ({url})"], {"name": "Lakshan Perera", "email": "lakshan@web2media.net", "url": "http://laktek.com" }],
[["Lakshan Perera <lakshan@web2media.net> (http://laktek.com)", "{name} <{email}> ({url})"], {"name": "Lakshan Perera", "email": "lakshan@web2media.net", "url": "http://laktek.com" }],

[["a:b,c:d", "a:{{a}},c:{{c}}", { delimiters: ["{{", "}}"] }], { "a": "b", "c": "d" }],
[["a:b,c:d", "a:{{a}},c:{{c}}", { delimiters: ["{{", "}}"] }], { "a": "b", "c": "d" }],

[["red blue green", "{first} {second} {third}", { whitespace: 1 }], {"first": "red", "second": "blue", "third": "green" }],
[["red blue green", "{first} {second} {third}", { whitespace: 1 }], {"first": "red", "second": "blue", "third": "green" }],

[["red\n blue\n\ngreen", "{first}\n{second}\n{third}", { whitespace: 1 }], {"first": "red", "second": "blue", "third": "green" }],
[["red\n blue\n\ngreen", "{first}\n{second}\n{third}", { whitespace: 1 }], {"first": "red", "second": "blue", "third": "green" }],

[["from 4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimiters: ["`", "`"] }], {"from": "4th October", "to": "10th October" }],
[["from 4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimiters: ["`", "`"] }], {"from": "4th October", "to": "10th October" }],

[["4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimiters: ["`", "`"] }], null],
[["4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimiters: ["`", "`"] }], null],

[["Convert 1500 Grams to Kilograms", "convert {quantity} {from_unit} to {to_unit}", { lowercase: true }], {"quantity": "1500", "from_unit": "grams", "to_unit": "kilograms" }],
[["Convert 1500 Grams to Kilograms", "convert {quantity} {from_unit} to {to_unit}", { lowercase: true }], {"quantity": "1500", "from_unit": "grams", "to_unit": "kilograms" }],
[["same thing", "same thing"], {}],

[["/app/les", "/app"], null]
]

for (var i = 0; i < cases.length; i++) {
assert.deepEqual(extract_values.apply(this, cases[i][0]), cases[i][1]);
assert.deepEqual(extract_values.apply(this, cases[i][0]), cases[i][1]);
}

console.log("" + cases.length + " tests pass");

0 comments on commit 5deb4d9

Please sign in to comment.