MCPcopy Create free account
hub / github.com/Snapchat/Valdi / lineAndOffset

Function lineAndOffset

npm_modules/cli/test/helpers/TypeScriptClient.ts:91–114  ·  view source on GitHub ↗
(searchToken: string, file: string)

Source from the content-addressed store, hash-verified

89}
90
91export async function lineAndOffset(searchToken: string, file: string): Promise<FilePosition> {
92 const buffer = await readFile(file);
93 const position = buffer.indexOf(searchToken);
94
95 if (position === -1) {
96 throw new Error('searchToken not found in file');
97 }
98
99 let line = 1;
100 let index = 0;
101 let offset = -1;
102 while (index <= position) {
103 const nextLine = buffer.indexOf('\n', index);
104 if (nextLine < position) {
105 line += 1;
106 index = nextLine + 1;
107 continue;
108 } else {
109 offset = position - index;
110 break;
111 }
112 }
113 return { line, offset: Math.max(0, offset), position };
114}
115
116function withTimeout<T>(promise: Promise<T>, ms: number | false = 300000): Promise<T> {
117 if (ms === false) {

Callers 2

ValdiSmokeTest.tsFile · 0.90

Calls 1

indexOfMethod · 0.45

Tested by

no test coverage detected