Initial commit
This commit is contained in:
3
node_modules/unpack-string/.npmignore
generated
vendored
Normal file
3
node_modules/unpack-string/.npmignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules/
|
||||
_private/
|
||||
npm-debug*
|
||||
3
node_modules/unpack-string/.travis.yml
generated
vendored
Normal file
3
node_modules/unpack-string/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "6"
|
||||
49
node_modules/unpack-string/README.md
generated
vendored
Normal file
49
node_modules/unpack-string/README.md
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
# unpack-string
|
||||
|
||||
[](https://travis-ci.org/DiegoZoracKy/unpack-string)
|
||||
|
||||
Unpacks the content found within a text, delimited by an opening char and a closing char, e.g., *'Can extract (**only the content found here within these parentheses**)'*
|
||||
|
||||
**Node.js** and **Browser** ready.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install unpack-string
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
It will extract the content found within the *opening char* and the *closing char* defined as parameters. If the parameter **openingChar** is not passed in, it will try to guess the first occurrence of some of these known chars `'([{<'`, and, when the parameter **closingChar** is not passed in, it will try to guess some of these chars `')]}>'`, to match the **openingChar**.
|
||||
|
||||
## Examples
|
||||
|
||||
```javascript
|
||||
const unpackString = require('unpack-string');
|
||||
|
||||
const str = 'Can extract (only the content [found {here} within] these parentheses)!!';
|
||||
|
||||
{
|
||||
const result = unpackString(str);
|
||||
|
||||
// Guessing openingChar and closingChar. "()" in that case.
|
||||
// result === 'only the content [found {here} within] these parentheses'
|
||||
}
|
||||
|
||||
{
|
||||
const openingChar = '[';
|
||||
const result = unpackString(str, openingChar);
|
||||
|
||||
// Guessing closingChar: "]"
|
||||
// result === 'found {here} within'
|
||||
}
|
||||
|
||||
{
|
||||
const openingChar = '{';
|
||||
const closingChar = ']';
|
||||
const result = unpackString(str, openingChar, closingChar);
|
||||
|
||||
// Defining any openingChar and closingChar
|
||||
// result === 'here} within'
|
||||
}
|
||||
```
|
||||
61
node_modules/unpack-string/lib/unpack-string.js
generated
vendored
Normal file
61
node_modules/unpack-string/lib/unpack-string.js
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Unpacks the content found within a text, delimited by an opening char and a closing char, e.g., "Can extract (only the content found here within these parentheses)"
|
||||
* @param {String} str Input String
|
||||
* @param {String} openingChar Opening char e.g. "(", "[", "{". If nothing is passed in it will try to guess some known opening chars: '([{<'
|
||||
* @param {String} closingChar Closing char e.g. ")", "]", "}". If nothing is passed in it will try to guess some known closing chars: ')]}>'
|
||||
* @return {String} The content found within the opening char and the closing char
|
||||
*/
|
||||
function unpackString(str, openingChar, closingChar) {
|
||||
|
||||
let knownOpeningChars = '([{<';
|
||||
let knownClosingChars = ')]}>';
|
||||
|
||||
let indexStart;
|
||||
let indexEnd;
|
||||
let skip = 0;
|
||||
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const char = str[i];
|
||||
|
||||
if (!openingChar) {
|
||||
if (knownOpeningChars.indexOf(char) < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
openingChar = char;
|
||||
}
|
||||
|
||||
closingChar = closingChar || knownClosingChars[knownOpeningChars.indexOf(openingChar)];
|
||||
|
||||
let matchOpeningChar = char === openingChar;
|
||||
let matchClosingChar = char === closingChar;
|
||||
|
||||
if (typeof(indexStart) === 'undefined' && matchOpeningChar) {
|
||||
indexStart = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (matchOpeningChar) {
|
||||
skip += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (skip && matchClosingChar) {
|
||||
skip -= 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (matchClosingChar) {
|
||||
indexEnd = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return str.substring(indexStart + 1, indexEnd);
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = unpackString;
|
||||
}
|
||||
25
node_modules/unpack-string/package.json
generated
vendored
Normal file
25
node_modules/unpack-string/package.json
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "unpack-string",
|
||||
"version": "0.0.2",
|
||||
"description": "Unpacks the content found within a text, delimited by an opening char and a closing char, e.g., 'Can extract (only the content found here within these parentheses)'",
|
||||
"main": "lib/unpack-string.js",
|
||||
"author": {
|
||||
"name": "Diego ZoracKy",
|
||||
"email": "diego.zoracky@gmail.com",
|
||||
"url": "https://github.com/DiegoZoracKy/"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha ./test/main.test.js"
|
||||
},
|
||||
"keywords": [
|
||||
"unpack",
|
||||
"string",
|
||||
"brackets",
|
||||
"parentheses"
|
||||
],
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"chai": "^4.0.0",
|
||||
"mocha": "^3.4.2"
|
||||
}
|
||||
}
|
||||
39
node_modules/unpack-string/test/main.test.js
generated
vendored
Normal file
39
node_modules/unpack-string/test/main.test.js
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
const unpackString = require(`../`);
|
||||
const assert = require(`assert`);
|
||||
|
||||
describe(`unpack-string`, function() {
|
||||
|
||||
const input = 'Can extract (only the content [found {here} within] these parentheses)!!';
|
||||
|
||||
it(`Must get the content found within "()" by guessing the opening char and the closing char`, function() {
|
||||
const expected = 'only the content [found {here} within] these parentheses';
|
||||
const result = unpackString(input);
|
||||
assert.equal(result, expected);
|
||||
});
|
||||
|
||||
it(`Must get the content found within "[]", defining the opening char parameter and letting it guess the closing char`, function() {
|
||||
const expected = 'found {here} within';
|
||||
const result = unpackString(input, '[');
|
||||
assert.equal(result, expected);
|
||||
});
|
||||
|
||||
it(`Must get the content found within "{}", defining the opening char and the closing char parameters`, function() {
|
||||
const expected = 'here';
|
||||
const result = unpackString(input, '{', '}');
|
||||
assert.equal(result, expected);
|
||||
});
|
||||
|
||||
it(`Must get the content found within "[}", defining the opening char and the closing char parameters`, function() {
|
||||
const expected = 'found {here';
|
||||
const result = unpackString(input, '[', '}');
|
||||
assert.equal(result, expected);
|
||||
});
|
||||
|
||||
it(`Must get the content found within "})", defining the opening char and the closing char parameters`, function() {
|
||||
const expected = ' within] these parentheses';
|
||||
const result = unpackString(input, '}', ')');
|
||||
assert.equal(result, expected);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user