Skip to content
Newer
Older
100644 73 lines (48 sloc) 2 KB
4bd8cdf @laktek Added README and package.json
authored
1 ## ExtractValues
2
3 This is a simple helper to extract values from a string based on a pattern.
4
5 ### Examples
6
7 ```javascript
8
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
9 extractValues("/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html")
10 >> { "year": "2012", "month": "08", "day": "12", "title": "test" }
4bd8cdf @laktek Added README and package.json
authored
11
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
12 extractValues("John Doe <john@example.com> (http://example.com)", "{name} <{email}> ({url})")
13 >> {"name": "John Doe", "email": "john@example.com", "url": "http://example.com" }
4bd8cdf @laktek Added README and package.json
authored
14
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
15 extractValues("from 4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimiters: ["`", "`"] })
16 >> {"from": "4th October", "to": "10th October" }
20619f2 @laktek Add the option to change the string to lowercase
authored
17
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
18 extractValues("Convert 1500 Grams to Kilograms", "convert {quantity} {from_unit} to {to_unit}", { lowercase: true })
19 >> {"quantity": "1500", "from_unit": "grams", "to_unit": "kilograms" }]
20619f2 @laktek Add the option to change the string to lowercase
authored
20
4bd8cdf @laktek Added README and package.json
authored
21 ```
22
23 ### How to Use
24
de2dbc1 @arunoda added browser support
arunoda authored
25 #### Install as a NPM package
4bd8cdf @laktek Added README and package.json
authored
26
27 ```
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
28 npm install extract-values
4bd8cdf @laktek Added README and package.json
authored
29 ```
30
31 * Then `require` in your project.
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
32
4bd8cdf @laktek Added README and package.json
authored
33 ```javascript
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
34 var extractValues = require("extract-values");
4bd8cdf @laktek Added README and package.json
authored
35 ```
36
de2dbc1 @arunoda added browser support
arunoda authored
37 #### Use with web apps (in Browser)
38
39 ```html
40 <!DOCTYPE html>
41 <html>
42 <head>
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
43 <script type="text/javascript" src='extract_values.js'></script>
44 <script type="text/javascript">
45 var parsedDate = extractValues("/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html")
46 //{ "year": "2012", "month": "08", "day": "12", "title": "test" }
47 </script>
de2dbc1 @arunoda added browser support
arunoda authored
48 </head>
49 <body></body>
50 </html>
51 ```
52
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
53 ### Unit Tests
54
55 Run `node tests.js`.
56
b485d5e @laktek Fixed code blocks in README
authored
57 ```shell
58 $ node tests.js
5deb4d9 @mcandre clarified unit test procedure in Readme
mcandre authored
59 14 tests pass
60 ```
61
20619f2 @laktek Add the option to change the string to lowercase
authored
62 ### Options
63
64 **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.
65
85bb50d @laktek typo
authored
66 **lowercase** - converts the input string to lowercase before matching.
20619f2 @laktek Add the option to change the string to lowercase
authored
67
833fdb8 @laktek Fixed the typo in delimiter
authored
68 **delimiters** - If specify the delimiters used in the pattern to define the values. Default delimiters are `{` and `}`.
20619f2 @laktek Add the option to change the string to lowercase
authored
69
4bd8cdf @laktek Added README and package.json
authored
70 ### Licence
71
72 [MIT LICENSE](https://github.com/laktek/punch/blob/master/LICENSE)
Something went wrong with that request. Please try again.