-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(util-body-length-browser): move calculateBodyLength to it's own…
… file (#3382)
- Loading branch information
Showing
5 changed files
with
57 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ const base = require("../../jest.config.base.js"); | |
|
||
module.exports = { | ||
...base, | ||
testEnvironment: "jsdom", | ||
}; |
36 changes: 36 additions & 0 deletions
36
packages/util-body-length-browser/src/calculateBodyLength.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
packages/util-body-length-browser/src/calculateBodyLength.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |