From 6760e9fb9f59f297c8385c85523850dc46abb1cd Mon Sep 17 00:00:00 2001 From: io-monad Date: Sat, 26 Mar 2016 10:50:29 +0900 Subject: [PATCH] Fix bug toIndex({ line: 0, col: 0}) failed --- lib/line-column.js | 2 +- test/line-column-test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/line-column.js b/lib/line-column.js index bdb32ff..2d9375b 100644 --- a/lib/line-column.js +++ b/lib/line-column.js @@ -76,7 +76,7 @@ LineColumnFinder.prototype.toIndex = function (line, column) { return this.toIndex(line[0], line[1]); } if (isObject(line) && "line" in line && ("col" in line || "column" in line)) { - return this.toIndex(line.line, line.col || line.column); + return this.toIndex(line.line, ("col" in line ? line.col : line.column)); } return -1; } diff --git a/test/line-column-test.js b/test/line-column-test.js index 1b02665..5e98d8d 100644 --- a/test/line-column-test.js +++ b/test/line-column-test.js @@ -174,6 +174,9 @@ describe("LineColumnFinder", function () { it("returns an index for the last line", function () { assert(lineColumn.toIndex(4, 7) === 43); }); + it("returns an index for the first character", function () { + assert(lineColumn.toIndex(0, 0) === 0); + }); it("returns an index for the last character", function () { assert(lineColumn.toIndex(4, 12) === 48); }); @@ -190,6 +193,15 @@ describe("LineColumnFinder", function () { it("accepts an Array of [ line, col ]", function () { assert(lineColumn.toIndex([1, 7]) === 15); }); + it("returns an index for { line: 0, col: 0 }", function () { + assert(lineColumn.toIndex({ line: 0, col: 0 }) === 0); + }); + it("returns an index for { line: 0, column: 0 }", function () { + assert(lineColumn.toIndex({ line: 0, column: 0 }) === 0); + }); + it("returns an index for [ 0, 0 ]", function () { + assert(lineColumn.toIndex([0, 0]) === 0); + }); it("returns -1 for line < 0", function () { assert(lineColumn.toIndex(-1, 3) === -1);