/
Blob.purs
85 lines (71 loc) · 2.79 KB
/
Blob.purs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module Web.File.Blob
( Blob
, fromString
, fromArray
, type_
, size
, StartByte(..)
, EndByte(..)
, ByteIdx
, idxFromInt
, idxFromNumber
, slice
, slice'
) where
import Data.Int (toNumber)
import Data.Maybe (Maybe(..))
import Data.MediaType (MediaType(..))
import Math (round)
import Prelude ((==), (>>>))
import Unsafe.Coerce (unsafeCoerce)
foreign import data Blob :: Type
-- | Creates a String with the given Mediatype
-- | For example:
-- | ```
-- | myBlob = fromString (unsafeStringify { name: "Carl", age: 25 }) (MediaType "application/json")
-- | ```
fromString :: String -> MediaType -> Blob
fromString str ct = blobImpl [str] ct
-- | Creates a Blob from an Array of strings with the given Mediatype
fromArray :: Array String -> MediaType -> Blob
fromArray args opts = blobImpl args opts
foreign import blobImpl :: Array String -> MediaType -> Blob
foreign import typeImpl :: Blob -> String
-- | `MediaType` of the data contained in the `Blob`.
-- | Returns `Nothing` if the `MediaType` is unknown.
type_ :: Blob -> Maybe MediaType
type_ blob =
let
blobType = typeImpl blob
in
if blobType == ""
then Nothing
else Just (MediaType blobType)
-- | The size (in bytes) of the data contained in the `Blob`.
foreign import size :: Blob -> Number
-- | An index into the Blob indicating the first byte to include in the new Blob.
-- | If you specify a negative value, it's treated as an offset from the end of the
-- | string toward the beginning. For example, -10 would be the 10th from last byte
-- | in the Blob. If you specify a value for start that is larger than the size
-- | of the source Blob, the returned Blob has size 0 and contains no data.
newtype StartByte = StartByte ByteIdx
-- | An index into the Blob indicating the first byte that will *not* be included
-- | in the new Blob (i.e. the byte exactly at this index is not included).
-- | If you specify a negative value, it's treated as an offset from the end of
-- | the string toward the beginning. For example, -10 would be the 10th from
-- | last byte in the Blob. The default value is size.
newtype EndByte = EndByte ByteIdx
foreign import data ByteIdx :: Type
-- | Creates `ByteIdx` from `Int` value
idxFromInt :: Int -> ByteIdx
idxFromInt = toNumber >>> unsafeCoerce
-- | Creates `ByteIdx` from `Number` value using `Math.round`.
idxFromNumber :: Number -> ByteIdx
idxFromNumber = round >>> unsafeCoerce
-- | Creates a new `Blob` object (with specified `MediaType`), containing the
-- | data in the specified range of bytes of the source Blob, by setting .
foreign import slice ∷ MediaType -> StartByte -> EndByte -> Blob -> Blob
-- | Creates a new `Blob` object containing the data in the specified range
-- | of bytes of the source Blob.
slice' ∷ StartByte -> EndByte -> Blob -> Blob
slice' = slice (MediaType "")