Package detail

split-string

jonschlinkert49.1mMIT6.1.0

Easy way to split a string on a given character unless it's quoted or escaped.

character, escape, split, string

readme

split-string NPM version NPM monthly downloads NPM total downloads Linux Build Status

Easy way to split a string on a given character unless it's quoted or escaped.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm:

$ npm install --save split-string

Usage

const split = require('split-string');

console.log(split('a.b.c'));
//=> ['a', 'b', 'c']

// respects escaped characters
console.log(split('a.b.c\\.d'));
//=> ['a', 'b', 'c.d']

// respects double-quoted strings
console.log(split('a."b.c.d".e'));
//=> ['a', '"b.c.d"', 'e']

Options

options.quotes

Type: Array|Boolean

Default: []

Description

Tell split-string not to split inside any of the quote characters specified on the quotes option. Each character signifies both the "opening" and "closing" character to use.

// default behavior
console.log(split('a.b."c.d.e.f.g".h.i'));
//=> [ 'a', 'b', '"c', 'd', 'e', 'f', 'g"', 'h', 'i' ]

// with quotes
console.log(split('a.b."c.d.e.f.g".h.i', { quotes: ['"'] }));
//=> [ 'a', 'b', '"c.d.e.f.g"', 'h', 'i' ]

// escaped quotes will be ignored
console.log(split('a.b.\\"c.d."e.f.g".h.i', { quotes: ['"'] }));
//=> [ 'a', 'b', '"c', 'd', '"e.f.g"', 'h', 'i' ]

// example of how to exclude non-escaped quotes from the result
let keep = (value, state) => {
  return value !== '\\' && (value !== '"' || state.prev() === '\\');
};
console.log(split('a.b.\\"c.d."e.f.g".h.i', { quotes: ['"'], keep }));
//=> [ 'a', 'b', '"c', 'd', 'e.f.g', 'h', 'i' ]

Options

options.brackets

Type: Object|Boolean

Default: {}

Description

By default, no special significance is given to bracket-like characters (such as square brackets, curly braces, angle brackets, and so on).

// default behavior
console.log(split('a.{b.c}.{d.e}'));
//=> [ 'a', '{b', 'c}', '{d', 'e}' ]

When options.brackets is true, the following brackets types are supported:

{
  '<': '>',
  '(': ')',
  '[': ']',
  '{': '}'
}

For example:

console.log(split('a.{b.c}.{d.e}', { brackets: true }));
//=> [ 'a', '{b.c}', '{d.e}' ]

Alternatively, an object of brackets may be passed, where each key is the opening bracket and each value is the corresponding closing bracket. Note that the key and value must be different characters. If you want to use the same character for both open and close, use the quotes option.

Examples

// no bracket support by default
console.log(split('a.{b.c}.[d.e].f'));
//=> [ 'a', '{b', 'c}', '[d', 'e]', 'f' ]

// tell split-string not to split inside curly braces
console.log(split('a.{b.c}.[d.e].f', { brackets: { '{': '}' }}));
//=> [ 'a', '{b.c}', '[d', 'e]', 'f' ]

// tell split-string not to split inside any of these types: "<>{}[]()"
console.log(split('a.{b.c}.[d.e].f', { brackets: true }));
//=> [ 'a', '{b.c}', '[d.e]', 'f' ]

// ...nested brackets are also supported
console.log(split('a.{b.{c.d}.e}.f', { brackets: true }));
//=> [ 'a', '{b.{c.d}.e}', 'f' ]

// tell split-string not to split inside the given custom types
console.log(split('«a.b».⟨c.d⟩.[e.f]', { brackets: { '«': '»', '⟨': '⟩' } }));
//=> [ '«a.b»', '⟨c.d⟩', '[e', 'f]' ]

options.keep

Type: function

Default: Function that returns true if the character is not \\.

Function that returns true when a character should be retained in the result.

Example

console.log(split('a.b\\.c')); //=> ['a', 'b.c']

// keep all characters
console.log(split('a.b.\\c', { keep: () => true })); //=> ['a', 'b\.c']

options.separator

Type: string

Default: .

The character to split on.

Example

console.log(split('a.b,c', { separator: ',' })); //=> ['a.b', 'c']

