Skip to content

Commit c6c1bd0

Browse files
committed
Merge pull request #8 from purescript-node/better-behaved-show-encoding
Better-behaved Show instance for Encoding
2 parents bc60f63 + 4507276 commit c6c1bd0

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/Node/Buffer.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ foreign import fromArray :: forall e. Array Octet -> Eff (buffer :: BUFFER | e)
8787
-- | Creates a new buffer from a string with the specified encoding, sized to
8888
-- | match the string.
8989
fromString :: forall e. String -> Encoding -> Eff (buffer :: BUFFER | e) Buffer
90-
fromString str = fromStringImpl str <<< show
90+
fromString str = fromStringImpl str <<< encodingToNode
9191

9292
foreign import fromStringImpl :: forall e. String -> String -> Eff (buffer :: BUFFER | e) Buffer
9393

@@ -99,14 +99,14 @@ foreign import readImpl :: forall e. String -> Offset -> Buffer -> Eff (buffer :
9999

100100
-- | Reads a section of a buffer as a string with the specified encoding.
101101
readString :: forall e. Encoding -> Offset -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) String
102-
readString = readStringImpl <<< show
102+
readString = readStringImpl <<< encodingToNode
103103

104104
foreign import readStringImpl ::
105105
forall e. String -> Offset -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) String
106106

107107
-- | Reads the buffer as a string with the specified encoding.
108108
toString :: forall e. Encoding -> Buffer -> Eff (buffer :: BUFFER | e) String
109-
toString = toStringImpl <<< show
109+
toString = toStringImpl <<< encodingToNode
110110

111111
foreign import toStringImpl :: forall e. String -> Buffer -> Eff (buffer :: BUFFER | e) String
112112

@@ -121,7 +121,7 @@ foreign import writeImpl ::
121121
-- | characters will not be written to the buffer if there is not enough capacity
122122
-- | to write them fully. The number of bytes written is returned.
123123
writeString :: forall e. Encoding -> Offset -> Int -> String -> Buffer -> Eff (buffer :: BUFFER | e) Int
124-
writeString = writeStringImpl <<< show
124+
writeString = writeStringImpl <<< encodingToNode
125125

126126
foreign import writeStringImpl ::
127127
forall e. String -> Offset -> Int -> String -> Buffer -> Eff (buffer :: BUFFER | e) Int

src/Node/Encoding.purs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Node.Encoding
22
( Encoding (..)
3+
, encodingToNode
34
, byteLength
45
) where
56

@@ -15,15 +16,26 @@ data Encoding
1516
| Hex
1617

1718
instance showEncoding :: Show Encoding where
18-
show ASCII = "ascii"
19-
show UTF8 = "utf8"
20-
show UTF16LE = "utf16le"
21-
show UCS2 = "ucs2"
22-
show Base64 = "base64"
23-
show Binary = "binary"
24-
show Hex = "hex"
19+
show ASCII = "ASCII"
20+
show UTF8 = "UTF8"
21+
show UTF16LE = "UTF16LE"
22+
show UCS2 = "UCS2"
23+
show Base64 = "Base64"
24+
show Binary = "Binary"
25+
show Hex = "Hex"
26+
27+
-- | Convert an `Encoding` to a `String` in the format expected by Node.js
28+
-- | APIs.
29+
encodingToNode :: Encoding -> String
30+
encodingToNode ASCII = "ascii"
31+
encodingToNode UTF8 = "utf8"
32+
encodingToNode UTF16LE = "utf16le"
33+
encodingToNode UCS2 = "ucs2"
34+
encodingToNode Base64 = "base64"
35+
encodingToNode Binary = "binary"
36+
encodingToNode Hex = "hex"
2537

2638
foreign import byteLengthImpl :: String -> String -> Int
2739

2840
byteLength :: String -> Encoding -> Int
29-
byteLength str enc = byteLengthImpl str (show enc)
41+
byteLength str enc = byteLengthImpl str (encodingToNode enc)

0 commit comments

Comments
 (0)