Simplify checking object keys for equality
This commit is contained in:
@@ -201,19 +201,14 @@ ensureUniform :: JSON.Array -> Maybe UniformObjects
|
|||||||
ensureUniform arr =
|
ensureUniform arr =
|
||||||
let objs :: V.Vector JSON.Object
|
let objs :: V.Vector JSON.Object
|
||||||
objs = foldr -- filter non-objects, map to raw objects
|
objs = foldr -- filter non-objects, map to raw objects
|
||||||
(\result val -> case val of
|
(\val result -> case val of
|
||||||
JSON.Object o -> V.cons o result
|
JSON.Object o -> V.cons o result
|
||||||
_ -> result)
|
_ -> result)
|
||||||
V.empty arr
|
V.empty arr
|
||||||
keysPerObj :: [S.Set T.Text]
|
keysPerObj = V.map (S.fromList . M.keys) objs
|
||||||
keysPerObj = V.toList $ V.map (S.fromList . M.keys) objs
|
canonicalKeys = fromMaybe S.empty $ keysPerObj V.!? 0
|
||||||
allKeys :: S.Set T.Text
|
areKeysUniform = all (==canonicalKeys) keysPerObj in
|
||||||
allKeys = S.unions keysPerObj
|
|
||||||
commonKeys :: S.Set T.Text
|
|
||||||
commonKeys = case keysPerObj of
|
|
||||||
h : _ -> foldr S.intersection h keysPerObj
|
|
||||||
[] -> S.empty in
|
|
||||||
|
|
||||||
if (length objs == length arr) && (allKeys == commonKeys)
|
if (V.length objs == V.length arr) && areKeysUniform
|
||||||
then Just (UniformObjects objs)
|
then Just (UniformObjects objs)
|
||||||
else Nothing
|
else Nothing
|
||||||
|
|||||||
Reference in New Issue
Block a user