split-string
Advanced tools
Comparing version 0.1.1 to 1.0.0
91
index.js
/*! | ||
* split-string <https://github.com/jonschlinkert/split-string> | ||
* | ||
* Copyright (c) 2015, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
* Copyright (c) 2015, 2017, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -10,23 +10,78 @@ | ||
var nonchar = require('noncharacters')[0]; | ||
var extend = require('extend-shallow'); | ||
function split(str, ch) { | ||
module.exports = function(str, options) { | ||
if (typeof str !== 'string') { | ||
throw new TypeError('expected a string.'); | ||
throw new TypeError('expected a string'); | ||
} | ||
ch = ch || '.'; | ||
var esc = str.split('\\' + ch).join(nonchar); | ||
var segs = esc.split(ch); | ||
var len = segs.length, i = -1; | ||
var res = []; | ||
while (++i < len) { | ||
res.push(segs[i].split(nonchar).join(ch)); | ||
// allow separator to be defined as a string | ||
if (typeof options === 'string') { | ||
options = {sep: options}; | ||
} | ||
return res; | ||
} | ||
/** | ||
* expose `split` | ||
*/ | ||
var opts = extend({sep: '.'}, options); | ||
var arr = ['']; | ||
var len = str.length; | ||
var idx = -1; | ||
var closeIdx; | ||
module.exports = split; | ||
while (++idx < len) { | ||
var substr = str[idx]; | ||
var next = str[idx + 1]; | ||
if (substr === '\\') { | ||
var val = opts.keepEscaping === true ? (substr + next) : next; | ||
arr[arr.length - 1] += val; | ||
idx++; | ||
continue; | ||
} else { | ||
if (substr === '"') { | ||
closeIdx = getClose(str, '"', idx + 1); | ||
if (closeIdx === -1) { | ||
throw new Error('unclosed double quote: ' + str); | ||
} | ||
if (opts.keepDoubleQuotes === true) { | ||
substr = str.slice(idx, closeIdx + 1); | ||
} else { | ||
substr = str.slice(idx + 1, closeIdx); | ||
} | ||
idx = closeIdx; | ||
} | ||
if (substr === '\'') { | ||
closeIdx = getClose(str, '\'', idx + 1); | ||
if (closeIdx === -1) { | ||
throw new Error('unclosed double quote: ' + str); | ||
} | ||
if (opts.keepSingleQuotes === true) { | ||
substr = str.slice(idx, closeIdx + 1); | ||
} else { | ||
substr = str.slice(idx + 1, closeIdx); | ||
} | ||
idx = closeIdx; | ||
} | ||
if (substr === opts.sep) { | ||
arr.push(''); | ||
} else { | ||
arr[arr.length - 1] += substr; | ||
} | ||
} | ||
} | ||
return arr; | ||
}; | ||
function getClose(str, substr, i) { | ||
var idx = str.indexOf(substr, i); | ||
if (str.charAt(idx - 1) === '\\') { | ||
return getClose(str, substr, idx + 1); | ||
} | ||
return idx; | ||
} |
{ | ||
"name": "split-string", | ||
"description": "Split a string on a character except when the character is escaped.", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/split-string", | ||
"author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"contributors": [ | ||
"Brian Woodward <brian.woodward@gmail.com> (https://github.com/doowb)", | ||
"Jon Schlinkert <jon.schlinkert@sellside.com> (http://twitter.com/jonschlinkert)" | ||
], | ||
"repository": "jonschlinkert/split-string", | ||
@@ -23,6 +27,7 @@ "bugs": { | ||
"dependencies": { | ||
"noncharacters": "^1.1.0" | ||
"extend-shallow": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
"gulp-format-md": "^0.1.11", | ||
"mocha": "^3.2.0" | ||
}, | ||
@@ -38,9 +43,20 @@ "keywords": [ | ||
"list": [ | ||
"deromanize", | ||
"randomatic", | ||
"repeat-string", | ||
"randomatic", | ||
"deromanize", | ||
"romanize" | ||
] | ||
}, | ||
"toc": false, | ||
"layout": "default", | ||
"tasks": [ | ||
"readme" | ||
], | ||
"plugins": [ | ||
"gulp-format-md" | ||
], | ||
"lint": { | ||
"reflinks": true | ||
} | ||
} | ||
} | ||
} |
141
README.md
@@ -1,2 +0,2 @@ | ||
# split-string [![NPM version](https://badge.fury.io/js/split-string.svg)](http://badge.fury.io/js/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) | ||
@@ -7,6 +7,6 @@ > Split a string on a character except when the character is escaped. | ||
Install with [npm](https://www.npmjs.com/) | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm i split-string --save | ||
$ npm install --save split-string | ||
``` | ||
@@ -21,44 +21,139 @@ | ||
//=> ['a', 'b', 'c'] | ||
// respects escaped characters | ||
split('a.b.c\\.d'); | ||
//=> ['a', 'b', 'c.d'] | ||
// respects double-quoted strings | ||
split('a."b.c.d".e'); | ||
//=> ['a', 'b.c.d', 'e'] | ||
``` | ||
**respects escaped characters** | ||
## Options | ||
### options.sep | ||
**Type**: `String` | ||
**Default**: `.` | ||
The separator/character to split on. | ||
**Example** | ||
```js | ||
split('a.b.c\\.d'); | ||
//=> ['a', 'b', 'c.d'] | ||
split('a.b,c', {sep: ','}); | ||
//=> ['a.b', 'c'] | ||
// you can also pass the separator as string as the last argument | ||
split('a.b,c', ','); | ||
//=> ['a.b', 'c'] | ||
``` | ||
## Related projects | ||
### options.keepEscaping | ||
* [deromanize](https://www.npmjs.com/package/deromanize): Convert roman numerals to arabic numbers (useful for books, outlines, documentation, slide decks, etc) | [homepage](https://github.com/jonschlinkert/deromanize) | ||
* [randomatic](https://www.npmjs.com/package/randomatic): Generate randomized strings of a specified length, fast. Only the length is necessary, but you… [more](https://www.npmjs.com/package/randomatic) | [homepage](https://github.com/jonschlinkert/randomatic) | ||
* [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) | ||
* [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) | ||
**Type**: `Boolean` | ||
## Running tests | ||
**Default**: `undefined` | ||
Install dev dependencies: | ||
Keep backslashes in the result. | ||
**Example** | ||
```js | ||
split('a.b\\.c'); | ||
//=> ['a', 'b.c'] | ||
split('a.b.\\c', {keepEscaping: true}); | ||
//=> ['a', 'b\.c'] | ||
``` | ||
### options.keepDoubleQuotes | ||
**Type**: `Boolean` | ||
**Default**: `undefined` | ||
Keep double-quotes in the result. | ||
**Example** | ||
```js | ||
split('a."b.c.d".e'); | ||
//=> ['a', 'b.c.d', 'e'] | ||
split('a."b.c.d".e', {keepDoubleQuotes: true}); | ||
//=> ['a', 'b.c.d', 'e'] | ||
``` | ||
### options.keepSingleQuotes | ||
**Type**: `Boolean` | ||
**Default**: `undefined` | ||
Keep single-quotes in the result. | ||
**Example** | ||
```js | ||
split('a.\'b.c.d\'.e'); | ||
//=> ['a', 'b.c.d', 'e'] | ||
split('a.\'b.c.d\'.e', {keepSingleQuotes: true}); | ||
//=> ['a', 'b.c.d', 'e'] | ||
``` | ||
## About | ||
### Related projects | ||
* [deromanize](https://www.npmjs.com/package/deromanize): Convert roman numerals to arabic numbers (useful for books, outlines, documentation, slide decks, etc) | [homepage](https://github.com/jonschlinkert/deromanize "Convert roman numerals to arabic numbers (useful for books, outlines, documentation, slide decks, etc)") | ||
* [randomatic](https://www.npmjs.com/package/randomatic): Generate randomized strings of a specified length, fast. Only the length is necessary, but you… [more](https://github.com/jonschlinkert/randomatic) | [homepage](https://github.com/jonschlinkert/randomatic "Generate randomized strings of a specified length, fast. Only the length is necessary, but you can optionally generate patterns using any combination of numeric, alpha-numeric, alphabetical, special or custom characters.") | ||
* [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)") | ||
### Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
### Contributors | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 3 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 1 | [doowb](https://github.com/doowb) | | ||
### Building docs | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm i -d && npm test | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
## Contributing | ||
### Running tests | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/split-string/issues/new). | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
## Author | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
### Author | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
## License | ||
### License | ||
Copyright © 2015 Jon Schlinkert | ||
Released under the MIT license. | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
MIT | ||
*** | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 27, 2015._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 21, 2017._ |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9011
70
0
157
2
+ Addedextend-shallow@^2.0.1
+ Addedextend-shallow@2.0.1(transitive)
+ Addedis-extendable@0.1.1(transitive)
- Removednoncharacters@^1.1.0
- Removednoncharacters@1.1.0(transitive)