Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using Object.prototype.hasOwnProperty.call(rec, label) so that a new object isn't created?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's using the same code the compiler generates in record updates. Engines will optimize this out.

};
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