dict: Make copying Dict constructor a class constructor method

(imported from commit 7bd5f6029c1290036a47688cf2b80f9317fe9c13)
This commit is contained in:
Zev Benjamin 2013-08-19 14:46:27 -04:00
parent 43847848c5
commit 3a70c4e928
5 changed files with 27 additions and 16 deletions

View File

@ -1,27 +1,38 @@
/* Constructs a new Dict object.
*
* Dict() -> the new Dict will be empty
* Dict(otherdict) -> create a shallow copy of otherdict
* Dict(jsobj) -> create a Dict with keys corresponding to the properties of
* jsobj and values corresponding to the value of the appropriate
* property
*/
function Dict(obj) {
function Dict() {
var self = this;
this._items = {};
}
/* Constructs a new Dict object from another object.
*
* Dict.from(otherdict) -> create a shallow copy of otherdict
* Dict.from(jsobj) -> create a Dict with keys corresponding to the properties of
* jsobj and values corresponding to the value of the appropriate
* property
*/
Dict.from = function Dict_from(obj) {
var ret = new Dict();
if (typeof obj === "object" && obj !== null) {
if (obj instanceof Dict) {
_.each(obj.items(), function (kv) {
self.set(kv[0], kv[1]);
ret.set(kv[0], kv[1]);
});
} else {
_.each(obj, function (val, key) {
self.set(key, val);
ret.set(key, val);
});
}
} else {
throw new TypeError("Cannot convert argument to Dict");
}
}
return ret;
};
(function () {

View File

@ -8,9 +8,9 @@ var deferred_work = [];
// We'll temporarily set stream colors for the streams we use in the demo
// tutorial messages.
var real_stream_info;
var tutorial_stream_info = new Dict({"design": {"color": "#76ce90"},
"social": {"color": "#fae589"},
"devel": {"color": "#a6c7e5"}});
var tutorial_stream_info = Dict.from({"design": {"color": "#76ce90"},
"social": {"color": "#fae589"},
"devel": {"color": "#a6c7e5"}});
// Each message object contains the minimal information necessary for it to be
// processed by our system for adding messages to your feed.

View File

@ -19,7 +19,7 @@ var activity = global.activity;
};
global.people_dict = new global.Dict({
global.people_dict = new global.Dict.from({
'alice@zulip.com': 'Alice Smith',
'fred@zulip.com': 'Fred Flintstone',
'jill@zulip.com': 'Jill Hill'

View File

@ -57,10 +57,10 @@ var _ = global._;
assert.deepEqual(d1.items(), []);
var d2 = new Dict({foo: 'bar', baz: 'qux'});
var d2 = Dict.from({foo: 'bar', baz: 'qux'});
assert.deepEqual(d2.items(), [['foo', 'bar'], ['baz', 'qux']]);
var d3 = new Dict(d2);
var d3 = Dict.from(d2);
d3.del('foo');
assert.deepEqual(d2.items(), [['foo', 'bar'], ['baz', 'qux']]);
assert.deepEqual(d3.items(), [['baz', 'qux']]);

View File

@ -108,7 +108,7 @@ var search = set_up_dependencies();
return 'office';
};
global.recent_subjects = new global.Dict({
global.recent_subjects = new global.Dict.from({
office: [
{subject: 'team'},
{subject: 'ignore'},
@ -178,7 +178,7 @@ var search = set_up_dependencies();
}
];
global.recent_subjects = new global.Dict({
global.recent_subjects = new global.Dict.from({
office: [
{subject: 'team'},
{subject: 'ignore'},