Coder Social home page Coder Social logo

Comments (6)

macdja38 avatar macdja38 commented on July 26, 2024

hmm. it wouldn't be a good idea to have that change the global deepmerge that's being required, so it would probably be best if menge.setDefaultOptions returns a copy of merge that can be used instead... something like

const merge = require('deepmerge').setDefaultOptions({
  arrayMerge: (dest, source) => source,
});

Otherwise two libraries in the same application that use deepmerge could interfere.

from deepmerge.

adamreisnz avatar adamreisnz commented on July 26, 2024

Yes, I was planning to make it chainable and return a copy of merge

from deepmerge.

TehShrike avatar TehShrike commented on July 26, 2024

Either way, you still have to do the same thing in your project - which is make a merge.js file in your shared/common folder for everything in your project to import.

I'm hesitant to add new code to this project, since setting defaults on your own is so easy to do, and there are even different flavors you might want to expose internally.

Able to override options:

import arrayMerge from './somewhere'
import deepmerge from 'deepmerge'
export default (a, b, options) => deepmerge(a, b, Object.assign({ arrayMerge }, options))

Unable to override options:

import arrayMerge from './somewhere'
import deepmerge from 'deepmerge'
export default (a, b) => deepmerge(a, b, { arrayMerge })

Able to merge any number of objects:

import arrayMerge from './somewhere'
import deepmerge from 'deepmerge'
export default (...objects) => deepmerge.all(objects, { arrayMerge })

I think it's better to let everyone decide how they want merge to be exposed to their project with a line of JS instead of using a baked-in extend function on the library.

from deepmerge.

adamreisnz avatar adamreisnz commented on July 26, 2024

@TehShrike no, that's incorrect. I am using ES6 and standard require's, as I imagine the majority of people using Node still do.

As such, all require's are cached, so I can simply set the defaults once, in my initialisation file, and then whenever I require deepmerge elsewhere in my application I won't have to worry about the defaults.

E.g.:

init.js

//Set default merge options for deep merge
require('deepmerge').setDefaultOptions({
  arrayMerge: (dest, source) => source,
});

anywhere-else.js

const merge = require('deepmerge');
merge(a, b); //Will use the defaults as defined earlier.

The thing is, there must already be defaults (e.g. the default array merge is to concatenate), so I simply want an interface to be able to modify those defaults. The current approach is too cumbersome to manage efficiently across a large codebase.

from deepmerge.

TehShrike avatar TehShrike commented on July 26, 2024

Yes, that's what @macdja38 was addressing - that syntax also affects any other modules that you're requiring. If you're using some gulp library or whatever that uses deepmerge, you don't want to mess with its default properties.

Your usage of deepmerge shouldn't affect some webpack library that also happens to use deepmerge.

Ignore the ES Module syntax in my previous response. The problems are exactly the same even if I use CommonJS in the examples.

from deepmerge.

adamreisnz avatar adamreisnz commented on July 26, 2024

from deepmerge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.