Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

offset-to-loc.test.ts

34 lines1.1 KB

1import { describe, expect, it } from 'vitest'
2import { createLocMapper } from './offset-to-loc'
3​
4describe('createLocMapper', () => {
5 // Source: "ab\ncde\nf"
6 // Indices: 0 1 2 3 4 5 6 7
7 // lineStarts: [0, 3, 7]
8 const source = 'ab\ncde\nf'
9 const toLoc = createLocMapper(source)
10​
11 it('maps offset 0 to line 1, column 0', () => {
12 expect(toLoc(0)).toEqual({ line: 1, column: 0 })
13 })
14​
15 it('maps the start of line 2 (offset 3, just after first \\n) to line 2, column 0', () => {
16 expect(toLoc(3)).toEqual({ line: 2, column: 0 })
17 })
18​
19 it('maps a mid-line-2 offset (offset 5, "e") to line 2, column 2', () => {
20 expect(toLoc(5)).toEqual({ line: 2, column: 2 })
21 })
22​
23 it('maps the final offset (offset 7, "f") to line 3, column 0', () => {
24 expect(toLoc(7)).toEqual({ line: 3, column: 0 })
25 })
26​
27 it('maps an offset on a \\n boundary char (offset 2) to line 1, column 2', () => {
28 expect(toLoc(2)).toEqual({ line: 1, column: 2 })
29 })
30​
31 it('maps the second \\n boundary (offset 6) to line 2, column 3', () => {
32 expect(toLoc(6)).toEqual({ line: 2, column: 3 })
33 })
34})