tutils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var getThenable = function(status,v,async){
  2. return {
  3. then: (function(){
  4. return function(ok,no){
  5. var run = function(){
  6. if(status){
  7. ok(v);
  8. }else{
  9. no(v);
  10. }
  11. }
  12. if(async){
  13. setTimeout(run,0);
  14. }else{
  15. run();
  16. }
  17. }
  18. })()
  19. }
  20. }
  21. var getThenableQT = function(status,v,async){
  22. return getThenable(true,getThenable(status,v,async));
  23. }
  24. function typeTest(fun,status){
  25. status = typeof status == 'undefined' ? true : status;
  26. var baseobj = {}, basefn = function(){};
  27. var error = new Error("test");
  28. var baseValue = [
  29. {
  30. name : "null",
  31. value : null,
  32. then : null
  33. },{
  34. name : "undefined",
  35. value : undefined,
  36. then : undefined
  37. },{
  38. name : "number",
  39. value : 1,
  40. then : 1
  41. },{
  42. name : "string",
  43. value : "string",
  44. then : "string"
  45. },{
  46. name : "bool",
  47. value : true,
  48. then : true
  49. },{
  50. name : "普通 object",
  51. value : baseobj,
  52. then :baseobj
  53. },{
  54. name : "普通 error",
  55. value : error,
  56. then :error
  57. },{
  58. name : "普通 function",
  59. value : basefn,
  60. then : basefn
  61. },{
  62. name : "promise",
  63. value : Promise.resolve(1),
  64. then : 1
  65. },{
  66. name : "同步getThenable",
  67. value : getThenable(status,2),
  68. then : 2
  69. },{
  70. name : "异步getThenable",
  71. value : getThenable(status,3,true),
  72. then : 3
  73. },{
  74. name : "嵌套同步 getThenable",
  75. value : getThenableQT(status,4),
  76. then : 4
  77. },{
  78. name : "嵌套异步 getThenable",
  79. value : getThenableQT(status,5,true),
  80. then : 5
  81. }
  82. ]
  83. baseValue.forEach(function(v,i){
  84. fun(v.name,v.value,v.then);
  85. })
  86. }
  87. module.exports = typeTest;