object.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Generated by CoffeeScript 1.6.3
  2. var object, _common,
  3. __hasProp = {}.hasOwnProperty;
  4. _common = require('./_common');
  5. module.exports = object = {
  6. isBareObject: _common.isBareObject.bind(_common),
  7. /*
  8. if object is an instance of a class
  9. */
  10. isInstance: function(what) {
  11. return !this.isBareObject(what);
  12. },
  13. /*
  14. Alias to _common.typeOf
  15. */
  16. typeOf: _common.typeOf.bind(_common),
  17. /*
  18. Alias to _common.clone
  19. */
  20. clone: _common.clone.bind(_common),
  21. /*
  22. Empties an object of its properties.
  23. */
  24. empty: function(o) {
  25. var prop;
  26. for (prop in o) {
  27. if (o.hasOwnProperty(prop)) {
  28. delete o[prop];
  29. }
  30. }
  31. return o;
  32. },
  33. /*
  34. Empties an object. Doesn't check for hasOwnProperty, so it's a tiny
  35. bit faster. Use it for plain objects.
  36. */
  37. fastEmpty: function(o) {
  38. var property;
  39. for (property in o) {
  40. delete o[property];
  41. }
  42. return o;
  43. },
  44. /*
  45. Overrides values fomr `newValues` on `base`, as long as they
  46. already exist in base.
  47. */
  48. overrideOnto: function(base, newValues) {
  49. var key, newVal, oldVal;
  50. if (!this.isBareObject(newValues) || !this.isBareObject(base)) {
  51. return base;
  52. }
  53. for (key in base) {
  54. oldVal = base[key];
  55. newVal = newValues[key];
  56. if (newVal === void 0) {
  57. continue;
  58. }
  59. if (typeof newVal !== 'object' || this.isInstance(newVal)) {
  60. base[key] = this.clone(newVal);
  61. } else {
  62. if (typeof oldVal !== 'object' || this.isInstance(oldVal)) {
  63. base[key] = this.clone(newVal);
  64. } else {
  65. this.overrideOnto(oldVal, newVal);
  66. }
  67. }
  68. }
  69. return base;
  70. },
  71. /*
  72. Takes a clone of 'base' and runs #overrideOnto on it
  73. */
  74. override: function(base, newValues) {
  75. return this.overrideOnto(this.clone(base), newValues);
  76. },
  77. append: function(base, toAppend) {
  78. return this.appendOnto(this.clone(base), toAppend);
  79. },
  80. appendOnto: function(base, toAppend) {
  81. var key, newVal, oldVal;
  82. if (!this.isBareObject(toAppend) || !this.isBareObject(base)) {
  83. return base;
  84. }
  85. for (key in toAppend) {
  86. if (!__hasProp.call(toAppend, key)) continue;
  87. newVal = toAppend[key];
  88. if (newVal === void 0) {
  89. continue;
  90. }
  91. if (typeof newVal !== 'object' || this.isInstance(newVal)) {
  92. base[key] = newVal;
  93. } else {
  94. oldVal = base[key];
  95. if (typeof oldVal !== 'object' || this.isInstance(oldVal)) {
  96. base[key] = this.clone(newVal);
  97. } else {
  98. this.appendOnto(oldVal, newVal);
  99. }
  100. }
  101. }
  102. return base;
  103. },
  104. groupProps: function(obj, groups) {
  105. var def, defs, grouped, key, name, shouldAdd, val, _i, _len;
  106. grouped = {};
  107. for (name in groups) {
  108. defs = groups[name];
  109. grouped[name] = {};
  110. }
  111. grouped['rest'] = {};
  112. top: //;
  113. for (key in obj) {
  114. val = obj[key];
  115. shouldAdd = false;
  116. for (name in groups) {
  117. defs = groups[name];
  118. if (!Array.isArray(defs)) {
  119. defs = [defs];
  120. }
  121. for (_i = 0, _len = defs.length; _i < _len; _i++) {
  122. def = defs[_i];
  123. if (typeof def === 'string') {
  124. if (key === def) {
  125. shouldAdd = true;
  126. }
  127. } else if (def instanceof RegExp) {
  128. if (def.test(key)) {
  129. shouldAdd = true;
  130. }
  131. } else if (def instanceof Function) {
  132. if (def(key)) {
  133. shouldAdd = true;
  134. }
  135. } else {
  136. throw Error('Group definitions must either\
  137. be strings, regexes, or functions.');
  138. }
  139. if (shouldAdd) {
  140. grouped[name][key] = val;
  141. continue top;
  142. }
  143. }
  144. }
  145. grouped['rest'][key] = val;
  146. }
  147. return grouped;
  148. }
  149. };