diff options
Diffstat (limited to 'js/dojo-1.7.2/dojox/lang/functional/array.js')
| -rw-r--r-- | js/dojo-1.7.2/dojox/lang/functional/array.js | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/js/dojo-1.7.2/dojox/lang/functional/array.js b/js/dojo-1.7.2/dojox/lang/functional/array.js new file mode 100644 index 0000000..8c47b5c --- /dev/null +++ b/js/dojo-1.7.2/dojox/lang/functional/array.js @@ -0,0 +1,168 @@ +//>>built +define("dojox/lang/functional/array", ["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array", "dojo/_base/window", "./lambda"], + function(dojo, lang, arr, win, df){ + +// This module adds high-level functions and related constructs: +// - array-processing functions similar to standard JS functions + +// Notes: +// - this module provides JS standard methods similar to high-level functions in dojo/_base/array.js: +// forEach, map, filter, every, some + +// Defined methods: +// - take any valid lambda argument as the functional argument +// - operate on dense arrays +// - take a string as the array argument +// - take an iterator objects as the array argument + + var empty = {}; + +/*===== + var df = dojox.lang.functional; + =====*/ + lang.mixin(df, { + // JS 1.6 standard array functions, which can take a lambda as a parameter. + // Consider using dojo._base.array functions, if you don't need the lambda support. + filter: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: creates a new array with all elements that pass the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || win.global; f = df.lambda(f); + var t = [], v, i, n; + if(lang.isArray(a)){ + // array + for(i = 0, n = a.length; i < n; ++i){ + v = a[i]; + if(f.call(o, v, i, a)){ t.push(v); } + } + }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ + // iterator + for(i = 0; a.hasNext();){ + v = a.next(); + if(f.call(o, v, i++, a)){ t.push(v); } + } + }else{ + // object/dictionary + for(i in a){ + if(!(i in empty)){ + v = a[i]; + if(f.call(o, v, i, a)){ t.push(v); } + } + } + } + return t; // Array + }, + forEach: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: executes a provided function once per array element. + if(typeof a == "string"){ a = a.split(""); } + o = o || win.global; f = df.lambda(f); + var i, n; + if(lang.isArray(a)){ + // array + for(i = 0, n = a.length; i < n; f.call(o, a[i], i, a), ++i); + }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ + // iterator + for(i = 0; a.hasNext(); f.call(o, a.next(), i++, a)); + }else{ + // object/dictionary + for(i in a){ + if(!(i in empty)){ + f.call(o, a[i], i, a); + } + } + } + return o; // Object + }, + map: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: creates a new array with the results of calling + // a provided function on every element in this array. + if(typeof a == "string"){ a = a.split(""); } + o = o || win.global; f = df.lambda(f); + var t, n, i; + if(lang.isArray(a)){ + // array + t = new Array(n = a.length); + for(i = 0; i < n; t[i] = f.call(o, a[i], i, a), ++i); + }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ + // iterator + t = []; + for(i = 0; a.hasNext(); t.push(f.call(o, a.next(), i++, a))); + }else{ + // object/dictionary + t = []; + for(i in a){ + if(!(i in empty)){ + t.push(f.call(o, a[i], i, a)); + } + } + } + return t; // Array + }, + every: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: tests whether all elements in the array pass the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || win.global; f = df.lambda(f); + var i, n; + if(lang.isArray(a)){ + // array + for(i = 0, n = a.length; i < n; ++i){ + if(!f.call(o, a[i], i, a)){ + return false; // Boolean + } + } + }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ + // iterator + for(i = 0; a.hasNext();){ + if(!f.call(o, a.next(), i++, a)){ + return false; // Boolean + } + } + }else{ + // object/dictionary + for(i in a){ + if(!(i in empty)){ + if(!f.call(o, a[i], i, a)){ + return false; // Boolean + } + } + } + } + return true; // Boolean + }, + some: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: tests whether some element in the array passes the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || win.global; f = df.lambda(f); + var i, n; + if(lang.isArray(a)){ + // array + for(i = 0, n = a.length; i < n; ++i){ + if(f.call(o, a[i], i, a)){ + return true; // Boolean + } + } + }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ + // iterator + for(i = 0; a.hasNext();){ + if(f.call(o, a.next(), i++, a)){ + return true; // Boolean + } + } + }else{ + // object/dictionary + for(i in a){ + if(!(i in empty)){ + if(f.call(o, a[i], i, a)){ + return true; // Boolean + } + } + } + } + return false; // Boolean + } + }); + + return df; +}); |
