Skip to content

Commit 561c507

Browse files
authored
chore(util-body-length-browser): move calculateBodyLength to it's own file (#3382)
1 parent a925900 commit 561c507

File tree

5 files changed

+57
-41
lines changed

5 files changed

+57
-41
lines changed

packages/util-body-length-browser/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ const base = require("../../jest.config.base.js");
22

33
module.exports = {
44
...base,
5+
testEnvironment: "jsdom",
56
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { calculateBodyLength } from "./calculateBodyLength";
2+
3+
const arrayBuffer = new ArrayBuffer(1);
4+
const typedArray = new Uint8Array(1);
5+
6+
describe(calculateBodyLength.name, () => {
7+
it("should handle string inputs", () => {
8+
expect(calculateBodyLength("foo")).toEqual(3);
9+
});
10+
11+
it("should handle string inputs with multi-byte characters", () => {
12+
expect(calculateBodyLength("2。")).toEqual(4);
13+
});
14+
15+
it("should handle inputs with byteLengths", () => {
16+
expect(calculateBodyLength(arrayBuffer)).toEqual(1);
17+
});
18+
19+
it("should handle TypedArray inputs", () => {
20+
expect(calculateBodyLength(typedArray)).toEqual(1);
21+
});
22+
23+
it("should handle File object", () => {
24+
// Mock File Object https://developer.mozilla.org/en-US/docs/Web/API/File/File#example
25+
const lastModifiedDate = new Date();
26+
const mockFileObject = {
27+
lastModified: lastModifiedDate.getTime(),
28+
lastModifiedDate,
29+
name: "foo.txt",
30+
size: 3,
31+
type: "text/plain",
32+
webkitRelativePath: "",
33+
};
34+
expect(calculateBodyLength(mockFileObject)).toEqual(mockFileObject.size);
35+
});
36+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const calculateBodyLength = (body: any): number | undefined => {
2+
if (typeof body === "string") {
3+
let len = body.length;
4+
5+
for (let i = len - 1; i >= 0; i--) {
6+
const code = body.charCodeAt(i);
7+
if (code > 0x7f && code <= 0x7ff) len++;
8+
else if (code > 0x7ff && code <= 0xffff) len += 2;
9+
}
10+
11+
return len;
12+
} else if (typeof body.byteLength === "number") {
13+
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
14+
return body.byteLength;
15+
} else if (typeof body.size === "number") {
16+
// handles browser File object
17+
return body.size;
18+
}
19+
};

packages/util-body-length-browser/src/index.spec.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1 @@
1-
export function calculateBodyLength(body: any): number | undefined {
2-
if (typeof body === "string") {
3-
let len = body.length;
4-
5-
for (let i = len - 1; i >= 0; i--) {
6-
const code = body.charCodeAt(i);
7-
if (code > 0x7f && code <= 0x7ff) len++;
8-
else if (code > 0x7ff && code <= 0xffff) len += 2;
9-
}
10-
11-
return len;
12-
} else if (typeof body.byteLength === "number") {
13-
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
14-
return body.byteLength;
15-
} else if (typeof body.size === "number") {
16-
// handles browser File object
17-
return body.size;
18-
}
19-
}
1+
export * from "./calculateBodyLength";

0 commit comments

Comments
 (0)