GitHub PullRequestReviewCommentEvent at 2014-02-09 17:07:25
Repository: linez
Actor: jedmao
Payload: {"comment":{"url":"https://api.github.com/repos/jedmao/linez/pulls/comments/9569528","id":9569528,"diff_hunk":"@@ -1,75 +1,53 @@\n ﻿///<reference path='../bower_components/dt-node/node.d.ts'/>\n-import fs = require('fs');\n import ILine = require('./ILine');\n import _events = require('./events');\n-import events = require('events');\n-import BOM = require('./BOM');\n-import stream = require('stream');\n+import INewlineFinder = require('./INewlineFinder');\n \n \n class LineEmitter extends _events.EventEmitter {\n \n-\tprivate rawLinesPattern = /([^\\r\\n]*)(\\r?\\n)?/g;\n-\tprivate savedLine: ILine;\n+\tprivate newlineFinder: INewlineFinder;\n+\tprivate buffer = '';\n \tprivate lineNumber = 0;\n+\tprivate offset = 0;\n \n-\tconstructor(streamOrString: any, newlines?: string[]) {\n+\tconstructor(newlineFinder: INewlineFinder) {\n \t\tsuper();\n-\t\tthis.rawLinesPattern = this.createRawLinesPattern(newlines) || this.rawLinesPattern;\n-\t\tif (typeof streamOrString === 'string') {\n-\t\t\tsetTimeout(() => {\n-\t\t\t\tthis.onData(streamOrString);\n-\t\t\t\tthis.onEnd();\n-\t\t\t});\n-\t\t} else if (streamOrString instanceof events.EventEmitter) {\n-\t\t\tstreamOrString.on('data', this.onData.bind(this));\n-\t\t\tstreamOrString.on('end', this.onEnd.bind(this));\n-\t\t} else {\n-\t\t\tthis.emit('error', new Error('LineEmitter requires string or stream as input'));\n-\t\t}\n+\t\tthis.newlineFinder = newlineFinder;\n \t}\n \n-\tprivate createRawLinesPattern(newlines: string[]) {\n-\t\tif (!newlines) {\n-\t\t\treturn;\n-\t\t}\n-\t\tnewlines = newlines.map((sequence: string) => {\n-\t\t\treturn '\\\\' + sequence.split('').join('\\\\');\n+\tpublic pushLines(lines: string) {\n+\t\tthis.buffer += lines;\n+\t\tvar offset = 0;\n+\t\tthis.newlineFinder.find(this.buffer).forEach((newline) => {\n+\t\t\tvar text = this.buffer.substring(offset, newline.index);\n+\t\t\tthis.emitLine(this.createLine(text, newline.text));\n+\t\t\toffset += text.length + newline.text.length;\n \t\t});\n-\t\tvar nl = '(?:' + newlines.join('|') + ')';\n-\t\treturn new RegExp('([^' + nl + ']*)(' + nl + ')?', 'g');\n+\t\tthis.buffer = this.buffer.substr(offset);\n \t}\n \n-\tprivate onData(chunk: any) {\n-\t\tchunk.toString().replace(this.rawLinesPattern, (match, text, newline, offset) => {\n-\t\t\tvar line: ILine = {\n-\t\t\t\tnumber: ++this.lineNumber,\n-\t\t\t\ttext: text,\n-\t\t\t\toffset: offset\n-\t\t\t};\n-\t\t\tif (newline) {\n-\t\t\t\tline.newline = newline;\n-\t\t\t}\n-\t\t\tthis.onLine(line);\n-\t\t});\n+\tprivate createLine(text: string, newline?: string) {\n+\t\tvar line: ILine = {\n+\t\t\tnumber: ++this.lineNumber,\n+\t\t\ttext: text,\n+\t\t\toffset: this.offset\n+\t\t};\n+\t\tif (newline) {\n+\t\t\tline.newline = newline;\n+\t\t}\n+\t\tthis.offset += text.length + (newline || '').length;","path":"lib/LineEmitter.ts","position":78,"original_position":78,"commit_id":"26d4ba1dec50da65f83c1facd3a5e6d400f9be9f","original_commit_id":"26d4ba1dec50da65f83c1facd3a5e6d400f9be9f","user":{"login":"jedmao","id":1058243,"avatar_url":"https://gravatar.com/avatar/92050607e71058000ac917704063232d?d=https%3A%2F%2Fidenticons.github.com%2F70ee11ca36a16f999b4fb81b1120f262.png&r=x","gravatar_id":"92050607e71058000ac917704063232d","url":"https://api.github.com/users/jedmao","html_url":"https://github.com/jedmao","followers_url":"https://api.github.com/users/jedmao/followers","following_url":"https://api.github.com/users/jedmao/following{/other_user}","gists_url":"https://api.github.com/users/jedmao/gists{/gist_id}","starred_url":"https://api.github.com/users/jedmao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jedmao/subscriptions","organizations_url":"https://api.github.com/users/jedmao/orgs","repos_url":"https://api.github.com/users/jedmao/repos","events_url":"https://api.github.com/users/jedmao/events{/privacy}","received_events_url":"https://api.github.com/users/jedmao/received_events","type":"User","site_admin":false},"body":"It looks like you're performing this calculation twice, as on line 25. Also, why is createLine modifying the instance offset? SRP?","created_at":"2014-02-09T17:07:25Z","updated_at":"2014-02-09T17:07:25Z","html_url":"https://github.com/jedmao/linez/pull/4#discussion_r9569528","pull_request_url":"https://api.github.com/repos/jedmao/linez/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/jedmao/linez/pulls/comments/9569528"},"html":{"href":"https://github.com/jedmao/linez/pull/4#discussion_r9569528"},"pull_request":{"href":"https://api.github.com/repos/jedmao/linez/pulls/4"}}}}