merge icon indicating copy to clipboard operation
merge copied to clipboard

version 2.x converts regular expressions to objects when cloning

Open cressie176 opened this issue 4 years ago • 2 comments

Thanks for sharing merge. When upgrading from v1 to v2 I found the following bug. Bit confused as what could have caused it as the clone code doesn't seem to have changed greatly (other than being converted to TypeScript)

const merge = require('merge');
const result = merge.recursive(true, { regex: /.*/ } , {});
console.log(result);

v1.x

{ regex: /.*/ }

v2.x

{ regex: {} }

cressie176 avatar Mar 27 '21 07:03 cressie176

This is the problem. It incorrectly assumes that anything with with typeof object that is not an array is a plain object.

export function isPlainObject(input: any): input is Object {
  return input && typeof input === 'object' && !Array.isArray(input)
}

The code previously had a custom function to determine the type.

function typeOf(input) {
  return ({}).toString.call(input).slice(8, -1).toLowerCase();
}

cressie176 avatar Mar 27 '21 07:03 cressie176

@cressie176 I realize this is slightly off-topic, but are you aware of any other breaking changes in 2.x? I'm thinking of updating this dep in a few places. Nothing was documented that I can see, and it doesn't appear there were any intentional changes to the API with the Typescript migration, but I thought I'd ask around to be safe.

@yeikos care to weigh in?

dmellstrom avatar May 14 '21 15:05 dmellstrom

This bug recently bit me and required more than a day of sleuthing before I figured it out. Hard to believe it is still an open issue more than 18 months after getting reported, especially since @cressie176 pointed out where the problem lies.

tferrin avatar Sep 30 '22 16:09 tferrin

Closed via e9bea183aa374810ab2262639e303eb0500e6790.

juanrgm avatar May 05 '23 17:05 juanrgm