-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
164 lines (133 loc) · 4.9 KB
/
helpers.js
File metadata and controls
164 lines (133 loc) · 4.9 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const Handlebars = require('handlebars');
let registered = false;
function registerDefaultHelpers() {
if (registered) return;
Handlebars.registerHelper('uppercase', function (str = '') {
return String(str).toUpperCase();
});
Handlebars.registerHelper('lowercase', function (str = '') {
return String(str).toLowerCase();
});
Handlebars.registerHelper('json', function (context) {
return new Handlebars.SafeString(JSON.stringify(context));
});
Handlebars.registerHelper('dateFormat', function (value, locale = 'en-US', options = {}) {
if (!value) return '';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
return new Intl.DateTimeFormat(locale, Object.keys(options || {}).length ? options : {
year: 'numeric',
month: '2-digit',
day: '2-digit'
}).format(date);
});
Handlebars.registerHelper('default', function (value, fallback) {
if (value === undefined || value === null || value === '') {
return fallback;
}
return value;
});
Handlebars.registerHelper('math', function (lvalue, operator, rvalue) {
const left = parseFloat(lvalue);
const right = parseFloat(rvalue);
switch (operator) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '/': return right === 0 ? null : left / right;
default: return null;
}
});
// 基础数学运算辅助函数
Handlebars.registerHelper('add', function (a, b) {
return parseFloat(a) + parseFloat(b);
});
Handlebars.registerHelper('subtract', function (a, b) {
return parseFloat(a) - parseFloat(b);
});
Handlebars.registerHelper('multiply', function (a, b) {
return parseFloat(a) * parseFloat(b);
});
Handlebars.registerHelper('divide', function (a, b) {
const divisor = parseFloat(b);
return divisor === 0 ? null : parseFloat(a) / divisor;
});
// 字符串处理
Handlebars.registerHelper('concat', function (...args) {
// 最后一个参数是 Handlebars options,需要移除
const values = args.slice(0, -1);
return values.join('');
});
Handlebars.registerHelper('trim', function (str) {
return String(str || '').trim();
});
Handlebars.registerHelper('replace', function (str, search, replacement) {
return String(str || '').replace(new RegExp(search, 'g'), replacement);
});
Handlebars.registerHelper('substring', function (str, start, end) {
return String(str || '').substring(start, end);
});
// 条件判断
Handlebars.registerHelper('eq', function (a, b) {
return a === b;
});
Handlebars.registerHelper('ne', function (a, b) {
return a !== b;
});
Handlebars.registerHelper('gt', function (a, b) {
return a > b;
});
Handlebars.registerHelper('gte', function (a, b) {
return a >= b;
});
Handlebars.registerHelper('lt', function (a, b) {
return a < b;
});
Handlebars.registerHelper('lte', function (a, b) {
return a <= b;
});
// 日期处理
Handlebars.registerHelper('formatDate', function (value, format) {
if (!value) return '';
// 支持 YYYYMMDD 格式
let date;
if (typeof value === 'string' && /^\d{8}$/.test(value)) {
const year = value.substring(0, 4);
const month = value.substring(4, 6);
const day = value.substring(6, 8);
date = new Date(`${year}-${month}-${day}`);
} else {
date = new Date(value);
}
if (Number.isNaN(date.getTime())) return value;
// 简单的格式化
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
if (format === 'YYYY-MM-DD') {
return `${year}-${month}-${day}`;
} else if (format === 'YYYY/MM/DD') {
return `${year}/${month}/${day}`;
} else if (format === 'YYYYMMDD') {
return `${year}${month}${day}`;
}
return date.toISOString().split('T')[0];
});
Handlebars.registerHelper('now', function (format) {
const date = new Date();
if (!format) return date.toISOString();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
if (format === 'YYYY-MM-DD') {
return `${year}-${month}-${day}`;
} else if (format === 'YYYYMMDD') {
return `${year}${month}${day}`;
}
return date.toISOString();
});
registered = true;
}
module.exports = {
registerDefaultHelpers
};