Skip to content

Commit 95b7c83

Browse files
ShouLiamGoodacre
authored andcommitted
Add getTemplateVars
We add `getTemplateVars` so we can access the array of variables contained within a template string. This is useful for finding superfluous template variables.
1 parent cf7a081 commit 95b7c83

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Diff for: src/Data/TemplateString/TemplateString.js

+7
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ exports._buildExclamationKeyObject = function (tuples) {
77
return valueMap;
88
};
99

10+
var templatePattern = /\$\{([^}]+)\}/g;
11+
12+
exports._getTemplateVars = function (str) {
13+
return (str.match(templatePattern) || []).map(function (str) {
14+
return str.substring(2, str.length - 1);
15+
});
16+
};

Diff for: src/Data/TemplateString/TemplateString.purs

+14
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ module Data.TemplateString
33
, template
44
, (<->)
55
, templateS
6+
, getTemplateVars
67
) where
78

89
import Prelude (map, (<<<), show, (<>))
10+
import Data.Function.Uncurried (Fn1, runFn1)
911
import Data.Tuple (Tuple)
1012
import Data.Show (class Show)
1113
import Data.TemplateString.Unsafe (templateBy)
@@ -34,5 +36,17 @@ templateS tmpl = template tmpl <<< map (map show)
3436

3537
infix 7 templateS as <->
3638

39+
-- | Get the template variables contained in a string.
40+
-- |
41+
-- | Example:
42+
-- | ```purescript
43+
-- | > getTemplateVars "Foo ${bar} baz ${qux}"
44+
-- | = ["bar", "qux"]
45+
-- | ```
46+
getTemplateVars :: String -> Array String
47+
getTemplateVars = runFn1 _getTemplateVars
48+
3749
foreign import _buildExclamationKeyObject :: forall e. Array (Tuple String String) -> { | e }
3850

51+
foreign import _getTemplateVars :: Fn1 String (Array String)
52+

0 commit comments

Comments
 (0)