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