1
1
module DOM.File.Blob
2
2
( type_
3
3
, size
4
+ , StartByte (..)
5
+ , EndByte (..)
6
+ , idxFromInt
7
+ , idxFromNumber
8
+ , ByteIdx
9
+ , slice
10
+ , slice'
4
11
) where
5
12
6
- import Prelude ((==))
13
+ import DOM.File.Types (Blob )
14
+ import Data.Int (toNumber )
7
15
import Data.Maybe (Maybe (..))
8
16
import Data.MediaType (MediaType (..))
9
- import DOM.File.Types (Blob )
17
+ import Data.Newtype (unwrap )
18
+ import Math (round )
19
+ import Prelude ((==), (>>>))
20
+ import Unsafe.Coerce (unsafeCoerce )
10
21
11
22
foreign import typeImpl :: Blob -> String
12
23
@@ -23,3 +34,40 @@ type_ blob =
23
34
24
35
-- | The size (in bytes) of the data contained in the `Blob`.
25
36
foreign import size :: Blob -> Number
37
+
38
+ -- | An index into the Blob indicating the first byte to include in the new Blob.
39
+ -- | If you specify a negative value, it's treated as an offset from the end of the
40
+ -- | string toward the beginning. For example, -10 would be the 10th from last byte
41
+ -- | in the Blob. If you specify a value for start that is larger than the size
42
+ -- | of the source Blob, the returned Blob has size 0 and contains no data.
43
+ newtype StartByte = StartByte ByteIdx
44
+
45
+ -- | An index into the Blob indicating the first byte that will *not* be included
46
+ -- | in the new Blob (i.e. the byte exactly at this index is not included).
47
+ -- | If you specify a negative value, it's treated as an offset from the end of
48
+ -- | the string toward the beginning. For example, -10 would be the 10th from
49
+ -- | last byte in the Blob. The default value is size.
50
+ newtype EndByte = EndByte ByteIdx
51
+
52
+ foreign import data ByteIdx :: Type
53
+
54
+ -- | Creates `ByteIdx` from `Int` value
55
+ idxFromInt :: Int -> ByteIdx
56
+ idxFromInt = toNumber >>> unsafeCoerce
57
+
58
+ -- | Creates `ByteIdx` from `Number` value using `Math.round`.
59
+ idxFromNumber :: Number -> ByteIdx
60
+ idxFromNumber = round >>> unsafeCoerce
61
+
62
+ -- | Creates a new `Blob` object (with specified `MediaType`), containing the
63
+ -- | data in the specified range of bytes of the source Blob, by setting .
64
+ slice ∷ MediaType -> StartByte -> EndByte -> Blob -> Blob
65
+ slice mediaType (StartByte start) (EndByte end) blob =
66
+ _slice blob start end (unwrap mediaType)
67
+
68
+ -- | Creates a new `Blob` object containing the data in the specified range
69
+ -- | of bytes of the source Blob.
70
+ slice' ∷ StartByte -> EndByte -> Blob -> Blob
71
+ slice' (StartByte start) (EndByte end) blob = _slice blob start end " "
72
+
73
+ foreign import _slice ∷ Blob -> ByteIdx -> ByteIdx -> String -> Blob
0 commit comments