Split function

Optionally pass a function as the last argument to tell split-string whether or not to split when the specified separator is encountered.

Example

// only split on "." when the "previous" character is "a"
console.log(split('a.b.c.a.d.e', state => state.prev() === 'a'));
//=> [ 'a', 'b.c.a', 'd.e' ]

The state object exposes the following properties:

  • input - (String) The un-modified, user-defined input string
  • separator - (String) the specified separator to split on.
  • index - (Number) The current cursor position
  • value - (String) The character at the current index
  • bos - (Function) Returns true if position is at the beginning-of-string
  • eos - (Function) Returns true if position is at the end-of-string
  • prev - (Function) Returns the previously scanned character
  • next - (Function) Returns the next character after the current position
  • block - (Object) The "current" AST node.
  • stack - (Array) AST nodes

About

<summary>Contributing</summary> Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
<summary>Running Tests</summary> 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: sh $ npm install && npm test
<summary>Building docs</summary> (This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.) To generate the readme, run the following command: sh $ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

  • deromanize: Convert roman numerals to arabic numbers (useful for books, outlines, documentation, slide decks, etc) | homepage
  • randomatic: Generate randomized strings of a specified length using simple character sequences. The original generate-password. | homepage
  • repeat-string: Repeat the given string n times. Fastest implementation for repeating a string. | homepage
  • romanize: Convert numbers to roman numerals (useful for books, outlines, documentation, slide decks, etc) | homepage

Contributors

Commits Contributor
56 jonschlinkert
12 doowb
6 Ovyerus
1 silverwind

Author

Jon Schlinkert

License

Copyright © 2019, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on April 22, 2019.

changelog

Release history

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

<summary>Guiding Principles</summary> - Changelogs are for humans, not machines. - There should be an entry for every single version. - The same types of changes should be grouped. - Versions and sections should be linkable. - The latest version comes first. - The release date of each versions is displayed. - Mention whether you follow Semantic Versioning.
<summary>Types of changes</summary> Changelog entries are classified using the following labels (from keep-a-changelog): - Added for new features. - Changed for changes in existing functionality. - Deprecated for soon-to-be removed features. - Removed for now removed features. - Fixed for any bug fixes. - Security in case of vulnerabilities.

[6.1.0] - 2019-04-22

Added

  • Add TypeScript definitions and tests.

[6.0.0] - 2018-08-14

  • Refactor to use faster scanner

Removed

The following options were all removed:

  • options.split - support for the split option was removed, but you may still pass a custom function for this as the last argument.
  • options.keepQuotes
  • options.keepSmartQuotes
  • options.keepDoubleQuotes
  • options.keepSingleQuotes
  • options.keepBackticks

Added

  • options.keep - function for determining whether or not to keep a character.

5.0.1 - 2018-01-08

  • update engine versions

5.0.0 - 2018-01-08

  • tweak quotes handling
  • refactor to use snapdragon-lexer
  • Merge pull request #5 from jonschlinkert/refactor

4.0.0 - 2017-11-22

  • allow options.quotes to be an object
  • Merge pull request #4 from jonschlinkert/quotes-object

3.1.0 - 2017-11-19

  • support keepEscaping as a function
  • run lint-deps to upgrade deps

3.0.2 - 2017-06-21

  • fix examples

3.0.1 - 2017-06-15

  • fix examples
  • remove unnecessary brackets

3.0.0 - 2017-06-15

  • support brackets
  • support nested brackets
  • update release history, run verb

2.1.1 - 2017-05-30

  • call custom function on escaped tokens

2.1.0 - 2017-04-27

  • support backticks
  • update docs

2.0.0 - 2017-04-11

  • example callback
  • adds support for a sync callback
  • update docs, run verb

1.0.1 - 2017-04-11

  • fix error message for unclosed quote
  • allow disabling strict closing of quotes
  • add tests for errors and strict option
  • Merge pull request #2 from jonschlinkert/strict
  • add documentation on options.strict

1.0.0 - 2017-02-21

  • refactor
  • update docs, run verb

[0.1.1] - 2015-08-27

  • first commit
  • only use 1 nonchar instead of entire array
  • Merge pull request #1 from doowb/master