├── .gitignore ├── LICENSE.md ├── README.md ├── package.json └── text-split.js /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Nick Poisson 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # text-split 2 | Utility for splitting text into chunks based on regex. 3 | 4 | ##Installation 5 | ``` 6 | $npm install text-split 7 | $import text-split from 'text-split'; 8 | ``` 9 | 10 | 11 | ##Usage 12 | 13 | **html** 14 | ``` 15 |

16 | It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 17 |

18 | ``` 19 | **javascript** 20 | ``` 21 | const textBox = document.getElementById("text-box"); 22 | text-split(textBox, ', but'); 23 | ``` 24 | 25 | result will look like this 26 | ``` 27 | [ 28 | It has survived not only five centuries, 29 | also the leap into electronic typesetting, remaining essentially unchanged. 30 | ] 31 | ``` 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "text-split", 3 | "version": "1.0.0", 4 | "description": "Utility for splitting text into chunks based on regex.", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Nick Poisson", 9 | "email": "nick@jam3.com", 10 | "url": "nick@jam3.com" 11 | }, 12 | "dependencies": {}, 13 | "devDependencies": { 14 | "tape": "*" 15 | }, 16 | "scripts": { 17 | "test": "node test/test.js" 18 | }, 19 | "keywords": [ 20 | "text,", 21 | "split,", 22 | "regex,", 23 | "array,", 24 | "explode,", 25 | "join" 26 | ], 27 | "repository": { 28 | "type": "git", 29 | "url": "git://github.com/Jam3/text-split.git" 30 | }, 31 | "homepage": "https://github.com/Jam3/text-split", 32 | "bugs": { 33 | "url": "https://github.com/Jam3/text-split/issues" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /text-split.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This function splits text from innerHTML based on regex. Then, it wrap sparated words with except
4 | * Finally, it returns array of span 5 | * @param {HTML element} element 6 | * @param {string} split This is separator for text (regex) 7 | * @return {Array.} Array of span html element 8 | */ 9 | module.exports = function(element,split) { 10 | var words = splitText(element.innerHTML.toString(),split); 11 | var str = ''; 12 | words.forEach(function(line) { 13 | if (line.indexOf('
')>-1) { 14 | var lines = line.split('
'); 15 | lines.forEach(function(line,id,arr) { 16 | str += (id>0) ? '
'+parseLine(line) : parseLine(line); 17 | }); 18 | } else { 19 | str += parseLine(line); 20 | } 21 | }); 22 | element.innerHTML = str; 23 | return element.querySelectorAll('span'); 24 | }; 25 | 26 | 27 | /** 28 | * This function split text into array including
29 | * @params {string} txt This is text that retrieved from html element 30 | * @params {string} split It is separator for text (regex) 31 | * @return {Array.} result of split using separator including
32 | */ 33 | function splitText(txt,split) { 34 | var splits = txt.split('
'); 35 | var arr = []; 36 | splits.forEach(function(item,id) { 37 | if (id>0) arr.push('
'); 38 | arr = arr.concat(item.split(split)); 39 | }); 40 | return arr; 41 | } 42 | /** 43 | * This function will wrap passed argument with if passed argument is not empty, space, or
44 | * @params {string} line It is words that are separated by seprator 45 | * @return {string} it is either '', ' ',
, or line 46 | */ 47 | function parseLine(line) { 48 | if (line==='' || line===' ') { 49 | return line; 50 | } else { 51 | return (line==='
') ? '
' : ''+line+''+((line.length>1)?' ':''); 52 | } 53 | } --------------------------------------------------------------------------------