0

I'm trying to log the steps inside a function but i'm facing a very weird error. I'm sure is something very basic but I looked to this code for over 1 hour and cant solve it.

the code might look absurd but this is a very very simplification of the original code just to reproduce the problem

const fecha = require('fecha');
const datePattern = "DD-MM-YYYY - HH:mm";

function f1(p, itemEnum, days) {
  console.log("p1: " + JSON.stringify(p));

  let today = new Date();
  let latestExp = today.getTime();
  let antecedence = 0;

  p.purchases[itemEnum] = {
    item: itemEnum,
    expDt: fecha.format(today, datePattern)
  };

  let dd = new Date(((days + Math.min(antecedence, 7)) * 86400000) + latestExp);
  p.purchases[itemEnum].expDt = fecha.format(dd, datePattern);

  return p.purchases[itemEnum];
}

let o = {
  enum: 'enum',
  dur: 1,
  pm: 'pm',
  id: 'id'
};

let p = {};
let today = new Date();

p.purchases = [];

p.lp_ts = today.getTime();

let m = f1(p, o.enum, (31 * o.dur) + 1);
m.purId = o.id;
m.item = o.enum;
m.pm = o.pm;
m.purDt = fecha.format(today, datePattern);
m.tDt = null;

console.log("p2 " + JSON.stringify(p));
console.log(p);

so according to my mind p1 should be {"purchases":[],"lp_ts":1620261967821} and p2 and p3 should be exact the same since there isn't any changes between them

but p2 ALWAYS print same as p1
then p3 is

{
  purchases: [
    enum: {
      item: 'enum',
      expDt: '07-06-2021 - 00:46',
      purId: 'id',
      pm: 'pm',
      purDt: '06-05-2021 - 00:46',
      tDt: null
    }
  ],
  lp_ts: 1620261967821
}

enter image description here

why ?
what is wrong

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105

0 Answers0