split-string
Advanced tools
+52
-46
@@ -12,3 +12,3 @@ /*! | ||
| module.exports = function(str, options) { | ||
| module.exports = function(str, options, fn) { | ||
| if (typeof str !== 'string') { | ||
@@ -18,9 +18,16 @@ throw new TypeError('expected a string'); | ||
| if (typeof options === 'function') { | ||
| fn = options; | ||
| options = null; | ||
| } | ||
| // allow separator to be defined as a string | ||
| if (typeof options === 'string') { | ||
| options = {sep: options}; | ||
| options = { sep: options }; | ||
| } | ||
| var opts = extend({sep: '.'}, options); | ||
| var tokens = []; | ||
| var arr = ['']; | ||
| var sep = opts.sep; | ||
| var len = str.length; | ||
@@ -31,54 +38,43 @@ var idx = -1; | ||
| while (++idx < len) { | ||
| var substr = str[idx]; | ||
| var ch = str[idx]; | ||
| var next = str[idx + 1]; | ||
| var tok = { val: ch, idx: idx, arr: arr, str: str }; | ||
| tokens.push(tok); | ||
| if (substr === '\\') { | ||
| var val = opts.keepEscaping === true ? (substr + next) : next; | ||
| arr[arr.length - 1] += val; | ||
| if (ch === '\\') { | ||
| tok.val = keepEscaping(opts, str, idx) === true ? (ch + next) : next; | ||
| arr[arr.length - 1] += tok.val; | ||
| idx++; | ||
| continue; | ||
| } | ||
| } else { | ||
| if (substr === '"') { | ||
| closeIdx = getClose(str, '"', idx + 1); | ||
| if (closeIdx === -1) { | ||
| if (opts.strict !== false) { | ||
| throw new Error('unclosed double quote: ' + str); | ||
| } | ||
| closeIdx = idx; | ||
| } | ||
| if (ch === '"' || ch === "'") { | ||
| closeIdx = getClose(str, ch, idx + 1); | ||
| if (closeIdx === -1) { | ||
| arr[arr.length - 1] += ch; | ||
| continue; | ||
| } | ||
| if (opts.keepDoubleQuotes === true) { | ||
| substr = str.slice(idx, closeIdx + 1); | ||
| } else { | ||
| substr = str.slice(idx + 1, closeIdx); | ||
| } | ||
| idx = closeIdx; | ||
| if (keepQuotes(ch, opts) === true) { | ||
| ch = str.slice(idx, closeIdx + 1); | ||
| } else { | ||
| ch = str.slice(idx + 1, closeIdx); | ||
| } | ||
| if (substr === '\'') { | ||
| closeIdx = getClose(str, '\'', idx + 1); | ||
| if (closeIdx === -1) { | ||
| if (opts.strict !== false) { | ||
| throw new Error('unclosed single quote: ' + str); | ||
| } | ||
| closeIdx = idx; | ||
| } | ||
| tok.val = ch; | ||
| tok.idx = idx = closeIdx; | ||
| } | ||
| if (opts.keepSingleQuotes === true) { | ||
| substr = str.slice(idx, closeIdx + 1); | ||
| } else { | ||
| substr = str.slice(idx + 1, closeIdx); | ||
| } | ||
| if (typeof fn === 'function') { | ||
| fn(tok, tokens); | ||
| ch = tok.val; | ||
| idx = tok.idx; | ||
| } | ||
| idx = closeIdx; | ||
| } | ||
| if (tok.val === sep && tok.split !== false) { | ||
| arr.push(''); | ||
| continue; | ||
| } | ||
| if (substr === opts.sep) { | ||
| arr.push(''); | ||
| } else { | ||
| arr[arr.length - 1] += substr; | ||
| } | ||
| } | ||
| arr[arr.length - 1] += tok.val; | ||
| } | ||
@@ -89,8 +85,18 @@ | ||
| function getClose(str, substr, i) { | ||
| var idx = str.indexOf(substr, i); | ||
| function getClose(str, ch, i) { | ||
| var idx = str.indexOf(ch, i); | ||
| if (str.charAt(idx - 1) === '\\') { | ||
| return getClose(str, substr, idx + 1); | ||
| return getClose(str, ch, idx + 1); | ||
| } | ||
| return idx; | ||
| } | ||
| function keepQuotes(ch, opts) { | ||
| if (opts.keepDoubleQuotes === true && ch === '"') return true; | ||
| if (opts.keepSingleQuotes === true && ch === "'") return true; | ||
| return opts.keepQuotes; | ||
| } | ||
| function keepEscaping(opts, str, idx) { | ||
| return opts.keepEscaping === true || str[idx + 1] === '\\'; | ||
| } |
+1
-1
| { | ||
| "name": "split-string", | ||
| "description": "Split a string on a character except when the character is escaped.", | ||
| "version": "1.0.1", | ||
| "version": "2.0.0", | ||
| "homepage": "https://github.com/jonschlinkert/split-string", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
+44
-10
@@ -69,2 +69,23 @@ # split-string [](https://www.npmjs.com/package/split-string) [](https://npmjs.org/package/split-string) [](https://npmjs.org/package/split-string) [](https://travis-ci.org/jonschlinkert/split-string) | ||
| ### options.keepQuotes | ||
| **Type**: `Boolean` | ||
| **Default**: `undefined` | ||
| Keep single- or double-quotes in the result. | ||
| **Example** | ||
| ```js | ||
| split('a."b.c.d".e'); | ||
| //=> ['a', 'b.c.d', 'e'] | ||
| split('a."b.c.d".e', {keepQuotes: true}); | ||
| //=> ['a', '"b.c.d"', 'e'] | ||
| split('a.\'b.c.d\'.e', {keepQuotes: true}); | ||
| //=> ['a', '\'b.c.d\'', 'e'] | ||
| ``` | ||
| ### options.keepDoubleQuotes | ||
@@ -85,3 +106,3 @@ | ||
| split('a."b.c.d".e', {keepDoubleQuotes: true}); | ||
| //=> ['a', 'b.c.d', 'e'] | ||
| //=> ['a', '"b.c.d"', 'e'] | ||
| ``` | ||
@@ -104,13 +125,12 @@ | ||
| split('a.\'b.c.d\'.e', {keepSingleQuotes: true}); | ||
| //=> ['a', 'b.c.d', 'e'] | ||
| //=> ['a', '\'b.c.d\'', 'e'] | ||
| ``` | ||
| ### options.strict | ||
| ## Customizer | ||
| **Type**: `Boolean` | ||
| **Type**: `Function` | ||
| **Default**: `undefined` | ||
| When `true` or `undefined`, throws an error on unclosed double and single quotes. | ||
| Set to `false` to ignore errors and continue parsing. | ||
| Pass a function as the last argument to customize how tokens are added to the array. | ||
@@ -120,6 +140,20 @@ **Example** | ||
| ```js | ||
| split('a.\'b.c', {strict: false}); | ||
| //=> ['a', 'b', 'c'] | ||
| var arr = split('a.b', function(tok) { | ||
| if (tok.arr[tok.arr.length - 1] === 'a') { | ||
| tok.split = false; | ||
| } | ||
| }); | ||
| console.log(arr); | ||
| //=> ['a.b'] | ||
| ``` | ||
| **Properties** | ||
| The `tok` object has the following properties: | ||
| * `tok.val` (string) The current value about to be pushed onto the result array | ||
| * `tok.idx` (number) the current index in the string | ||
| * `tok.str` (string) the entire string | ||
| * `tok.arr` (array) the result array | ||
| ## About | ||
@@ -142,4 +176,4 @@ | ||
| | --- | --- | | ||
| | 9 | [doowb](https://github.com/doowb) | | ||
| | 7 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| | 1 | [doowb](https://github.com/doowb) | | ||
@@ -178,2 +212,2 @@ ### Building docs | ||
| _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.3, on April 11, 2017._ | ||
| _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 11, 2017._ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
10196
7.42%80
5.26%207
19.65%0
-100%