How to use the merge function from hoek

Find comprehensive JavaScript hoek.merge code examples handpicked from public code repositorys.

hoek.merge is a utility function that merges the properties of two or more objects into a single object.

39
40
41
42
43
44
45
46
47
48
        verifyOptions: { algorithms: [ 'HS256' ] },
    };
}

exports.register = function (server, options, next) {
    const config = Hoek.merge(generateDefaultOptions(), options);
    server.auth.strategy('jwt', 'jwt', config);
    server.auth.default('jwt');

    server.ext('onPreHandler', async (request, reply) => {
fork icon0
star icon2
watch icon1

104
105
106
107
108
109
110
111
112

var newTarget = Hoek.merge(target, source);     // results in {a: 0, b: 2, c: 5}
newTarget = Hoek.merge(target, source2);        // results in {a: null, b: 2, c: 5}
newTarget = Hoek.merge(target, source2, false); // results in {a: 1, b: 2, c: 5}

newTarget = Hoek.merge(targetArray, sourceArray)              // results in [1, 2, 3, 4, 5]
newTarget = Hoek.merge(targetArray, sourceArray, true, false) // results in [4, 5]


fork icon170
star icon0
watch icon1

+ 9 other calls in file

How does hoek.merge work?

hoek.merge is a utility function provided by the Hoek module of the Hapi framework in Node.js, and it is used to deep merge two or more objects into a new object. When hoek.merge is called with two or more objects as arguments, it creates a new object and deep merges the properties of the input objects into this new object, with the properties of the later objects taking precedence over earlier objects in the list. The merging is done recursively, so that nested objects and arrays are also merged, with properties of later objects overwriting those of earlier objects at the same level of nesting. If a property exists in only one object, it is copied as-is into the new object. The resulting object is returned as the output of the function.

102
103
104
105
106
107
108
109
110
var targetArray = [1, 2, 3];
var sourceArray = [4, 5];

var newTarget = Hoek.merge(target, source);     // results in {a: 0, b: 2, c: 5}
newTarget = Hoek.merge(target, source2);        // results in {a: null, b: 2, c: 5}
newTarget = Hoek.merge(target, source2, false); // results in {a: null, b:2, c: 5}

newTarget = Hoek.merge(targetArray, sourceArray)              // results in [1, 2, 3, 4, 5]
newTarget = Hoek.merge(targetArray, sourceArray, true, false) // results in [4, 5]
fork icon170
star icon0
watch icon2

+ 9 other calls in file

55
56
57
58
59
60
61
62
63
64
/**
 * Merges an object's properties into the custom properties collection.
 * @param obj
 */
Representation.prototype.merge = function(obj) {
    hoek.merge(this._props, obj);
};

/**
 * @param {...String || String[]} props properties to ignore
fork icon23
star icon108
watch icon9

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const Hoek = require("hoek");

const first = {
  a: {
    b: 1,
    c: 2,
  },
  d: 3,
};

const second = {
  a: {
    b: 4,
    e: 5,
  },
  f: 6,
};

const result = Hoek.merge(first, second);

console.log(result);
// Output: { a: { b: 4, c: 2, e: 5 }, d: 3, f: 6 }

In this example, hoek.merge is used to merge two objects first and second into a single object result. The resulting object contains all the properties from both first and second, with the properties of second taking precedence over those of first when there is a conflict.

82
83
84
85
86
87
88
89
90
91
var statusResponses = Hoek.merge({default: defaultResponseSchema}, statusResponseSchema)
responses = _.reduce(statusResponses, function (memo, responseSchema, key) {
  if (utils.isSupportedSchema(responseSchema)) {
    var memoType = memo[key]
    var responseType = internals.prepareResponseSchema(definitions, responseSchema)
    memo[key] = memoType ? Hoek.merge(memoType, responseType) : responseType
  }

  return memo
}, responses)
fork icon38
star icon0
watch icon2

+ 3 other calls in file

44
45
46
47
48
49
50
51
52
53
ES.UPDATE(timer, function(record) {
  console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ')
  console.log(record);
  console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ')
  
  Hoek.merge(record, timer); // http://git.io/Amv6
  record.id = record._id;
  delete record._index;
  delete record._type;
  delete record._version;
fork icon19
star icon0
watch icon2

109
110
111
112
113
114
115
116
117
118
                    internals.reachDelete(input, error.path);
                });
            }
        }

        Hoek.merge(this, input);
    }
};

module.exports = generate;
fork icon9
star icon0
watch icon3

184
185
186
187
188
189
190
191
192
193
    }
  }
}

if (headers) {
  Hoek.merge(options.headers, headers)
}

if (
  settings.xforward &&
fork icon2
star icon11
watch icon4

15
16
17
18
19
20
21
22
23
24
25
  }
  return next()
}


exports.cleanBodyRequest = (object, ...body) => returnObject => {
  object = Hoek.merge({}, object)
  body.map(key => {
    if (object[key] !== undefined) returnObject[key] = object[key]
    return returnObject
  })
fork icon0
star icon2
watch icon1

135
136
137
138
139
140
141
142
143
144
obj._settings = obj._settings ? internals.concatSettings(obj._settings, schema._settings) : schema._settings;
obj._valids.merge(schema._valids, schema._invalids);
obj._invalids.merge(schema._invalids, schema._valids);
obj._tests = obj._tests.concat(schema._tests);
obj._refs = obj._refs.concat(schema._refs);
Hoek.merge(obj._flags, schema._flags);

obj._description = schema._description || obj._description;
obj._unit = schema._unit || obj._unit;
obj._notes = obj._notes.concat(schema._notes);
fork icon0
star icon2
watch icon0

27
28
29
30
31
32
33
34
35
36

// Clone options

var defaults = Hoek.applyToDefaultsWithShallow(Defaults.views, options, ['engines']);
if (_override) {
    Hoek.merge(defaults, _override);     // _override cannot contain non-clonable objects
}

delete defaults.engines;
delete defaults.defaultExtension;
fork icon0
star icon0
watch icon71

73
74
75
76
77
78
79
80
81
82
        }
    }
};

delete localOptions.context[source];
Hoek.merge(localOptions, request.route.settings.validate.options);

let value;
let validationError;
try {
fork icon0
star icon0
watch icon0

+ 3 other calls in file