deep-copy.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * @license
  5. * Copyright 2017 Google Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. exports.deepExtend = exports.deepCopy = void 0;
  21. /**
  22. * Returns a deep copy of an object or array.
  23. *
  24. * @param value - The object or array to deep copy.
  25. * @returns A deep copy of the provided object or array.
  26. */
  27. function deepCopy(value) {
  28. return deepExtend(undefined, value);
  29. }
  30. exports.deepCopy = deepCopy;
  31. /**
  32. * Copies properties from source to target (recursively allows extension of objects and arrays).
  33. * Scalar values in the target are over-written. If target is undefined, an object of the
  34. * appropriate type will be created (and returned).
  35. *
  36. * We recursively copy all child properties of plain objects in the source - so that namespace-like
  37. * objects are merged.
  38. *
  39. * Note that the target can be a function, in which case the properties in the source object are
  40. * copied onto it as static properties of the function.
  41. *
  42. * @param target - The value which is being extended.
  43. * @param source - The value whose properties are extending the target.
  44. * @returns The target value.
  45. */
  46. function deepExtend(target, source) {
  47. if (!(source instanceof Object)) {
  48. return source;
  49. }
  50. switch (source.constructor) {
  51. case Date: {
  52. // Treat Dates like scalars; if the target date object had any child
  53. // properties - they will be lost!
  54. const dateValue = source;
  55. return new Date(dateValue.getTime());
  56. }
  57. case Object:
  58. if (target === undefined) {
  59. target = {};
  60. }
  61. break;
  62. case Array:
  63. // Always copy the array source and overwrite the target.
  64. target = [];
  65. break;
  66. default:
  67. // Not a plain Object - treat it as a scalar.
  68. return source;
  69. }
  70. for (const prop in source) {
  71. if (!Object.prototype.hasOwnProperty.call(source, prop)) {
  72. continue;
  73. }
  74. target[prop] = deepExtend(target[prop], source[prop]);
  75. }
  76. return target;
  77. }
  78. exports.deepExtend = deepExtend;