Skip to content

Commit 47e766a

Browse files
committed
Add function to join arguments/array into a string
Example usage: mkjson "x=join('', join(replicate(randomInt(40, 48), randomInt(1, 9))), '.', join(replicate(2, randomInt(1, 9))))"
1 parent 9bc26d1 commit 47e766a

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ You may also want to read this [introduction and sample use case][1].
8585
- object(key, value [, ...])
8686
- fromFile(fileName)
8787
- fromRegex(pattern)
88+
- join(sep, val1, [, ...])
89+
- join(array)
8890

8991

9092
## Installation

src/Aeson.hs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE OverloadedStrings #-}
12

23
module Aeson (
34
asInt,
@@ -65,7 +66,12 @@ asArray o = Left $ "Expected an array, but received: " <> show o
6566
--
6667
-- >>> asText (Number 10.3)
6768
-- Right "10.3"
69+
--
70+
-- >>> asText (Number 4)
71+
-- Right "4"
6872
asText :: Value -> Either String T.Text
6973
asText (String t) = Right t
70-
asText (Number n) = Right . T.pack $ show n
74+
asText (Number n) = Right $ case (S.toBoundedInteger n :: Maybe Int) of
75+
Nothing -> T.pack $ show n
76+
Just int -> T.pack $ show int
7177
asText o = Left $ "Expected a string, but received: " <> show o

src/Fake.hs

+40
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import qualified Text.Regex.TDFA.ReadRegex as R
5454
import Prelude hiding (lines, replicate)
5555
import Data.Aeson.Key (fromText)
5656
import Data.Aeson.Types (Pair)
57+
import qualified Data.Either as V
5758

5859

5960
-- $setup
@@ -154,6 +155,44 @@ oneOfArray arr = do
154155
pure $ arr' V.! idx
155156

156157

158+
-- | Join argument expressions into a string.
159+
--
160+
-- If there is more than one argument the first first argument is used as separator.
161+
-- If there is a single argument and it is an array, the array is concattenated.
162+
--
163+
-- >>> exec "join(['Hello', 'World'])"
164+
-- String "HelloWorld"
165+
--
166+
-- >>> exec "join('-', 'Hello', 'World')"
167+
-- String "Hello-World"
168+
--
169+
-- >>> exec "join('foo')"
170+
-- String "foo"
171+
--
172+
-- >>> exec "join([1, 2, 3])"
173+
-- String "123"
174+
--
175+
-- >>> exec "join()"
176+
-- String ""
177+
joinArgs :: [Expr] -> Fake Value
178+
joinArgs [] = pure $ String ""
179+
joinArgs [arg] = do
180+
val <- eval arg
181+
pure $ case val of
182+
(String _) -> val
183+
(Array arr') -> do
184+
let strings = V.rights . V.toList $ fmap A.asText arr'
185+
text = T.intercalate "" strings
186+
String text
187+
_ -> error $ "Expected a string or array, but received: " <> show val
188+
joinArgs (x:xs) = do
189+
sep <- Except.liftEither . A.asText =<< eval x
190+
values <- mapM eval xs
191+
let strings = V.rights $ fmap A.asText values
192+
text = T.intercalate sep strings
193+
pure $ String text
194+
195+
157196
-- | Select one random argument
158197
--
159198
-- >>> exec "oneOf(37, 42, 21)"
@@ -443,6 +482,7 @@ eval (Fn "randomDateTime" [lower, upper]) = do
443482
upper' <- rightToMaybe . A.asText <$> eval upper
444483
randomDateTime lower' upper'
445484
eval (Fn "array" args) = Array . V.fromList <$> mapM eval args
485+
eval (Fn "join" args) = joinArgs args
446486
eval (Fn "oneOf" [arg]) = oneOfArray arg
447487
eval (Fn "oneOf" args) = oneOfArgs args
448488
eval (Fn "replicate" [num, expr]) = replicate num expr

stack.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
resolver: lts-22.13
2+
resolver: lts-22.29
33

44
packages:
55
- .

stack.yaml.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
packages: []
77
snapshots:
88
- completed:
9-
sha256: 6f0bea3ba5b07360f25bc886e8cff8d847767557a492a6f7f6dcb06e3cc79ee9
10-
size: 712905
11-
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/13.yaml
12-
original: lts-22.13
9+
sha256: 98fe98dae6f42f9b4405e5467f62f57df2896c57b742a09772688c900c722510
10+
size: 719579
11+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/29.yaml
12+
original: lts-22.29

0 commit comments

Comments
 (0)