[Pkg-javascript-commits] [node-split-string] 01/02: New upstream version 4.0.0

Julien Puydt julien.puydt at laposte.net
Sat Nov 25 21:04:42 UTC 2017


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-split-string.

commit 3abea0d93e80425de27e9962c19e06e0e25c0140
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Sat Nov 25 21:50:49 2017 +0100

    New upstream version 4.0.0
---
 .editorconfig  |  14 +++
 .eslintrc.json | 117 +++++++++++++++++++++
 .gitattributes |  10 ++
 .gitignore     |  29 ++++++
 .travis.yml    |  15 +++
 .verb.md       | 244 +++++++++++++++++++++++++++++++++++++++++++
 LICENSE        |  21 ++++
 README.md      | 321 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 example.js     |  13 +++
 index.js       | 183 ++++++++++++++++++++++++++++++++
 package.json   |  65 ++++++++++++
 test.js        | 178 ++++++++++++++++++++++++++++++++
 12 files changed, 1210 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..449f0da
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,14 @@
+# http://editorconfig.org/
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+indent_size = 2
+indent_style = space
+insert_final_newline = true
+trim_trailing_whitespace = true
+
+[{**/{actual,fixtures,expected,templates}/**,*.md}]
+trim_trailing_whitespace = false
+insert_final_newline = false
diff --git a/.eslintrc.json b/.eslintrc.json
new file mode 100644
index 0000000..61e8895
--- /dev/null
+++ b/.eslintrc.json
@@ -0,0 +1,117 @@
+{
+  "env": {
+    "browser": false,
+    "es6": true,
+    "node": true,
+    "mocha": true
+  },
+
+  "globals": {
+    "document": false,
+    "navigator": false,
+    "window": false
+  },
+
+  "rules": {
+    "accessor-pairs": 2,
+    "arrow-spacing": [2, { "before": true, "after": true }],
+    "block-spacing": [2, "always"],
+    "brace-style": [2, "1tbs", { "allowSingleLine": true }],
+    "comma-dangle": [2, "never"],
+    "comma-spacing": [2, { "before": false, "after": true }],
+    "comma-style": [2, "last"],
+    "constructor-super": 2,
+    "curly": [2, "multi-line"],
+    "dot-location": [2, "property"],
+    "eol-last": 2,
+    "eqeqeq": [2, "allow-null"],
+    "generator-star-spacing": [2, { "before": true, "after": true }],
+    "handle-callback-err": [2, "^(err|error)$" ],
+    "indent": [2, 2, { "SwitchCase": 1 }],
+    "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
+    "keyword-spacing": [2, { "before": true, "after": true }],
+    "new-cap": [2, { "newIsCap": true, "capIsNew": false }],
+    "new-parens": 2,
+    "no-array-constructor": 2,
+    "no-caller": 2,
+    "no-class-assign": 2,
+    "no-cond-assign": 2,
+    "no-const-assign": 2,
+    "no-control-regex": 2,
+    "no-debugger": 2,
+    "no-delete-var": 2,
+    "no-dupe-args": 2,
+    "no-dupe-class-members": 2,
+    "no-dupe-keys": 2,
+    "no-duplicate-case": 2,
+    "no-empty-character-class": 2,
+    "no-eval": 2,
+    "no-ex-assign": 2,
+    "no-extend-native": 2,
+    "no-extra-bind": 2,
+    "no-extra-boolean-cast": 2,
+    "no-extra-parens": [2, "functions"],
+    "no-fallthrough": 2,
+    "no-floating-decimal": 2,
+    "no-func-assign": 2,
+    "no-implied-eval": 2,
+    "no-inner-declarations": [2, "functions"],
+    "no-invalid-regexp": 2,
+    "no-irregular-whitespace": 2,
+    "no-iterator": 2,
+    "no-label-var": 2,
+    "no-labels": 2,
+    "no-lone-blocks": 2,
+    "no-mixed-spaces-and-tabs": 2,
+    "no-multi-spaces": 2,
+    "no-multi-str": 2,
+    "no-multiple-empty-lines": [2, { "max": 1 }],
+    "no-native-reassign": 0,
+    "no-negated-in-lhs": 2,
+    "no-new": 2,
+    "no-new-func": 2,
+    "no-new-object": 2,
+    "no-new-require": 2,
+    "no-new-wrappers": 2,
+    "no-obj-calls": 2,
+    "no-octal": 2,
+    "no-octal-escape": 2,
+    "no-proto": 0,
+    "no-redeclare": 2,
+    "no-regex-spaces": 2,
+    "no-return-assign": 2,
+    "no-self-compare": 2,
+    "no-sequences": 2,
+    "no-shadow-restricted-names": 2,
+    "no-spaced-func": 2,
+    "no-sparse-arrays": 2,
+    "no-this-before-super": 2,
+    "no-throw-literal": 2,
+    "no-trailing-spaces": 0,
+    "no-undef": 2,
+    "no-undef-init": 2,
+    "no-unexpected-multiline": 2,
+    "no-unneeded-ternary": [2, { "defaultAssignment": false }],
+    "no-unreachable": 2,
+    "no-unused-vars": [2, { "vars": "all", "args": "none" }],
+    "no-useless-call": 0,
+    "no-with": 2,
+    "one-var": [0, { "initialized": "never" }],
+    "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
+    "padded-blocks": [0, "never"],
+    "quotes": [2, "single", "avoid-escape"],
+    "radix": 2,
+    "semi": [2, "always"],
+    "semi-spacing": [2, { "before": false, "after": true }],
+    "space-before-blocks": [2, "always"],
+    "space-before-function-paren": [2, "never"],
+    "space-in-parens": [2, "never"],
+    "space-infix-ops": 2,
+    "space-unary-ops": [2, { "words": true, "nonwords": false }],
+    "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
+    "use-isnan": 2,
+    "valid-typeof": 2,
+    "wrap-iife": [2, "any"],
+    "yoda": [2, "never"]
+  }
+}
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..660957e
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,10 @@
+# Enforce Unix newlines
+* text eol=lf
+
+# binaries
+*.ai binary
+*.psd binary
+*.jpg binary
+*.gif binary
+*.png binary
+*.jpeg binary
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4bf0a60
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,29 @@
+# always ignore files
+*.DS_Store
+.idea
+*.sublime-*
+
+# test related, or directories generated by tests
+test/actual
+actual
+coverage
+.nyc*
+
+# npm
+node_modules
+npm-debug.log
+
+# yarn
+yarn.lock
+yarn-error.log
+
+# misc
+_gh_pages
+_draft
+_drafts
+bower_components
+vendor
+temp
+tmp
+TODO.md
+package-lock.json
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..1686664
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,15 @@
+sudo: false
+os:
+  - linux
+  - osx
+language: node_js
+node_js:
+  - node
+  - '9'
+  - '8'
+  - '7'
+  - '6'
+  - '5'
+  - '4'
+  - '0.12'
+  - '0.10'
diff --git a/.verb.md b/.verb.md
new file mode 100644
index 0000000..3179632
--- /dev/null
+++ b/.verb.md
@@ -0,0 +1,244 @@
+<!-- section: Why use this? -->
+<details>
+  <summary><strong>Why use this?</strong></summary>
+
+<br>
+
+Although it's easy to split on a string:
+
+```js
+console.log('a.b.c'.split('.'));
+//=> ['a', 'b', 'c']
+```
+
+It's more challenging to split a string whilst respecting escaped or quoted characters.
+
+**Bad**
+
+```js
+console.log('a\\.b.c'.split('.'));
+//=> ['a\\', 'b', 'c']
+
+console.log('"a.b.c".d'.split('.'));
+//=> ['"a', 'b', 'c"', 'd']
+```
+
+**Good**
+
+```js
+var split = require('{%= name %}');
+console.log(split('a\\.b.c'));
+//=> ['a.b', 'c']
+
+console.log(split('"a.b.c".d'));
+//=> ['a.b.c', 'd']
+```
+
+See the [options](#options) to learn how to choose the separator or retain quotes or escaping.
+
+<br>
+
+</details>
+
+
+## Usage
+
+```js
+var split = require('{%= name %}');
+
+split('a.b.c');
+//=> ['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']
+```
+
+**Brackets**
+
+Also respects brackets [unless disabled](#optionsbrackets):
+
+```js
+split('a (b c d) e', ' ');
+//=> ['a', '(b c d)', 'e']
+```
+
+## Options
+
+### options.brackets
+
+**Type**: `object|boolean` 
+
+**Default**: `undefined`
+
+**Description**
+
+If enabled, split-string will not split inside brackets. The following brackets types are supported when `options.brackets` is `true`,
+
+```js
+{
+  '<': '>',
+  '(': ')',
+  '[': ']',
+  '{': '}'
+}
+```
+
+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.
+
+**Examples**
+
+```js
+// no bracket support by default
+split('a.{b.c}');
+//=> [ 'a', '{b', 'c}' ]
+
+// support all basic bracket types: "<>{}[]()"
+split('a.{b.c}', {brackets: true});
+//=> [ 'a', '{b.c}' ]
+
+// also supports nested brackets 
+split('a.{b.{c.d}.e}.f', {brackets: true});
+//=> [ 'a', '{b.{c.d}.e}', 'f' ]
+
+// support only the specified brackets
+split('[a.b].(c.d)', {brackets: {'[': ']'}});
+//=> [ '[a.b]', '(c', 'd)' ]
+```
+
+### options.sep
+
+**Type**: `string`
+
+**Default**: `.`
+
+The separator/character to split on.
+
+**Example**
+
+```js
+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']
+```
+
+### options.keepEscaping
+
+**Type**: `boolean`
+
+**Default**: `undefined`
+
+Keep backslashes in the result.
+
+**Example**
+
+```js
+split('a.b\\.c');
+//=> ['a', 'b.c']
+
+split('a.b.\\c', {keepEscaping: true});
+//=> ['a', 'b\.c']
+```
+
+### 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
+
+**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']
+```
+
+
+## Customizer
+
+**Type**: `function`
+
+**Default**: `undefined`
+
+Pass a function as the last argument to customize how tokens are added to the array.
+
+**Example**
+
+```js
+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
+
+
+## Release history
+
+### v3.0.0 - 2017-06-17
+
+**Added**
+
+- adds support for brackets
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..e33d14b
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2015-2017, Jon Schlinkert.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..26a6ae1
--- /dev/null
+++ b/README.md
@@ -0,0 +1,321 @@
+# 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) [...]
+
+> Split a string on a character except when the character is escaped.
+
+Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
+
+## Install
+
+Install with [npm](https://www.npmjs.com/):
+
+```sh
+$ npm install --save split-string
+```
+
+<!-- section: Why use this? -->
+
+<details>
+<summary><strong>Why use this?</strong></summary>
+
+<br>
+
+Although it's easy to split on a string:
+
+```js
+console.log('a.b.c'.split('.'));
+//=> ['a', 'b', 'c']
+```
+
+It's more challenging to split a string whilst respecting escaped or quoted characters.
+
+**Bad**
+
+```js
+console.log('a\\.b.c'.split('.'));
+//=> ['a\\', 'b', 'c']
+
+console.log('"a.b.c".d'.split('.'));
+//=> ['"a', 'b', 'c"', 'd']
+```
+
+**Good**
+
+```js
+var split = require('split-string');
+console.log(split('a\\.b.c'));
+//=> ['a.b', 'c']
+
+console.log(split('"a.b.c".d'));
+//=> ['a.b.c', 'd']
+```
+
+See the [options](#options) to learn how to choose the separator or retain quotes or escaping.
+
+<br>
+
+</details>
+
+## Usage
+
+```js
+var split = require('split-string');
+
+split('a.b.c');
+//=> ['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']
+```
+
+**Brackets**
+
+Also respects brackets [unless disabled](#optionsbrackets):
+
+```js
+split('a (b c d) e', ' ');
+//=> ['a', '(b c d)', 'e']
+```
+
+## Options
+
+### options.brackets
+
+**Type**: `object|boolean`
+
+**Default**: `undefined`
+
+**Description**
+
+If enabled, split-string will not split inside brackets. The following brackets types are supported when `options.brackets` is `true`,
+
+```js
+{
+  '<': '>',
+  '(': ')',
+  '[': ']',
+  '{': '}'
+}
+```
+
+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.
+
+**Examples**
+
+```js
+// no bracket support by default
+split('a.{b.c}');
+//=> [ 'a', '{b', 'c}' ]
+
+// support all basic bracket types: "<>{}[]()"
+split('a.{b.c}', {brackets: true});
+//=> [ 'a', '{b.c}' ]
+
+// also supports nested brackets 
+split('a.{b.{c.d}.e}.f', {brackets: true});
+//=> [ 'a', '{b.{c.d}.e}', 'f' ]
+
+// support only the specified brackets
+split('[a.b].(c.d)', {brackets: {'[': ']'}});
+//=> [ '[a.b]', '(c', 'd)' ]
+```
+
+### options.sep
+
+**Type**: `string`
+
+**Default**: `.`
+
+The separator/character to split on.
+
+**Example**
+
+```js
+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']
+```
+
+### options.keepEscaping
+
+**Type**: `boolean`
+
+**Default**: `undefined`
+
+Keep backslashes in the result.
+
+**Example**
+
+```js
+split('a.b\\.c');
+//=> ['a', 'b.c']
+
+split('a.b.\\c', {keepEscaping: true});
+//=> ['a', 'b\.c']
+```
+
+### 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
+
+**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']
+```
+
+## Customizer
+
+**Type**: `function`
+
+**Default**: `undefined`
+
+Pass a function as the last argument to customize how tokens are added to the array.
+
+**Example**
+
+```js
+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
+
+## Release history
+
+### v3.0.0 - 2017-06-17
+
+**Added**
+
+* adds support for brackets
+
+## About
+
+<details>
+<summary><strong>Contributing</strong></summary>
+
+Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
+
+</details>
+
+<details>
+<summary><strong>Running Tests</strong></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
+```
+
+</details>
+
+<details>
+<summary><strong>Building docs</strong></summary>
+
+_(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 install -g verbose/verb#dev verb-generate-readme && verb
+```
+
+</details>
+
+### Related projects
+
+You might also be interested in these 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 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.")
+* [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 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)")
+
+### Contributors
+
+| **Commits** | **Contributor** | 
+| --- | --- |
+| 34 | [jonschlinkert](https://github.com/jonschlinkert) |
+| 10 | [doowb](https://github.com/doowb) |
+
+### Author
+
+**Jon Schlinkert**
+
+* [github/jonschlinkert](https://github.com/jonschlinkert)
+* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
+
+### License
+
+Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
+Released under the [MIT License](LICENSE).
+
+***
+
+_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on November 22, 2017._
\ No newline at end of file
diff --git a/example.js b/example.js
new file mode 100644
index 0000000..37cd1ee
--- /dev/null
+++ b/example.js
@@ -0,0 +1,13 @@
+var split = require('./');
+// var arr = split('a.b', function(tok) {
+//   if (tok.arr[tok.arr.length - 1] === 'a') {
+//     tok.split = false;
+//   }
+// });
+// console.log(arr);
+//=> ['a.b']
+
+var brackets = split('a.{a.{b.c}.}.c', {brackets: true});
+var brackets = split('a.{a.{b.c.}.c', {brackets: true});
+console.log(brackets);
+//=> ['a.b']
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..b440a82
--- /dev/null
+++ b/index.js
@@ -0,0 +1,183 @@
+/*!
+ * split-string <https://github.com/jonschlinkert/split-string>
+ *
+ * Copyright (c) 2015-2017, Jon Schlinkert.
+ * Released under the MIT License.
+ */
+
+'use strict';
+
+var extend = require('extend-shallow');
+
+module.exports = function(str, options, fn) {
+  if (typeof str !== 'string') {
+    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 };
+  }
+
+  var opts = extend({sep: '.'}, options);
+  var quotes = opts.quotes || {
+    '"': '"',
+    "'": "'",
+    '`': '`',
+    '“': '”'
+  };
+
+  if (Array.isArray(quotes)) {
+    quotes = quotes.reduce(function(acc, ele) {
+      acc[ele] = ele;
+      return acc;
+    }, {});
+  }
+
+  var brackets;
+  if (opts.brackets === true) {
+    brackets = {
+      '<': '>',
+      '(': ')',
+      '[': ']',
+      '{': '}'
+    };
+  } else if (opts.brackets) {
+    brackets = opts.brackets;
+  }
+
+  var tokens = [];
+  var stack = [];
+  var arr = [''];
+  var sep = opts.sep;
+  var len = str.length;
+  var idx = -1;
+  var closeIdx;
+
+  function expected() {
+    if (brackets && stack.length) {
+      return brackets[stack[stack.length - 1]];
+    }
+  }
+
+  while (++idx < len) {
+    var ch = str[idx];
+    var next = str[idx + 1];
+    var tok = { val: ch, idx: idx, arr: arr, str: str };
+    tokens.push(tok);
+
+    if (ch === '\\') {
+      tok.val = keepEscaping(opts, str, idx) === true ? (ch + next) : next;
+      tok.escaped = true;
+      if (typeof fn === 'function') {
+        fn(tok);
+      }
+      arr[arr.length - 1] += tok.val;
+      idx++;
+      continue;
+    }
+
+    if (brackets && brackets[ch]) {
+      stack.push(ch);
+      var e = expected();
+      var i = idx + 1;
+
+      if (str.indexOf(e, i + 1) !== -1) {
+        while (stack.length && i < len) {
+          var s = str[++i];
+          if (s === '\\') {
+            s++;
+            continue;
+          }
+
+          if (quotes[s]) {
+            i = getClosingQuote(str, quotes[s], i + 1);
+            continue;
+          }
+
+          e = expected();
+          if (stack.length && str.indexOf(e, i + 1) === -1) {
+            break;
+          }
+
+          if (brackets[s]) {
+            stack.push(s);
+            continue;
+          }
+
+          if (e === s) {
+            stack.pop();
+          }
+        }
+      }
+
+      closeIdx = i;
+      if (closeIdx === -1) {
+        arr[arr.length - 1] += ch;
+        continue;
+      }
+
+      ch = str.slice(idx, closeIdx + 1);
+      tok.val = ch;
+      tok.idx = idx = closeIdx;
+    }
+
+    if (quotes[ch]) {
+      closeIdx = getClosingQuote(str, quotes[ch], idx + 1);
+      if (closeIdx === -1) {
+        arr[arr.length - 1] += ch;
+        continue;
+      }
+
+      if (keepQuotes(ch, opts) === true) {
+        ch = str.slice(idx, closeIdx + 1);
+      } else {
+        ch = str.slice(idx + 1, closeIdx);
+      }
+
+      tok.val = ch;
+      tok.idx = idx = closeIdx;
+    }
+
+    if (typeof fn === 'function') {
+      fn(tok, tokens);
+      ch = tok.val;
+      idx = tok.idx;
+    }
+
+    if (tok.val === sep && tok.split !== false) {
+      arr.push('');
+      continue;
+    }
+
+    arr[arr.length - 1] += tok.val;
+  }
+
+  return arr;
+};
+
+function getClosingQuote(str, ch, i, brackets) {
+  var idx = str.indexOf(ch, i);
+  if (str.charAt(idx - 1) === '\\') {
+    return getClosingQuote(str, ch, idx + 1);
+  }
+  return idx;
+}
+
+function keepQuotes(ch, opts) {
+  if (opts.keepDoubleQuotes === true && (ch === '"' || ch === '“' || ch === '”')) return true;
+  if (opts.keepSingleQuotes === true && ch === "'") return true;
+  return opts.keepQuotes;
+}
+
+function keepEscaping(opts, str, idx) {
+  if (typeof opts.keepEscaping === 'function') {
+    return opts.keepEscaping(str, idx);
+  }
+  return opts.keepEscaping === true || str[idx + 1] === '\\';
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c0f2cc3
--- /dev/null
+++ b/package.json
@@ -0,0 +1,65 @@
+{
+  "name": "split-string",
+  "description": "Split a string on a character except when the character is escaped.",
+  "version": "4.0.0",
+  "homepage": "https://github.com/jonschlinkert/split-string",
+  "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
+  "contributors": [
+    "Brian Woodward (https://twitter.com/doowb)",
+    "Jon Schlinkert (http://twitter.com/jonschlinkert)"
+  ],
+  "repository": "jonschlinkert/split-string",
+  "bugs": {
+    "url": "https://github.com/jonschlinkert/split-string/issues"
+  },
+  "license": "MIT",
+  "files": [
+    "index.js"
+  ],
+  "main": "index.js",
+  "engines": {
+    "node": ">=0.10.0"
+  },
+  "scripts": {
+    "test": "mocha"
+  },
+  "dependencies": {
+    "extend-shallow": "^3.0.0"
+  },
+  "devDependencies": {
+    "gulp-format-md": "^1.0.0",
+    "mocha": "^3.5.3"
+  },
+  "keywords": [
+    "character",
+    "escape",
+    "split",
+    "string"
+  ],
+  "verb": {
+    "toc": false,
+    "layout": "default",
+    "titles": [
+      ".",
+      "install",
+      "Why use this?"
+    ],
+    "related": {
+      "list": [
+        "deromanize",
+        "randomatic",
+        "repeat-string",
+        "romanize"
+      ]
+    },
+    "tasks": [
+      "readme"
+    ],
+    "plugins": [
+      "gulp-format-md"
+    ],
+    "lint": {
+      "reflinks": true
+    }
+  }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..02a3d07
--- /dev/null
+++ b/test.js
@@ -0,0 +1,178 @@
+'use strict';
+
+require('mocha');
+var assert = require('assert');
+var split = require('./');
+
+describe('split-string', function() {
+  describe('split-string', function() {
+    it('should split a string on the given character:', function() {
+      assert.deepEqual(split('a/b/c', '/'), ['a', 'b', 'c']);
+    });
+
+    it('should not split on an escaped character:', function() {
+      assert.deepEqual(split('a/b/c\\/d', '/'), ['a', 'b', 'c/d']);
+    });
+
+    it('should split a string on dots by default:', function() {
+      assert.deepEqual(split('a.b.c'), ['a', 'b', 'c']);
+    });
+
+    it('should respect double-quoted strings', function() {
+      assert.deepEqual(split('"b.c"'), ['b.c']);
+      assert.deepEqual(split('a."b.c"'), ['a', 'b.c']);
+      assert.deepEqual(split('a".b.c"'), ['a.b.c']);
+      assert.deepEqual(split('a."b.c".d'), ['a', 'b.c', 'd']);
+      assert.deepEqual(split('a."b.c".d.".e.f.g.".h'), ['a', 'b.c', 'd', '.e.f.g.', 'h']);
+    });
+
+    it('should respect singlequoted strings', function() {
+      assert.deepEqual(split('\'b.c\''), ['b.c']);
+      assert.deepEqual(split('a.\'b.c\''), ['a', 'b.c']);
+      assert.deepEqual(split('a.\'b.c\'.d'), ['a', 'b.c', 'd']);
+      assert.deepEqual(split('a.\'b.c\'.d.\'.e.f.g.\'.h'), ['a', 'b.c', 'd', '.e.f.g.', 'h']);
+    });
+
+    it('should respect strings in backticks', function() {
+      assert.deepEqual(split('`b.c`'), ['b.c']);
+      assert.deepEqual(split('a.`b.c`'), ['a', 'b.c']);
+      assert.deepEqual(split('a.`b.c`.d'), ['a', 'b.c', 'd']);
+      assert.deepEqual(split('a.`b.c`.d.`.e.f.g.`.h'), ['a', 'b.c', 'd', '.e.f.g.', 'h']);
+    });
+
+    it('should respect strings in “” double quotes', function() {
+      assert.deepEqual(split('“b.c”'), ['b.c']);
+      assert.deepEqual(split('a.“b.c”'), ['a', 'b.c']);
+      assert.deepEqual(split('a.“b.c”.d'), ['a', 'b.c', 'd']);
+      assert.deepEqual(split('a.“b.c”.d.“.e.f.g.”.h'), ['a', 'b.c', 'd', '.e.f.g.', 'h']);
+    });
+
+    it('should not split on escaped dots:', function() {
+      assert.deepEqual(split('a.b.c\\.d'), ['a', 'b', 'c.d']);
+    });
+
+    it('should keep escaping when followed by a backslash:', function() {
+      assert.deepEqual(split('a.b.c\\\\.d'), ['a', 'b', 'c\\\\', 'd']);
+      assert.deepEqual(split('a.b.c\\\\d'), ['a', 'b', 'c\\\\d']);
+    });
+
+    it('should retain unclosed double quotes in the results', function() {
+      assert.deepEqual(split('a."b.c'), ['a', '"b', 'c']);
+    });
+
+    it('should retain unclosed single quotes in the results', function() {
+      assert.deepEqual(split('brian\'s'), ['brian\'s']);
+      assert.deepEqual(split('a.\'b.c'), ['a', '\'b', 'c']);
+    });
+  });
+
+  describe('options', function() {
+    it('should keep double quotes', function() {
+      assert.deepEqual(split('a."b.c".d', {keepDoubleQuotes: true}), ['a', '"b.c"', 'd']);
+    });
+
+    it('should keep “” double quotes', function() {
+      assert.deepEqual(split('a.“b.c”.d', {keepDoubleQuotes: true}), ['a', '“b.c”', 'd']);
+    });
+
+    it('should not split inside brackets', function() {
+      var opts = { brackets: true };
+      assert.deepEqual(split('a.(b.c).d', opts), ['a', '(b.c)', 'd']);
+      assert.deepEqual(split('a.[(b.c)].d', opts), ['a', '[(b.c)]', 'd']);
+      assert.deepEqual(split('a.[b.c].d', opts), ['a', '[b.c]', 'd']);
+      assert.deepEqual(split('a.{b.c}.d', opts), ['a', '{b.c}', 'd']);
+      assert.deepEqual(split('a.<b.c>.d', opts), ['a', '<b.c>', 'd']);
+    });
+
+    it('should support nested brackets', function() {
+      var opts = { brackets: true };
+      assert.deepEqual(split('a.{b.{c}.d}.e', opts), ['a', '{b.{c}.d}', 'e']);
+      assert.deepEqual(split('a.{b.{c.d}.e}.f', opts), ['a', '{b.{c.d}.e}', 'f']);
+      assert.deepEqual(split('a.{[b.{{c.d}}.e]}.f', opts), ['a', '{[b.{{c.d}}.e]}', 'f']);
+    });
+
+    it('should support escaped brackets', function() {
+      var opts = { brackets: true };
+      assert.deepEqual(split('a.\\{b.{c.c}.d}.e', opts), ['a', '{b', '{c.c}', 'd}', 'e']);
+      assert.deepEqual(split('a.{b.c}.\\{d.e}.f', opts), ['a', '{b.c}', '{d', 'e}', 'f']);
+    });
+
+    it('should support quoted brackets', function() {
+      var opts = { brackets: true };
+      assert.deepEqual(split('a.{b.c}."{d.e}".f', opts), ['a', '{b.c}', '{d.e}', 'f']);
+      assert.deepEqual(split('a.{b.c}.{"d.e"}.f', opts), ['a', '{b.c}', '{"d.e"}', 'f']);
+    });
+
+    it('should ignore imbalanced brackets', function() {
+      var opts = { brackets: true };
+      assert.deepEqual(split('a.{b.c', opts), ['a', '{b', 'c']);
+      assert.deepEqual(split('a.{a.{b.c}.d', opts), ['a', '{a.{b.c}', 'd']);
+    });
+
+    it('should keep single quotes', function() {
+      assert.deepEqual(split('a.\'b.c\'.d', {keepSingleQuotes: true}), ['a', '\'b.c\'', 'd']);
+    });
+
+    it('should keep escape characters', function() {
+      assert.deepEqual(split('a.b\\.c', {keepEscaping: true}), ['a', 'b\\.c']);
+    });
+
+    it('should split on a custom separator', function() {
+      assert.deepEqual(split('a,b,c', {sep: ','}), ['a', 'b', 'c']);
+    });
+
+    it('should allow custom quotes array', function() {
+      assert.deepEqual(split('a.^b.c^', {quotes: ['^']}), ['a', 'b.c']);
+    });
+
+    it('should allow custom quotes object', function() {
+      assert.deepEqual(split('a.^b.c$', {quotes: {'^': '$'}}), ['a', 'b.c']);
+    });
+  });
+
+  describe('function', function() {
+    it('should call a custom function on every token', function() {
+      function fn(tok, tokens) {
+        if (tok.escaped && tok.val === 'b') {
+          tok.val = '\\b';
+          return;
+        }
+
+        if (!/[@!*+]/.test(tok.val)) return;
+        var stack = [];
+        var val = tok.val;
+        var str = tok.str;
+        var i = tok.idx;
+
+        while (++i < str.length) {
+          var ch = str[i];
+          if (ch === '(') {
+            stack.push(ch);
+          }
+
+          if (ch === ')') {
+            stack.pop();
+            if (!stack.length) {
+              val += ch;
+              break;
+            }
+          }
+          val += ch;
+        }
+
+        tok.split = false;
+        tok.val = val;
+        tok.idx = i;
+      }
+
+      var opts = {sep: ',', brackets: null};
+      assert.deepEqual(split('a,(\\b,c)', opts, fn), ['a', '(\\b', 'c)']);
+      assert.deepEqual(split('a,(b,c)', opts, fn), ['a', '(b', 'c)']);
+      assert.deepEqual(split('a,@(b,c)', opts, fn), ['a', '@(b,c)']);
+      assert.deepEqual(split('a,@(b,(a,b)c)', opts, fn), ['a', '@(b,(a,b)c)']);
+      assert.deepEqual(split('a,@(b,(a,b)c),z', opts, fn), ['a', '@(b,(a,b)c)', 'z']);
+      assert.deepEqual(split('a,+(b,c)', opts, fn), ['a', '+(b,c)']);
+      assert.deepEqual(split('a,*(b|c,d)', opts, fn), ['a', '*(b|c,d)']);
+    });
+  });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-split-string.git



More information about the Pkg-javascript-commits mailing list