Skip to content

Commit

Permalink
Simplify interface to countColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Aug 7, 2021
1 parent f0edad3 commit 684da54
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {findClusterBreak} from "./char"

/// Count the column position at the given offset into the string,
/// taking extending characters and tab size into account.
export function countColumn(string: string, n: number, tabSize: number): number {
for (let i = 0; i < string.length;) {
export function countColumn(string: string, tabSize: number, to = string.length): number {
let n = 0
for (let i = 0; i < to;) {
if (string.charCodeAt(i) == 9) {
n += tabSize - (n % tabSize)
i++
Expand Down
6 changes: 3 additions & 3 deletions test/test-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ describe("findClusterBreak", () => {
})

describe("countColumn", () => {
it("counts characters", () => ist(countColumn("abc", 0, 4), 3))
it("counts characters", () => ist(countColumn("abc", 4), 3))

it("counts tabs correctly", () => ist(countColumn("a\t\tbc\tx", 0, 4), 13))
it("counts tabs correctly", () => ist(countColumn("a\t\tbc\tx", 4), 13))

it("handles clusters", () => ist(countColumn("a😎🇫🇷", 0, 4), 3))
it("handles clusters", () => ist(countColumn("a😎🇫🇷", 4), 3))
})

describe("findColumn", () => {
Expand Down

0 comments on commit 684da54

Please sign in to comment.