├── .babelrc ├── LICENSE.txt ├── README.md ├── dist └── index.js ├── index.js └── package.json /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2017 Efog 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | react-process-string 2 | ----- 3 | The library allows you to process strings with regular expressions in ReactJS. 4 | 5 | Installation 6 | --- 7 | Via npm: 8 | ``` 9 | npm install react-process-string --save 10 | ``` 11 | 12 | Syntax 13 | --- 14 | ```javascript 15 | processString(options)(string); 16 | ``` 17 | 18 | Options should be an array of objects containing `regex` and `fn` fields. 19 | `fn` is a function that takes two arguments: `key`, to pass it to a react component and `result` — the result of regex executing. 20 | 21 | Example usage 22 | --- 23 | ```javascript 24 | const processString = require('react-process-string'); 25 | 26 | class HelloWorld extends React.Component { 27 | render() { 28 | let config = [{ 29 | regex: /(http|https):\/\/(\S+)\.([a-z]{2,}?)(.*?)( |\,|$|\.)/gim, 30 | fn: (key, result) => 31 | {result[2]}.{result[3]}{result[4]}{result[5]} 32 | 33 | }, { 34 | regex: /(\S+)\.([a-z]{2,}?)(.*?)( |\,|$|\.)/gim, 35 | fn: (key, result) => 36 | {result[1]}.{result[2]}{result[3]}{result[4]} 37 | 38 | }]; 39 | 40 | let stringWithLinks = "Watch this on youtube.com"; 41 | let processed = processString(config)(stringWithLinks); 42 | 43 | return ( 44 |
Hello world! {processed}
45 | ); 46 | } 47 | } 48 | ``` 49 | On the user side, `processed` will contain clickable links. 50 | 51 | Example №2 52 | --- 53 | ```javascript 54 | let users = ourStore.users; 55 | let stringWithUsername = "Hello @efog, how do you feel today?"; 56 | 57 | let processed = processString([{ 58 | regex: /\@([a-z0-9_\-]+?)( |\,|$|\.)/gim, //regex to match a username 59 | fn: (key, result) => { 60 | let username = result[1]; 61 | let foundUsers = users.filter(user => user.username === username); 62 | 63 | if (!foundUsers.length) 64 | return result[0]; //@username 65 | 66 | return @{username}; 67 | } 68 | }]); 69 | ``` 70 | This code allows us to make `@usernames` clickable. 71 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function processString(options) { 4 | var key = 0; 5 | 6 | function processInputWithRegex(option, input) { 7 | if (!option.fn || typeof option.fn !== 'function') return input; 8 | 9 | if (!option.regex || !(option.regex instanceof RegExp)) return input; 10 | 11 | if (typeof input === 'string') { 12 | var regex = option.regex; 13 | var result = null; 14 | var output = []; 15 | 16 | while ((result = regex.exec(input)) !== null) { 17 | var index = result.index; 18 | var match = result[0]; 19 | 20 | output.push(input.substring(0, index)); 21 | output.push(option.fn(++key, result)); 22 | 23 | input = input.substring(index + match.length, input.length + 1); 24 | regex.lastIndex = 0; 25 | } 26 | 27 | output.push(input); 28 | return output; 29 | } else if (Array.isArray(input)) { 30 | return input.map(function (chunk) { 31 | return processInputWithRegex(option, chunk); 32 | }); 33 | } else return input; 34 | } 35 | 36 | return function (input) { 37 | if (!options || !Array.isArray(options) || !options.length) return input; 38 | 39 | options.forEach(function (option) { 40 | return input = processInputWithRegex(option, input); 41 | }); 42 | 43 | return input; 44 | }; 45 | } 46 | 47 | module.exports = processString; 48 | 49 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function processString(options) { 2 | let key = 0; 3 | 4 | function processInputWithRegex(option, input) { 5 | if (!option.fn || typeof option.fn !== 'function') 6 | return input; 7 | 8 | if (!option.regex || !(option.regex instanceof RegExp)) 9 | return input; 10 | 11 | if (typeof input === 'string') { 12 | let regex = option.regex; 13 | let result = null; 14 | let output = []; 15 | 16 | while ((result = regex.exec(input)) !== null) { 17 | let index = result.index; 18 | let match = result[0]; 19 | 20 | output.push(input.substring(0, index)); 21 | output.push(option.fn(++key, result)); 22 | 23 | input = input.substring(index + match.length, input.length + 1); 24 | regex.lastIndex = 0; 25 | } 26 | 27 | output.push(input); 28 | return output; 29 | } else if (Array.isArray(input)) { 30 | return input.map(chunk => processInputWithRegex(option, chunk)); 31 | } else return input; 32 | } 33 | 34 | return function (input) { 35 | if (!options || !Array.isArray(options) || !options.length) 36 | return input; 37 | 38 | options.forEach(option => input = processInputWithRegex(option, input)); 39 | 40 | return input; 41 | }; 42 | } 43 | 44 | module.exports = processString; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-process-string", 3 | "version": "1.2.0", 4 | "description": "A library to process strings with regular expressions.", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "build": "babel index.js > dist/index.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/EfogDev/react-process-string.git" 12 | }, 13 | "keywords": [ 14 | "react", 15 | "regex", 16 | "string" 17 | ], 18 | "author": "Efog ", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "https://github.com/EfogDev/react-process-string/issues" 22 | }, 23 | "homepage": "https://github.com/EfogDev/react-process-string#readme", 24 | "devDependencies": { 25 | "babel-cli": "^6.24.1", 26 | "babel-preset-es2015": "^6.24.1" 27 | } 28 | } 29 | --------------------------------------------------------------------------------