Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

split-string

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

split-string - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

98

index.js

@@ -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] === '\\';
}
{
"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)",

@@ -69,2 +69,23 @@ # 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)

### 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._
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc