Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specify more characters as separator and also more separators i… #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ console.log(split('a.b.\\c', { keep: () => true })); //=> ['a', 'b\.c']

### options.separator

**Type**: `string`
**Type**: `string|Array<string>`

**Default**: `.`

Expand All @@ -153,6 +153,8 @@ The character to split on.

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

## Split function
Expand All @@ -171,6 +173,7 @@ The `state` object exposes the following properties:

* `input` - (String) The un-modified, user-defined input string
* `separator` - (String) the specified separator to split on.
* `separators` - (Array<String>) the specified separators 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
Expand Down
2 changes: 2 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ console.log(split('a.b.\\"c.d."e.f.g".h.i', { quotes: ['"'], keep }));

// console.log(split('zzz.{a.{b.{c.{d}.e}.f}.g}.xxx'));
// console.log(split('a.{b.c}|{d.e}', { separator: '|' }));
// console.log(split('a.{b.c}|{d.e}', { separator: '||' }));
// console.log(split('a.{b.c}|{d.e}', { separator: ['||', '&&'] }));
// console.log(split('a.{b.c}|{d.e}'));
// console.log(split('a.{b.c}.{d.e}'));
// console.log(split('a.{b.c}.{d.e}', { brackets: false }));
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ module.exports = (input, options = {}, fn) => {
}

let separator = options.separator || '.';
let separators = typeof separator === 'string' ? [separator] : separator;
let ast = { type: 'root', nodes: [], stash: [''] };
let stack = [ast];
let state = { input, separator, stack };
let state = { input, separator, separators, stack };
let string = input;
let value, node;
let i = -1;
Expand Down Expand Up @@ -112,7 +113,9 @@ module.exports = (input, options = {}, fn) => {
}

// push separator onto stash
if (value === separator && state.block.type === 'root') {
const usedSeparator = separators.find((sep) => sep === string.substr(i, sep.length));
if (usedSeparator && state.block.type === 'root') {
i = i + usedSeparator.length - 1;
if (typeof fn === 'function' && fn(state) === false) {
append(value);
continue;
Expand Down
8 changes: 8 additions & 0 deletions test/options.brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ describe('options.brackets', () => {
assert.deepEqual(split('a.{a.{b.c}.d', opts), ['a', '{a', '{b', 'c}', 'd']);
assert.deepEqual(split('a.{a.{b.c.d', opts), ['a', '{a', '{b', 'c', 'd']);
});

it('should take more separators in array with more characters as custom separator and prevent brackets', () => {
const opts = { brackets: true, separator: ['||', '&&'] };
assert.deepEqual(split('a&&[b&&d]&&c', opts), ['a', '[b&&d]', 'c']);
assert.deepEqual(split('a||[b&&d]&&c', opts), ['a', '[b&&d]', 'c']);
assert.deepEqual(split('a||[b||d]&&c', opts), ['a', '[b||d]', 'c']);
assert.deepEqual(split('[a&&a]&&[b||c]', opts), ['[a&&a]', '[b||c]']);
});
});
19 changes: 19 additions & 0 deletions test/options.separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,24 @@ describe('separator', () => {
assert.deepEqual(split('a.b.c', state => state.next() !== 'b'), ['a.b', 'c']);
assert.deepEqual(split('a.b.c', state => state.next() !== 'c'), ['a', 'b.c']);
});

it('should take more characters as custom separator', () => {
assert.deepEqual(split('a&&b&&c', { separator: '&&' }), ['a', 'b', 'c']);
assert.deepEqual(split('.a&&b&&c', { separator: '&&' }), ['.a', 'b', 'c']);
assert.deepEqual(split('a||b&&c', { separator: '||' }), ['a', 'b&&c']);
assert.deepEqual(split('a||b&&c', { separator: '&&' }), ['a||b', 'c']);
assert.deepEqual(split('a&&&c', { separator: '&&' }), ['a', '&c']);
assert.deepEqual(split('a&&&&c', { separator: '&&' }), ['a', '', 'c']);
});

it('should take more separators in array with more characters as custom separator', () => {
const opts = { separator: ['||', '&&'] };
assert.deepEqual(split('a&&b&&c', opts), ['a', 'b', 'c']);
assert.deepEqual(split('.a&&b&&c', opts), ['.a', 'b', 'c']);
assert.deepEqual(split('a||b&&c', opts), ['a', 'b', 'c']);
assert.deepEqual(split('a||b&&c', opts), ['a', 'b', 'c']);
assert.deepEqual(split('a&&&c', opts), ['a', '&c']);
assert.deepEqual(split('a&&&&c', opts), ['a', '', 'c']);
});
});

5 changes: 3 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export interface ASTNode {

export interface State {
input: string;
separator: string;
separator: string | string[];
separators: string[];
stack: ASTNode[];
bos(): boolean;
eos(): boolean;
Expand All @@ -19,7 +20,7 @@ export interface State {
export interface Options {
brackets?: { [key: string]: string } | boolean;
quotes?: string[] | boolean;
separator?: string;
separator?: string | string[];
strict?: boolean;
keep?(value: string, state: State): boolean;
}
Expand Down