lib/util.js

  1. // Copyright 2018 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. 'use strict';
  15. module.exports = {
  16. merge2: merge2
  17. }
  18. /**
  19. * merges two objects, with obj1 taking override precedent
  20. * @function merge2
  21. * @instance
  22. * @param {object} obj1 dominant object to merge with
  23. * @param {object} obj2 subordinate object to merge with
  24. * @return {object}
  25. */
  26. function merge2(obj1, obj2) {
  27. if (!obj1) {
  28. return obj2;
  29. } else if (!obj2) {
  30. return obj1;
  31. }
  32. var result = obj2
  33. Object.keys(obj1).forEach(function (key, ndx, arr) {
  34. result[key] = obj1[key];
  35. });
  36. return result
  37. }