-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgetByteSize.ts
More file actions
27 lines (23 loc) · 722 Bytes
/
getByteSize.ts
File metadata and controls
27 lines (23 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { requireString } from "../../lang";
/**
* Returns the length of a string in `UTF-8` encoding, measured in bytes.
*
* @param {string} value - The string whose length will be calculated in bytes.
* @returns {number} The length of the input string in bytes.
* @since 1.0.0
* @version 1.0.0
* @environment `Google Apps Script`
*/
export function getByteSize(value: string): number {
return (
requireString(value)
// 0
// .replace(/[\u{0000}-\u{007F}]/u, '0')
// 00
// .replace(/[\u{0080}-\u{07FF}]/u, '0')
// 000
// .replace(/[\u{0800}-\u{D7FF}\u{E000}-\u{FFFF}]/u, '0')
// 0000
.replace(/[\u{D800}-\u{DFFF}]/u, "00").length
);
}