-
Notifications
You must be signed in to change notification settings - Fork 7
/
filters.js
46 lines (33 loc) · 1.22 KB
/
filters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const CleanCSS = require("clean-css");
const { DateTime } = require('luxon');
module.exports = {
// Minify CSS: https://github.com/jakubpawlowicz/clean-css
cssmin: code => new CleanCSS({
level: {
1: { specialComments: "0" },
2: { mergeMedia: true }
}
}).minify(code).styles,
dateFormat: function (date, format) {
return DateTime.fromJSDate(date, { zone: 'utc' }).toFormat(
String(format)
);
},
dateISO: date => DateTime.fromJSDate(date, { zone: 'utc' }).toISO({
includeOffset: false,
suppressMilliseconds: true
}),
// Filter out all elements from a list (eg: products) that has a 'hidden: true' property in it
hiddenFilter: list => list.filter(item => item.hidden ? false : true),
// Filter out all elements from a list (eg: products) that has a 'disabled: true' property in it
disabledFilter: list => list.filter(item => item.disabled ? false : true),
// Useful to change property of an object in the Nunjucks 'set' method which is fairly limited
mergeObjectFilter: (obj1, obj2) => { return { ...obj1, ...obj2 } },
// Reduce length of a large Array
trimArray: (array, max_length) => {
if (array && array.length && array.length > max_length) {
array.length = max_length;
}
return array;
},
}