MCPcopy Index your code
hub / github.com/kpdecker/jsdiff / structuredPatch

Function structuredPatch

src/patch/create.ts:191–343  ·  view source on GitHub ↗
(
  oldFileName: string,
  newFileName: string,
  oldStr: string,
  newStr: string,
  oldHeader?: string,
  newHeader?: string,
  options?: StructuredPatchOptionsAbortable | StructuredPatchOptionsNonabortable | StructuredPatchCallbackNonabortable
)

Source from the content-addressed store, hash-verified

189 options?: StructuredPatchOptionsNonabortable
190): StructuredPatch
191export function structuredPatch(
192 oldFileName: string,
193 newFileName: string,
194 oldStr: string,
195 newStr: string,
196 oldHeader?: string,
197 newHeader?: string,
198 options?: StructuredPatchOptionsAbortable | StructuredPatchOptionsNonabortable | StructuredPatchCallbackNonabortable
199): StructuredPatch | undefined {
200 let optionsObj: StructuredPatchOptionsAbortable | StructuredPatchOptionsNonabortable;
201 if (!options) {
202 optionsObj = {};
203 } else if (typeof options === 'function') {
204 optionsObj = {callback: options};
205 } else {
206 optionsObj = options;
207 }
208
209
210 if (typeof optionsObj.context === 'undefined') {
211 optionsObj.context = 4;
212 }
213
214 // We copy this into its own variable to placate TypeScript, which thinks
215 // optionsObj.context might be undefined in the callbacks below.
216 const context = optionsObj.context;
217
218 // @ts-expect-error (runtime check for something that is correctly a static type error)
219 if (optionsObj.newlineIsToken) {
220 throw new Error('newlineIsToken may not be used with patch-generation functions, only with diffing functions');
221 }
222
223 if (!optionsObj.callback) {
224 return diffLinesResultToPatch(diffLines(oldStr, newStr, optionsObj as any));
225 } else {
226 const {callback} = optionsObj;
227 diffLines(
228 oldStr,
229 newStr,
230 {
231 ...optionsObj,
232 callback: (diff) => {
233 const patch = diffLinesResultToPatch(diff);
234 // TypeScript is unhappy without the cast because it does not understand that `patch` may
235 // be undefined here only if `callback` is StructuredPatchCallbackAbortable:
236 (callback as any)(patch);
237 }
238 }
239 );
240 }
241
242 function diffLinesResultToPatch(diff: ChangeObjectPlusLines[] | undefined) {
243 // STEP 1: Build up the patch with no "\ No newline at end of file" lines and with the arrays
244 // of lines containing trailing newline characters. We'll tidy up later...
245
246 if(!diff) {
247 return;
248 }

Callers 4

reverse.jsFile · 0.85
create.jsFile · 0.85
apply.jsFile · 0.85
createTwoFilesPatchFunction · 0.85

Calls 2

diffLinesResultToPatchFunction · 0.85
diffLinesFunction · 0.85

Tested by

no test coverage detected