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

Function insertLine

npm_modules/cli/test/helpers/ProjectHelpers.ts:96–116  ·  view source on GitHub ↗
(position: 'before' | 'after', search: LineMatcher, content: string | Buffer)

Source from the content-addressed store, hash-verified

94export const atBeginning: LineMatcher = () => true;
95
96export function insertLine(position: 'before' | 'after', search: LineMatcher, content: string | Buffer): FileUpdater {
97 const match = (content: Buffer | string): boolean => search(content.toString());
98 return contents => {
99 let found = false;
100 let buffer = Buffer.from([]);
101 for (const line of eachLine(contents)) {
102 const bufferLine = typeof line === 'string' ? Buffer.from(line) : line;
103 if (position === 'after') {
104 buffer = Buffer.concat([buffer, bufferLine]);
105 }
106 if (!found && match(line)) {
107 found = true;
108 buffer = Buffer.concat([buffer, typeof content === 'string' ? Buffer.from(content) : content]);
109 }
110 if (position === 'before') {
111 buffer = Buffer.concat([buffer, bufferLine]);
112 }
113 }
114 return Promise.resolve(buffer);
115 };
116}
117
118export function insertLineBefore(search: LineMatcher, content: string | Buffer): FileUpdater {
119 return insertLine('before', search, content);

Callers 2

insertLineBeforeFunction · 0.85
insertLineAfterFunction · 0.85

Calls 5

eachLineFunction · 0.90
matchFunction · 0.85
resolveMethod · 0.65
fromMethod · 0.45
concatMethod · 0.45

Tested by

no test coverage detected