本文整理汇总了TypeScript中text-encoding.TextEncoder.encode方法的典型用法代码示例。如果您正苦于以下问题:TypeScript TextEncoder.encode方法的具体用法?TypeScript TextEncoder.encode怎么用?TypeScript TextEncoder.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类text-encoding.TextEncoder
的用法示例。
在下文中一共展示了TextEncoder.encode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test_encoder
function test_encoder() {
var text = "plain text";
var uint8array = new Uint8Array([]);
// constructor
uint8array = new TextEncoder().encode(text);
uint8array = new TextEncoder("utf-8").encode(text);
uint8array = new TextEncoder("windows-1252", {
NONSTANDARD_allowLegacyEncoding: true
}).encode(text);
uint8array = TextEncoder().encode(text);
uint8array = TextEncoder("utf-8").encode(text);
uint8array = TextEncoder("windows-1252", {
NONSTANDARD_allowLegacyEncoding: true
}).encode(text);
// attributes
var encoder = new TextEncoder();
var encoding: string = encoder.encoding;
// methods
encoder.encode();
encoder.encode(text);
encoder.encode(text, { stream: true });
}
示例2: fn
false, ['encrypt','decrypt']).then((dekKey: CryptoKey) => {
let dat: Uint8Array;
if (encrypt) {
// Encode the message string as a byte array
dat = textEncoder.encode(em.message);
// Generate a random IV for seeding AES-CBC
em.iv = new Uint8Array(16);
crypto.getRandomValues(em.iv);
// Encode the IV to be persisted later
em.ivB64 = b64.fromByteArray(em.iv);
}
else {
// Decode the IV
em.iv = b64.toByteArray(em.ivB64);
// Decode the encrypted message
em.messageEnc = b64.toByteArray(em.messageEncB64);
dat = em.messageEnc;
}
// Setup the encryptiong/decryption parameters
let encryptAlgor = {
name: 'AES-CBC',
iv: em.iv,
};
// Either encrypt or decrypt
if (encrypt) {
crypto.subtle.encrypt(encryptAlgor, dekKey, dat).then((crypt: ArrayBuffer) => {
em.messageEnc = new Uint8Array(crypt);
em.messageEncB64 = b64.fromByteArray(em.messageEnc);
if (fn != null) {
fn();
}
})
}
else {
crypto.subtle.decrypt(encryptAlgor, dekKey, dat).then((clear: ArrayBuffer) => {
em.message = textDecoder.decode(new Uint8Array(clear));
if (fn != null) {
fn();
}
})
}
});
示例3: encode
export function encode(text : string){
return encoder.encode(text);
}