diff --git a/src/utils/getColNumber.ts b/src/utils/getColNumber.ts deleted file mode 100644 index 4333bd6..0000000 --- a/src/utils/getColNumber.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const getColNumber = (statement: string, text: string): number => { - return (statement.split('\n').pop() as string).indexOf(text) + 1 -} diff --git a/src/utils/getColumnNumber.spec.ts b/src/utils/getColumnNumber.spec.ts new file mode 100644 index 0000000..7a4e405 --- /dev/null +++ b/src/utils/getColumnNumber.spec.ts @@ -0,0 +1,13 @@ +import { getColumnNumber } from './getColumnNumber' + +describe('getColumnNumber', () => { + it('should return the column number of the specified string within a line of text', () => { + expect(getColumnNumber('foo bar', 'bar')).toEqual(5) + }) + + it('should throw an error when the specified string is not found within the text', () => { + expect(() => getColumnNumber('foo bar', 'baz')).toThrowError( + "String 'baz' was not found in statement 'foo bar'" + ) + }) +}) diff --git a/src/utils/getColumnNumber.ts b/src/utils/getColumnNumber.ts new file mode 100644 index 0000000..8d42dfd --- /dev/null +++ b/src/utils/getColumnNumber.ts @@ -0,0 +1,9 @@ +export const getColumnNumber = (statement: string, text: string): number => { + const index = (statement.split('\n').pop() as string).indexOf(text) + if (index < 0) { + throw new Error( + `String '${text}' was not found in statement '${statement}'` + ) + } + return (statement.split('\n').pop() as string).indexOf(text) + 1 +}