2013-08-07 22:28:45 +02:00
|
|
|
/* Constructs a new Dict object.
|
|
|
|
*
|
2013-08-19 20:58:31 +02:00
|
|
|
* Dict(opt) -> the new Dict will be empty
|
|
|
|
*
|
|
|
|
* Available options:
|
|
|
|
* fold_case - Make has() and get() case-insensitive. keys() and
|
|
|
|
* other methods that implicitly return keys return the
|
|
|
|
* casing used for the most recent set()
|
|
|
|
*
|
2013-08-07 22:28:45 +02:00
|
|
|
*/
|
2013-08-19 20:58:31 +02:00
|
|
|
function Dict(opts) {
|
2013-08-07 22:28:45 +02:00
|
|
|
var self = this;
|
|
|
|
this._items = {};
|
2013-08-19 20:58:31 +02:00
|
|
|
this._opts = _.extend({}, {fold_case: false}, opts);
|
2013-08-19 20:46:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Constructs a new Dict object from another object.
|
|
|
|
*
|
2013-08-19 20:58:31 +02:00
|
|
|
* Dict.from(jsobj, opts) -> create a Dict with keys corresponding to the
|
|
|
|
* properties of jsobj and values corresponding to
|
|
|
|
* the value of the appropriate property. `opts` is
|
|
|
|
* passed to the Dict constructor.
|
2013-08-19 20:46:27 +02:00
|
|
|
*/
|
2013-08-19 20:58:31 +02:00
|
|
|
Dict.from = function Dict_from(obj, opts) {
|
2013-08-20 21:32:55 +02:00
|
|
|
if (typeof obj !== "object" || obj === null) {
|
2013-08-19 20:46:27 +02:00
|
|
|
throw new TypeError("Cannot convert argument to Dict");
|
2013-08-07 22:28:45 +02:00
|
|
|
}
|
2013-08-19 20:46:27 +02:00
|
|
|
|
2013-08-20 21:32:55 +02:00
|
|
|
var ret = new Dict(opts);
|
|
|
|
_.each(obj, function (val, key) {
|
|
|
|
ret.set(key, val);
|
|
|
|
});
|
2013-08-19 20:46:27 +02:00
|
|
|
return ret;
|
|
|
|
};
|
2013-08-07 22:28:45 +02:00
|
|
|
|
|
|
|
(function () {
|
2013-08-07 23:56:51 +02:00
|
|
|
Dict.prototype = _.object(_.map({
|
2013-08-19 20:58:31 +02:00
|
|
|
_munge: function Dict__munge(k) {
|
|
|
|
if (this._opts.fold_case) {
|
|
|
|
k = k.toLowerCase();
|
|
|
|
}
|
|
|
|
return ':' + k;
|
|
|
|
},
|
|
|
|
|
2013-08-20 21:32:55 +02:00
|
|
|
clone: function Dict_clone() {
|
|
|
|
var ret = new Dict(this._opts);
|
|
|
|
ret._items = _.clone(this._items);
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
2013-08-07 22:28:45 +02:00
|
|
|
get: function Dict_get(key) {
|
2013-08-19 20:58:31 +02:00
|
|
|
var mapping = this._items[this._munge(key)];
|
2013-08-19 20:33:18 +02:00
|
|
|
if (mapping === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return mapping.v;
|
2013-08-07 22:28:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function Dict_set(key, value) {
|
2013-08-20 22:34:36 +02:00
|
|
|
this._items[this._munge(key)] = {k: key, v: value};
|
|
|
|
return value;
|
2013-08-07 22:28:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
has: function Dict_has(key) {
|
2013-08-19 20:58:31 +02:00
|
|
|
return _.has(this._items, this._munge(key));
|
2013-08-07 22:28:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
del: function Dict_del(key) {
|
2013-08-19 20:58:31 +02:00
|
|
|
return delete this._items[this._munge(key)];
|
2013-08-07 22:28:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
keys: function Dict_keys() {
|
2013-08-19 20:33:18 +02:00
|
|
|
return _.pluck(_.values(this._items), 'k');
|
2013-08-07 22:28:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
values: function Dict_values() {
|
2013-08-19 20:33:18 +02:00
|
|
|
return _.pluck(_.values(this._items), 'v');
|
2013-08-07 22:28:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
items: function Dict_items() {
|
2013-08-19 20:33:18 +02:00
|
|
|
return _.map(_.values(this._items), function (mapping) {
|
|
|
|
return [mapping.k, mapping.v];
|
2013-08-07 22:28:45 +02:00
|
|
|
});
|
2013-08-14 19:08:03 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Iterates through the Dict calling f(value, key) for each (key, value) pair in the Dict
|
|
|
|
each: function Dict_each(f) {
|
2013-08-19 20:33:18 +02:00
|
|
|
return _.each(this._items, function (mapping) {
|
|
|
|
f(mapping.v, mapping.k);
|
2013-08-14 19:08:03 +02:00
|
|
|
});
|
2013-08-07 22:28:45 +02:00
|
|
|
}
|
2013-08-07 23:56:51 +02:00
|
|
|
}, function (value, key) {
|
|
|
|
return [key, util.enforce_arity(value)];
|
|
|
|
}));
|
2013-08-07 22:28:45 +02:00
|
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = Dict;
|
|
|
|
}
|