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. /* Constructs a new Dict object.
* *
* Dict() -> the new Dict will be empty * 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; var self = this;
this._items = {}; 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 (typeof obj === "object" && obj !== null) {
if (obj instanceof Dict) { if (obj instanceof Dict) {
_.each(obj.items(), function (kv) { _.each(obj.items(), function (kv) {
self.set(kv[0], kv[1]); ret.set(kv[0], kv[1]);
}); });
} else { } else {
_.each(obj, function (val, key) { _.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 () { (function () {

View File

@@ -8,9 +8,9 @@ var deferred_work = [];
// We'll temporarily set stream colors for the streams we use in the demo // We'll temporarily set stream colors for the streams we use in the demo
// tutorial messages. // tutorial messages.
var real_stream_info; var real_stream_info;
var tutorial_stream_info = new Dict({"design": {"color": "#76ce90"}, var tutorial_stream_info = Dict.from({"design": {"color": "#76ce90"},
"social": {"color": "#fae589"}, "social": {"color": "#fae589"},
"devel": {"color": "#a6c7e5"}}); "devel": {"color": "#a6c7e5"}});
// Each message object contains the minimal information necessary for it to be // Each message object contains the minimal information necessary for it to be
// processed by our system for adding messages to your feed. // 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', 'alice@zulip.com': 'Alice Smith',
'fred@zulip.com': 'Fred Flintstone', 'fred@zulip.com': 'Fred Flintstone',
'jill@zulip.com': 'Jill Hill' 'jill@zulip.com': 'Jill Hill'

View File

@@ -57,10 +57,10 @@ var _ = global._;
assert.deepEqual(d1.items(), []); 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']]); assert.deepEqual(d2.items(), [['foo', 'bar'], ['baz', 'qux']]);
var d3 = new Dict(d2); var d3 = Dict.from(d2);
d3.del('foo'); d3.del('foo');
assert.deepEqual(d2.items(), [['foo', 'bar'], ['baz', 'qux']]); assert.deepEqual(d2.items(), [['foo', 'bar'], ['baz', 'qux']]);
assert.deepEqual(d3.items(), [['baz', 'qux']]); assert.deepEqual(d3.items(), [['baz', 'qux']]);

View File

@@ -108,7 +108,7 @@ var search = set_up_dependencies();
return 'office'; return 'office';
}; };
global.recent_subjects = new global.Dict({ global.recent_subjects = new global.Dict.from({
office: [ office: [
{subject: 'team'}, {subject: 'team'},
{subject: 'ignore'}, {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: [ office: [
{subject: 'team'}, {subject: 'team'},
{subject: 'ignore'}, {subject: 'ignore'},