Gathering detailed insights and metrics for split-string
Gathering detailed insights and metrics for split-string
Gathering detailed insights and metrics for split-string
Gathering detailed insights and metrics for split-string
Split a string on a given character or characters, with support for escaping.
npm install split-string
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
57 Stars
78 Commits
17 Forks
4 Watching
1 Branches
4 Contributors
Updated on 03 Nov 2024
Minified
Minified + Gzipped
JavaScript (94.13%)
TypeScript (5.87%)
Cumulative downloads
Total Downloads
Last day
-4.7%
2,889,254
Compared to previous day
Last week
5.8%
16,867,445
Compared to previous week
Last month
36.1%
61,132,265
Compared to previous month
Last year
-19.2%
619,491,301
Compared to previous year
4
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 with npm:
1$ npm install --save split-string
1const split = require('split-string'); 2 3console.log(split('a.b.c')); 4//=> ['a', 'b', 'c'] 5 6// respects escaped characters 7console.log(split('a.b.c\\.d')); 8//=> ['a', 'b', 'c.d'] 9 10// respects double-quoted strings 11console.log(split('a."b.c.d".e')); 12//=> ['a', '"b.c.d"', 'e']
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.
1// default behavior 2console.log(split('a.b."c.d.e.f.g".h.i')); 3//=> [ 'a', 'b', '"c', 'd', 'e', 'f', 'g"', 'h', 'i' ] 4 5// with quotes 6console.log(split('a.b."c.d.e.f.g".h.i', { quotes: ['"'] })); 7//=> [ 'a', 'b', '"c.d.e.f.g"', 'h', 'i' ] 8 9// escaped quotes will be ignored 10console.log(split('a.b.\\"c.d."e.f.g".h.i', { quotes: ['"'] })); 11//=> [ 'a', 'b', '"c', 'd', '"e.f.g"', 'h', 'i' ] 12 13// example of how to exclude non-escaped quotes from the result 14let keep = (value, state) => { 15 return value !== '\\' && (value !== '"' || state.prev() === '\\'); 16}; 17console.log(split('a.b.\\"c.d."e.f.g".h.i', { quotes: ['"'], keep })); 18//=> [ 'a', 'b', '"c', 'd', 'e.f.g', 'h', 'i' ]
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).
1// default behavior 2console.log(split('a.{b.c}.{d.e}')); 3//=> [ 'a', '{b', 'c}', '{d', 'e}' ]
When options.brackets
is true
, the following brackets types are supported:
1{ 2 '<': '>', 3 '(': ')', 4 '[': ']', 5 '{': '}' 6}
For example:
1console.log(split('a.{b.c}.{d.e}', { brackets: true })); 2//=> [ '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
1// no bracket support by default 2console.log(split('a.{b.c}.[d.e].f')); 3//=> [ 'a', '{b', 'c}', '[d', 'e]', 'f' ] 4 5// tell split-string not to split inside curly braces 6console.log(split('a.{b.c}.[d.e].f', { brackets: { '{': '}' }})); 7//=> [ 'a', '{b.c}', '[d', 'e]', 'f' ] 8 9// tell split-string not to split inside any of these types: "<>{}[]()" 10console.log(split('a.{b.c}.[d.e].f', { brackets: true })); 11//=> [ 'a', '{b.c}', '[d.e]', 'f' ] 12 13// ...nested brackets are also supported 14console.log(split('a.{b.{c.d}.e}.f', { brackets: true })); 15//=> [ 'a', '{b.{c.d}.e}', 'f' ] 16 17// tell split-string not to split inside the given custom types 18console.log(split('«a.b».⟨c.d⟩.[e.f]', { brackets: { '«': '»', '⟨': '⟩' } })); 19//=> [ '«a.b»', '⟨c.d⟩', '[e', 'f]' ]
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
1console.log(split('a.b\\.c')); //=> ['a', 'b.c'] 2 3// keep all characters 4console.log(split('a.b.\\c', { keep: () => true })); //=> ['a', 'b\.c']
Type: string
Default: .
The character to split on.
Example
1console.log(split('a.b,c', { separator: ',' })); //=> ['a.b', 'c']
Optionally pass a function as the last argument to tell split-string whether or not to split when the specified separator is encountered.
Example
1// only split on "." when the "previous" character is "a" 2console.log(split('a.b.c.a.d.e', state => state.prev() === 'a')); 3//=> [ 'a', 'b.c.a', 'd.e' ]
The state
object exposes the following properties:
input
- (String) The un-modified, user-defined input stringseparator
- (String) the specified separator to split on.index
- (Number) The current cursor positionvalue
- (String) The character at the current indexbos
- (Function) Returns true if position is at the beginning-of-stringeos
- (Function) Returns true if position is at the end-of-stringprev
- (Function) Returns the previously scanned characternext
- (Function) Returns the next character after the current positionblock
- (Object) The "current" AST node.stack
- (Array) AST nodesPull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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:
1$ npm install && npm test
(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:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
Commits | Contributor |
---|---|
56 | jonschlinkert |
12 | doowb |
6 | Ovyerus |
1 | silverwind |
Jon Schlinkert
Copyright © 2019, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on April 22, 2019.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/21 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More