-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathEncoding.purs
More file actions
49 lines (42 loc) · 1.07 KB
/
Encoding.purs
File metadata and controls
49 lines (42 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Node.Encoding
( Encoding(..)
, encodingToNode
, byteLength
) where
import Prelude
import Data.Function.Uncurried (Fn2, runFn2)
data Encoding
= ASCII
| UTF8
| UTF16LE
| UCS2
| Base64
| Base64Url
| Latin1
| Binary
| Hex
instance showEncoding :: Show Encoding where
show ASCII = "ASCII"
show UTF8 = "UTF8"
show UTF16LE = "UTF16LE"
show UCS2 = "UCS2"
show Base64 = "Base64"
show Base64Url = "Base64Url"
show Latin1 = "Latin1"
show Binary = "Binary"
show Hex = "Hex"
-- | Convert an `Encoding` to a `String` in the format expected by Node.js
-- | APIs.
encodingToNode :: Encoding -> String
encodingToNode ASCII = "ascii"
encodingToNode UTF8 = "utf8"
encodingToNode UTF16LE = "utf16le"
encodingToNode UCS2 = "ucs2"
encodingToNode Base64 = "base64"
encodingToNode Base64Url = "base64url"
encodingToNode Latin1 = "latin1"
encodingToNode Binary = "binary"
encodingToNode Hex = "hex"
foreign import byteLengthImpl :: Fn2 String String Int
byteLength :: String -> Encoding -> Int
byteLength str enc = runFn2 byteLengthImpl str (encodingToNode enc)