Skip to content

Commit

Permalink
chore(util-body-length-browser): move calculateBodyLength to it's own…
Browse files Browse the repository at this point in the history
… file (#3382)
  • Loading branch information
trivikr authored Mar 8, 2022
1 parent a925900 commit 561c507
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
1 change: 1 addition & 0 deletions packages/util-body-length-browser/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ const base = require("../../jest.config.base.js");

module.exports = {
...base,
testEnvironment: "jsdom",
};
36 changes: 36 additions & 0 deletions packages/util-body-length-browser/src/calculateBodyLength.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { calculateBodyLength } from "./calculateBodyLength";

const arrayBuffer = new ArrayBuffer(1);
const typedArray = new Uint8Array(1);

describe(calculateBodyLength.name, () => {
it("should handle string inputs", () => {
expect(calculateBodyLength("foo")).toEqual(3);
});

it("should handle string inputs with multi-byte characters", () => {
expect(calculateBodyLength("2。")).toEqual(4);
});

it("should handle inputs with byteLengths", () => {
expect(calculateBodyLength(arrayBuffer)).toEqual(1);
});

it("should handle TypedArray inputs", () => {
expect(calculateBodyLength(typedArray)).toEqual(1);
});

it("should handle File object", () => {
// Mock File Object https://developer.mozilla.org/en-US/docs/Web/API/File/File#example
const lastModifiedDate = new Date();
const mockFileObject = {
lastModified: lastModifiedDate.getTime(),
lastModifiedDate,
name: "foo.txt",
size: 3,
type: "text/plain",
webkitRelativePath: "",
};
expect(calculateBodyLength(mockFileObject)).toEqual(mockFileObject.size);
});
});
19 changes: 19 additions & 0 deletions packages/util-body-length-browser/src/calculateBodyLength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const calculateBodyLength = (body: any): number | undefined => {
if (typeof body === "string") {
let len = body.length;

for (let i = len - 1; i >= 0; i--) {
const code = body.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) len++;
else if (code > 0x7ff && code <= 0xffff) len += 2;
}

return len;
} else if (typeof body.byteLength === "number") {
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
return body.byteLength;
} else if (typeof body.size === "number") {
// handles browser File object
return body.size;
}
};
22 changes: 0 additions & 22 deletions packages/util-body-length-browser/src/index.spec.ts

This file was deleted.

20 changes: 1 addition & 19 deletions packages/util-body-length-browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
export function calculateBodyLength(body: any): number | undefined {
if (typeof body === "string") {
let len = body.length;

for (let i = len - 1; i >= 0; i--) {
const code = body.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) len++;
else if (code > 0x7ff && code <= 0xffff) len += 2;
}

return len;
} else if (typeof body.byteLength === "number") {
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
return body.byteLength;
} else if (typeof body.size === "number") {
// handles browser File object
return body.size;
}
}
export * from "./calculateBodyLength";

0 comments on commit 561c507

Please sign in to comment.