Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ export type Difference = DifferenceCreate | DifferenceRemove | DifferenceChange;

interface Options {
cyclesFix: boolean;
shallow: boolean;
}

const richTypes = { Date: true, RegExp: true, String: true, Number: true };

export default function diff(
obj: Record<string, any> | any[],
newObj: Record<string, any> | any[],
options: Partial<Options> = { cyclesFix: true },
options: Partial<Options> = { cyclesFix: true, shallow: false },
_stack: Record<string, any>[] = [],
): Difference[] {
let diffs: Difference[] = [];
Expand All @@ -38,6 +39,10 @@ export default function diff(
const objKey = obj[key];
const path = isObjArray ? +key : key;
if (!(key in newObj)) {
// En modo shallow, ignorar propiedades que son objetos/arrays
if (options.shallow && typeof objKey === "object" && objKey !== null) {
continue;
}
diffs.push({
type: "REMOVE",
path: [path],
Expand All @@ -50,6 +55,12 @@ export default function diff(
typeof objKey === "object" &&
typeof newObjKey === "object" &&
Array.isArray(objKey) === Array.isArray(newObjKey);

// En modo shallow, ignorar completamente propiedades que son objetos/arrays
if (options.shallow && typeof objKey === "object" && objKey !== null) {
continue;
}

if (
objKey &&
newObjKey &&
Expand Down Expand Up @@ -92,6 +103,11 @@ export default function diff(
const isNewObjArray = Array.isArray(newObj);
for (const key in newObj) {
if (!(key in obj)) {
const newObjKey = newObj[key];
// En modo shallow, ignorar propiedades que son objetos/arrays
if (options.shallow && typeof newObjKey === "object" && newObjKey !== null) {
continue;
}
diffs.push({
type: "CREATE",
path: [isNewObjArray ? +key : key],
Expand Down
66 changes: 66 additions & 0 deletions tests/data/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export const original = {
IdAuto: 101,
Marca: "Toyota",
Modelo: "Corolla",
Año: 2020,
Color: "Rojo",
Precio: 250000,
EnStock: 1,
DatosAuto: [
{
IdDatosAuto: 1,
IdAuto: 101,
NumeroSerie: "ABC123XYZ456",
Kilometraje: 15000,
Transmision: "Manual",
Motor: {
Tipo: "Gasolina",
Cilindros: 4,
Potencia: "140 HP",
},
Equipamiento: [
{ IdEquipamiento: 1, Nombre: "Aire Acondicionado" },
{ IdEquipamiento: 2, Nombre: "Bluetooth" },
{ IdEquipamiento: 3, Nombre: "Rines de Aluminio" },
],
Ubicacion: {
Sucursal: "Centro",
Ciudad: "Ciudad de México",
CodigoPostal: "06000",
},
},
],
};

export const editado = {
IdAuto: 101,
Marca: "Toyota",
Modelo: "Corolla",
Año: 2020,
Color: "Azul",
Precio: 245000,
EnStock: 1,
DatosAuto: [
{
IdDatosAuto: 1,
IdAuto: 101,
NumeroSerie: "ABC123XYZ456",
Kilometraje: 18000,
Transmision: "Automática",
Motor: {
Tipo: "Gasolina",
Cilindros: 4,
Potencia: "150 HP",
},
Equipamiento: [
{ IdEquipamiento: 1, Nombre: "Aire Acondicionado" },
{ IdEquipamiento: 4, Nombre: "Cámara de Reversa" },
],
Ubicacion: {
Sucursal: "Norte",
Ciudad: "Guadalajara",
CodigoPostal: "44100",
},
},
],
};
20 changes: 20 additions & 0 deletions tests/shallow-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import diff from "../dist/index.js";
import { original, editado } from "./data/data.js";

console.log("=== Comparación SHALLOW del objeto principal (solo primitivos) ===");
const shallowDiffPrincipal = diff(original, editado, { shallow: true });
console.log(shallowDiffPrincipal);

console.log("\n=== Comparación PROFUNDA del objeto principal ===");
const deepDiffPrincipal = diff(original, editado);
console.log(deepDiffPrincipal);

console.log("\n\n=== Comparación SHALLOW de DatosAuto[0] (solo primitivos) ===");
const shallowDiffDatos = diff(original.DatosAuto[0], editado.DatosAuto[0], {
shallow: true,
});
console.log(shallowDiffDatos);

console.log("\n=== Comparación PROFUNDA de DatosAuto[0] ===");
const deepDiffDatos = diff(original.DatosAuto[0], editado.DatosAuto[0]);
console.log(deepDiffDatos);