| 2 | var util = require('util'); |
| 3 | |
| 4 | var tokenize = function(/*String*/ str, /*RegExp*/ re, /*Function?*/ parseDelim, /*Object?*/ instance){ |
| 5 | // summary: |
| 6 | // Split a string by a regular expression with the ability to capture the delimeters |
| 7 | // parseDelim: |
| 8 | // Each group (excluding the 0 group) is passed as a parameter. If the function returns |
| 9 | // a value, it's added to the list of tokens. |
| 10 | // instance: |
| 11 | // Used as the "this' instance when calling parseDelim |
| 12 | var tokens = []; |
| 13 | var match, content, lastIndex = 0; |
| 14 | while((match = re.exec(str))){ |
| 15 | content = str.slice(lastIndex, re.lastIndex - match[0].length); |
| 16 | if(content.length){ |
| 17 | tokens.push(content); |
| 18 | } |
| 19 | if(parseDelim){ |
| 20 | var parsed = parseDelim.apply(instance, match.slice(1).concat(tokens.length)); |
| 21 | if(typeof parsed != 'undefined'){ |
| 22 | if(parsed.specifier === '%'){ |
| 23 | tokens.push('%'); |
| 24 | }else{ |
| 25 | tokens.push(parsed); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | lastIndex = re.lastIndex; |
| 30 | } |
| 31 | content = str.slice(lastIndex); |
| 32 | if(content.length){ |
| 33 | tokens.push(content); |
| 34 | } |
| 35 | return tokens; |
| 36 | }; |
| 37 | |
| 38 | var Formatter = function(/*String*/ format){ |
| 39 | this._mapped = false; |