MCPcopy Create free account
hub / github.com/anomalyco/models.dev / applyOmit

Function applyOmit

packages/core/script/compare-model-migrations.ts:338–384  ·  view source on GitHub ↗
(target: Record<string, unknown>, paths: string[])

Source from the content-addressed store, hash-verified

336}
337
338function applyOmit(target: Record<string, unknown>, paths: string[]) {
339 omitLoop: for (const omit of paths) {
340 const parts = omit.split(".");
341 const parents: Array<{
342 value: Record<string, unknown>;
343 key: string;
344 }> = [];
345 let current = target;
346
347 for (const part of parts.slice(0, -1)) {
348 const next = current[part];
349 if (
350 next === undefined ||
351 next === null ||
352 typeof next !== "object" ||
353 Array.isArray(next)
354 ) {
355 continue omitLoop;
356 }
357 parents.push({ value: current, key: part });
358 current = next as Record<string, unknown>;
359 }
360
361 const lastPart = parts.at(-1);
362 if (lastPart === undefined || !(lastPart in current)) {
363 continue;
364 }
365
366 delete current[lastPart];
367
368 for (let index = parents.length - 1; index >= 0; index--) {
369 const parent = parents[index];
370 if (parent === undefined) continue;
371 const value = parent.value[parent.key];
372 if (
373 value === null ||
374 value === undefined ||
375 typeof value !== "object" ||
376 Array.isArray(value) ||
377 Object.keys(value).length > 0
378 ) {
379 break;
380 }
381 delete parent.value[parent.key];
382 }
383 }
384}
385
386function sortedJson(value: unknown) {
387 return JSON.stringify(sortJson(value), null, 2);

Callers 1

generateLegacyExtendsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected