@rimiti/stimmy
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -7,18 +7,29 @@ 'use strict'; | ||
exports.default = (value, items) => { | ||
if (typeof value === 'string' && items instanceof Array) { | ||
// $FlowFixMe: Flow doesn't understand computed values | ||
return value.replace(/({\d})/g, i => items[i.replace(/{/, '').replace(/}/, '')]); | ||
} else if (typeof value === 'string' && items instanceof Object) { | ||
return value.replace(/({([^}]+)})/g, i => { | ||
const key = i.replace(/{/, '').replace(/}/, ''); | ||
exports.default = function (start = '{', end = '}') { | ||
// remove RegExp specials | ||
const theStart = start.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); | ||
const theEnd = end.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); | ||
const arrayReg = new RegExp(`(${theStart}(.+?)${theEnd})`, 'g'); | ||
const objectReg = new RegExp(`(${theStart}([^}]+)${theEnd})`, 'g'); | ||
const startReg = new RegExp(`${theStart}`, 'g'); | ||
const endReg = new RegExp(`${theEnd}`, 'g'); | ||
return (value, items) => { | ||
if (typeof value === 'string' && items instanceof Array) { | ||
// $FlowFixMe: Flow doesn't understand computed values | ||
if (!items[key]) return i; | ||
return items[key]; | ||
}); | ||
} | ||
return value; | ||
return value.replace(arrayReg, i => items[i.replace(startReg, '').replace(endReg, '')]); | ||
} else if (typeof value === 'string' && items instanceof Object) { | ||
return value.replace(objectReg, i => { | ||
const key = i.replace(startReg, '').replace(endReg, ''); | ||
// $FlowFixMe: Flow doesn't understand computed values | ||
if (!items[key]) return i; | ||
return items[key]; | ||
}); | ||
} | ||
return value; | ||
}; | ||
}; | ||
module.exports = exports['default']; | ||
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC5qcyJdLCJuYW1lcyI6WyJ2YWx1ZSIsIml0ZW1zIiwiQXJyYXkiLCJyZXBsYWNlIiwiaSIsIk9iamVjdCIsImtleSJdLCJtYXBwaW5nc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nID0+IHtcbiAgICBpZiAodHlwZW9mIHZhbHVlID09PSAnc3RyaW5nJyAmJiBpdGVtcyBpbnN0YW5jZW9mIEFycmF5KSB7XG4gICAgICAvLyAkRmxvd0ZpeE1lOiBGbG93IGRvZXNuJ3QgdW5kZXJzdGFuZCBjb21wdXRlZCB2YWx1ZXNcbiAgICAgIHJldHVybiB2YWx1ZS5yZXBsYWNlKGFycmF5UmVnLCAoaSkgPT4gaXRlbXNbaS5yZXBsYWNlKHN0YXJ0UmVnLCAnJykucmVwbGFjZShlbmRSZWcsICcnKV0pO1xuICAgIH0gZWxzZSBpZiAodHlwZW9mIHZhbHVlID09PSAnc3RyaW5nJyAmJiBpdGVtcyBpbnN0YW5jZW9mIE9iamVjdCkge1xuICAgICAgcmV0dXJuIHZhbHVlLnJlcGxhY2Uob2JqZWN0UmVnLCAoaSkgPT4ge1xuICAgICAgICBjb25zdCBrZXkgPSBpLnJlcGxhY2Uoc3RhcnRSZWcsICcnKS5yZXBsYWNlKGVuZFJlZywgJycpO1xuICAgICAgICAvLyAkRmxvd0ZpeE1lOiBGbG93IGRvZXNuJ3QgdW5kZXJzdGFuZCBjb21wdXRlZCB2YWx1ZXNcbiAgICAgICAgaWYgKCFpdGVtc1trZXldKSByZXR1cm4gaTtcbiAgICAgICAgcmV0dXJuIGl0ZW1zW2tleV07XG4gICAgICB9KTtcbiAgICB9XG4gICAgcmV0dXJuIHZhbHVlO1xuICB9O1xufVxuXG4iXX0= |
{ | ||
"name": "@rimiti/stimmy", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Replace your string variables by your values easily.", | ||
@@ -58,3 +58,3 @@ "license": "MIT", | ||
"eslint-plugin-import": "~2.9.0", | ||
"flow-bin": "~0.67.1", | ||
"flow-bin": "~0.69.0", | ||
"jest": "^22.4.2" | ||
@@ -61,0 +61,0 @@ }, |
@@ -22,3 +22,3 @@ # stimmy | ||
``` | ||
```bash | ||
$ npm install stimmy --save | ||
@@ -50,3 +50,11 @@ ``` | ||
``` | ||
**init pattern** | ||
```javascript | ||
// As default, use the {myVariable} pattern | ||
const replacer = stimmy(); | ||
// If you want to customized default pattern (ex: {{myVariable}} | ||
const replacer = stimmy('{{', '}}') | ||
``` | ||
<a name="fromArray"/> | ||
@@ -57,9 +65,9 @@ | ||
```javascript | ||
const str = stimmy('This {0} is {1}!', ['module', 'awesome']); | ||
// This module is awesome! | ||
const str = replacer('This {0} is {1}!', ['module', 'awesome']); | ||
// This module is awesome! | ||
``` | ||
```javascript | ||
const str = stimmy('{0}% of code coverage, it\'s {1}...', [100, 'so amazing']); | ||
// 100% of code coverage, it's so amazing... | ||
const str = replacer('{0}% of code coverage, it\'s {1}...', [100, 'so amazing']); | ||
// 100% of code coverage, it's so amazing... | ||
``` | ||
@@ -72,12 +80,11 @@ | ||
```javascript | ||
const str = stimmy('{timmy} My hobby is {hobby}...', {timmy: 'Timmmmmmmyy !!', hobby: 'running'}); | ||
// Timmmmmmmyy !! My hobby is running... | ||
const str = replacer('{timmy} My hobby is {hobby}...', {timmy: 'Timmmmmmmyy !!', hobby: 'running'}); | ||
// Timmmmmmmyy !! My hobby is running... | ||
``` | ||
```javascript | ||
const str = stimmy('My name is {name}, I\'m {age}.', {name: 'stimmy', age: 25}); | ||
// My name is stimmy, I'm 25. | ||
const str = replacer('My name is {name}, I\'m {age}.', {name: 'stimmy', age: 25}); | ||
// My name is stimmy, I'm 25. | ||
``` | ||
## Scripts | ||
@@ -84,0 +91,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30620
28
124
6