String insert values
This commit is contained in:
6
string-insert-values/index.js
Normal file
6
string-insert-values/index.js
Normal file
@@ -0,0 +1,6 @@
|
||||
// https://www.codewars.com/kata/529b54d9aba78c924d00088e/javascript
|
||||
|
||||
const format = function (str, obj) {
|
||||
const truthy = (val, def) => typeof val === 'undefined' ? def : val;
|
||||
return str.replace(/\{\w+\}/g, a => truthy(obj[a.slice(1, a.length-1)], a));
|
||||
};
|
||||
Reference in New Issue
Block a user