Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 8b703ee

Browse files
authored
Merge pull request #126 from safareli/blob
Add Blob.slice
2 parents d452fa9 + 44ea9da commit 8b703ee

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/DOM/File/Blob.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@
33
exports.typeImpl = function (blob) { return blob.type; };
44

55
exports.size = function (blob) { return blob.size; };
6+
7+
exports.slice = function (contentType) {
8+
return function (start) {
9+
return function (end) {
10+
return function (blob) {
11+
return blob.slice(start, end, contentType);
12+
};
13+
};
14+
};
15+
};

src/DOM/File/Blob.purs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
module 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)
715
import Data.Maybe (Maybe(..))
816
import Data.MediaType (MediaType(..))
9-
import DOM.File.Types (Blob)
17+
import Math (round)
18+
import Prelude ((==), (>>>))
19+
import Unsafe.Coerce (unsafeCoerce)
1020

1121
foreign import typeImpl :: Blob -> String
1222

@@ -23,3 +33,36 @@ type_ blob =
2333

2434
-- | The size (in bytes) of the data contained in the `Blob`.
2535
foreign 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

Comments
 (0)