-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partially support TextEncoder Web API (#5863)
- Loading branch information
1 parent
98091b5
commit cb684d5
Showing
2 changed files
with
95 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
|
||
/** | ||
* The `textcodec` module provides access to TextEncoder Web API class. | ||
* | ||
* Example: | ||
* ``` | ||
* import * as ccftextcodec from '@microsoft/ccf-app/textcodec.js'; | ||
* | ||
* const bytes = new ccftextcodec.TextEncoder().encode("foo") | ||
* ``` | ||
* | ||
* If you need TextEncoder Web API as a globally accessible class: | ||
* ``` | ||
* import * as ccftextcodec from '@microsoft/ccf-app/textcodec.js'; | ||
* | ||
* if (globalThis != undefined && (globalThis as any).TextEncoder == undefined) { | ||
* (globalThis as any).TextEncoder = ccftextcodec.TextEncoder; | ||
* } | ||
* | ||
* ``` | ||
* | ||
* @module | ||
*/ | ||
|
||
import { ccf } from "./global.js"; | ||
|
||
export type TextEncoderEncodeIntoResult = { | ||
read?: number; | ||
written?: number; | ||
}; | ||
|
||
/** | ||
* TextEncoder can be used to encode string to Uint8Array. | ||
*/ | ||
export class TextEncoder { | ||
/** | ||
* Always returns "utf-8". | ||
*/ | ||
public readonly encoding: string = "utf-8"; | ||
|
||
/** | ||
* Returns Uint8Array containing UTF-8 encoded text. | ||
* @param input Input string to encode. | ||
* @returns Encoded bytes. | ||
*/ | ||
encode(input: string): Uint8Array { | ||
return new Uint8Array(ccf.strToBuf(input)); | ||
} | ||
|
||
/** | ||
* Not implemented. | ||
* @param input | ||
* @param output | ||
* @throws Always throws an Error object. | ||
*/ | ||
encodeInto(input: string, output: Uint8Array): TextEncoderEncodeIntoResult { | ||
throw new Error("Not implemented"); | ||
} | ||
} |
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