本文整理匯總了TypeScript中ix.Iterable類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Iterable類的具體用法?TypeScript Iterable怎麽用?TypeScript Iterable使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Iterable類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: addJsImports
/**
* Injects JS imports at the top of the program based on html imports and
* the imports in this.module.importedReferences.
*/
private addJsImports(
program: Program,
importedReferences:
ReadonlyMap<ConvertedDocumentUrl, ReadonlySet<ImportReference>>):
boolean {
// Collect Identifier nodes within trees that will be completely replaced
// with an import reference.
const ignoredIdentifiers: Set<Identifier> = new Set();
for (const referenceSet of importedReferences.values()) {
for (const reference of referenceSet) {
astTypes.visit(reference.path.node, {
visitIdentifier(path: NodePath<Identifier>): (boolean | void) {
ignoredIdentifiers.add(path.node);
this.traverse(path);
},
});
}
}
const usedIdentifiers = collectIdentifierNames(program, ignoredIdentifiers);
const jsExplicitImports = new Set<string>();
// Rewrite HTML Imports to JS imports
const jsImportDeclarations = [];
for (const htmlImport of this.getHtmlImports()) {
const importedJsDocumentUrl = this.convertDocumentUrl(
this.urlHandler.getDocumentUrl(htmlImport.document));
const references = importedReferences.get(importedJsDocumentUrl);
const namedExports =
new Set(IterableX.from(references || []).map((ref) => ref.target));
const jsFormattedImportUrl =
this.formatImportUrl(importedJsDocumentUrl, htmlImport.originalUrl);
jsImportDeclarations.push(...getImportDeclarations(
jsFormattedImportUrl, namedExports, references, usedIdentifiers));
jsExplicitImports.add(importedJsDocumentUrl);
}
// Add JS imports for any additional, implicit HTML imports
for (const jsImplicitImportUrl of importedReferences.keys()) {
if (jsExplicitImports.has(jsImplicitImportUrl)) {
continue;
}
const references = importedReferences.get(jsImplicitImportUrl);
const namedExports =
new Set(IterableX.from(references || []).map((ref) => ref.target));
const jsFormattedImportUrl = this.formatImportUrl(jsImplicitImportUrl);
jsImportDeclarations.push(...getImportDeclarations(
jsFormattedImportUrl, namedExports, references, usedIdentifiers));
}
// Prepend JS imports into the program body
program.body.splice(0, 0, ...jsImportDeclarations);
// Return true if any imports were added, false otherwise
return jsImportDeclarations.length > 0;
}
示例2: writeFileResults
export async function writeFileResults(
outDir: string, files: Map<ConvertedDocumentFilePath, string|undefined>) {
return Promise.all(IterableX.from(files).map(async ([newPath, newSource]) => {
const filePath = path.join(outDir, newPath);
await mkdirp(path.dirname(filePath));
if (newSource !== undefined) {
await fse.writeFile(filePath, newSource);
} else if (await fse.pathExists(filePath)) {
await fse.unlink(filePath);
}
}));
}
示例3: getNamespaceNames
/**
* Get all namespace names for an analysis object.
*/
function getNamespaceNames(analysis: Analysis) {
return IterableX
.from(analysis.getFeatures(
{kind: 'namespace', externalPackages: true, imported: true}))
.map((n) => {
const name = n.name;
if (name.startsWith('window.')) {
return name.slice('window.'.length);
}
return name;
});
}
示例4: rewriteReferencesToLocalExports
export function rewriteReferencesToLocalExports(
program: estree.Program,
exportMigrationRecords: Iterable<NamespaceMemberToExport>) {
const rewriteMap = new Map<string|undefined, string>(
IterableX.from(exportMigrationRecords)
.filter((m) => m.es6ExportName !== '*')
.map(
(m) => [m.oldNamespacedName,
m.es6ExportName] as [string, string]));
astTypes.visit(program, {
visitMemberExpression(path: NodePath<estree.MemberExpression>) {
const memberName = getMemberName(path.node);
const newLocalName = rewriteMap.get(memberName);
if (newLocalName) {
path.replace(jsc.identifier(newLocalName));
return false;
}
this.traverse(path);
return;
}
});
}
示例5: getCommentsBetween
export function getCommentsBetween(
document: parse5.ASTNode,
from: parse5.ASTNode|undefined,
until: parse5.ASTNode|undefined): string[] {
const nodesStart =
from === undefined ? nodesInside(document) : nodesAfter(from);
const nodesBetween =
IterableX.from(nodesStart).takeWhile((node) => node !== until);
const commentNodesBetween =
nodesBetween.filter((node) => dom5.isCommentNode(node));
const commentStringsBetween =
commentNodesBetween.map((node) => dom5.getTextContent(node));
const formattedCommentStringsBetween =
commentStringsBetween.map((commentText) => {
// If it looks like there might be jsdoc in the comment, start the
// comment with an extra * so that the js comment looks like a jsdoc
// comment.
if (/@\w+/.test(commentText)) {
return '*' + commentText;
}
return commentText;
});
return Array.from(formattedCommentStringsBetween);
}