object.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Utilities: A classic collection of JavaScript utilities
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /**
  19. @name object
  20. @namespace object
  21. */
  22. var object = new (function () {
  23. /**
  24. @name object#merge
  25. @public
  26. @function
  27. @return {Object} Returns the merged object
  28. @description Merge merges `otherObject` into `object` and takes care of deep
  29. merging of objects
  30. @param {Object} object Object to merge into
  31. @param {Object} otherObject Object to read from
  32. */
  33. this.merge = function (object, otherObject) {
  34. var obj = object || {}
  35. , otherObj = otherObject || {}
  36. , key, value;
  37. for (key in otherObj) {
  38. value = otherObj[key];
  39. // Check if a value is an Object, if so recursively add it's key/values
  40. if (typeof value === 'object' && !(value instanceof Array)) {
  41. // Update value of object to the one from otherObj
  42. obj[key] = this.merge(obj[key], value);
  43. }
  44. // Value is anything other than an Object, so just add it
  45. else {
  46. obj[key] = value;
  47. }
  48. }
  49. return obj;
  50. };
  51. /**
  52. @name object#reverseMerge
  53. @public
  54. @function
  55. @return {Object} Returns the merged object
  56. @description ReverseMerge merges `object` into `defaultObject`
  57. @param {Object} object Object to read from
  58. @param {Object} defaultObject Object to merge into
  59. */
  60. this.reverseMerge = function (object, defaultObject) {
  61. // Same as `merge` except `defaultObject` is the object being changed
  62. // - this is useful if we want to easily deal with default object values
  63. return this.merge(defaultObject, object);
  64. };
  65. /**
  66. @name object#isEmpty
  67. @public
  68. @function
  69. @return {Boolean} Returns true if empty false otherwise
  70. @description isEmpty checks if an Object is empty
  71. @param {Object} object Object to check if empty
  72. */
  73. this.isEmpty = function (object) {
  74. // Returns true if a object is empty false if not
  75. for (var i in object) { return false; }
  76. return true;
  77. };
  78. /**
  79. @name object#toArray
  80. @public
  81. @function
  82. @return {Array} Returns an array of objects each including the original key and value
  83. @description Converts an object to an array of objects each including the original key/value
  84. @param {Object} object Object to convert
  85. */
  86. this.toArray = function (object) {
  87. // Converts an object into an array of objects with the original key, values
  88. array = [];
  89. for (var i in object) {
  90. array.push({ key: i, value: object[i] });
  91. }
  92. return array;
  93. };
  94. })();
  95. module.exports = object;