diff --git a/src/Node/Buffer.js b/src/Node/Buffer.js index 09c95c9..8c60fed 100644 --- a/src/Node/Buffer.js +++ b/src/Node/Buffer.js @@ -89,14 +89,14 @@ exports.toArray = function (buff) { }; }; -exports.getAtOffsetImpl = function (nothing) { - return function (just) { - return function (buff) { - return function (offset) { +exports.getAtOffsetImpl = function (just) { + return function (nothing) { + return function (offset) { + return function (buff) { return function() { var octet = buff[offset]; return octet == null ? nothing - : just(buff[i]); + : just(octet); }; }; }; diff --git a/src/Node/Buffer.purs b/src/Node/Buffer.purs index 415a96d..e07bc23 100644 --- a/src/Node/Buffer.purs +++ b/src/Node/Buffer.purs @@ -28,7 +28,7 @@ import Data.Maybe (Maybe(..)) import Node.Encoding (Encoding, encodingToNode) -- | Type synonym indicating the value should be an octet (0-255). If the value --- | provided is outside this range it will be used as modulo 255. +-- | provided is outside this range it will be used as modulo 256. type Octet = Int -- | Type synonym indicating the value refers to an offset in a buffer. diff --git a/test/Main.purs b/test/Main.purs index 45956d7..b569898 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -3,8 +3,9 @@ module Test.Main where import Prelude import Control.Monad.Eff (Eff) import Control.Monad.Eff.Console (log, CONSOLE()) +import Data.Maybe (Maybe(..)) import Data.Traversable (traverse) -import Node.Buffer (BUFFER, BufferValueType(..), toArray, concat', fromArray, fill, copy, readString, fromString, toString, read, write, create) +import Node.Buffer (BUFFER, BufferValueType(..), toArray, concat', fromArray, fill, copy, readString, fromString, toString, read, write, create, getAtOffset) import Node.Encoding (Encoding(..)) import Test.Assert (ASSERT, assert') @@ -41,6 +42,9 @@ main = do log "concat'" testConcat' + log "getAtOffset" + testGetAtOffset + testReadWrite :: Test testReadWrite = do buf <- create 1 @@ -120,6 +124,13 @@ testConcat' = do assertEq [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] out +testGetAtOffset :: Test +testGetAtOffset = do + buf <- fromArray [1, 2, 3, 4] + assertEq (Just 2) =<< getAtOffset 1 buf + assertEq Nothing =<< getAtOffset 4 buf + assertEq Nothing =<< getAtOffset (-1) buf + assertEq :: forall a. (Eq a, Show a) => a -> a -> Test assertEq x y = if x == y