utils.js 3.3 KB

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