Skip to content

Commit 5251266

Browse files
committed
chore: move calculateBodyLength to it's own file
1 parent a331a3b commit 5251266

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { calculateBodyLength } from "./";
1+
import { calculateBodyLength } from "./calculateBodyLength";
22

33
const arrayBuffer = new ArrayBuffer(1);
44
const typedArray = new Uint8Array(1);
55

6-
describe("caclulateBodyLength", () => {
6+
describe(calculateBodyLength.name, () => {
77
it("should handle string inputs", () => {
88
expect(calculateBodyLength("foo")).toEqual(3);
99
});
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+
};
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)