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

Commit a2182b1

Browse files
committed
add Blob.slice
1 parent d452fa9 commit a2182b1

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-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 (blob) {
8+
return function (start) {
9+
return function (end) {
10+
return function (contentType) {
11+
return blob.slice(start, end, contentType);
12+
}
13+
}
14+
}
15+
}

src/DOM/File/Blob.purs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
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 Data.Newtype (unwrap)
18+
import Math (round)
19+
import Prelude ((==), (>>>))
20+
import Unsafe.Coerce (unsafeCoerce)
1021

1122
foreign import typeImpl :: Blob -> String
1223

@@ -23,3 +34,40 @@ type_ blob =
2334

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

Comments
 (0)