utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.lang.utils"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.utils"] = true;
  8. dojo.provide("dojox.lang.utils");
  9. (function(){
  10. var empty = {}, du = dojox.lang.utils, opts = Object.prototype.toString;
  11. var clone = function(o){
  12. if(o){
  13. switch(opts.call(o)){
  14. case "[object Array]":
  15. return o.slice(0);
  16. case "[object Object]":
  17. return dojo.delegate(o);
  18. }
  19. }
  20. return o;
  21. }
  22. dojo.mixin(du, {
  23. coerceType: function(target, source){
  24. // summary: Coerces one object to the type of another.
  25. // target: Object: object, which typeof result is used to coerce "source" object.
  26. // source: Object: object, which will be forced to change type.
  27. switch(typeof target){
  28. case "number": return Number(eval("(" + source + ")"));
  29. case "string": return String(source);
  30. case "boolean": return Boolean(eval("(" + source + ")"));
  31. }
  32. return eval("(" + source + ")");
  33. },
  34. updateWithObject: function(target, source, conv){
  35. // summary: Updates an existing object in place with properties from an "source" object.
  36. // target: Object: the "target" object to be updated
  37. // source: Object: the "source" object, whose properties will be used to source the existed object.
  38. // conv: Boolean?: force conversion to the original type
  39. if(!source){ return target; }
  40. for(var x in target){
  41. if(x in source && !(x in empty)){
  42. var t = target[x];
  43. if(t && typeof t == "object"){
  44. du.updateWithObject(t, source[x], conv);
  45. }else{
  46. target[x] = conv ? du.coerceType(t, source[x]) : clone(source[x]);
  47. }
  48. }
  49. }
  50. return target; // Object
  51. },
  52. updateWithPattern: function(target, source, pattern, conv){
  53. // summary: Updates an existing object in place with properties from an "source" object.
  54. // target: Object: the "target" object to be updated
  55. // source: Object: the "source" object, whose properties will be used to source the existed object.
  56. // pattern: Object: object, whose properties will be used to pull values from the "source"
  57. // conv: Boolean?: force conversion to the original type
  58. if(!source || !pattern){ return target; }
  59. for(var x in pattern){
  60. if(x in source && !(x in empty)){
  61. target[x] = conv ? du.coerceType(pattern[x], source[x]) : clone(source[x]);
  62. }
  63. }
  64. return target; // Object
  65. },
  66. merge: function(object, mixin){
  67. // summary: Merge two objects structurally, mixin properties will override object's properties.
  68. // object: Object: original object.
  69. // mixin: Object: additional object, which properties will override object's properties.
  70. if(mixin){
  71. var otype = opts.call(object), mtype = opts.call(mixin), t, i, l, m;
  72. switch(mtype){
  73. case "[object Array]":
  74. if(mtype == otype){
  75. t = new Array(Math.max(object.length, mixin.length));
  76. for(i = 0, l = t.length; i < l; ++i){
  77. t[i] = du.merge(object[i], mixin[i]);
  78. }
  79. return t;
  80. }
  81. return mixin.slice(0);
  82. case "[object Object]":
  83. if(mtype == otype && object){
  84. t = dojo.delegate(object);
  85. for(i in mixin){
  86. if(i in object){
  87. l = object[i];
  88. m = mixin[i];
  89. if(m !== l){
  90. t[i] = du.merge(l, m);
  91. }
  92. }else{
  93. t[i] = dojo.clone(mixin[i]);
  94. }
  95. }
  96. return t;
  97. }
  98. return dojo.clone(mixin);
  99. }
  100. }
  101. return mixin;
  102. }
  103. });
  104. })();
  105. }