split-string
Advanced tools
Comparing version 2.1.1 to 3.0.0
71
index.js
@@ -29,3 +29,17 @@ /*! | ||
var quotes = opts.quotes || ['"', "'", '`']; | ||
var brackets; | ||
if (opts.brackets === true) { | ||
brackets = { | ||
'<': '>', | ||
'(': ')', | ||
'[': ']', | ||
'{': '}' | ||
}; | ||
} else if (opts.brackets) { | ||
brackets = opts.brackets; | ||
} | ||
var tokens = []; | ||
var stack = []; | ||
var arr = ['']; | ||
@@ -37,2 +51,8 @@ var sep = opts.sep; | ||
function expected() { | ||
if (brackets && stack.length) { | ||
return brackets[stack[stack.length - 1]]; | ||
} | ||
} | ||
while (++idx < len) { | ||
@@ -55,4 +75,49 @@ var ch = str[idx]; | ||
if (brackets && brackets[ch]) { | ||
stack.push(ch); | ||
var e = expected(); | ||
var i = idx + 1; | ||
if (str.indexOf(e, i + 1) !== -1) { | ||
while (stack.length && i < len) { | ||
var s = str[++i]; | ||
if (s === '\\') { | ||
s++; | ||
continue; | ||
} | ||
if (quotes.indexOf(s) !== -1) { | ||
i = getClosingQuote(str, s, i + 1); | ||
continue; | ||
} | ||
e = expected(); | ||
if (stack.length && str.indexOf(e, i + 1) === -1) { | ||
break; | ||
} | ||
if (brackets[s]) { | ||
stack.push(s); | ||
continue; | ||
} | ||
if (e === s) { | ||
stack.pop(); | ||
} | ||
} | ||
} | ||
closeIdx = i; | ||
if (closeIdx === -1) { | ||
arr[arr.length - 1] += ch; | ||
continue; | ||
} | ||
ch = str.slice(idx, closeIdx + 1); | ||
tok.val = ch; | ||
tok.idx = idx = closeIdx; | ||
} | ||
if (quotes.indexOf(ch) !== -1) { | ||
closeIdx = getClose(str, ch, idx + 1); | ||
closeIdx = getClosingQuote(str, ch, idx + 1); | ||
if (closeIdx === -1) { | ||
@@ -90,6 +155,6 @@ arr[arr.length - 1] += ch; | ||
function getClose(str, ch, i) { | ||
function getClosingQuote(str, ch, i, brackets) { | ||
var idx = str.indexOf(ch, i); | ||
if (str.charAt(idx - 1) === '\\') { | ||
return getClose(str, ch, idx + 1); | ||
return getClosingQuote(str, ch, idx + 1); | ||
} | ||
@@ -96,0 +161,0 @@ return idx; |
{ | ||
"name": "split-string", | ||
"description": "Split a string on a character except when the character is escaped.", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"homepage": "https://github.com/jonschlinkert/split-string", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
@@ -1,5 +0,13 @@ | ||
# split-string [![NPM version](https://img.shields.io/npm/v/split-string.svg?style=flat)](https://www.npmjs.com/package/split-string) [![NPM monthly downloads](https://img.shields.io/npm/dm/split-string.svg?style=flat)](https://npmjs.org/package/split-string) [![NPM total downloads](https://img.shields.io/npm/dt/split-string.svg?style=flat)](https://npmjs.org/package/split-string) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/split-string.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/split-string) | ||
# split-string [![NPM version](https://img.shields.io/npm/v/split-string.svg?style=flat)](https://www.npmjs.com/package/split-string) [![NPM monthly downloads](https://img.shields.io/npm/dm/split-string.svg?style=flat)](https://npmjs.org/package/split-string) [![NPM total downloads](https://img.shields.io/npm/dt/split-string.svg?style=flat)](https://npmjs.org/package/split-string) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/split-string.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/split-string) | ||
> Split a string on a character except when the character is escaped. | ||
## Install | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm install --save split-string | ||
``` | ||
<!-- section: Why use this? --> | ||
@@ -47,16 +55,2 @@ <details> | ||
## Install | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm install --save split-string | ||
``` | ||
Install with [yarn](https://yarnpkg.com): | ||
```sh | ||
$ yarn add split-string | ||
``` | ||
## Usage | ||
@@ -79,7 +73,52 @@ | ||
**Brackets** | ||
Also respects brackets (`{}`, `[] | ||
```js | ||
split('a (b c d) e', ' '); | ||
//=> ['a', '(b c d)', 'e'] | ||
``` | ||
## Options | ||
### options.brackets | ||
**Type**: `object|boolean` | ||
**Default**: `undefined` | ||
**Description** | ||
If enabled, split-string will not split inside brackets. The following brackets types are supported when `options.brackets` is `true`, | ||
```js | ||
{ | ||
'<': '>', | ||
'(': ')', | ||
'[': ']', | ||
'{': '}' | ||
} | ||
``` | ||
Or, if object of brackets must be passed, each property on the object must be a bracket type, where the property key is the opening delimiter and property value is the closing delimiter. | ||
**Examples** | ||
```js | ||
// default (no bracket support) | ||
split('a.{b.c}'); | ||
//=> ['a', '{b', 'c}'] | ||
// support all default bracket types | ||
split('a.{b.c}', {brackets: true}); | ||
// support only the specified brackets | ||
split('[a.b].(c.d)', {brackets: {'[', ']'}}); | ||
//=> ['a.b', 'c'] | ||
``` | ||
### options.sep | ||
**Type**: `String` | ||
**Type**: `string` | ||
@@ -103,3 +142,3 @@ **Default**: `.` | ||
**Type**: `Boolean` | ||
**Type**: `boolean` | ||
@@ -122,3 +161,3 @@ **Default**: `undefined` | ||
**Type**: `Boolean` | ||
**Type**: `boolean` | ||
@@ -144,3 +183,3 @@ **Default**: `undefined` | ||
**Type**: `Boolean` | ||
**Type**: `boolean` | ||
@@ -163,3 +202,3 @@ **Default**: `undefined` | ||
**Type**: `Boolean` | ||
**Type**: `boolean` | ||
@@ -182,3 +221,3 @@ **Default**: `undefined` | ||
**Type**: `Function` | ||
**Type**: `function` | ||
@@ -210,2 +249,10 @@ **Default**: `undefined` | ||
## Release history | ||
### v3.0.0 - 2017-06-17 | ||
**Added** | ||
* adds support for brackets | ||
## About | ||
@@ -218,3 +265,3 @@ | ||
* [repeat-string](https://www.npmjs.com/package/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | [homepage](https://github.com/jonschlinkert/repeat-string "Repeat the given string n times. Fastest implementation for repeating a string.") | ||
* [romanize](https://www.npmjs.com/package/romanize): Convert arabic numbers to roman numerals (useful for books, outlines, documentation, slide decks, etc) | [homepage](https://github.com/jonschlinkert/romanize "Convert arabic numbers to roman numerals (useful for books, outlines, documentation, slide decks, etc)") | ||
* [romanize](https://www.npmjs.com/package/romanize): Convert numbers to roman numerals (useful for books, outlines, documentation, slide decks, etc) | [homepage](https://github.com/jonschlinkert/romanize "Convert numbers to roman numerals (useful for books, outlines, documentation, slide decks, etc)") | ||
@@ -229,3 +276,3 @@ ### Contributing | ||
| --- | --- | | ||
| 12 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 17 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 9 | [doowb](https://github.com/doowb) | | ||
@@ -265,2 +312,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on April 27, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 15, 2017._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13359
139
302