Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make links into actual Zone objects instead of just referencing the linked Zone #104

Merged
merged 1 commit into from
Jul 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,19 @@
************************************/

function Zone (packedString) {
var unpacked = unpack(packedString);
this.name = unpacked.name;
this.abbrs = unpacked.abbrs;
this.untils = unpacked.untils;
this.offsets = unpacked.offsets;
if (packedString) {
this._set(unpack(packedString));
}
}

Zone.prototype = {
_set : function (unpacked) {
this.name = unpacked.name;
this.abbrs = unpacked.abbrs;
this.untils = unpacked.untils;
this.offsets = unpacked.offsets;
},

_index : function (timestamp) {
var target = +timestamp,
untils = this.untils,
Expand Down Expand Up @@ -183,27 +188,22 @@
}

function addZone (packed) {
var i, zone;
var i, zone, zoneName;

if (typeof packed === "string") {
packed = [packed];
}

for (i = 0; i < packed.length; i++) {
zone = new Zone(packed[i]);
zones[normalizeName(zone.name)] = zone;
zoneName = normalizeName(zone.name);
zones[zoneName] = zone;
upgradeLinksToZones(zoneName);
}
}

function getZone (name) {
name = normalizeName(name);
var linkName = links[name];

if (linkName && zones[linkName]) {
name = linkName;
}

return zones[name] || null;
return zones[normalizeName(name)] || null;
}

function getNames () {
Expand All @@ -226,9 +226,42 @@
}

for (i = 0; i < aliases.length; i++) {
alias = normalizeName(aliases[i]).split('|');
links[alias[0]] = alias[1];
links[alias[1]] = alias[0];
alias = aliases[i].split('|');
pushLink(alias[0], alias[1]);
pushLink(alias[1], alias[0]);
}
}

function upgradeLinksToZones (zoneName) {
if (!links[zoneName]) {
return;
}

var i,
zone = zones[zoneName],
linkNames = links[zoneName];

for (i = 0; i < linkNames.length; i++) {
copyZoneWithName(zone, linkNames[i]);
}

links[zoneName] = null;
}

function copyZoneWithName (zone, name) {
var linkZone = zones[normalizeName(name)] = new Zone();
linkZone._set(zone);
linkZone.name = name;
}

function pushLink (zoneName, linkName) {
zoneName = normalizeName(zoneName);

if (zones[zoneName]) {
copyZoneWithName(zones[zoneName], linkName);
} else {
links[zoneName] = links[zoneName] || [];
links[zoneName].push(linkName);
}
}

Expand Down
61 changes: 48 additions & 13 deletions tests/moment-timezone/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@

var tz = require("../../").tz;

function clearObject (obj) {
var tempLinks = {},
tempZones = {};

function moveProperties (a, b) {
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
delete obj[key];
for (key in a) {
if (a.hasOwnProperty(key)) {
b[key] = a[key];
delete a[key];
}
}

}

exports.link = {
setUp : function (done) {
clearObject(tz._links);
clearObject(tz._zones);
moveProperties(tz._links, tempLinks);
moveProperties(tz._zones, tempZones);
done();
},

tearDown : function (done) {
moveProperties(tz._links, {});
moveProperties(tz._zones, {});
moveProperties(tempLinks, tz._links);
moveProperties(tempZones, tz._zones);
done();
},

Expand All @@ -39,9 +52,9 @@ exports.link = {
test.ok(tz.zone('Alias3'), "Should be able to add an alias with ['Alias|Zone'] format");

test.equal(tz.zone('Zone1').name, 'Zone1', "Should get the right zone.");
test.equal(tz.zone('Alias1').name, 'Zone1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias2').name, 'Zone1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias3').name, 'Zone1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias1').name, 'Alias1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias2').name, 'Alias2', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias3').name, 'Alias3', "Should get the right zone from an alias.");

test.done();
},
Expand All @@ -67,9 +80,9 @@ exports.link = {
test.ok(tz.zone('Alias3'), "Should be able to add an alias with ['Zone|Alias'] format");

test.equal(tz.zone('Zone1').name, 'Zone1', "Should get the right zone.");
test.equal(tz.zone('Alias1').name, 'Zone1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias2').name, 'Zone1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias3').name, 'Zone1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias1').name, 'Alias1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias2').name, 'Alias2', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias3').name, 'Alias3', "Should get the right zone from an alias.");

test.done();
},
Expand All @@ -87,11 +100,33 @@ exports.link = {
test.ok(tz.zone('Alias1'), "Should be able to add an alias before adding the zone.");

test.equal(tz.zone('Zone1').name, 'Zone1', "Should get the right zone.");
test.equal(tz.zone('Alias1').name, 'Zone1', "Should get the right zone from an alias.");
test.equal(tz.zone('Alias1').name, 'Alias1', "Should get the right zone from an alias.");

tz.link("Alias2|Zone2");
test.ok(!tz.zone('Alias2'), "Aliases without zones should be null.");

test.done();
},

namesOfAliases : function (test) {
test.ok(!tz.zone('Zone1'), "Zones should have been reset.");
test.ok(!tz.zone('Alias1'), "Links should have been reset.");
test.ok(!tz.zone('Alias2'), "Links should have been reset.");
test.ok(!tz.zone('Alias3'), "Links should have been reset.");

tz.link("Alias1|Zone1");
tz.add("Zone1|ASDF|0|0|0");
test.deepEqual(tz.names(), ["Alias1", "Zone1"], "Should be able to get the names of aliased zones.");

tz.link("Alias2|Zone1");
test.deepEqual(tz.names(), ["Alias1", "Alias2", "Zone1"], "Should be able to get the names of aliased zones.");

tz.link("Alias3|Zone2");
test.deepEqual(tz.names(), ["Alias1", "Alias2", "Zone1"], "Should be able to get the names of aliased zones.");

tz.link("Alias3|Zone1");
test.deepEqual(tz.names(), ["Alias1", "Alias2", "Alias3", "Zone1"], "Should be able to get the names of aliased zones.");

test.done();
}
};