Skip to content

Commit

Permalink
Add unsafeHas (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
natefaubion authored and paf31 committed Sep 26, 2017
1 parent d39eb6e commit 058cdde
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Data/Record/Unsafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ exports.unsafeDeleteFn = function(label, rec) {
}
return copy;
};

exports.unsafeHasFn = function(label, rec) {
return {}.hasOwnProperty.call(rec, label);
};
6 changes: 6 additions & 0 deletions src/Data/Record/Unsafe.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ module Data.Record.Unsafe
( unsafeGetFn
, unsafeSetFn
, unsafeDeleteFn
, unsafeHasFn
, unsafeGet
, unsafeSet
, unsafeDelete
, unsafeHas
) where

import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)

foreign import unsafeGetFn :: forall r a. Fn2 String (Record r) a
foreign import unsafeSetFn :: forall r1 r2 a. Fn3 String a (Record r1) (Record r2)
foreign import unsafeDeleteFn :: forall r1 r2. Fn2 String (Record r1) (Record r2)
foreign import unsafeHasFn :: forall r1. Fn2 String (Record r1) Boolean

unsafeGet :: forall r a. String -> Record r -> a
unsafeGet = runFn2 unsafeGetFn
Expand All @@ -21,3 +24,6 @@ unsafeSet = runFn3 unsafeSetFn

unsafeDelete :: forall r1 r2. String -> Record r1 -> Record r2
unsafeDelete = runFn2 unsafeDeleteFn

unsafeHas :: forall r1. String -> Record r1 -> Boolean
unsafeHas = runFn2 unsafeHasFn
5 changes: 5 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Prelude
import Control.Monad.Eff (Eff)
import Data.Record.Builder as Builder
import Data.Record (delete, get, insert, modify, set, equal)
import Data.Record.Unsafe (unsafeHas)
import Data.Symbol (SProxy(..))
import Test.Assert (ASSERT, assert')

Expand All @@ -27,6 +28,10 @@ main = do
equal { a: 1, b: "b", c: true } { a: 1, b: "b", c: true }
assert' "equal2" $
not $ equal { a: 1, b: "b", c: true } { a: 1, b: "b", c: false }
assert' "unsafeHas1" $
unsafeHas "a" { a: 42 }
assert' "unsafeHas2" $
not $ unsafeHas "b" { a: 42 }

let testBuilder = Builder.build (Builder.insert x 42
>>> Builder.merge { y: true, z: "testing" }
Expand Down

0 comments on commit 058cdde

Please sign in to comment.