You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
7.6 KiB

4 years ago
  1. # 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)
  2. > Split a string on a character except when the character is escaped.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save split-string
  8. ```
  9. <!-- section: Why use this? -->
  10. <details>
  11. <summary><strong>Why use this?</strong></summary>
  12. <br>
  13. Although it's easy to split on a string:
  14. ```js
  15. console.log('a.b.c'.split('.'));
  16. //=> ['a', 'b', 'c']
  17. ```
  18. It's more challenging to split a string whilst respecting escaped or quoted characters.
  19. **Bad**
  20. ```js
  21. console.log('a\\.b.c'.split('.'));
  22. //=> ['a\\', 'b', 'c']
  23. console.log('"a.b.c".d'.split('.'));
  24. //=> ['"a', 'b', 'c"', 'd']
  25. ```
  26. **Good**
  27. ```js
  28. var split = require('split-string');
  29. console.log(split('a\\.b.c'));
  30. //=> ['a.b', 'c']
  31. console.log(split('"a.b.c".d'));
  32. //=> ['a.b.c', 'd']
  33. ```
  34. See the [options](#options) to learn how to choose the separator or retain quotes or escaping.
  35. <br>
  36. </details>
  37. ## Usage
  38. ```js
  39. var split = require('split-string');
  40. split('a.b.c');
  41. //=> ['a', 'b', 'c']
  42. // respects escaped characters
  43. split('a.b.c\\.d');
  44. //=> ['a', 'b', 'c.d']
  45. // respects double-quoted strings
  46. split('a."b.c.d".e');
  47. //=> ['a', 'b.c.d', 'e']
  48. ```
  49. **Brackets**
  50. Also respects brackets [unless disabled](#optionsbrackets):
  51. ```js
  52. split('a (b c d) e', ' ');
  53. //=> ['a', '(b c d)', 'e']
  54. ```
  55. ## Options
  56. ### options.brackets
  57. **Type**: `object|boolean`
  58. **Default**: `undefined`
  59. **Description**
  60. If enabled, split-string will not split inside brackets. The following brackets types are supported when `options.brackets` is `true`,
  61. ```js
  62. {
  63. '<': '>',
  64. '(': ')',
  65. '[': ']',
  66. '{': '}'
  67. }
  68. ```
  69. 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.
  70. **Examples**
  71. ```js
  72. // no bracket support by default
  73. split('a.{b.c}');
  74. //=> [ 'a', '{b', 'c}' ]
  75. // support all basic bracket types: "<>{}[]()"
  76. split('a.{b.c}', {brackets: true});
  77. //=> [ 'a', '{b.c}' ]
  78. // also supports nested brackets
  79. split('a.{b.{c.d}.e}.f', {brackets: true});
  80. //=> [ 'a', '{b.{c.d}.e}', 'f' ]
  81. // support only the specified brackets
  82. split('[a.b].(c.d)', {brackets: {'[': ']'}});
  83. //=> [ '[a.b]', '(c', 'd)' ]
  84. ```
  85. ### options.sep
  86. **Type**: `string`
  87. **Default**: `.`
  88. The separator/character to split on.
  89. **Example**
  90. ```js
  91. split('a.b,c', {sep: ','});
  92. //=> ['a.b', 'c']
  93. // you can also pass the separator as string as the last argument
  94. split('a.b,c', ',');
  95. //=> ['a.b', 'c']
  96. ```
  97. ### options.keepEscaping
  98. **Type**: `boolean`
  99. **Default**: `undefined`
  100. Keep backslashes in the result.
  101. **Example**
  102. ```js
  103. split('a.b\\.c');
  104. //=> ['a', 'b.c']
  105. split('a.b.\\c', {keepEscaping: true});
  106. //=> ['a', 'b\.c']
  107. ```
  108. ### options.keepQuotes
  109. **Type**: `boolean`
  110. **Default**: `undefined`
  111. Keep single- or double-quotes in the result.
  112. **Example**
  113. ```js
  114. split('a."b.c.d".e');
  115. //=> ['a', 'b.c.d', 'e']
  116. split('a."b.c.d".e', {keepQuotes: true});
  117. //=> ['a', '"b.c.d"', 'e']
  118. split('a.\'b.c.d\'.e', {keepQuotes: true});
  119. //=> ['a', '\'b.c.d\'', 'e']
  120. ```
  121. ### options.keepDoubleQuotes
  122. **Type**: `boolean`
  123. **Default**: `undefined`
  124. Keep double-quotes in the result.
  125. **Example**
  126. ```js
  127. split('a."b.c.d".e');
  128. //=> ['a', 'b.c.d', 'e']
  129. split('a."b.c.d".e', {keepDoubleQuotes: true});
  130. //=> ['a', '"b.c.d"', 'e']
  131. ```
  132. ### options.keepSingleQuotes
  133. **Type**: `boolean`
  134. **Default**: `undefined`
  135. Keep single-quotes in the result.
  136. **Example**
  137. ```js
  138. split('a.\'b.c.d\'.e');
  139. //=> ['a', 'b.c.d', 'e']
  140. split('a.\'b.c.d\'.e', {keepSingleQuotes: true});
  141. //=> ['a', '\'b.c.d\'', 'e']
  142. ```
  143. ## Customizer
  144. **Type**: `function`
  145. **Default**: `undefined`
  146. Pass a function as the last argument to customize how tokens are added to the array.
  147. **Example**
  148. ```js
  149. var arr = split('a.b', function(tok) {
  150. if (tok.arr[tok.arr.length - 1] === 'a') {
  151. tok.split = false;
  152. }
  153. });
  154. console.log(arr);
  155. //=> ['a.b']
  156. ```
  157. **Properties**
  158. The `tok` object has the following properties:
  159. * `tok.val` (string) The current value about to be pushed onto the result array
  160. * `tok.idx` (number) the current index in the string
  161. * `tok.str` (string) the entire string
  162. * `tok.arr` (array) the result array
  163. ## Release history
  164. ### v3.0.0 - 2017-06-17
  165. **Added**
  166. * adds support for brackets
  167. ## About
  168. <details>
  169. <summary><strong>Contributing</strong></summary>
  170. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  171. </details>
  172. <details>
  173. <summary><strong>Running Tests</strong></summary>
  174. 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:
  175. ```sh
  176. $ npm install && npm test
  177. ```
  178. </details>
  179. <details>
  180. <summary><strong>Building docs</strong></summary>
  181. _(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.)_
  182. To generate the readme, run the following command:
  183. ```sh
  184. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  185. ```
  186. </details>
  187. ### Related projects
  188. You might also be interested in these projects:
  189. * [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)")
  190. * [randomatic](https://www.npmjs.com/package/randomatic): Generate randomized strings of a specified length using simple character sequences. The original generate-password. | [homepage](https://github.com/jonschlinkert/randomatic "Generate randomized strings of a specified length using simple character sequences. The original generate-password.")
  191. * [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.")
  192. * [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)")
  193. ### Contributors
  194. | **Commits** | **Contributor** |
  195. | --- | --- |
  196. | 28 | [jonschlinkert](https://github.com/jonschlinkert) |
  197. | 9 | [doowb](https://github.com/doowb) |
  198. ### Author
  199. **Jon Schlinkert**
  200. * [github/jonschlinkert](https://github.com/jonschlinkert)
  201. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  202. ### License
  203. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  204. Released under the [MIT License](LICENSE).
  205. ***
  206. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on November 19, 2017._