Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/react-native/Libraries/Blob/Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class Blob {
let {offset, size} = this.data;

if (typeof start === 'number') {
if (start < 0) {
// A negative start is relative to the end of the blob.
// $FlowFixMe[reassign-const]
start = Math.max(this.size + start, 0);
}
if (start > size) {
// $FlowFixMe[reassign-const]
start = size;
Expand All @@ -102,7 +107,8 @@ class Blob {
// $FlowFixMe[reassign-const]
end = this.size;
}
size = end - start;
// Clamp to 0 so an end that precedes start yields an empty blob.
size = Math.max(end - start, 0);
}
}
return BlobManager.createFromOptions({
Expand Down
32 changes: 32 additions & 0 deletions packages/react-native/Libraries/Blob/__tests__/Blob-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@ describe('Blob', function () {
expect(sliceC.size).toBe(Math.min(blob.data.size, 34569) - 34543);
});

it('should slice a blob with a negative start', () => {
const blob = new Blob();
blob.data.size = 34546;

// A negative start is relative to the end of the blob.
const slice = blob.slice(-100);

expect(slice.data.offset).toBe(34446);
expect(slice.size).toBe(100);
});

it('should slice a blob with a negative end', () => {
const blob = new Blob();
blob.data.size = 34546;

// A negative end is relative to the end of the blob.
const slice = blob.slice(0, -100);

expect(slice.data.offset).toBe(0);
expect(slice.size).toBe(34446);
});

it('should return an empty slice when end precedes start', () => {
const blob = new Blob();
blob.data.size = 34546;

const slice = blob.slice(200, 100);

expect(slice.data.offset).toBe(200);
expect(slice.size).toBe(0);
});

it('should slice a blob and sets a contentType', () => {
const blob = new Blob();

Expand Down
Loading