From 9eb4e9f9f79eedc0c8f7322c6ef9d48c7c6ba689 Mon Sep 17 00:00:00 2001 From: Grzegorz Kucmierz Date: Wed, 29 Jan 2020 09:23:55 +0100 Subject: [PATCH] String insert values --- string-insert-values/index.js | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 string-insert-values/index.js diff --git a/string-insert-values/index.js b/string-insert-values/index.js new file mode 100644 index 0000000..a1e71b2 --- /dev/null +++ b/string-insert-values/index.js @@ -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)); +};