util#TextDecoder TypeScript Examples
The following examples show how to use
util#TextDecoder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: serializer.ts From vscode-sql-notebook with MIT License | 6 votes |
async deserializeNotebook(
context: Uint8Array,
_token: vscode.CancellationToken
): Promise<vscode.NotebookData> {
const str = new TextDecoder().decode(context);
const blocks = splitSqlBlocks(str);
const cells = blocks.map((query) => {
const isMarkdown = query.startsWith('/*markdown') && query.endsWith('*/');
if (isMarkdown) {
const lines = query.split('\n');
const innerMarkdown =
lines.length > 2 ? lines.slice(1, lines.length - 1).join('\n') : '';
return new vscode.NotebookCellData(
vscode.NotebookCellKind.Markup,
innerMarkdown,
'markdown'
);
}
return new vscode.NotebookCellData(
vscode.NotebookCellKind.Code,
query,
'sql'
);
});
return new vscode.NotebookData(cells);
}
Example #2
Source File: EosBlockchainModel.ts From aloxide with Apache License 2.0 | 6 votes |
constructor(
name: string,
url: string,
actions: BlockchainAction[],
contract: string,
account?: BlockchainAccount,
) {
super(name, url, actions, contract, account);
let keys = [];
if (this.account) {
keys = [this.account.privateKey];
}
const signatureProvider = new JsSignatureProvider(keys);
const rpc = new JsonRpc(url, { fetch });
this.client = new Api({
rpc,
signatureProvider,
textEncoder: new TextEncoder(),
textDecoder: new TextDecoder(),
});
}
Example #3
Source File: EosBlockchainService.ts From aloxide with Apache License 2.0 | 6 votes |
constructor(config: NetworkConfig) {
super(config);
const signatureProvider = new JsSignatureProvider([]);
const rpc = new JsonRpc(this.url(), { fetch });
this.client = new Api({
rpc,
signatureProvider,
textEncoder: new TextEncoder(),
textDecoder: new TextDecoder(),
});
}
Example #4
Source File: aws-message-utils.ts From aws-transcribe with MIT License | 6 votes |
decoder = new TextDecoder("utf-8")
Example #5
Source File: extension.ts From markdown-links with MIT License | 6 votes |
async function getWebviewContent(
context: vscode.ExtensionContext,
panel: vscode.WebviewPanel
) {
const webviewPath = vscode.Uri.file(
path.join(context.extensionPath, "static", "webview.html")
);
const file = await vscode.workspace.fs.readFile(webviewPath);
const text = new TextDecoder("utf-8").decode(file);
const webviewUri = (fileName: string) =>
panel.webview
.asWebviewUri(
vscode.Uri.file(path.join(context.extensionPath, "static", fileName))
)
.toString();
const graphDirectory = path.join("graphs", getConfiguration("graphType"));
const textWithVariables = text
.replace(
"${graphPath}",
"{{" + path.join(graphDirectory, "graph.js") + "}}"
)
.replace(
"${graphStylesPath}",
"{{" + path.join(graphDirectory, "graph.css") + "}}"
);
// Basic templating. Will replace {{someScript.js}} with the
// appropriate webview URI.
const filled = textWithVariables.replace(/\{\{.*\}\}/g, (match) => {
const fileName = match.slice(2, -2).trim();
return webviewUri(fileName);
});
return filled;
}
Example #6
Source File: decode-encode.ts From rtcm with GNU General Public License v3.0 | 5 votes |
static readonly utf8Decoder = Coder.define(new TextDecoder('utf8'));
Example #7
Source File: decode-encode.ts From rtcm with GNU General Public License v3.0 | 5 votes |
static readonly latin1Decoder = Coder.define(new TextDecoder('latin1'));
Example #8
Source File: setupJest.ts From erda-ui with GNU Affero General Public License v3.0 | 5 votes |
customGlobal.TextDecoder = TextDecoder;
Example #9
Source File: DataInput.ts From lavaclient with Apache License 2.0 | 5 votes |
readUTF(): string {
const length = this.readUnsignedShort()
, start = this.advance(length);
return new TextDecoder().decode(this.buf.slice(start, start + length));
}
Example #10
Source File: dummy.ts From aloxide with Apache License 2.0 | 5 votes |
api = new Api({
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder(),
})
Example #11
Source File: cache.mixin.acceptance.ts From loopback4-microservice-catalog with MIT License | 5 votes |
decoder = new TextDecoder('utf-8')
Example #12
Source File: cache.mixin.ts From loopback4-microservice-catalog with MIT License | 5 votes |
decoder = new TextDecoder('utf-8')
Example #13
Source File: ratelimit-datasource.provider.ts From loopback4-ratelimiter with MIT License | 5 votes |
decoder = new TextDecoder('utf-8')
Example #14
Source File: parsing.ts From markdown-links with MIT License | 5 votes |
parseFile = async (graph: Graph, filePath: string) => {
filePath = path.normalize(filePath);
const buffer = await vscode.workspace.fs.readFile(vscode.Uri.file(filePath));
const content = new TextDecoder("utf-8").decode(buffer);
const ast: MarkdownNode = parser.parse(content);
let title: string | null = findTitle(ast);
const index = graph.nodes.findIndex((node) => node.path === filePath);
if (!title) {
if (index !== -1) {
graph.nodes.splice(index, 1);
}
return;
}
if (index !== -1) {
graph.nodes[index].label = title;
} else {
graph.nodes.push({ id: id(filePath), path: filePath, label: title });
}
// Remove edges based on an old version of this file.
graph.edges = graph.edges.filter((edge) => edge.source !== id(filePath));
// Returns a list of decoded links (by default markdown only supports encoded URI)
const links = findLinks(ast).map(uri => decodeURI(uri));
const parentDirectory = filePath.split(path.sep).slice(0, -1).join(path.sep);
for (const link of links) {
let target = path.normalize(link);
if (!path.isAbsolute(link)) {
target = path.normalize(`${parentDirectory}/${link}`);
}
graph.edges.push({ source: id(filePath), target: id(target) });
}
}
Example #15
Source File: parsing.ts From markdown-links with MIT License | 5 votes |
findFileId = async (filePath: string): Promise<string | null> => {
const buffer = await vscode.workspace.fs.readFile(vscode.Uri.file(filePath));
const content = new TextDecoder("utf-8").decode(buffer);
const match = content.match(FILE_ID_REGEXP);
return match ? match[1] : null;
}
Example #16
Source File: contract-files-reader.test.ts From aloxide with Apache License 2.0 | 4 votes |
describe('test contract-files-reader', () => {
describe('readABIFile()', () => {
it('should throw error when missing file path', () => {
expect(() => {
// @ts-ignore
readABIFile(undefined);
}).toThrowError('File path is required.');
});
it('should throw error when missing API instance', () => {
expect(() => {
// @ts-ignore
readABIFile('some/path');
}).toThrowError('Missing EOS API instance');
});
it('should read file and return serialized hex content', () => {
const api = new Api({
rpc: new JsonRpc('some_url', { fetch }),
signatureProvider: new JsSignatureProvider([]),
textEncoder: new TextEncoder(),
textDecoder: new TextDecoder(),
});
const testFileBuffer = Buffer.from('{ "test": "test content" }', 'utf-8');
const readFileSyncMock = jest
.spyOn(fs, 'readFileSync')
.mockReturnValueOnce(testFileBuffer.toString('utf-8'));
const res = readABIFile('some/path', api);
expect(typeof res).toBe('string');
});
});
describe('readWASMFile()', () => {
it('should throw error when missing file path', () => {
expect(() => {
// @ts-ignore
readWASMFile(undefined);
}).toThrowError('File path is required.');
});
it('should throw Not Found error when reading from not-existing file', () => {
const path = 'not/existing/file/path';
expect(() => {
readWASMFile(path);
}).toThrowError('File not found!');
});
it('should throw error when reading failed', () => {
const path = 'not/existing/file/path';
const readFileSyncMock = jest.spyOn(fs, 'readFileSync').mockImplementationOnce(() => {
throw new Error('Test Error');
});
expect(() => {
readWASMFile(path);
}).toThrowError('Test Error');
expect(readFileSyncMock).toBeCalledWith(path);
});
it('should read file and convert to hex string', () => {
const testFileBuffer = Buffer.from('test content', 'utf-8');
const readFileSyncMock = jest.spyOn(fs, 'readFileSync').mockReturnValueOnce(testFileBuffer);
const wasmPath = 'some/wasm/path';
const result = readWASMFile(wasmPath);
expect(result).toBe(testFileBuffer.toString('hex'));
expect(readFileSyncMock).toBeCalledWith(wasmPath);
});
});
describe('readPSFile()', () => {
it('should throw error when missing file path', () => {
expect(() => {
// @ts-ignore
readPSFile(undefined);
}).toThrowError('File path is required.');
});
it('should throw Not Found error when reading from not-existing file', () => {
const path = 'not/existing/file/path';
expect(() => {
readPSFile(path);
}).toThrowError('File not found!');
});
it('should throw error when reading failed', () => {
const path = 'not/existing/file/path';
const readFileSyncMock = jest.spyOn(fs, 'readFileSync').mockImplementationOnce(() => {
throw new Error('Test Error');
});
expect(() => {
readPSFile(path);
}).toThrowError('Test Error');
expect(readFileSyncMock).toBeCalledWith(path);
});
it('should read file and convert to hex string', () => {
const testFileBuffer = Buffer.from('test content', 'utf-8');
const readFileSyncMock = jest.spyOn(fs, 'readFileSync').mockReturnValueOnce(testFileBuffer);
const psPath = 'some/ps/path';
const result = readPSFile(psPath);
expect(result).toBe(testFileBuffer.toString('hex'));
expect(readFileSyncMock).toBeCalledWith(psPath);
});
});
});