Skip to content

Commit 7619e9c

Browse files
Fix Blob.slice end handling when start is omitted (#59533)
1 parent eaf4c26 commit 7619e9c

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎packages/react-native/Libraries/Blob/Blob.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Blob {
8181
return this._data;
8282
}
8383

84-
slice(start?: number, end?: number, contentType?: string = ''): Blob {
84+
slice(start?: number = 0, end?: number, contentType?: string = ''): Blob {
8585
const BlobManager = require('./BlobManager').default;
8686
let {offset, size} = this.data;
8787

‎packages/react-native/Libraries/Blob/__tests__/Blob-test.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ describe('Blob', function () {
9292
expect(slice.size).toBe(100);
9393
});
9494

95+
it.each([
96+
[3, 3],
97+
[0, 0],
98+
[-2, 4],
99+
[-10, 0],
100+
[10, 6],
101+
[undefined, 6],
102+
])('should use an end of %s when start is omitted', (end, expectedSize) => {
103+
const blob = new Blob(['abcdef']);
104+
const slice = blob.slice(undefined, end, 'text/plain');
105+
106+
expect(slice.data.offset).toBe(0);
107+
expect(slice.size).toBe(expectedSize);
108+
expect(slice.type).toBe('text/plain');
109+
expect(slice.data.blobId).toBe(blob.data.blobId);
110+
});
111+
112+
it('should preserve the source offset when start is omitted', () => {
113+
const blob = new Blob(['abcdefgh']).slice(2, 7);
114+
const slice = blob.slice(undefined, 2);
115+
116+
expect(slice.data.offset).toBe(2);
117+
expect(slice.size).toBe(2);
118+
expect(blob.size).toBe(5);
119+
});
120+
95121
it('should slice a blob with a negative end', () => {
96122
const blob = new Blob();
97123
blob.data.size = 34546;

0 commit comments

Comments
 (0)