-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwig_extension.js
56 lines (43 loc) · 2.02 KB
/
twig_extension.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
47
48
49
50
51
52
53
54
55
56
const Twig = require('twig');
const fixtures = require(`./src/fixtures.json`);
function htmlNumberFormat(number, unit, precision) {
if (precision == null && typeof number === 'string') {
const position = number.lastIndexOf('.');
if (position !== -1) {
precision = number.length - position - 1;
}
}
if (precision != null) {
number = Number(number).toFixed(precision);
}
let [string, decimals] = number.toString().split('.');
if (!decimals) {
decimals = '';
}
const decimalsLength = decimals.length;
const isZerofill = Number(decimals) == 0;
decimals = decimals.replace(/0+$/, '<span class="number-format__zero-padding">$&</span>');
return `<span class="number-format">${string.replace(/\B(?=(\d{3})+(?!\d))/g, '<span class="number-format__thousands-separator"></span>')}<span class="number-format__decimals ${isZerofill ? 'number-format__decimals--zerofill' : ''}" data-decimals-length="${decimalsLength}"><span class="number-format__decimals-separator">${decimals ? '.' : ''}</span>${decimals}</span>${unit ? `<span class="number-format__unit">${unit}</span>` : ''}</span>`;
}
function slug(text) {
let from = 'ąàáäâãåæăćčĉďęèéëêěĝĥìíïîĵłľńňòóöőôõðøřśșşšŝťțţŭùúüűûůñÿýçżźž';
let to = 'aaaaaaaaacccdeeeeeeghiiiijllnnoooooooorssssstttuuuuuuunyyczzz';
from += from.toUpperCase();
from += 'ß';
to += to.toUpperCase();
to = to.split('');
to.push('ss');
let slug = text.toLowerCase();
slug = slug.replace(/.{1}/g, (character) => {
const index = from.indexOf(character);
return index === -1 ? character : to[index];
});
slug = slug.replace(/[^a-z0-9]+/g, '-');
slug = slug.replace(/^\-+|\-+$/g, '');
return slug;
}
exports.default = () => {
Twig.extendFilter('html_number_format', (value, args) => htmlNumberFormat(value, ...args));
Twig.extendFunction('slug', slug);
Twig.extendFunction('fixtures', () => fixtures);
};