mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 03:36:18 +08:00
DEV: introduces prettier for es6 files
This commit is contained in:
@ -1,10 +1,18 @@
|
||||
var clock;
|
||||
|
||||
import { relativeAge, autoUpdatingRelativeAge, updateRelativeAge, breakUp, number, longDate, durationTiny } from 'discourse/lib/formatter';
|
||||
import {
|
||||
relativeAge,
|
||||
autoUpdatingRelativeAge,
|
||||
updateRelativeAge,
|
||||
breakUp,
|
||||
number,
|
||||
longDate,
|
||||
durationTiny
|
||||
} from "discourse/lib/formatter";
|
||||
|
||||
QUnit.module("lib:formatter", {
|
||||
beforeEach() {
|
||||
clock = sinon.useFakeTimers(new Date(2012,11,31,12,0).getTime());
|
||||
clock = sinon.useFakeTimers(new Date(2012, 11, 31, 12, 0).getTime());
|
||||
},
|
||||
|
||||
afterEach() {
|
||||
@ -14,12 +22,12 @@ QUnit.module("lib:formatter", {
|
||||
|
||||
var format = "tiny";
|
||||
var leaveAgo = false;
|
||||
var mins_ago = function(mins){
|
||||
return new Date((new Date()) - mins * 60 * 1000);
|
||||
var mins_ago = function(mins) {
|
||||
return new Date(new Date() - mins * 60 * 1000);
|
||||
};
|
||||
|
||||
var formatMins = function(mins) {
|
||||
return relativeAge(mins_ago(mins), {format: format, leaveAgo: leaveAgo});
|
||||
return relativeAge(mins_ago(mins), { format: format, leaveAgo: leaveAgo });
|
||||
};
|
||||
|
||||
var formatHours = function(hours) {
|
||||
@ -30,19 +38,22 @@ var formatDays = function(days) {
|
||||
return formatHours(days * 24);
|
||||
};
|
||||
|
||||
var shortDate = function(days){
|
||||
return moment().subtract(days, 'days').format('MMM D');
|
||||
var shortDate = function(days) {
|
||||
return moment()
|
||||
.subtract(days, "days")
|
||||
.format("MMM D");
|
||||
};
|
||||
|
||||
QUnit.test("formating medium length dates", assert => {
|
||||
|
||||
format = "medium";
|
||||
var strip = function(html){
|
||||
var strip = function(html) {
|
||||
return $(html).text();
|
||||
};
|
||||
|
||||
var shortDateYear = function(days){
|
||||
return moment().subtract(days, 'days').format("MMM D, 'YY");
|
||||
var shortDateYear = function(days) {
|
||||
return moment()
|
||||
.subtract(days, "days")
|
||||
.format("MMM D, 'YY");
|
||||
};
|
||||
|
||||
leaveAgo = true;
|
||||
@ -76,15 +87,17 @@ QUnit.test("formating medium length dates", assert => {
|
||||
assert.equal($(formatDays(0)).attr("class"), "date");
|
||||
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers(new Date(2012,0,9,12,0).getTime()); // Jan 9, 2012
|
||||
clock = sinon.useFakeTimers(new Date(2012, 0, 9, 12, 0).getTime()); // Jan 9, 2012
|
||||
|
||||
assert.equal(strip(formatDays(8)), shortDate(8));
|
||||
assert.equal(strip(formatDays(10)), shortDateYear(10));
|
||||
});
|
||||
|
||||
QUnit.test("formating tiny dates", assert => {
|
||||
var shortDateYear = function(days){
|
||||
return moment().subtract(days, 'days').format("MMM 'YY");
|
||||
var shortDateYear = function(days) {
|
||||
return moment()
|
||||
.subtract(days, "days")
|
||||
.format("MMM 'YY");
|
||||
};
|
||||
|
||||
format = "tiny";
|
||||
@ -103,7 +116,7 @@ QUnit.test("formating tiny dates", assert => {
|
||||
assert.equal(formatDays(365), shortDate(365));
|
||||
assert.equal(formatDays(366), shortDateYear(366)); // leap year
|
||||
assert.equal(formatDays(500), shortDateYear(500));
|
||||
assert.equal(formatDays(365*2 + 1), shortDateYear(365*2 + 1)); // one leap year
|
||||
assert.equal(formatDays(365 * 2 + 1), shortDateYear(365 * 2 + 1)); // one leap year
|
||||
|
||||
var originalValue = Discourse.SiteSettings.relative_date_duration;
|
||||
Discourse.SiteSettings.relative_date_duration = 7;
|
||||
@ -124,14 +137,14 @@ QUnit.test("formating tiny dates", assert => {
|
||||
assert.equal(formatDays(366), shortDateYear(366));
|
||||
|
||||
Discourse.SiteSettings.relative_date_duration = null;
|
||||
assert.equal(formatDays(1), '1d');
|
||||
assert.equal(formatDays(14), '14d');
|
||||
assert.equal(formatDays(1), "1d");
|
||||
assert.equal(formatDays(14), "14d");
|
||||
assert.equal(formatDays(15), shortDate(15));
|
||||
|
||||
Discourse.SiteSettings.relative_date_duration = 14;
|
||||
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers(new Date(2012,0,12,12,0).getTime()); // Jan 12, 2012
|
||||
clock = sinon.useFakeTimers(new Date(2012, 0, 12, 12, 0).getTime()); // Jan 12, 2012
|
||||
|
||||
assert.equal(formatDays(11), "11d");
|
||||
assert.equal(formatDays(14), "14d");
|
||||
@ -139,7 +152,7 @@ QUnit.test("formating tiny dates", assert => {
|
||||
assert.equal(formatDays(366), shortDateYear(366));
|
||||
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers(new Date(2012,0,20,12,0).getTime()); // Jan 20, 2012
|
||||
clock = sinon.useFakeTimers(new Date(2012, 0, 20, 12, 0).getTime()); // Jan 20, 2012
|
||||
|
||||
assert.equal(formatDays(14), "14d");
|
||||
assert.equal(formatDays(15), shortDate(15));
|
||||
@ -149,51 +162,59 @@ QUnit.test("formating tiny dates", assert => {
|
||||
});
|
||||
|
||||
QUnit.test("autoUpdatingRelativeAge", assert => {
|
||||
var d = moment().subtract(1, 'day').toDate();
|
||||
var d = moment()
|
||||
.subtract(1, "day")
|
||||
.toDate();
|
||||
|
||||
var $elem = $(autoUpdatingRelativeAge(d));
|
||||
assert.equal($elem.data('format'), "tiny");
|
||||
assert.equal($elem.data('time'), d.getTime());
|
||||
assert.equal($elem.attr('title'), undefined);
|
||||
assert.equal($elem.data("format"), "tiny");
|
||||
assert.equal($elem.data("time"), d.getTime());
|
||||
assert.equal($elem.attr("title"), undefined);
|
||||
|
||||
$elem = $(autoUpdatingRelativeAge(d, {title: true}));
|
||||
assert.equal($elem.attr('title'), longDate(d));
|
||||
$elem = $(autoUpdatingRelativeAge(d, { title: true }));
|
||||
assert.equal($elem.attr("title"), longDate(d));
|
||||
|
||||
$elem = $(autoUpdatingRelativeAge(d,{format: 'medium', title: true, leaveAgo: true}));
|
||||
assert.equal($elem.data('format'), "medium-with-ago");
|
||||
assert.equal($elem.data('time'), d.getTime());
|
||||
assert.equal($elem.attr('title'), longDate(d));
|
||||
assert.equal($elem.html(), '1 day ago');
|
||||
$elem = $(
|
||||
autoUpdatingRelativeAge(d, {
|
||||
format: "medium",
|
||||
title: true,
|
||||
leaveAgo: true
|
||||
})
|
||||
);
|
||||
assert.equal($elem.data("format"), "medium-with-ago");
|
||||
assert.equal($elem.data("time"), d.getTime());
|
||||
assert.equal($elem.attr("title"), longDate(d));
|
||||
assert.equal($elem.html(), "1 day ago");
|
||||
|
||||
$elem = $(autoUpdatingRelativeAge(d,{format: 'medium'}));
|
||||
assert.equal($elem.data('format'), "medium");
|
||||
assert.equal($elem.data('time'), d.getTime());
|
||||
assert.equal($elem.attr('title'), undefined);
|
||||
assert.equal($elem.html(), '1 day');
|
||||
$elem = $(autoUpdatingRelativeAge(d, { format: "medium" }));
|
||||
assert.equal($elem.data("format"), "medium");
|
||||
assert.equal($elem.data("time"), d.getTime());
|
||||
assert.equal($elem.attr("title"), undefined);
|
||||
assert.equal($elem.html(), "1 day");
|
||||
});
|
||||
|
||||
QUnit.test("updateRelativeAge", assert =>{
|
||||
|
||||
QUnit.test("updateRelativeAge", assert => {
|
||||
var d = new Date();
|
||||
var $elem = $(autoUpdatingRelativeAge(d));
|
||||
$elem.data('time', d.getTime() - 2 * 60 * 1000);
|
||||
$elem.data("time", d.getTime() - 2 * 60 * 1000);
|
||||
|
||||
updateRelativeAge($elem);
|
||||
|
||||
assert.equal($elem.html(), "2m");
|
||||
|
||||
d = new Date();
|
||||
$elem = $(autoUpdatingRelativeAge(d, {format: 'medium', leaveAgo: true}));
|
||||
$elem.data('time', d.getTime() - 2 * 60 * 1000);
|
||||
$elem = $(autoUpdatingRelativeAge(d, { format: "medium", leaveAgo: true }));
|
||||
$elem.data("time", d.getTime() - 2 * 60 * 1000);
|
||||
|
||||
updateRelativeAge($elem);
|
||||
|
||||
assert.equal($elem.html(), "2 mins ago");
|
||||
});
|
||||
|
||||
QUnit.test("breakUp", assert =>{
|
||||
|
||||
var b = function(s,hint){ return breakUp(s,hint); };
|
||||
QUnit.test("breakUp", assert => {
|
||||
var b = function(s, hint) {
|
||||
return breakUp(s, hint);
|
||||
};
|
||||
|
||||
assert.equal(b("hello"), "hello");
|
||||
assert.equal(b("helloworld"), "helloworld");
|
||||
@ -201,8 +222,10 @@ QUnit.test("breakUp", assert =>{
|
||||
assert.equal(b("he_man"), "he_<wbr>​man");
|
||||
assert.equal(b("he11111"), "he<wbr>​11111");
|
||||
assert.equal(b("HRCBob"), "HRC<wbr>​Bob");
|
||||
assert.equal(b("bobmarleytoo","Bob Marley Too"), "bob<wbr>​marley<wbr>​too");
|
||||
|
||||
assert.equal(
|
||||
b("bobmarleytoo", "Bob Marley Too"),
|
||||
"bob<wbr>​marley<wbr>​too"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("number", assert => {
|
||||
@ -214,32 +237,56 @@ QUnit.test("number", assert => {
|
||||
assert.equal(number("2499999.5"), "2.5M", "it abbreviates millions");
|
||||
assert.equal(number(1000000), "1.0M", "it abbreviates a million");
|
||||
assert.equal(number(999999), "999k", "it abbreviates hundreds of thousands");
|
||||
assert.equal(number(18.2), "18", "it returns a float number rounded to an integer as a string");
|
||||
assert.equal(number(18.6), "19", "it returns a float number rounded to an integer as a string");
|
||||
assert.equal(number("12.3"), "12", "it returns a string float rounded to an integer as a string");
|
||||
assert.equal(number("12.6"), "13", "it returns a string float rounded to an integer as a string");
|
||||
assert.equal(
|
||||
number(18.2),
|
||||
"18",
|
||||
"it returns a float number rounded to an integer as a string"
|
||||
);
|
||||
assert.equal(
|
||||
number(18.6),
|
||||
"19",
|
||||
"it returns a float number rounded to an integer as a string"
|
||||
);
|
||||
assert.equal(
|
||||
number("12.3"),
|
||||
"12",
|
||||
"it returns a string float rounded to an integer as a string"
|
||||
);
|
||||
assert.equal(
|
||||
number("12.6"),
|
||||
"13",
|
||||
"it returns a string float rounded to an integer as a string"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test("durationTiny", assert => {
|
||||
assert.equal(durationTiny(), '—', "undefined is a dash");
|
||||
assert.equal(durationTiny(null), '—', "null is a dash");
|
||||
assert.equal(durationTiny(0), '< 1m', "0 seconds shows as < 1m");
|
||||
assert.equal(durationTiny(59), '< 1m', "59 seconds shows as < 1m");
|
||||
assert.equal(durationTiny(60), '1m', "60 seconds shows as 1m");
|
||||
assert.equal(durationTiny(90), '2m', "90 seconds shows as 2m");
|
||||
assert.equal(durationTiny(120), '2m', "120 seconds shows as 2m");
|
||||
assert.equal(durationTiny(60 * 45), '1h', "45 minutes shows as 1h");
|
||||
assert.equal(durationTiny(60 * 60), '1h', "60 minutes shows as 1h");
|
||||
assert.equal(durationTiny(60 * 90), '2h', "90 minutes shows as 2h");
|
||||
assert.equal(durationTiny(3600 * 23), '23h', "23 hours shows as 23h");
|
||||
assert.equal(durationTiny(3600 * 24 - 29), '1d', "23 hours 31 mins shows as 1d");
|
||||
assert.equal(durationTiny(3600 * 24 * 89), '89d', "89 days shows as 89d");
|
||||
assert.equal(durationTiny(60 * (525600 - 1)), '12mon', "364 days shows as 12mon");
|
||||
assert.equal(durationTiny(60 * 525600), '1y', "365 days shows as 1y");
|
||||
assert.equal(durationTiny(86400 * 456), '1y', "456 days shows as 1y");
|
||||
assert.equal(durationTiny(86400 * 457), '> 1y', "457 days shows as > 1y");
|
||||
assert.equal(durationTiny(86400 * 638), '> 1y', "638 days shows as > 1y");
|
||||
assert.equal(durationTiny(86400 * 639), '2y', "639 days shows as 2y");
|
||||
assert.equal(durationTiny(86400 * 821), '2y', "821 days shows as 2y");
|
||||
assert.equal(durationTiny(86400 * 822), '> 2y', "822 days shows as > 2y");
|
||||
assert.equal(durationTiny(), "—", "undefined is a dash");
|
||||
assert.equal(durationTiny(null), "—", "null is a dash");
|
||||
assert.equal(durationTiny(0), "< 1m", "0 seconds shows as < 1m");
|
||||
assert.equal(durationTiny(59), "< 1m", "59 seconds shows as < 1m");
|
||||
assert.equal(durationTiny(60), "1m", "60 seconds shows as 1m");
|
||||
assert.equal(durationTiny(90), "2m", "90 seconds shows as 2m");
|
||||
assert.equal(durationTiny(120), "2m", "120 seconds shows as 2m");
|
||||
assert.equal(durationTiny(60 * 45), "1h", "45 minutes shows as 1h");
|
||||
assert.equal(durationTiny(60 * 60), "1h", "60 minutes shows as 1h");
|
||||
assert.equal(durationTiny(60 * 90), "2h", "90 minutes shows as 2h");
|
||||
assert.equal(durationTiny(3600 * 23), "23h", "23 hours shows as 23h");
|
||||
assert.equal(
|
||||
durationTiny(3600 * 24 - 29),
|
||||
"1d",
|
||||
"23 hours 31 mins shows as 1d"
|
||||
);
|
||||
assert.equal(durationTiny(3600 * 24 * 89), "89d", "89 days shows as 89d");
|
||||
assert.equal(
|
||||
durationTiny(60 * (525600 - 1)),
|
||||
"12mon",
|
||||
"364 days shows as 12mon"
|
||||
);
|
||||
assert.equal(durationTiny(60 * 525600), "1y", "365 days shows as 1y");
|
||||
assert.equal(durationTiny(86400 * 456), "1y", "456 days shows as 1y");
|
||||
assert.equal(durationTiny(86400 * 457), "> 1y", "457 days shows as > 1y");
|
||||
assert.equal(durationTiny(86400 * 638), "> 1y", "638 days shows as > 1y");
|
||||
assert.equal(durationTiny(86400 * 639), "2y", "639 days shows as 2y");
|
||||
assert.equal(durationTiny(86400 * 821), "2y", "821 days shows as 2y");
|
||||
assert.equal(durationTiny(86400 * 822), "> 2y", "822 days shows as > 2y");
|
||||
});
|
||||
|
Reference in New Issue
Block a user