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

Add formatted date calculations support #18

Merged
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
7 changes: 6 additions & 1 deletion filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ var filters = {
url_encode: encodeURIComponent
};

const CF_DATE_FORMAT = "YYYY-MM-DD"
const ISO_STRING_FORMAT = "YYYY-MM-DDTHH:mm:ss.SSSZ"

function escape(str) {
return stringify(str).replace(/&|<|>|"|'/g, m => escapeMap[m]);
}
Expand Down Expand Up @@ -196,7 +199,9 @@ function add(v, arg) {
}

function isValidDateString(dateString) {
return typeof dateString === "string" && moment(dateString, "YYYY-MM-DDTHH:mm:ss.SSSZ", true).isValid();
return typeof dateString === "string" &&
(moment(dateString, moment.ISO_8601, true).isValid() ||
moment(dateString, CF_DATE_FORMAT, true).isValid());
}

function isValidNumber(argument) {
Expand Down
60 changes: 59 additions & 1 deletion test/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const moment = require("moment");
var ctx = {
date: new Date(),
date_string: new Date().toISOString(),
formatted_date_string: moment().format("YYYY-MM-DD"),
foo: 'bar',
arr: [-2, 'a'],
obj: {
Expand All @@ -32,7 +33,9 @@ var ctx = {
from_date: new Date("January 1, 2020"),
to_date: new Date("March 1, 2020"),
from_date_string: new Date("January 1, 2020").toISOString(),
to_date_string: new Date("March 1, 2020").toISOString()
to_date_string: new Date("March 1, 2020").toISOString(),
from_date_formatted_string: moment(new Date("January 1, 2020")).format("YYYY-MM-DD"),
to_date_formatted_string: moment(new Date("March 1, 2020")).format("YYYY-MM-DD")
}

function test (src, dst) {
Expand Down Expand Up @@ -256,7 +259,48 @@ describe('filters', function () {
console.error(e.message)
}
})
it('should return {"type":"days","value":6,"days": 6} for date and formatted-string', () => {
try {
const dst = {type: "DAYS", value: 60, days: 60};
return test('{% assign duration = to_date | minus: from_date_formatted_string %}{{duration}}', JSON.stringify(dst))
} catch(e) {
console.error(e.message)
}
})
it('should return {"type":"days","value":6,"days": 6} for ISOstring and formatted-string', () => {
try {
const dst = {type: "DAYS", value: 60, days: 60};
return test('{% assign duration = to_date_string | minus: from_date_formatted_string %}{{duration}}', JSON.stringify(dst))
} catch(e) {
console.error(e.message)
}
})
it('should return {"type":"days","value":6,"days": 6} for formatted-string and formatted-string', () => {
try {
const dst = {type: "DAYS", value: 60, days: 60};
return test('{% assign duration = to_date_formatted_string | minus: from_date_formatted_string %}{{duration}}', JSON.stringify(dst))
} catch(e) {
console.error(e.message)
}
})
it('should return {"type":"days","value":6,"days": 6} for formatted-string and date', () => {
try {
const dst = {type: "DAYS", value: 60, days: 60};
return test('{% assign duration = to_date_formatted_string | minus: from_date %}{{duration}}', JSON.stringify(dst))
} catch(e) {
console.error(e.message)
}
})
it('should return {"type":"days","value":6,"days": 6} for formatted-string and ISOstring', () => {
try {
const dst = {type: "DAYS", value: 60, days: 60};
return test('{% assign duration = to_date_formatted_string | minus: from_date_string %}{{duration}}', JSON.stringify(dst))
} catch(e) {
console.error(e.message)
}
})
/* Tests for date minus duration */
/* Tests for ISO string */
it('should minus 3 years to current date when date in string', () => {
const dst = new Date(moment(new Date()).subtract(3, "years")).toDateString()
return test('{{ date_string | minus: duration_3_years | date: "%a %b %d %Y"}}', dst)
Expand All @@ -269,6 +313,7 @@ describe('filters', function () {
const dst = new Date(moment(new Date()).subtract(20, "days")).toDateString()
return test('{{ date_string | minus: duration_20_days | date: "%a %b %d %Y"}}', dst)
})
/* Tests for date */
it('should minus 3 years to current date', () => {
const dst = new Date(moment(new Date()).subtract(3, "years")).toDateString()
return test('{{ date | minus: duration_3_years | date: "%a %b %d %Y"}}', dst)
Expand All @@ -281,6 +326,19 @@ describe('filters', function () {
const dst = new Date(moment(new Date()).subtract(20, "days")).toDateString()
return test('{{ date | minus: duration_20_days | date: "%a %b %d %Y"}}', dst)
})
/* Tests for formatted strings */
it('should minus 3 years to current date when date is formatted string', () => {
const dst = new Date(moment(new Date()).subtract(3, "years")).toDateString()
return test('{{ formatted_date_string | minus: duration_3_years | date: "%a %b %d %Y"}}', dst)
})
it('should subtract 2 months to current date when date is formatted string', () => {
const dst = new Date(moment(new Date()).subtract(2, "months")).toDateString()
return test('{{ formatted_date_string | minus: duration_2_months | date: "%a %b %d %Y"}}', dst)
})
it('should subtract 20 days to current date when date is formatted string', () => {
const dst = new Date(moment(new Date()).subtract(20, "days")).toDateString()
return test('{{ formatted_date_string | minus: duration_20_days | date: "%a %b %d %Y"}}', dst)
})
})

describe('modulo', function () {
Expand Down