@lowdefy/nunjucks 3.23.3 → 4.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dateFilter.js +41 -70
- package/dist/index.js +71 -0
- package/package.json +14 -13
- package/dist/nunjucks.js +0 -101
package/dist/dateFilter.js
CHANGED
|
@@ -1,20 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _moment = _interopRequireDefault(require("moment"));
|
|
9
|
-
|
|
10
|
-
var _nunjucks = _interopRequireDefault(require("nunjucks"));
|
|
11
|
-
|
|
12
|
-
var _helpers = require("@lowdefy/helpers");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
1
|
/*
|
|
17
|
-
Copyright 2020-
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
18
3
|
|
|
19
4
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
20
5
|
you may not use this file except in compliance with the License.
|
|
@@ -27,67 +12,53 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
27
12
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
28
13
|
See the License for the specific language governing permissions and
|
|
29
14
|
limitations under the License.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
/** DERIVED FROM
|
|
15
|
+
*/ /** DERIVED FROM
|
|
33
16
|
* nunjucks-date-filter-local
|
|
34
17
|
* Fork from https://github.com/piwi/nunjucks-date-filter
|
|
35
18
|
*
|
|
36
19
|
* Copyright (c) 2015 Pierre Cassat
|
|
37
20
|
* Licensed under the Apache 2.0 license.
|
|
38
|
-
*/
|
|
21
|
+
*/ import moment from 'moment';
|
|
22
|
+
import nunjucks from 'nunjucks';
|
|
23
|
+
import { type } from '@lowdefy/helpers';
|
|
39
24
|
// default default format (ISO 8601)
|
|
40
|
-
|
|
25
|
+
let dateFilterDefaultFormat = null;
|
|
26
|
+
// a date filter for Nunjucks
|
|
41
27
|
// usage: {{ my_date | date(format) }}
|
|
42
28
|
// see: <http://momentjs.com/docs/>
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return '';
|
|
48
|
-
} // allow for moment function chaining, but return "Invalid date" for objects and arrays.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if ((_helpers.type.isArray(date) || _helpers.type.isObject(date)) && !(date instanceof _moment.default)) {
|
|
52
|
-
return 'Invalid date';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
var result;
|
|
56
|
-
var errs = [];
|
|
57
|
-
var obj;
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
obj = (0, _moment.default)(date);
|
|
61
|
-
|
|
62
|
-
if (obj[format] && _helpers.type.isFunction(obj[format])) {
|
|
63
|
-
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
64
|
-
args[_key - 2] = arguments[_key];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
result = obj[format](...args);
|
|
68
|
-
} else {
|
|
69
|
-
result = obj.format(format || dateFilterDefaultFormat);
|
|
29
|
+
const dateFilter = (date, format, ...args)=>{
|
|
30
|
+
// for no date, return undefined.
|
|
31
|
+
if (type.isNone(date)) {
|
|
32
|
+
return '';
|
|
70
33
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
34
|
+
// allow for moment function chaining, but return "Invalid date" for objects and arrays.
|
|
35
|
+
if ((type.isArray(date) || type.isObject(date)) && !(date instanceof moment)) {
|
|
36
|
+
return 'Invalid date';
|
|
37
|
+
}
|
|
38
|
+
let result;
|
|
39
|
+
const errs = [];
|
|
40
|
+
let obj;
|
|
41
|
+
try {
|
|
42
|
+
obj = moment(date);
|
|
43
|
+
if (obj[format] && type.isFunction(obj[format])) {
|
|
44
|
+
result = obj[format](...args);
|
|
45
|
+
} else {
|
|
46
|
+
result = obj.format(format || dateFilterDefaultFormat);
|
|
47
|
+
}
|
|
48
|
+
} catch (err) {
|
|
49
|
+
errs.push(err);
|
|
50
|
+
}
|
|
51
|
+
if (errs.length) {
|
|
52
|
+
return errs.join('\n');
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
90
55
|
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
56
|
+
// set default format for date
|
|
57
|
+
dateFilter.setDefaultFormat = (format)=>{
|
|
58
|
+
dateFilterDefaultFormat = format;
|
|
59
|
+
};
|
|
60
|
+
// install the filter to nunjucks environment
|
|
61
|
+
dateFilter.install = (env, customName)=>{
|
|
62
|
+
(env || nunjucks.configure()).addFilter(customName || 'date', dateFilter);
|
|
63
|
+
};
|
|
64
|
+
export default dateFilter;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import nunjucks from 'nunjucks';
|
|
16
|
+
import { type } from '@lowdefy/helpers';
|
|
17
|
+
import dateFilter from './dateFilter.js';
|
|
18
|
+
// dateFilter.setDefaultFormat('YYYY-MM-DD');
|
|
19
|
+
export const nunjucksEnv = new nunjucks.Environment();
|
|
20
|
+
nunjucksEnv.addFilter('date', dateFilter);
|
|
21
|
+
const nunjucksTemplates = {};
|
|
22
|
+
// slow
|
|
23
|
+
export const nunjucksString = (templateString, value)=>{
|
|
24
|
+
if (type.isPrimitive(value)) {
|
|
25
|
+
return nunjucksEnv.renderString(templateString, {
|
|
26
|
+
value
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return nunjucksEnv.renderString(templateString, value);
|
|
30
|
+
};
|
|
31
|
+
export const validNunjucksString = (templateString, returnError = false)=>{
|
|
32
|
+
try {
|
|
33
|
+
nunjucksString(templateString, {});
|
|
34
|
+
return true;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
if (returnError) {
|
|
37
|
+
return {
|
|
38
|
+
name: e.name,
|
|
39
|
+
message: e.message
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
// fast
|
|
46
|
+
// test with memoization
|
|
47
|
+
// this method compiles a nunjucks string only if the client has not compiled the same string before.
|
|
48
|
+
export const nunjucksFunction = (templateString)=>{
|
|
49
|
+
// template was already compiled
|
|
50
|
+
if (type.isFunction(nunjucksTemplates[templateString])) {
|
|
51
|
+
return nunjucksTemplates[templateString];
|
|
52
|
+
}
|
|
53
|
+
if (type.isString(templateString)) {
|
|
54
|
+
const template = nunjucks.compile(templateString, nunjucksEnv);
|
|
55
|
+
// execute once to throw catch template errors
|
|
56
|
+
template.render({});
|
|
57
|
+
nunjucksTemplates[templateString] = (value)=>{
|
|
58
|
+
if (type.isPrimitive(value)) {
|
|
59
|
+
return template.render({
|
|
60
|
+
value
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return template.render(value);
|
|
64
|
+
};
|
|
65
|
+
} else {
|
|
66
|
+
// for non string types like booleans or objects
|
|
67
|
+
nunjucksTemplates[templateString] = ()=>templateString
|
|
68
|
+
;
|
|
69
|
+
}
|
|
70
|
+
return nunjucksTemplates[templateString];
|
|
71
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/nunjucks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha.11",
|
|
4
4
|
"licence": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -27,30 +27,31 @@
|
|
|
27
27
|
"type": "git",
|
|
28
28
|
"url": "https://github.com/lowdefy/lowdefy.git"
|
|
29
29
|
},
|
|
30
|
-
"
|
|
30
|
+
"type": "module",
|
|
31
|
+
"exports": "./dist/index.js",
|
|
31
32
|
"files": [
|
|
32
33
|
"dist/*"
|
|
33
34
|
],
|
|
34
35
|
"scripts": {
|
|
35
|
-
"build": "
|
|
36
|
+
"build": "yarn swc",
|
|
36
37
|
"clean": "rm -rf dist",
|
|
37
|
-
"
|
|
38
|
-
"
|
|
38
|
+
"prepare": "yarn build",
|
|
39
|
+
"swc": "swc src --out-dir dist --config-file ../../../.swcrc --delete-dir-on-start",
|
|
40
|
+
"test": "jest --coverage"
|
|
39
41
|
},
|
|
40
42
|
"dependencies": {
|
|
41
|
-
"@lowdefy/helpers": "
|
|
42
|
-
"moment": "2.29.
|
|
43
|
+
"@lowdefy/helpers": "4.0.0-alpha.11",
|
|
44
|
+
"moment": "2.29.3",
|
|
43
45
|
"nunjucks": "3.2.3"
|
|
44
46
|
},
|
|
45
47
|
"devDependencies": {
|
|
46
|
-
"@
|
|
47
|
-
"@
|
|
48
|
-
"@
|
|
49
|
-
"
|
|
50
|
-
"jest": "26.6.3"
|
|
48
|
+
"@swc/cli": "0.1.55",
|
|
49
|
+
"@swc/core": "1.2.135",
|
|
50
|
+
"@swc/jest": "0.2.17",
|
|
51
|
+
"jest": "27.5.1"
|
|
51
52
|
},
|
|
52
53
|
"publishConfig": {
|
|
53
54
|
"access": "public"
|
|
54
55
|
},
|
|
55
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "810fb2d8cb9ee8b0586b55fbbf26a5a5a0da3b2a"
|
|
56
57
|
}
|
package/dist/nunjucks.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.nunjucksFunction = exports.validNunjucksString = exports.nunjucksString = exports.nunjucksEnv = void 0;
|
|
7
|
-
|
|
8
|
-
var _nunjucks = _interopRequireDefault(require("nunjucks"));
|
|
9
|
-
|
|
10
|
-
var _helpers = require("@lowdefy/helpers");
|
|
11
|
-
|
|
12
|
-
var _dateFilter = _interopRequireDefault(require("./dateFilter"));
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
/*
|
|
17
|
-
Copyright 2020-2021 Lowdefy, Inc
|
|
18
|
-
|
|
19
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
20
|
-
you may not use this file except in compliance with the License.
|
|
21
|
-
You may obtain a copy of the License at
|
|
22
|
-
|
|
23
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
24
|
-
|
|
25
|
-
Unless required by applicable law or agreed to in writing, software
|
|
26
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
27
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
28
|
-
See the License for the specific language governing permissions and
|
|
29
|
-
limitations under the License.
|
|
30
|
-
*/
|
|
31
|
-
// dateFilter.setDefaultFormat('YYYY-MM-DD');
|
|
32
|
-
var nunjucksEnv = new _nunjucks.default.Environment();
|
|
33
|
-
exports.nunjucksEnv = nunjucksEnv;
|
|
34
|
-
nunjucksEnv.addFilter('date', _dateFilter.default);
|
|
35
|
-
var nunjucksTemplates = {}; // slow
|
|
36
|
-
|
|
37
|
-
var nunjucksString = (templateString, value) => {
|
|
38
|
-
if (_helpers.type.isPrimitive(value)) {
|
|
39
|
-
return nunjucksEnv.renderString(templateString, {
|
|
40
|
-
value
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return nunjucksEnv.renderString(templateString, value);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
exports.nunjucksString = nunjucksString;
|
|
48
|
-
|
|
49
|
-
var validNunjucksString = function validNunjucksString(templateString) {
|
|
50
|
-
var returnError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
nunjucksString(templateString, {});
|
|
54
|
-
return true;
|
|
55
|
-
} catch (e) {
|
|
56
|
-
if (returnError) {
|
|
57
|
-
return {
|
|
58
|
-
name: e.name,
|
|
59
|
-
message: e.message
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
}; // fast
|
|
66
|
-
// test with memoization
|
|
67
|
-
// this method compiles a nunjucks string only if the client has not compiled the same string before.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
exports.validNunjucksString = validNunjucksString;
|
|
71
|
-
|
|
72
|
-
var nunjucksFunction = templateString => {
|
|
73
|
-
// template was already compiled
|
|
74
|
-
if (_helpers.type.isFunction(nunjucksTemplates[templateString])) {
|
|
75
|
-
return nunjucksTemplates[templateString];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (_helpers.type.isString(templateString)) {
|
|
79
|
-
var template = _nunjucks.default.compile(templateString, nunjucksEnv); // execute once to throw catch template errors
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
template.render({});
|
|
83
|
-
|
|
84
|
-
nunjucksTemplates[templateString] = value => {
|
|
85
|
-
if (_helpers.type.isPrimitive(value)) {
|
|
86
|
-
return template.render({
|
|
87
|
-
value
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return template.render(value);
|
|
92
|
-
};
|
|
93
|
-
} else {
|
|
94
|
-
// for non string types like booleans or objects
|
|
95
|
-
nunjucksTemplates[templateString] = () => templateString;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return nunjucksTemplates[templateString];
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
exports.nunjucksFunction = nunjucksFunction;
|