本文整理汇总了TypeScript中line-by-line.on函数的典型用法代码示例。如果您正苦于以下问题:TypeScript on函数的具体用法?TypeScript on怎么用?TypeScript on使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了on函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: resolve
return new Promise<Map<number, vscode.Position>>((resolve, reject) => {
var lineNumbersIndexedByPosition = new Map<number, vscode.Position>();
//Find the largest (last position we have to check)
var largestPosition = 0;
positions.forEach(pos => largestPosition = pos > largestPosition ? pos : largestPosition);
//If the file is open use the editor to get the line number
var docs = vscode.workspace.textDocuments.filter(doc => doc.uri.fsPath.toUpperCase() === filePath.toUpperCase());
if (docs.length > 0) {
var doc = docs[0];
positions.forEach(pos => {
lineNumbersIndexedByPosition.set(pos, doc.positionAt(pos));
});
return resolve(lineNumbersIndexedByPosition);
}
//Open the file and get the line numbers untill the last position required
var lr = new LineByLineReader(filePath);
var lineNumber = 0;
var lengthOfEachLine = new Map<number, number>();
var totalCharactersRead = 0;
lr.on('error', reject);
lr.on('line', (line: string) => {
//Two characters for the charriage return and line feeds
lengthOfEachLine.set(lineNumber, line.length + 2);
var totalCharactersUntillPreviousLine = totalCharactersRead;
totalCharactersRead += line.length + 2;
positions.forEach(pos => {
if (totalCharactersRead === pos) {
lineNumbersIndexedByPosition.set(pos, new vscode.Position(lineNumber, line.length));
return;
}
if (totalCharactersRead > pos && pos > totalCharactersUntillPreviousLine) {
lineNumbersIndexedByPosition.set(pos, new vscode.Position(lineNumber, pos - totalCharactersUntillPreviousLine));
}
});
//If we have read untill the position required, then bugger off from here
if (totalCharactersRead >= largestPosition) {
lr.close();
lineNumber++;
}
});
lr.on('end', function () {
resolve(lineNumbersIndexedByPosition);
});
});
示例2: LineByLineReader
import * as LineByLineReader from "line-by-line";
let reader: LineByLineReader = new LineByLineReader("index.d.ts");
reader.on("line", (line: any) => {
console.log(line);
});
reader.on("end", () => {
console.log("end");
});
reader.on("error", (err: any) => {
throw("Error reading file");
});
示例3: start
start(jsonFilename: string, collection: Collection, callback: any ) {
let fn = jsonFilename.substr(jsonFilename.lastIndexOf('\\')+1);
let source = fn.split('-')[0]; // shopname
let defaults = require("./"+source+"-defaults.json");
let defaultCat = defaults.category;
let defaultShipCost:number = defaults.shipCost;
console.log("processing:",fn,', shopname:', source,', defaults:', defaults);
let report:RefreshReport = new RefreshReport((new Date()).getTime(),[],source,"portia");
var LineByLineReader = require('line-by-line'),
lr = new LineByLineReader(jsonFilename);
lr.on('error', function (err:any) {
// 'err' contains error object
console.log("error:", err);
callback(report);
});
lr.on('line', function (line:string) {
// 'line' contains the current line without the trailing newline character.
let item = JSON.parse(line);
report.total++;
if (item.price === undefined) {
report.errors++;
report.errList[item.name] = 'no price';
process.stdout.write("x");
return;
}
process.stdout.write(".");
let cItem:MsCartItem = new MsCartItem(undefined,item.name[0],getFloat(item.price,undefined));
cItem.crawler = report.crawler;
cItem.shopname = source;
cItem.category = item.category[0] || defaultCat;
if (cItem.category && report.categories.indexOf(cItem.category) === -1) report.categories.push(cItem.category);
cItem.currency = 'EUR';
cItem.description = buildDesc(item);
let oprice:number = getFloat(item.oldPrice,undefined);
cItem.discount = (oprice)?+((oprice - cItem.price)/oprice).toFixed(2):undefined;
cItem.imgUrl = item.imgUrl[0];
cItem.link = (item.buyLink)? item.buyLink[0] : item.url;
cItem.BOPIS = (item.bopisLink)? item.bopisLink[0] : null;
cItem.outOfStock = false;
cItem.qty = 1;
cItem.sku = item.sku || getSku(cItem.link);
cItem.secondhand = false;
cItem.shippedPrice = getFloat(item.shippedPrice,' ');
cItem.shipCost = (cItem.shippedPrice>0)?cItem.shippedPrice-cItem.price:defaultShipCost;
cItem.refresh = report.mills;
if((report.skus[cItem.sku]===undefined)){
report.skus[cItem.sku] = 1;
collection.findOneAndReplace({"sku":cItem.sku,"shopname":cItem.shopname},cItem, { upsert : true }, function(err:any,result:any){
if (!err){
report.updates += (result.lastErrorObject.updatedExisting)?1:0;
report.inserts += (result.lastErrorObject.updatedExisting)?0:1;
}else
report.errors++;
});
}else{
report.duplicates++;
report.skus[cItem.sku]++;
}
});
lr.on('end', function () {
// All lines are read, file is closed now.
console.log('stream ended. removing old...');
report.categories.push("null");
collection.remove({"shopname":report.shopname, "category": { $in: report.categories }, "refresh":{"$lt":report.mills}},function(err:any,res:any){
if (!err) report.deletes = res.result.n;
else report.errors++;
});
callback(report);
});
}
示例4: populateObjects
export function populateObjects(file: string) : Q.Promise<{objSet: {[site:string]: Array<types.ObjectRecord>}; maxTime: number}> {
var objectSet : {[site:string]: Array<types.ObjectRecord>} = {}, maxTime = 0;
var recordArr: Array<types.ObjectRecord> = [];
var lr = new LineByLineReader(file);
var deferred = Q.defer<{objSet: {[site:string]: Array<types.ObjectRecord>}; maxTime: number}>();
lr.on('line', (line:string) => {
var data = JSON.parse(line);
//[objectId,type,allocSite,allocTime,allocCallStack,lastUseTime,lastUseSite,unreachableTime,unreachableSite]
var objectId = parseInt(data[0]),
type = data[1],
allocSite = data[2],
allocTime = parseInt(data[3]),
allocCallStack = data[4],
lastUseTime = parseInt(data[5]),
lastUseSite = data[6],
unreachableTime = parseInt(data[7]),
unreachableSite = data[8];
if (recordArr[objectId]) {
// this is an update record. if we have a new lastUseTime, update it.
// definitely update the unreachability time
var oldRecord = recordArr[objectId];
if (lastUseTime !== 0) {
oldRecord.lastUseTime = lastUseTime;
}
oldRecord.unreachableTime = unreachableTime;
oldRecord.staleness = oldRecord.lastUseTime === 0 ? unreachableTime - oldRecord.creationTime
: unreachableTime - oldRecord.lastUseTime;
} else {
// first record for this object
var staleness = lastUseTime === 0 ? unreachableTime - allocTime : unreachableTime - lastUseTime;
var or = new types.ObjectRecord(
objectId, type, allocSite, allocTime, unreachableTime, 1, staleness, lastUseTime, [], allocCallStack);
recordArr[objectId] = or;
var objs = objectSet[allocSite];
if (!objs) {
objectSet[allocSite] = objs = [];
}
objs.push(or);
}
if (unreachableTime > maxTime) {
maxTime = unreachableTime;
}
});
lr.on('error', (err: any) => {
deferred.reject(err);
});
lr.on('end', () => {
deferred.resolve({
objSet: objectSet,
maxTime: maxTime
});
});
return deferred.promise;
//for (var e in jsonObject["objectInfo"]) {
// //console.log(e);
// var ks = jsonObject["objectInfo"][e];
// objectSet[e] = [];
// for (var k in ks) {
// var or : types.ObjectRecord = new types.ObjectRecord(
// e.toString(),
// ks[k].type,
// ks[k].creationTime,
// ks[k].unreachableTime,
// ks[k].shallowSize,
// /* ks[k].freeableSize, */
// ks[k].staleness,
// ks[k].unreachableTime - ks[k].staleness,
// /* ks[k].leastChildStaleness, */
// [],
// ks[k].creationCallStack
// )
// objectSet[e].push(or);
// }
//}
//
//return objectSet;
};
示例5: LineByLineReader
return new Promise<ITryStatement[]>(resolve=> {
var lr = new LineByLineReader(pythonFile);
var lineNumber = 0;
var tryStatements: ITryStatementEx[] = [];
var tryColumnBlocks = new Map<number, ITryStatementEx>();
lr.on('error', function(err) {
resolve(tryStatements);
});
lr.on('line', function(line) {
lineNumber++;
//Valid parts of a try block include
//try:, except <error>:, except: else: finally:
//Anything other than this in the same column indicates a termination of the try block
var trimmedLine = line.trim();
var matches = line.match(/^\s*try(\s*):/);
if (matches !== null && matches.length > 0) {
let column = line.indexOf("try");
if (column === -1) {
return;
}
//If the new try starts at the same column
//Then the previous try block has ended
if (tryColumnBlocks.has(column)) {
var tryBlockClosed = tryColumnBlocks.get(column);
tryColumnBlocks.delete(column);
tryStatements.push(tryBlockClosed);
}
var tryStatement: ITryStatementEx = {
Column: column,
EndLineNumber: 0,
Exceptions: [],
StartLineNumber: lineNumber
};
tryColumnBlocks.set(column, tryStatement);
return;
}
//look for excepts
matches = line.match(/^\s*except/);
if (matches !== null && matches.length > 0 &&
(trimmedLine.startsWith("except ") || trimmedLine.startsWith("except:"))) {
//Oops something has gone wrong
if (tryColumnBlocks.size === 0) {
resolve(tryStatements);
lr.close();
return;
}
let column = line.indexOf("except");
//Do we have a try block for this same column
if (!tryColumnBlocks.has(column)) {
return;
}
let currentTryBlock = tryColumnBlocks.get(column);
let exceptions = extractExceptions(line);
currentTryBlock.Exceptions = currentTryBlock.Exceptions.concat(exceptions);
if (currentTryBlock.EndLineNumber === 0) {
currentTryBlock.EndLineNumber = lineNumber;
}
return;
}
//look for else
matches = line.match(/^\s*else(\s*):/);
if (matches !== null && matches.length > 0 &&
(trimmedLine.startsWith("else ") || trimmedLine.startsWith("else:"))) {
//This is possibly an if else...
if (tryColumnBlocks.size === 0) {
return;
}
let column = line.indexOf("else");
//Check if we have a try associated with this column
//If not found, this is probably an if else block or something else
if (!tryColumnBlocks.has(column)) {
return;
}
//Else marks the end of the try block (of course there could be a finally too)
let currentTryBlock = tryColumnBlocks.get(column);
if (currentTryBlock.EndLineNumber === 0) {
currentTryBlock.EndLineNumber = lineNumber;
}
tryColumnBlocks.delete(column);
tryStatements.push(currentTryBlock);
return;
}
//look for finally
matches = line.match(/^\s*finally(\s*):/);
if (matches !== null && matches.length > 0 &&
//.........这里部分代码省略.........