@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 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
- * @see https://github.com/parshap/truncate-utf8-bytes
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 const truncateToBytes: (string: string, byteLength: number) => string;
14
+ export declare function truncateToBytes(str: string, byteSize: number): string;
7
15
  //# sourceMappingURL=bytes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAuCA;;;;GAIG;AACH,eAAO,MAAM,eAAe,gDAAuD,CAAC"}
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
- function _truncate(getLength, string, byteLength) {
2
- if (typeof string !== "string") {
3
- throw new Error("Input must be string");
4
- }
5
- let curByteLength = 0;
6
- let codePoint;
7
- let segment;
8
- for (let i = 0; i < string.length; i += 1) {
9
- codePoint = string.charCodeAt(i);
10
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
11
- segment = string[i];
12
- if (isHighSurrogate(codePoint) && isLowSurrogate(string.charCodeAt(i + 1))) {
13
- i += 1;
14
- segment += string[i];
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 string;
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
- * @see https://github.com/parshap/truncate-utf8-bytes
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 const truncateToBytes = _truncate.bind(null, Buffer.byteLength.bind(Buffer));
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fern-api/ui-core-utils",
3
- "version": "0.126.2-bc708329b",
3
+ "version": "0.126.2-e97952e48",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/fern-api/fern-platform.git",