javascript-extras (empty) → 0.1.0.0
raw patch · 5 files changed
+130/−0 lines, 5 filesdep +basedep +ghcjs-basesetup-changed
Dependencies added: base, ghcjs-base
Files
- LICENSE +30/−0
- README.md +3/−0
- Setup.hs +2/−0
- javascript-extras.cabal +27/−0
- src/JavaScript/Extras.hs +68/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,3 @@+[](https://hackage.haskell.org/package/javascript-extras)++Extra javascript functions when using GHCJS
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ javascript-extras.cabal view
@@ -0,0 +1,27 @@+name: javascript-extras+version: 0.1.0.0+synopsis: Extra javascript functions when using GHCJS+description: Extra javascript functions when using GHCJS+homepage: https://github.com/louispan/javascript-extras#readme+license: BSD3+license-file: LICENSE+author: Louis Pan+maintainer: louis@pan.me+copyright: 2017 Louis Pan+category: Web+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: JavaScript.Extras+ build-depends: base >= 4.7 && < 5+ default-language: Haskell2010+ ghc-options: -Wall+ if impl(ghcjs)+ build-depends: ghcjs-base++source-repository head+ type: git+ location: https://github.com/louispan/javascript-extras
+ src/JavaScript/Extras.hs view
@@ -0,0 +1,68 @@+{-# LANGUAGE GeneralizedNewtypeDeriving #-}++module JavaScript.Extras+ ( strval+ , Property+ , getProperty+ , setProperty+ , PureJSVal(..)+ , toMaybeJSObject+ , toJSObject+ ) where++import Data.Foldable+import qualified Data.JSString as J+import qualified GHCJS.Marshal.Pure as J+import qualified GHCJS.Types as J+import qualified JavaScript.Object as JO++-- | This makes it easier to use OverloadedStrings with inputs that accept a JSVal that could be a JSString+strval :: J.JSString -> J.JSVal+strval = J.jsval++type Property = (J.JSString, J.JSVal)++-- | throws an exception if undefined or null+foreign import javascript unsafe+ "$2[$1]"+ js_unsafeGetProperty :: J.JSString -> J.JSVal -> IO J.JSVal++-- | throws an exception if undefined or null+foreign import javascript unsafe+ "$3[$1] = $2;"+ js_unsafeSetProperty :: J.JSString -> J.JSVal -> J.JSVal -> IO ()++-- | get a property of any JSVal. If a null or undefined is queried, the result will also be null+getProperty :: J.JSString -> J.JSVal -> IO J.JSVal+getProperty k x = let k' = J.pToJSVal k+ in if J.isUndefined x || J.isNull x+ || J.isUndefined k' || J.isNull k'+ then pure J.nullRef+ else js_unsafeGetProperty k x++-- | set a property of any JSVal+setProperty :: Property -> J.JSVal -> IO ()+setProperty (k, v) x = let k' = J.pToJSVal k+ in if J.isUndefined x || J.isNull x+ || J.isUndefined k' || J.isNull k'+ then pure ()+ else js_unsafeSetProperty k v x++-- | This is to enable conversion between equivalent classes 'IsJSVal' and 'PToJSVal'+-- So you can convert ((PureJSVal Object) to JSVal with pToJSVal without requiring IO)+newtype PureJSVal a = PureJSVal a+ deriving J.IsJSVal++instance J.IsJSVal a => J.PToJSVal (PureJSVal a) where+ pToJSVal (PureJSVal a) = J.jsval a++toMaybeJSObject :: [Property] -> IO (Maybe JO.Object)+toMaybeJSObject [] = pure Nothing+toMaybeJSObject xs = Just <$> toJSObject xs++toJSObject :: [Property] -> IO JO.Object+toJSObject [] = JO.create+toJSObject xs = do+ obj <- JO.create+ traverse_ (\(k, v) -> JO.unsafeSetProp k v obj) xs+ pure obj