Initial commit
This commit is contained in:
3
node_modules/split-skip/.npmignore
generated
vendored
Normal file
3
node_modules/split-skip/.npmignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules/
|
||||
_private/
|
||||
npm-debug*
|
||||
3
node_modules/split-skip/.travis.yml
generated
vendored
Normal file
3
node_modules/split-skip/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "6"
|
||||
69
node_modules/split-skip/README.md
generated
vendored
Normal file
69
node_modules/split-skip/README.md
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
# split-skip
|
||||
|
||||
[](https://travis-ci.org/DiegoZoracKy/split-skip)
|
||||
|
||||
Splits a String into an Array of substrings with the option to skip some cases where the separator is found, based on some *truthy* condition.
|
||||
|
||||
**Node.js** and **Browser** ready.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install split-skip
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
const splitSkip = require('split-skip');
|
||||
|
||||
//@param {String} str - Input String
|
||||
const str = 'Some,String,as,an,Input';
|
||||
|
||||
// @param {String} separator - Specifies the character(s) to use for separating the string
|
||||
const separator = ',';
|
||||
|
||||
// @param {Function} skipState - Function to be called on each iteration, to manage the skip state. Parameters: `(state, char, i)`
|
||||
const skipState = (state, char, i) => {
|
||||
|
||||
/*
|
||||
Some logic to define state.skip equals to some truthy value
|
||||
e.g. state.skip = true, state.skip = 1
|
||||
when it should skip if the current char matches the separator
|
||||
*/
|
||||
|
||||
/*
|
||||
And define state.skip equals to some falsy value
|
||||
e.g. state.skip = false, state.skip = 0
|
||||
when it should get back splitting if the current char matches the separator
|
||||
*/
|
||||
|
||||
// Must alway return the state;
|
||||
return state;
|
||||
};
|
||||
|
||||
const resultArray = splitSkip(str, separator, skipState);
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
As an input we have a string representing the parameters definition of a function from where we want to get each individual parameter. One idea of doing this is to split on every comma, but skipping the commas that are present on destructuring parameters definitions.
|
||||
Using splitSkip, it could be like:
|
||||
|
||||
```javascript
|
||||
const input = `[destru,cturu,cing]=[1],param,{dd,ee,ff}={dd:{b:1,c:2,arr:[1,6]}},last`;
|
||||
|
||||
const result = splitSkip(input, ',', (state, char, i) => {
|
||||
if ('{[('.indexOf(char) >= 0) {
|
||||
state.skip += 1;
|
||||
}
|
||||
|
||||
if ('}])'.indexOf(char) >= 0) {
|
||||
state.skip -= 1;
|
||||
}
|
||||
|
||||
return state;
|
||||
});
|
||||
|
||||
// result === ['[destru,cturu,cing]=[1]', 'param', '{dd,ee,ff}={dd:{b:1,c:2,arr:[1,6]}}', 'last'];
|
||||
```
|
||||
43
node_modules/split-skip/lib/split-skip.js
generated
vendored
Normal file
43
node_modules/split-skip/lib/split-skip.js
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Splits a String into an Array of substrings with the option to skip some cases where the separator is found, based on some truthy condition.
|
||||
* @param {String} str Input String
|
||||
* @param {String} separator Specifies the character(s) to use for separating the string
|
||||
* @param {Function} skipState Function to be called on each iteration, to manage the skip state. `(state, char, i)`
|
||||
* @return {Array} An array of strings split at each point where the separator occurs in the given string while the skipState were truthy.
|
||||
*/
|
||||
function splitSkip(str, separator, skipState) {
|
||||
|
||||
const result = [];
|
||||
|
||||
if (!str) {
|
||||
return [str];
|
||||
}
|
||||
|
||||
let indexStart = 0;
|
||||
let indexEnd = 0;
|
||||
let state = {
|
||||
skip: 0
|
||||
};
|
||||
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const char = separator.length === 1 ? str[i] : str.substr(i, separator.length);
|
||||
|
||||
state = skipState ? skipState(state, char, i) : state;
|
||||
const compareSeparator = char === separator;
|
||||
|
||||
if ((!state.skip && compareSeparator) || i === str.length - 1) {
|
||||
indexEnd = i === str.length - 1 ? i + 1 : i;
|
||||
result.push(str.substring(indexStart, indexEnd));
|
||||
indexStart = indexEnd + separator.length;
|
||||
i = i;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = splitSkip;
|
||||
}
|
||||
23
node_modules/split-skip/package.json
generated
vendored
Normal file
23
node_modules/split-skip/package.json
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "split-skip",
|
||||
"version": "0.0.2",
|
||||
"description": "Splits a String into an Array of substrings with the option to skip some cases where the separator is found, based on some truthy condition.",
|
||||
"main": "lib/split-skip.js",
|
||||
"author": {
|
||||
"name": "Diego ZoracKy",
|
||||
"email": "diego.zoracky@gmail.com",
|
||||
"url": "https://github.com/DiegoZoracKy/"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha ./test/main.test.js"
|
||||
},
|
||||
"keywords": [
|
||||
"split",
|
||||
"skip"
|
||||
],
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"chai": "^4.0.0",
|
||||
"mocha": "^3.4.2"
|
||||
}
|
||||
}
|
||||
35
node_modules/split-skip/test/main.test.js
generated
vendored
Normal file
35
node_modules/split-skip/test/main.test.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
|
||||
const splitSkip = require(`../`);
|
||||
const assert = require(`assert`);
|
||||
|
||||
describe(``, function() {
|
||||
|
||||
it(`must skip commas found in destructuring parameters`, function() {
|
||||
const test = `[destru,cturu,cing]=[1],param,{dd,ee,ff}={dd:{b:1,c:2,arr:[1,6]}},last`;
|
||||
const expected = ['[destru,cturu,cing]=[1]', 'param', '{dd,ee,ff}={dd:{b:1,c:2,arr:[1,6]}}', 'last'];
|
||||
|
||||
const result = splitSkip(test, ',', (state, char, i) => {
|
||||
if ('{[('.indexOf(char) >= 0) {
|
||||
state.skip += 1;
|
||||
}
|
||||
|
||||
if ('}])'.indexOf(char) >= 0) {
|
||||
state.skip -= 1;
|
||||
}
|
||||
|
||||
return state;
|
||||
});
|
||||
|
||||
assert(result.every(item => expected.includes(item)));
|
||||
});
|
||||
|
||||
it(`must not skip commas found in destructuring parameters`, function() {
|
||||
const test = `[destru,cturu,cing]=[1],param,{dd,ee,ff}={dd:{b:1,c:2,arr:[1,6]}},last`;
|
||||
const expected = [ '[destru', 'cturu', 'cing]=[1]', 'param', '{dd', 'ee', 'ff}={dd:{b:1', 'c:2', 'arr:[1', '6]}}', 'last' ];
|
||||
|
||||
const result = splitSkip(test, ',');
|
||||
|
||||
assert(result.every(item => expected.includes(item)));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user