Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. /**
  2. * Replace %x parameters in string
  3. *
  4. * @param {String} str The chain of characters with %x...%x+1 parameters
  5. * @param {Array} params The parameters to be included in the string. Accept: {string, number, node, component}
  6. * @returns {Array}
  7. *
  8. * Usage:
  9. * @example
  10. * // returns ['Example with ', 'param1', 'and', '<strong>param2</strong>']
  11. * replacePramsInString('Example with %0 and %1", ["param1", <strong>param2</strong>]);
  12. */
  13.  
  14. const replacePramsInString = (str, params) => {
  15. const items = Array.isArray(params) ? params : [params];
  16.  
  17. return str.split(/%\d/).reduce((accumulator, currentValue, index) => {
  18. const item = items[index];
  19.  
  20. accumulator.push(currentValue);
  21. accumulator.push(item);
  22.  
  23. return accumulator;
  24. }, []);
  25. };
  26.  
  27. export {
  28. replacePramsInString
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement