[483] | 1 | define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array", "./lambda"], |
---|
| 2 | function(kernel, lang, arr, df){ |
---|
| 3 | |
---|
| 4 | // This module adds high-level functions and related constructs: |
---|
| 5 | // - array-processing functions similar to standard JS functions |
---|
| 6 | |
---|
| 7 | // Notes: |
---|
| 8 | // - this module provides JS standard methods similar to high-level functions in dojo/_base/array.js: |
---|
| 9 | // forEach, map, filter, every, some |
---|
| 10 | |
---|
| 11 | // Defined methods: |
---|
| 12 | // - take any valid lambda argument as the functional argument |
---|
| 13 | // - operate on dense arrays |
---|
| 14 | // - take a string as the array argument |
---|
| 15 | // - take an iterator objects as the array argument |
---|
| 16 | |
---|
| 17 | var empty = {}; |
---|
| 18 | |
---|
| 19 | lang.mixin(df, { |
---|
| 20 | // JS 1.6 standard array functions, which can take a lambda as a parameter. |
---|
| 21 | // Consider using dojo._base.array functions, if you don't need the lambda support. |
---|
| 22 | filter: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 23 | // summary: |
---|
| 24 | // creates a new array with all elements that pass the test |
---|
| 25 | // implemented by the provided function. |
---|
| 26 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 27 | o = o || kernel.global; f = df.lambda(f); |
---|
| 28 | var t = [], v, i, n; |
---|
| 29 | if(lang.isArray(a)){ |
---|
| 30 | // array |
---|
| 31 | for(i = 0, n = a.length; i < n; ++i){ |
---|
| 32 | v = a[i]; |
---|
| 33 | if(f.call(o, v, i, a)){ t.push(v); } |
---|
| 34 | } |
---|
| 35 | }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
---|
| 36 | // iterator |
---|
| 37 | for(i = 0; a.hasNext();){ |
---|
| 38 | v = a.next(); |
---|
| 39 | if(f.call(o, v, i++, a)){ t.push(v); } |
---|
| 40 | } |
---|
| 41 | }else{ |
---|
| 42 | // object/dictionary |
---|
| 43 | for(i in a){ |
---|
| 44 | if(!(i in empty)){ |
---|
| 45 | v = a[i]; |
---|
| 46 | if(f.call(o, v, i, a)){ t.push(v); } |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | return t; // Array |
---|
| 51 | }, |
---|
| 52 | forEach: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 53 | // summary: |
---|
| 54 | // executes a provided function once per array element. |
---|
| 55 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 56 | o = o || kernel.global; f = df.lambda(f); |
---|
| 57 | var i, n; |
---|
| 58 | if(lang.isArray(a)){ |
---|
| 59 | // array |
---|
| 60 | for(i = 0, n = a.length; i < n; f.call(o, a[i], i, a), ++i); |
---|
| 61 | }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
---|
| 62 | // iterator |
---|
| 63 | for(i = 0; a.hasNext(); f.call(o, a.next(), i++, a)); |
---|
| 64 | }else{ |
---|
| 65 | // object/dictionary |
---|
| 66 | for(i in a){ |
---|
| 67 | if(!(i in empty)){ |
---|
| 68 | f.call(o, a[i], i, a); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | return o; // Object |
---|
| 73 | }, |
---|
| 74 | map: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 75 | // summary: |
---|
| 76 | // creates a new array with the results of calling |
---|
| 77 | // a provided function on every element in this array. |
---|
| 78 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 79 | o = o || kernel.global; f = df.lambda(f); |
---|
| 80 | var t, n, i; |
---|
| 81 | if(lang.isArray(a)){ |
---|
| 82 | // array |
---|
| 83 | t = new Array(n = a.length); |
---|
| 84 | for(i = 0; i < n; t[i] = f.call(o, a[i], i, a), ++i); |
---|
| 85 | }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
---|
| 86 | // iterator |
---|
| 87 | t = []; |
---|
| 88 | for(i = 0; a.hasNext(); t.push(f.call(o, a.next(), i++, a))); |
---|
| 89 | }else{ |
---|
| 90 | // object/dictionary |
---|
| 91 | t = []; |
---|
| 92 | for(i in a){ |
---|
| 93 | if(!(i in empty)){ |
---|
| 94 | t.push(f.call(o, a[i], i, a)); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | return t; // Array |
---|
| 99 | }, |
---|
| 100 | every: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 101 | // summary: |
---|
| 102 | // tests whether all elements in the array pass the test |
---|
| 103 | // implemented by the provided function. |
---|
| 104 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 105 | o = o || kernel.global; f = df.lambda(f); |
---|
| 106 | var i, n; |
---|
| 107 | if(lang.isArray(a)){ |
---|
| 108 | // array |
---|
| 109 | for(i = 0, n = a.length; i < n; ++i){ |
---|
| 110 | if(!f.call(o, a[i], i, a)){ |
---|
| 111 | return false; // Boolean |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
---|
| 115 | // iterator |
---|
| 116 | for(i = 0; a.hasNext();){ |
---|
| 117 | if(!f.call(o, a.next(), i++, a)){ |
---|
| 118 | return false; // Boolean |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | }else{ |
---|
| 122 | // object/dictionary |
---|
| 123 | for(i in a){ |
---|
| 124 | if(!(i in empty)){ |
---|
| 125 | if(!f.call(o, a[i], i, a)){ |
---|
| 126 | return false; // Boolean |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | return true; // Boolean |
---|
| 132 | }, |
---|
| 133 | some: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 134 | // summary: |
---|
| 135 | // tests whether some element in the array passes the test |
---|
| 136 | // implemented by the provided function. |
---|
| 137 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 138 | o = o || kernel.global; f = df.lambda(f); |
---|
| 139 | var i, n; |
---|
| 140 | if(lang.isArray(a)){ |
---|
| 141 | // array |
---|
| 142 | for(i = 0, n = a.length; i < n; ++i){ |
---|
| 143 | if(f.call(o, a[i], i, a)){ |
---|
| 144 | return true; // Boolean |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
---|
| 148 | // iterator |
---|
| 149 | for(i = 0; a.hasNext();){ |
---|
| 150 | if(f.call(o, a.next(), i++, a)){ |
---|
| 151 | return true; // Boolean |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | }else{ |
---|
| 155 | // object/dictionary |
---|
| 156 | for(i in a){ |
---|
| 157 | if(!(i in empty)){ |
---|
| 158 | if(f.call(o, a[i], i, a)){ |
---|
| 159 | return true; // Boolean |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | return false; // Boolean |
---|
| 165 | } |
---|
| 166 | }); |
---|
| 167 | |
---|
| 168 | return df; |
---|
| 169 | }); |
---|