Skip to content

Commit

Permalink
wip: refactor date handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres committed Jun 11, 2017
1 parent 6e4b112 commit 75d2801
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 70 deletions.
43 changes: 22 additions & 21 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { pub } from "core/pubsubhub";

export const name = "core/utils";

export const ISODate = new Intl.DateTimeFormat(["en-ca-iso8601"], {
"year": "numeric",
"month": "2-digit",
"day": "2-digit",
});

const inlineElems = new Set([
"a",
"abbr",
Expand Down Expand Up @@ -357,6 +363,11 @@ export function concatDate(date, sep) {
);
}

// formats a date to "yyyy-mm-dd"
export function toShortIsoDate(date){
return ISODate.format(date);
}

// takes a string, prepends a "0" if it is of length 1, does nothing otherwise
export function lead0(str) {
str = "" + str;
Expand All @@ -365,14 +376,13 @@ export function lead0(str) {

// takes a YYYY-MM-DD date and returns a Date object for it
export function parseSimpleDate(str) {
return new Date(Date.parse(str));
return new Date(str);
}

// takes what document.lastModified returns and produces a Date object for it
export function parseLastModified(str) {
if (!str) return new Date();
return new Date(Date.parse(str));
// return new Date(str.substr(6, 4), (str.substr(0, 2) - 1), str.substr(3, 2));
}

// list of human names for months (in English)
Expand All @@ -391,29 +401,20 @@ export const humanMonths = [
"December",
];

// given either a Date object or a date in YYYY-MM-DD format, return a human-formatted
// date suitable for use in a W3C specification
export function humanDate(date, lang = "en") {
if (!(date instanceof Date)) date = parseSimpleDate(date);
if (window.Intl) {
const day = date.toLocaleString([lang, "en"], { day: "2-digit" });
const month = date.toLocaleString([lang, "en"], { month: "long" });
const year = date.toLocaleString([lang, "en"], { year: "numeric" });
//date month year
return `${day} ${month} ${year}`;
}
return (
lead0(date.getDate()) +
" " +
humanMonths[date.getMonth()] +
" " +
date.getFullYear()
);
// given either a Date object or a date in YYYY-MM-DD format,
// return a human-formatted date suitable for use in a W3C specification
export function humanDate(date=new Date(), lang = document.documentElement.lang || "en") {
if (!(date instanceof Date)) date = new Date(date);
const day = date.toLocaleString([lang, "en"], { day: "2-digit" });
const month = date.toLocaleString([lang, "en"], { month: "long" });
const year = date.toLocaleString([lang, "en"], { year: "numeric" });
//date month year
return `${day} ${month} ${year}`;
}
// given either a Date object or a date in YYYY-MM-DD format, return an ISO formatted
// date suitable for use in a xsd:datetime item
export function isoDate(date) {
if (!(date instanceof Date)) date = parseSimpleDate(date);
if (!(date instanceof Date)) date = new Date(date);
return date.toISOString();
}

Expand Down
88 changes: 51 additions & 37 deletions src/w3c/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,22 @@
// - "w3c-software", a permissive and attributions license (but GPL-compatible).
// - "w3c-software-doc", the W3C Software and Document License
// https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
import {
concatDate,
humanDate,
isoDate,
joinAnd,
parseLastModified,
parseSimpleDate,
} from "core/utils";
import { concatDate, joinAnd, ISODate } from "core/utils";
import hb from "handlebars.runtime";
import { pub } from "core/pubsubhub";
import tmpls from "templates";

const cgbgHeadersTmpl = tmpls["cgbg-headers.html"];
const headersTmpl = tmpls["headers.html"];

const W3CDate = new Intl.DateTimeFormat(["en-AU"], {
year: "numeric",
month: "long",
day: "2-digit"
});

const humanNow = W3CDate.format(new Date());

hb.registerHelper("showPeople", function(name, items) {
// stuff to handle RDFa
var re = "",
Expand Down Expand Up @@ -270,7 +271,7 @@ const status2maturity = {
"WD-NOTE": "WD",
"LC-NOTE": "LC",
"IG-NOTE": "NOTE",
"WG-NOTE": "NOTE",
"WG-NOTE": "NOTE"
};

const status2rdf = {
Expand All @@ -281,7 +282,7 @@ const status2rdf = {
PR: "w3p:PR",
REC: "w3p:REC",
PER: "w3p:PER",
RSCND: "w3p:RSCND",
RSCND: "w3p:RSCND"
};
const status2text = {
NOTE: "Working Group Note",
Expand Down Expand Up @@ -311,11 +312,11 @@ const status2text = {
"CG-DRAFT": "Draft Community Group Report",
"CG-FINAL": "Final Community Group Report",
"BG-DRAFT": "Draft Business Group Report",
"BG-FINAL": "Final Business Group Report",
"BG-FINAL": "Final Business Group Report"
};
const status2long = {
"FPWD-NOTE": "First Public Working Group Note",
"LC-NOTE": "Last Call Working Draft",
"LC-NOTE": "Last Call Working Draft"
};
const recTrackStatus = ["FPWD", "WD", "FPLC", "LC", "CR", "PR", "PER", "REC"];
const noTrackStatus = [
Expand All @@ -328,31 +329,31 @@ const noTrackStatus = [
"CG-FINAL",
"BG-DRAFT",
"BG-FINAL",
"webspec",
"webspec"
];
const cgbg = ["CG-DRAFT", "CG-FINAL", "BG-DRAFT", "BG-FINAL"];
const precededByAn = ["ED", "IG-NOTE"];
const licenses = {
cc0: {
name: "Creative Commons 0 Public Domain Dedication",
short: "CC0",
url: "https://creativecommons.org/publicdomain/zero/1.0/",
url: "https://creativecommons.org/publicdomain/zero/1.0/"
},
"w3c-software": {
name: "W3C Software Notice and License",
short: "W3C Software",
url: "https://www.w3.org/Consortium/Legal/2002/copyright-software-20021231",
url: "https://www.w3.org/Consortium/Legal/2002/copyright-software-20021231"
},
"w3c-software-doc": {
name: "W3C Software and Document Notice and License",
short: "W3C Software and Document",
url: "https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document",
url: "https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document"
},
"cc-by": {
name: "Creative Commons Attribution 4.0 International Public License",
short: "CC-BY",
url: "https://creativecommons.org/licenses/by/4.0/legalcode",
},
url: "https://creativecommons.org/licenses/by/4.0/legalcode"
}
};

export function run(conf, doc, cb) {
Expand Down Expand Up @@ -384,14 +385,12 @@ export function run(conf, doc, cb) {
pub("error", "Missing required configuration: shortName");
conf.title = doc.title || "No Title";
if (!conf.subtitle) conf.subtitle = "";
if (!conf.publishDate) {
conf.publishDate = parseLastModified(doc.lastModified);
} else {
if (!(conf.publishDate instanceof Date))
conf.publishDate = parseSimpleDate(conf.publishDate);
}
conf.publishDate = conf.publishDate
? new Date(conf.publishDate)
: new Date(doc.lastModified);

conf.publishYear = conf.publishDate.getFullYear();
conf.publishHumanDate = humanDate(conf.publishDate);
conf.publishHumanDate = W3CDate.format(conf.publishDate);
conf.isNoTrack = $.inArray(conf.specStatus, noTrackStatus) >= 0;
conf.isRecTrack = conf.noRecTrack
? false
Expand Down Expand Up @@ -433,19 +432,19 @@ export function run(conf, doc, cb) {
if (conf.isTagFinding) {
conf.latestVersion = "https://www.w3.org/2001/tag/doc/" + conf.shortName;
conf.thisVersion =
conf.latestVersion + "-" + concatDate(conf.publishDate, "-");
conf.latestVersion + "-" + ISODate.format(conf.publishDate);
}
if (conf.previousPublishDate) {
if (!conf.previousMaturity && !conf.isTagFinding)
pub("error", "previousPublishDate is set, but not previousMaturity");
if (!(conf.previousPublishDate instanceof Date))
conf.previousPublishDate = parseSimpleDate(conf.previousPublishDate);
conf.previousPublishDate = new Date(conf.previousPublishDate);
var pmat = status2maturity[conf.previousMaturity]
? status2maturity[conf.previousMaturity]
: conf.previousMaturity;
if (conf.isTagFinding) {
conf.prevVersion =
conf.latestVersion + "-" + concatDate(conf.previousPublishDate, "-");
conf.latestVersion + "-" + ISODate.format(conf.previousPublishDate);
} else if (conf.isCGBG) {
conf.prevVersion = conf.prevVersion || "";
} else if (conf.isBasic) {
Expand Down Expand Up @@ -575,8 +574,8 @@ export function run(conf, doc, cb) {
conf.isMO = conf.specStatus === "MO";
conf.isIGNote = conf.specStatus === "IG-NOTE";
conf.dashDate = concatDate(conf.publishDate, "-");
conf.publishISODate = isoDate(conf.publishDate);
conf.shortISODate = conf.publishISODate.replace(/T.*/, "");
conf.publishISODate = conf.publishDate.toISOString();
conf.shortISODate = ISODate.format(conf.publishDate);
conf.processVersion = conf.processVersion || "2017";
if (conf.processVersion == "2014" || conf.processVersion == "2015") {
pub(
Expand Down Expand Up @@ -656,20 +655,35 @@ export function run(conf, doc, cb) {
conf.multipleWGs = false;
conf.wgHTML = "the <a href='" + conf.wgURI + "'>" + conf.wg + "</a>";
}
if (conf.specStatus === "PR" && !conf.crEnd)
if (conf.specStatus === "PR" && !conf.crEnd) {
pub(
"error",
"Status is PR but no crEnd is specified (needed to indicate end of previous CR)"
);
if (conf.specStatus === "CR" && !conf.crEnd)
}

if (conf.specStatus === "CR" && !conf.crEnd) {
pub("error", "Status is CR but no crEnd is specified");
conf.humanCREnd = humanDate(conf.crEnd || "");
if (conf.specStatus === "PR" && !conf.prEnd)
}
conf.humanCREnd = conf.crEnd
? W3CDate.format(new Date(conf.crEnd))
: humanNow;

if (conf.specStatus === "PR" && !conf.prEnd) {
pub("error", "Status is PR but no prEnd is specified");
conf.humanPREnd = humanDate(conf.prEnd || "");
conf.humanPEREnd = humanDate(conf.perEnd || "");
if (conf.specStatus === "PER" && !conf.perEnd)
conf.prEnd = new Date();
}
conf.humanPREnd = conf.prEnd
? W3CDate.format(new Date(conf.prEnd))
: humanNow;

if (conf.specStatus === "PER" && !conf.perEnd) {
pub("error", "Status is PER but no perEnd is specified");
conf.perEnd = new Date();
}
conf.humanPEREnd = conf.perEnd
? W3CDate.format(new Date(conf.perEnd))
: humanNow;

conf.recNotExpected =
!conf.isRecTrack &&
Expand Down
23 changes: 11 additions & 12 deletions tests/spec/w3c/headers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,17 +498,17 @@ describe("W3C — Headers", function() {
subjectPrefix: "[The Prefix]",
};
Object.assign(ops.config, newProps);
makeRSDoc(
ops,
function(doc) {
makeRSDoc(ops,
(doc) => {
var $sotd = $("#sotd", doc);
expect($sotd.find("p:contains('CUSTOM PARAGRAPH')").length).toEqual(1);
expect($sotd.find("a:contains('WGNAME')").length).toEqual(1);
expect($sotd.find("a:contains('WGNAME')").attr("href")).toEqual(
"WGURI"
);
expect($sotd.find("a:contains('WGLIST@w3.org')").length).toEqual(1);
expect($sotd.find("a:contains('WGLIST@w3.org')").attr("href")).toEqual(
const wgname = $sotd.find("a:contains('WGNAME')").length
expect(wgname).toEqual(1);
const href = $sotd.find("a:contains('WGNAME')").attr("href")
expect(href).toEqual("WGURI");
const list = $sotd.find("a:contains('WGLIST@w3.org')").length;
expect(list).toEqual(1);
const listHref = $sotd.find("a:contains('WGLIST@w3.org')").attr("href");
expect(listHref).toEqual(
"mailto:WGLIST@w3.org?subject=%5BThe%20Prefix%5D"
);
expect($sotd.find("a:contains('subscribe')").attr("href")).toEqual(
Expand All @@ -520,8 +520,7 @@ describe("W3C — Headers", function() {
expect($sotd.find("a:contains('disclosures')").attr("href")).toEqual(
"WGPATENT"
);
},
simpleSpecURL
}
).then(done);
});

Expand Down

0 comments on commit 75d2801

Please sign in to comment.