0

How to extend object in gulp without installing npm?

var a = {a: 1};
var b = {b: 2};
var c = extend({}, a, b, {c:3});

c is {a: 1, b: 2, c: 3}
Warlock
  • 7,321
  • 10
  • 55
  • 75
  • Using loop, copying key-value pairs from `a` and `b` to `c`? – thefourtheye Mar 27 '15 at 14:03
  • I don't wanna write `extend` function for every gulp file – Warlock Mar 27 '15 at 14:30
  • Can you clarify the question? At face this has nothing to do with gulp or npm. Are you asking how to extend an object's properties in plain Javascript? That's [very well documented](http://stackoverflow.com/a/11197343/605707). – deefour Mar 27 '15 at 16:10
  • I'm wondering if there any utility available in `gulp`, which could extend objects like `jQuery.extend` or `angular.extend` or `npm extend`. I know that the same feature could be called in grunt by `grunt.util._.merge`. – Warlock Mar 27 '15 at 19:50
  • That's because grunt includes Lodash, you can also do this in your gulpfile, but you'll have to get Lodash first – Preview Mar 28 '15 at 00:50
  • So, there is no way to do `extend` in gulp without installing additional packages, right? – Warlock Mar 28 '15 at 08:39

1 Answers1

1

Not that I know of. I would recommend that you use object-assign, a forward compatible module that will delegate to the ES6 Object.assign if available. It's extremely lightweight at 26 lines (as of this writing).

var assign = require('object-assign');

var a = {a: 1};
var b = {b: 2};
var c = assign(a, b, {c:3});

console.log(c);

// => {a: 1, b: 2, c: 3}
Ben
  • 10,106
  • 3
  • 40
  • 58