@@ -3,6 +3,7 @@
33{-# LANGUAGE GeneralizedNewtypeDeriving #-}
44{-# LANGUAGE LambdaCase #-}
55{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE ScopedTypeVariables #-}
67
78-- Module : Gen.Types.Id
89-- Copyright : (c) 2015 Brendan Hay
@@ -30,6 +31,8 @@ module Gen.Types.Id
3031
3132 , reference
3233
34+ , orderParams
35+
3336 -- * Naming
3437 , aname
3538 , mname
@@ -44,8 +47,12 @@ module Gen.Types.Id
4447 ) where
4548
4649import Data.Aeson hiding (Bool, String)
50+import Data.CaseInsensitive (CI)
4751import qualified Data.CaseInsensitive as CI
52+import Data.Coerce
53+import Data.Foldable (foldl')
4854import Data.Hashable
55+import Data.List (elemIndex, sortOn)
4956import Data.Semigroup hiding (Sum)
5057import Data.String
5158import Data.Text (Text)
@@ -146,3 +153,27 @@ reference (Global g) (Local l) = Global (g <> Text.split (== '.') l)
146153
147154localise :: Global -> Local
148155localise = Local . global
156+
157+orderParams :: (a -> Local) -> [a] -> [Local] -> [a]
158+orderParams f xs ys = orderBy f zs (del zs [] ++ global)
159+ where
160+ zs = orderBy f (sortOn f xs) ys
161+
162+ del _ [] = []
163+ del [] rs = reverse rs
164+ del (r:qs) rs
165+ | f r `elem` global = del qs rs
166+ | otherwise = del qs (f r:rs)
167+
168+ global =
169+ [ "quotaUser"
170+ , "prettyPrint"
171+ , "userIp"
172+ , "fields"
173+ , "key"
174+ , "oauth_token"
175+ , "alt"
176+ ]
177+
178+orderBy :: Eq b => (a -> b) -> [a] -> [b] -> [a]
179+orderBy g xs ys = sortOn (flip elemIndex ys . g) xs