本文整理汇总了TypeScript中text-encoding-utf-8.TextEncoder.encode方法的典型用法代码示例。如果您正苦于以下问题:TypeScript TextEncoder.encode方法的具体用法?TypeScript TextEncoder.encode怎么用?TypeScript TextEncoder.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类text-encoding-utf-8.TextEncoder
的用法示例。
在下文中一共展示了TextEncoder.encode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: describe
describe(`Utf8Vector`, () => {
const values = ['foo', 'bar', 'baz', 'foo bar', 'bar'], n = values.length;
let offset = 0;
const offsets = Uint32Array.of(0, ...values.map((d) => { offset += d.length; return offset; }));
const vector = new Utf8Vector(new FlatListData(new Utf8(), n, null, offsets, utf8Encoder.encode(values.join(''))));
basicVectorTests(vector, values, ['abc', '123']);
describe(`sliced`, () => {
basicVectorTests(vector.slice(1,3), values.slice(1,3), ['foo', 'abc']);
});
});
示例2: test_encoder
function test_encoder() {
const text = "plain text";
let uint8array: Uint8Array;
// constructor
uint8array = new TextEncoder().encode(text);
uint8array = new TextEncoder('utf-8').encode(text);
uint8array = TextEncoder().encode(text);
uint8array = TextEncoder('utf-8').encode(text);
// attributes
const encoder = new TextEncoder();
encoder.encoding = 'utf-8';
const encoding: string = encoder.encoding;
// methods
encoder.encode();
encoder.encode(text);
}
示例3: Uint8Array
protected readData<T extends DataType>(type: T, { offset }: BufferMetadata = this.getBufferMetadata()) {
const { sources } = this;
if (DataType.isTimestamp(type) === true) {
return new Uint8Array(int64DataFromJSON(sources[offset] as string[]));
} else if ((DataType.isInt(type) || DataType.isTime(type)) && type.bitWidth === 64) {
return new Uint8Array(int64DataFromJSON(sources[offset] as string[]));
} else if (DataType.isDate(type) && type.unit === DateUnit.MILLISECOND) {
return new Uint8Array(int64DataFromJSON(sources[offset] as string[]));
} else if (DataType.isDecimal(type) === true) {
return new Uint8Array(decimalDataFromJSON(sources[offset] as string[]));
} else if (DataType.isBinary(type) === true || DataType.isFixedSizeBinary(type) === true) {
return new Uint8Array(binaryDataFromJSON(sources[offset] as string[]));
} else if (DataType.isBool(type) === true) {
return new Uint8Array(packBools(sources[offset] as number[]).buffer);
} else if (DataType.isUtf8(type) === true) {
return utf8Encoder.encode((sources[offset] as string[]).join(''));
} else {
return toTypedArray(type.ArrayType, sources[offset].map((x) => +x)) as any;
}
}
示例4: int64sFromJSON
function createDataArray<T extends TypedArray>(sources: any[][], field: Field, _fieldNode: FieldNode, buffer: Buffer, ArrayConstructor: TypedArrayConstructor<T>): T {
let type = field.type, data: ArrayLike<number> | ArrayBufferLike;
if (type.isTimestamp() === true) {
data = int64sFromJSON(sources[buffer.offset.low] as string[]);
} else if ((type.isInt() || type.isTime()) && type.bitWidth === 64) {
data = int64sFromJSON(sources[buffer.offset.low] as string[]);
} else if (type.isDate() && type.unit === DateUnit.MILLISECOND) {
data = int64sFromJSON(sources[buffer.offset.low] as string[]);
} else if (type.isDecimal() === true) {
data = decimalFromJSON(sources[buffer.offset.low] as string[]);
} else if (type.isBinary() === true) {
data = binaryFromJSON(sources[buffer.offset.low] as string[]);
} else if (type.isBool() === true) {
data = booleanFromJSON(sources[buffer.offset.low] as number[]).buffer;
} else if (type.isUtf8() === true) {
data = encoder.encode((sources[buffer.offset.low] as string[]).join(''));
} else {
data = (sources[buffer.offset.low]).map((x) => +x);
}
return new ArrayConstructor(data);
}