@fern-api/ui-core-utils 0.126.2-bc708329b → 0.126.2-e97952e48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bytes.d.ts +11 -3
- package/dist/bytes.d.ts.map +1 -1
- package/dist/bytes.js +24 -32
- package/package.json +1 -1
package/dist/bytes.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chunks a string into an array of strings, each of the specified byte size.
|
|
3
|
+
* @param str - The string to chunk.
|
|
4
|
+
* @param byteSize - The byte size of each chunk. i.e. 10KB = 50 * 1000
|
|
5
|
+
* @returns An array of strings, each of the specified byte size.
|
|
6
|
+
*/
|
|
7
|
+
export declare function chunkToBytes(str: string, byteSize: number): string[];
|
|
1
8
|
/**
|
|
2
9
|
* Truncates a string to the specified byte length.
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
10
|
+
* @param str - The string to truncate.
|
|
11
|
+
* @param byteSize - The byte size of the truncated string. i.e. 10KB = 50 * 1000
|
|
12
|
+
* @returns The truncated string.
|
|
5
13
|
*/
|
|
6
|
-
export declare
|
|
14
|
+
export declare function truncateToBytes(str: string, byteSize: number): string;
|
|
7
15
|
//# sourceMappingURL=bytes.d.ts.map
|
package/dist/bytes.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAapE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAKrE"}
|
package/dist/bytes.js
CHANGED
|
@@ -1,37 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
curByteLength += getLength(segment);
|
|
17
|
-
if (curByteLength === byteLength) {
|
|
18
|
-
return string.slice(0, i + 1);
|
|
19
|
-
}
|
|
20
|
-
else if (curByteLength > byteLength) {
|
|
21
|
-
return string.slice(0, i - segment.length + 1);
|
|
22
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Chunks a string into an array of strings, each of the specified byte size.
|
|
3
|
+
* @param str - The string to chunk.
|
|
4
|
+
* @param byteSize - The byte size of each chunk. i.e. 10KB = 50 * 1000
|
|
5
|
+
* @returns An array of strings, each of the specified byte size.
|
|
6
|
+
*/
|
|
7
|
+
export function chunkToBytes(str, byteSize) {
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
// TODO: what if the string isn't utf8?
|
|
10
|
+
const utf8Bytes = encoder.encode(str);
|
|
11
|
+
const numChunks = Math.ceil(utf8Bytes.length / byteSize);
|
|
12
|
+
const chunks = new Array(numChunks);
|
|
13
|
+
for (let i = 0, o = 0; i < numChunks; ++i, o += byteSize) {
|
|
14
|
+
chunks[i] = new TextDecoder().decode(utf8Bytes.slice(o, o + byteSize));
|
|
23
15
|
}
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
function isHighSurrogate(codePoint) {
|
|
27
|
-
return codePoint >= 0xd800 && codePoint <= 0xdbff;
|
|
28
|
-
}
|
|
29
|
-
function isLowSurrogate(codePoint) {
|
|
30
|
-
return codePoint >= 0xdc00 && codePoint <= 0xdfff;
|
|
16
|
+
return chunks;
|
|
31
17
|
}
|
|
32
18
|
/**
|
|
33
19
|
* Truncates a string to the specified byte length.
|
|
34
|
-
*
|
|
35
|
-
* @
|
|
20
|
+
* @param str - The string to truncate.
|
|
21
|
+
* @param byteSize - The byte size of the truncated string. i.e. 10KB = 50 * 1000
|
|
22
|
+
* @returns The truncated string.
|
|
36
23
|
*/
|
|
37
|
-
export
|
|
24
|
+
export function truncateToBytes(str, byteSize) {
|
|
25
|
+
const encoder = new TextEncoder();
|
|
26
|
+
const utf8Bytes = encoder.encode(str);
|
|
27
|
+
const truncatedBytes = utf8Bytes.slice(0, byteSize);
|
|
28
|
+
return new TextDecoder().decode(truncatedBytes);
|
|
29
|
+
}
|