2007-03-09 03:15:03 +00:00
|
|
|
var Hash = function(object) {
|
|
|
|
if (object instanceof Hash) this.merge(object);
|
|
|
|
else Object.extend(this, object || {});
|
2007-01-18 22:24:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.extend(Hash, {
|
|
|
|
toQueryString: function(obj) {
|
|
|
|
var parts = [];
|
2007-03-09 03:15:03 +00:00
|
|
|
parts.add = arguments.callee.addPair;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 03:15:03 +00:00
|
|
|
this.prototype._each.call(obj, function(pair) {
|
2007-01-18 22:24:27 +00:00
|
|
|
if (!pair.key) return;
|
2007-03-09 03:15:03 +00:00
|
|
|
var value = pair.value;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 03:15:03 +00:00
|
|
|
if (value && typeof value == 'object') {
|
|
|
|
if (value.constructor == Array) value.each(function(value) {
|
|
|
|
parts.add(pair.key, value);
|
|
|
|
});
|
|
|
|
return;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2007-03-09 03:15:03 +00:00
|
|
|
parts.add(pair.key, value);
|
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
return parts.join('&');
|
2007-03-09 03:23:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function(object) {
|
|
|
|
var results = [];
|
|
|
|
this.prototype._each.call(object, function(pair){
|
|
|
|
var value = Object.toJSON(pair.value);
|
|
|
|
if (value !== undefined) results.push(pair.key.toJSON() + ':' + value);
|
|
|
|
});
|
|
|
|
return '{' + results.join(',') + '}';
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2007-03-09 03:15:03 +00:00
|
|
|
Hash.toQueryString.addPair = function(key, value, prefix) {
|
|
|
|
if (value == null) return;
|
|
|
|
key = encodeURIComponent(key);
|
|
|
|
this.push(key + '=' + (value == null ? '' : encodeURIComponent(value)));
|
|
|
|
}
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
Object.extend(Hash.prototype, Enumerable);
|
|
|
|
Object.extend(Hash.prototype, {
|
|
|
|
_each: function(iterator) {
|
|
|
|
for (var key in this) {
|
|
|
|
var value = this[key];
|
|
|
|
if (value && value == Hash.prototype[key]) continue;
|
|
|
|
|
|
|
|
var pair = [key, value];
|
|
|
|
pair.key = key;
|
|
|
|
pair.value = value;
|
|
|
|
iterator(pair);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
keys: function() {
|
|
|
|
return this.pluck('key');
|
|
|
|
},
|
|
|
|
|
|
|
|
values: function() {
|
|
|
|
return this.pluck('value');
|
|
|
|
},
|
|
|
|
|
|
|
|
merge: function(hash) {
|
|
|
|
return $H(hash).inject(this, function(mergedHash, pair) {
|
|
|
|
mergedHash[pair.key] = pair.value;
|
|
|
|
return mergedHash;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function() {
|
|
|
|
var result;
|
|
|
|
for(var i = 0, length = arguments.length; i < length; i++) {
|
|
|
|
var value = this[arguments[i]];
|
|
|
|
if (value !== undefined){
|
|
|
|
if (result === undefined) result = value;
|
|
|
|
else {
|
|
|
|
if (result.constructor != Array) result = [result];
|
|
|
|
result.push(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete this[arguments[i]];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
toQueryString: function() {
|
|
|
|
return Hash.toQueryString(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
inspect: function() {
|
|
|
|
return '#<Hash:{' + this.map(function(pair) {
|
|
|
|
return pair.map(Object.inspect).join(': ');
|
|
|
|
}).join(', ') + '}>';
|
2007-03-09 03:23:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function() {
|
|
|
|
return Hash.toJSON(this);
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function $H(object) {
|
2007-03-09 03:15:03 +00:00
|
|
|
if (object instanceof Hash) return object;
|
2007-01-18 22:24:27 +00:00
|
|
|
return new Hash(object);
|
|
|
|
};
|
2007-03-09 03:15:03 +00:00
|
|
|
|
|
|
|
// Safari iterates over shadowed properties
|
|
|
|
if (function() {
|
|
|
|
var i = 0, Test = function(value) { this.key = value };
|
|
|
|
Test.prototype.key = 'foo';
|
|
|
|
for (var property in new Test('bar')) i++;
|
|
|
|
return i > 1;
|
|
|
|
}()) Hash.prototype._each = function(iterator) {
|
|
|
|
var cache = [];
|
|
|
|
for (var key in this) {
|
|
|
|
var value = this[key];
|
|
|
|
if ((value && value == Hash.prototype[key]) || cache.include(key)) continue;
|
|
|
|
cache.push(key);
|
|
|
|
var pair = [key, value];
|
|
|
|
pair.key = key;
|
|
|
|
pair.value = value;
|
|
|
|
iterator(pair);
|
|
|
|
}
|
|
|
|
};
|