Skip to content

Commit

Permalink
Merge pull request #35 from purescript-node/number-read-write
Browse files Browse the repository at this point in the history
Use Number for reading and writing, fixes #25
  • Loading branch information
hdgarrood authored Jun 30, 2019
2 parents 7098df6 + 60692d6 commit dac7d47
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Node/Buffer/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Monad m <= MutableBuffer buf m | buf -> m where
toArrayBuffer :: buf -> m ArrayBuffer

-- | Reads a numeric value from a buffer at the specified offset.
read :: BufferValueType -> Offset -> buf -> m Int
read :: BufferValueType -> Offset -> buf -> m Number

-- | Reads a section of a buffer as a string with the specified encoding.
readString :: Encoding -> Offset -> Offset -> buf -> m String
Expand All @@ -77,7 +77,7 @@ class Monad m <= MutableBuffer buf m | buf -> m where
toString :: Encoding -> buf -> m String

-- | Writes a numeric value to a buffer at the specified offset.
write :: BufferValueType -> Int -> Offset -> buf -> m Unit
write :: BufferValueType -> Number -> Offset -> buf -> m Unit

-- | Writes octets from a string to a buffer at the specified offset. Multi-byte
-- | characters will not be written to the buffer if there is not enough capacity
Expand Down
4 changes: 2 additions & 2 deletions src/Node/Buffer/Immutable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ fromString str = fromStringImpl str <<< encodingToNode
foreign import fromStringImpl :: String -> String -> ImmutableBuffer

-- | Reads a numeric value from a buffer at the specified offset.
read :: BufferValueType -> Offset -> ImmutableBuffer -> Int
read :: BufferValueType -> Offset -> ImmutableBuffer -> Number
read = readImpl <<< show

foreign import readImpl :: String -> Offset -> ImmutableBuffer -> Int
foreign import readImpl :: String -> Offset -> ImmutableBuffer -> Number

-- | Reads a section of a buffer as a string with the specified encoding.
readString :: Encoding -> Offset -> Offset -> ImmutableBuffer -> String
Expand Down
6 changes: 3 additions & 3 deletions src/Node/Buffer/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fromArrayBuffer = usingToImmutable Immutable.fromArrayBuffer
toArrayBuffer :: forall buf m. Monad m => buf -> m ArrayBuffer
toArrayBuffer = usingFromImmutable Immutable.toArrayBuffer

read :: forall buf m. Monad m => BufferValueType -> Offset -> buf -> m Int
read :: forall buf m. Monad m => BufferValueType -> Offset -> buf -> m Number
read t o = usingFromImmutable $ Immutable.read t o

readString :: forall buf m. Monad m => Encoding -> Offset -> Offset -> buf -> m String
Expand All @@ -73,10 +73,10 @@ readString m o o' = usingFromImmutable $ Immutable.readString m o o'
toString :: forall buf m. Monad m => Encoding -> buf -> m String
toString m = usingFromImmutable $ Immutable.toString m

write :: forall buf m. Monad m => BufferValueType -> Int -> Offset -> buf -> m Unit
write :: forall buf m. Monad m => BufferValueType -> Number -> Offset -> buf -> m Unit
write = writeInternal <<< show

foreign import writeInternal :: forall buf m. String -> Int -> Offset -> buf -> m Unit
foreign import writeInternal :: forall buf m. String -> Number -> Offset -> buf -> m Unit

writeString :: forall buf m. Monad m => Encoding -> Offset -> Int -> String -> buf -> m Int
writeString = writeStringInternal <<< encodingToNode
Expand Down
6 changes: 3 additions & 3 deletions test/Test/Node/Buffer/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ testMutableBuffer _ run = do

testReadWrite :: Effect Unit
testReadWrite = do
let val = 42
let val = 42.0
readVal <- run do
buf <- create 1 :: m buf
write UInt8 val 0 buf
Expand All @@ -94,7 +94,7 @@ testMutableBuffer _ run = do
buf <- fromArray [1,2,3,4,5] :: m buf
read UInt8 2 buf

assertEqual {expected: 3, actual: readVal}
assertEqual {expected: 3.0, actual: readVal}

testToArray :: Effect Unit
testToArray = do
Expand All @@ -112,7 +112,7 @@ testMutableBuffer _ run = do
buf <- fromString str ASCII :: m buf
read UInt8 6 buf

assertEqual {expected: 32, actual: val} -- ASCII space
assertEqual {expected: 32.0, actual: val} -- ASCII space

testToFromArrayBuffer :: Effect Unit
testToFromArrayBuffer = do
Expand Down
2 changes: 1 addition & 1 deletion test/Test/Node/Buffer/Immutable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ testCreate = do
testFromString :: Effect Unit
testFromString = do
let buf = Immutable.fromString "hello, world" ASCII
assertEqual {expected: 32, actual: Immutable.read UInt8 6 buf}
assertEqual {expected: 32.0, actual: Immutable.read UInt8 6 buf}

testToString :: Effect Unit
testToString = do
Expand Down

0 comments on commit dac7d47

Please sign in to comment.