Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.lang.array");
12
dojo.require("dojo.lang.common");
13
dojo.lang.mixin(dojo.lang, {has:function (obj, name) {
14
	try {
15
		return typeof obj[name] != "undefined";
16
	}
17
	catch (e) {
18
		return false;
19
	}
20
}, isEmpty:function (obj) {
21
	if (dojo.lang.isObject(obj)) {
22
		var tmp = {};
23
		var count = 0;
24
		for (var x in obj) {
25
			if (obj[x] && (!tmp[x])) {
26
				count++;
27
				break;
28
			}
29
		}
30
		return count == 0;
31
	} else {
32
		if (dojo.lang.isArrayLike(obj) || dojo.lang.isString(obj)) {
33
			return obj.length == 0;
34
		}
35
	}
36
}, map:function (arr, obj, unary_func) {
37
	var isString = dojo.lang.isString(arr);
38
	if (isString) {
39
		arr = arr.split("");
40
	}
41
	if (dojo.lang.isFunction(obj) && (!unary_func)) {
42
		unary_func = obj;
43
		obj = dj_global;
44
	} else {
45
		if (dojo.lang.isFunction(obj) && unary_func) {
46
			var tmpObj = obj;
47
			obj = unary_func;
48
			unary_func = tmpObj;
49
		}
50
	}
51
	if (Array.map) {
52
		var outArr = Array.map(arr, unary_func, obj);
53
	} else {
54
		var outArr = [];
55
		for (var i = 0; i < arr.length; ++i) {
56
			outArr.push(unary_func.call(obj, arr[i]));
57
		}
58
	}
59
	if (isString) {
60
		return outArr.join("");
61
	} else {
62
		return outArr;
63
	}
64
}, reduce:function (arr, initialValue, obj, binary_func) {
65
	var reducedValue = initialValue;
66
	if (arguments.length == 2) {
67
		binary_func = initialValue;
68
		reducedValue = arr[0];
69
		arr = arr.slice(1);
70
	} else {
71
		if (arguments.length == 3) {
72
			if (dojo.lang.isFunction(obj)) {
73
				binary_func = obj;
74
				obj = null;
75
			}
76
		} else {
77
			if (dojo.lang.isFunction(obj)) {
78
				var tmp = binary_func;
79
				binary_func = obj;
80
				obj = tmp;
81
			}
82
		}
83
	}
84
	var ob = obj || dj_global;
85
	dojo.lang.map(arr, function (val) {
86
		reducedValue = binary_func.call(ob, reducedValue, val);
87
	});
88
	return reducedValue;
89
}, forEach:function (anArray, callback, thisObject) {
90
	if (dojo.lang.isString(anArray)) {
91
		anArray = anArray.split("");
92
	}
93
	if (Array.forEach) {
94
		Array.forEach(anArray, callback, thisObject);
95
	} else {
96
		if (!thisObject) {
97
			thisObject = dj_global;
98
		}
99
		for (var i = 0, l = anArray.length; i < l; i++) {
100
			callback.call(thisObject, anArray[i], i, anArray);
101
		}
102
	}
103
}, _everyOrSome:function (every, arr, callback, thisObject) {
104
	if (dojo.lang.isString(arr)) {
105
		arr = arr.split("");
106
	}
107
	if (Array.every) {
108
		return Array[every ? "every" : "some"](arr, callback, thisObject);
109
	} else {
110
		if (!thisObject) {
111
			thisObject = dj_global;
112
		}
113
		for (var i = 0, l = arr.length; i < l; i++) {
114
			var result = callback.call(thisObject, arr[i], i, arr);
115
			if (every && !result) {
116
				return false;
117
			} else {
118
				if ((!every) && (result)) {
119
					return true;
120
				}
121
			}
122
		}
123
		return Boolean(every);
124
	}
125
}, every:function (arr, callback, thisObject) {
126
	return this._everyOrSome(true, arr, callback, thisObject);
127
}, some:function (arr, callback, thisObject) {
128
	return this._everyOrSome(false, arr, callback, thisObject);
129
}, filter:function (arr, callback, thisObject) {
130
	var isString = dojo.lang.isString(arr);
131
	if (isString) {
132
		arr = arr.split("");
133
	}
134
	var outArr;
135
	if (Array.filter) {
136
		outArr = Array.filter(arr, callback, thisObject);
137
	} else {
138
		if (!thisObject) {
139
			if (arguments.length >= 3) {
140
				dojo.raise("thisObject doesn't exist!");
141
			}
142
			thisObject = dj_global;
143
		}
144
		outArr = [];
145
		for (var i = 0; i < arr.length; i++) {
146
			if (callback.call(thisObject, arr[i], i, arr)) {
147
				outArr.push(arr[i]);
148
			}
149
		}
150
	}
151
	if (isString) {
152
		return outArr.join("");
153
	} else {
154
		return outArr;
155
	}
156
}, unnest:function () {
157
	var out = [];
158
	for (var i = 0; i < arguments.length; i++) {
159
		if (dojo.lang.isArrayLike(arguments[i])) {
160
			var add = dojo.lang.unnest.apply(this, arguments[i]);
161
			out = out.concat(add);
162
		} else {
163
			out.push(arguments[i]);
164
		}
165
	}
166
	return out;
167
}, toArray:function (arrayLike, startOffset) {
168
	var array = [];
169
	for (var i = startOffset || 0; i < arrayLike.length; i++) {
170
		array.push(arrayLike[i]);
171
	}
172
	return array;
173
}});
174