diff --git a/README.md b/README.md index b110c15..d14d417 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,8 @@ You may also want to read this [introduction and sample use case][1]. - object(key, value [, ...]) - fromFile(fileName) - fromRegex(pattern) + - join(sep, val1, [, ...]) + - join(array) ## Installation diff --git a/src/Aeson.hs b/src/Aeson.hs index d83485d..87c9337 100644 --- a/src/Aeson.hs +++ b/src/Aeson.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE OverloadedStrings #-} module Aeson ( asInt, @@ -65,7 +66,12 @@ asArray o = Left $ "Expected an array, but received: " <> show o -- -- >>> asText (Number 10.3) -- Right "10.3" +-- +-- >>> asText (Number 4) +-- Right "4" asText :: Value -> Either String T.Text asText (String t) = Right t -asText (Number n) = Right . T.pack $ show n +asText (Number n) = Right $ case (S.toBoundedInteger n :: Maybe Int) of + Nothing -> T.pack $ show n + Just int -> T.pack $ show int asText o = Left $ "Expected a string, but received: " <> show o diff --git a/src/Fake.hs b/src/Fake.hs index d866c6d..18e1c26 100644 --- a/src/Fake.hs +++ b/src/Fake.hs @@ -54,6 +54,7 @@ import qualified Text.Regex.TDFA.ReadRegex as R import Prelude hiding (lines, replicate) import Data.Aeson.Key (fromText) import Data.Aeson.Types (Pair) +import qualified Data.Either as V -- $setup @@ -154,6 +155,44 @@ oneOfArray arr = do pure $ arr' V.! idx +-- | Join argument expressions into a string. +-- +-- If there is more than one argument the first first argument is used as separator. +-- If there is a single argument and it is an array, the array is concattenated. +-- +-- >>> exec "join(['Hello', 'World'])" +-- String "HelloWorld" +-- +-- >>> exec "join('-', 'Hello', 'World')" +-- String "Hello-World" +-- +-- >>> exec "join('foo')" +-- String "foo" +-- +-- >>> exec "join([1, 2, 3])" +-- String "123" +-- +-- >>> exec "join()" +-- String "" +joinArgs :: [Expr] -> Fake Value +joinArgs [] = pure $ String "" +joinArgs [arg] = do + val <- eval arg + pure $ case val of + (String _) -> val + (Array arr') -> do + let strings = V.rights . V.toList $ fmap A.asText arr' + text = T.intercalate "" strings + String text + _ -> error $ "Expected a string or array, but received: " <> show val +joinArgs (x:xs) = do + sep <- Except.liftEither . A.asText =<< eval x + values <- mapM eval xs + let strings = V.rights $ fmap A.asText values + text = T.intercalate sep strings + pure $ String text + + -- | Select one random argument -- -- >>> exec "oneOf(37, 42, 21)" @@ -443,6 +482,7 @@ eval (Fn "randomDateTime" [lower, upper]) = do upper' <- rightToMaybe . A.asText <$> eval upper randomDateTime lower' upper' eval (Fn "array" args) = Array . V.fromList <$> mapM eval args +eval (Fn "join" args) = joinArgs args eval (Fn "oneOf" [arg]) = oneOfArray arg eval (Fn "oneOf" args) = oneOfArgs args eval (Fn "replicate" [num, expr]) = replicate num expr diff --git a/stack.yaml b/stack.yaml index 289af30..5b544fc 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,5 +1,5 @@ --- -resolver: lts-22.13 +resolver: lts-22.29 packages: - . diff --git a/stack.yaml.lock b/stack.yaml.lock index fcf6d2a..65f4265 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -6,7 +6,7 @@ packages: [] snapshots: - completed: - sha256: 6f0bea3ba5b07360f25bc886e8cff8d847767557a492a6f7f6dcb06e3cc79ee9 - size: 712905 - url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/13.yaml - original: lts-22.13 + sha256: 98fe98dae6f42f9b4405e5467f62f57df2896c57b742a09772688c900c722510 + size: 719579 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/29.yaml + original: lts-22.29