core.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. var core = new (function () {
  19. var _mix = function (targ, src, merge, includeProto) {
  20. for (var p in src) {
  21. // Don't copy stuff from the prototype
  22. if (src.hasOwnProperty(p) || includeProto) {
  23. if (merge &&
  24. // Assumes the source property is an Object you can
  25. // actually recurse down into
  26. (typeof src[p] == 'object') &&
  27. (src[p] !== null) &&
  28. !(src[p] instanceof Array)) {
  29. // Create the source property if it doesn't exist
  30. // Double-equal to undefined includes both null and undefined
  31. if (targ[p] == undefined) {
  32. targ[p] = {};
  33. }
  34. _mix(targ[p], src[p], merge, includeProto); // Recurse
  35. }
  36. // If it's not a merge-copy, just set and forget
  37. else {
  38. targ[p] = src[p];
  39. }
  40. }
  41. }
  42. };
  43. /*
  44. * Mix in the properties on an object to another object
  45. * yam.mixin(target, source, [source,] [source, etc.] [merge-flag]);
  46. * 'merge' recurses, to merge object sub-properties together instead
  47. * of just overwriting with the source object.
  48. */
  49. this.mixin = function () {
  50. var args = Array.prototype.slice.apply(arguments),
  51. merge = false,
  52. targ, sources;
  53. if (args.length > 2) {
  54. if (typeof args[args.length - 1] == 'boolean') {
  55. merge = args.pop();
  56. }
  57. }
  58. targ = args.shift();
  59. sources = args;
  60. for (var i = 0, ii = sources.length; i < ii; i++) {
  61. _mix(targ, sources[i], merge);
  62. }
  63. return targ;
  64. };
  65. this.enhance = function () {
  66. var args = Array.prototype.slice.apply(arguments),
  67. merge = false,
  68. targ, sources;
  69. if (args.length > 2) {
  70. if (typeof args[args.length - 1] == 'boolean') {
  71. merge = args.pop();
  72. }
  73. }
  74. targ = args.shift();
  75. sources = args;
  76. for (var i = 0, ii = sources.length; i < ii; i++) {
  77. _mix(targ, sources[i], merge, true);
  78. }
  79. return targ;
  80. };
  81. // Idea to add invalid number & Date from Michael J. Ryan,
  82. // http://frugalcoder.us/post/2010/02/15/js-is-empty.aspx
  83. this.isEmpty = function (val) {
  84. // Empty string, null or undefined (these two are double-equal)
  85. if (val === '' || val == undefined) {
  86. return true;
  87. }
  88. // Invalid numerics
  89. if (typeof val == 'number' && isNaN(val)) {
  90. return true;
  91. }
  92. // Invalid Dates
  93. if (val instanceof Date && isNaN(val.getTime())) {
  94. return true;
  95. }
  96. return false;
  97. };
  98. /*
  99. binds a function to an object
  100. */
  101. this.bind = function () {
  102. var args = Array.prototype.slice.call(arguments)
  103. , ctxt = args.shift()
  104. , fn = args.shift();
  105. if (typeof fn === 'function') {
  106. if (typeof Function.bind === 'function') {
  107. return fn.bind.apply(ctxt, args);
  108. }
  109. else {
  110. return fn.apply(ctxt, args);
  111. }
  112. }
  113. // in IE, native methods are not functions so they cannot be bound,
  114. // and don't need to be
  115. else {
  116. return fn;
  117. }
  118. }
  119. })();
  120. module.exports = core;