Skip to content

Commit 02d09b9

Browse files
committed
Finished initial version
1 parent 0f43de9 commit 02d09b9

File tree

7 files changed

+74
-17
lines changed

7 files changed

+74
-17
lines changed

bower.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
"output"
1515
],
1616
"dependencies": {
17-
"purescript-console": "^1.0.0"
17+
"purescript-console": "^1.0.0",
18+
"purescript-functions": "^1.0.0",
19+
"purescript-tuples": "^1.0.0"
1820
},
1921
"devDependencies": {
20-
"purescript-psci-support": "^1.0.0"
22+
"purescript-psci-support": "^1.0.0",
23+
"purescript-test-unit": "^7.0.0"
2124
}
2225
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
// module Data.TemplateString
3+
4+
exports._template = function(str, tuples) {
5+
return tuples.reduce(function(newStr, tuple) {
6+
return newStr.replace(new RegExp('\\${'+ tuple.value0 +'}', 'g'), tuple.value1);
7+
}, str);
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Data.TemplateString
2+
((<^>)
3+
, template
4+
) where
5+
6+
import Data.Function.Uncurried (Fn2, runFn2)
7+
import Data.Tuple (Tuple)
8+
9+
-- | This is the safe version. The user is required to give a String representation of the object
10+
template :: String -> Array (Tuple String String) -> String
11+
template = runFn2 _template
12+
13+
infix 7 template as <^>
14+
15+
foreign import _template :: Fn2 String (Array (Tuple String String)) String
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
// module Data.TemplateString.Unsafe
3+
4+
exports._template = function(str, obj) {
5+
return Object.keys(obj).reduce(function(newStr, key) {
6+
return newStr.replace(new RegExp('\\${'+ key +'}', 'g'), obj[key]);
7+
}, str);
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Data.TemplateString.Unsafe
2+
((<~>)
3+
, template
4+
) where
5+
6+
import Data.Function.Uncurried (Fn2, runFn2)
7+
8+
-- | This is the unsafe version. Javascript will coerce whatever it's given to a string
9+
template :: forall a. String -> { | a } -> String
10+
template = runFn2 _template
11+
12+
infix 7 template as <~>
13+
14+
foreign import _template :: forall a. Fn2 String { | a } String

src/TemplateString.purs

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/TemplateString.purs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
module Test.Main where
1+
module Test.TemplateString where
22

3-
import Prelude
3+
import Prelude (Unit, bind, (==))
44
import Control.Monad.Eff (Eff)
5-
import Control.Monad.Eff.Console (CONSOLE, log)
5+
import Control.Monad.Eff.Console (CONSOLE)
66

7-
main :: forall e. Eff (console :: CONSOLE | e) Unit
8-
main = do
9-
log "You should add some tests."
7+
import Test.Unit (test, suite)
8+
import Test.Unit.Main (runTest)
9+
import Test.Unit.Assert (assert)
10+
import Test.Unit.Console (TESTOUTPUT)
11+
import Data.TemplateString.Unsafe ((<~>))
12+
import Data.TemplateString ((<^>))
13+
14+
import Data.Tuple.Nested
15+
16+
17+
main :: Eff ( console :: CONSOLE, testOutput :: TESTOUTPUT ) Unit
18+
main = runTest do
19+
suite "Unsafe" do
20+
test "template" do
21+
assert "single" ("Hello, ${name}!" <~> { name: "Mr. Foo" } == "Hello, Mr. Foo!")
22+
assert "multi" ("Hello, ${fName} ${lName}!" <~> { fName: "Foo", lName: "Bar" } == "Hello, Foo Bar!")
23+
24+
suite "Safe" do
25+
test "template" do
26+
assert "single" ("Hello, ${name}!" <^> ["name" /\ "Mr. Foo"] == "Hello, Mr. Foo!")
27+
assert "multi" ("Hello, ${fName} ${lName}!" <^> ["fName" /\ "Foo", "lName" /\ "Bar"] == "Hello, Foo Bar!")

0 commit comments

Comments
 (0)