summaryrefslogtreecommitdiff
path: root/js/dojo-1.6/dojox/lang/oo/rearrange.js
blob: 855e92f73d2163fbbdade637c2020f4736189bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.lang.oo.rearrange"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.lang.oo.rearrange"] = true;
dojo.provide("dojox.lang.oo.rearrange");

(function(){
	var extraNames = dojo._extraNames, extraLen = extraNames.length,
		opts = Object.prototype.toString, empty = {};

	dojox.lang.oo.rearrange = function(bag, map){
		//	summary:
		//		Process properties in place by removing and renaming them.
		//	description:
		//		Properties of an object are to be renamed or removed specified
		//		by "map" argument. Only own properties of "map" are processed.
		//	example:
		//	|	oo.rearrange(bag, {
		//	|		abc: "def",	// rename "abc" attribute to "def"
		//	|		ghi: null	// remove/hide "ghi" attribute
		//	|	});
		//	bag: Object:
		//		the object to be processed
		//	map: Object:
		//		the dictionary for renaming (false value indicates removal of the named property)
		//	returns: Object:
		//		the original object

	var name, newName, prop, i, t;

		for(name in map){
			newName = map[name];
			if(!newName || opts.call(newName) == "[object String]"){
				prop = bag[name];
				if(!(name in empty) || empty[name] !== prop){
					if(!(delete bag[name])){
						// can't delete => hide it
						bag[name] = undefined;
					}
					if(newName){
						bag[newName] = prop;
					}
				}
			}
		}
		if(extraLen){
			for(i = 0; i < extraLen; ++i){
				name = extraNames[i];
				// repeating the body above
				newName = map[name];
				if(!newName || opts.call(newName) == "[object String]"){
					prop = bag[name];
					if(!(name in empty) || empty[name] !== prop){
						if(!(delete bag[name])){
							// can't delete => hide it
							bag[name] = undefined;
						}
						if(newName){
							bag[newName] = prop;
						}
					}
				}
			}
		}

		return bag;	// Object
	};
})();

}