@emulsify/core 4.0.0 → 4.0.1
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/package.json
CHANGED
|
@@ -13,6 +13,47 @@ import { getTwigTagDefinitions } from './tag-map.js';
|
|
|
13
13
|
*/
|
|
14
14
|
const registeredTwigInstances = new WeakSet();
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* PHP-style boolean conversion used by Twig.js truthiness checks.
|
|
18
|
+
*
|
|
19
|
+
* Twig.js normally provides this through `Twig.lib.boolval`, but dependency
|
|
20
|
+
* optimizer interop can drop the helper while leaving the rest of Twig usable.
|
|
21
|
+
*
|
|
22
|
+
* @param {*} value - Value to coerce using PHP/Twig truthiness rules.
|
|
23
|
+
* @returns {boolean} TRUE when the value should be truthy in Twig.
|
|
24
|
+
*/
|
|
25
|
+
function phpBoolval(value) {
|
|
26
|
+
return (
|
|
27
|
+
value !== false &&
|
|
28
|
+
value !== 0 &&
|
|
29
|
+
value !== '' &&
|
|
30
|
+
value !== '0' &&
|
|
31
|
+
!(Array.isArray(value) && value.length === 0) &&
|
|
32
|
+
value !== null &&
|
|
33
|
+
typeof value !== 'undefined'
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Ensure Twig.js has the internal boolean helper required by conditionals.
|
|
39
|
+
*
|
|
40
|
+
* @param {Object} Twig - Twig.js module or compatible extension target.
|
|
41
|
+
* @returns {Object} The same Twig instance after compatibility patching.
|
|
42
|
+
*/
|
|
43
|
+
function ensureTwigBoolval(Twig) {
|
|
44
|
+
Twig.extend((InternalTwig) => {
|
|
45
|
+
if (!InternalTwig.lib) {
|
|
46
|
+
InternalTwig.lib = {};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (typeof InternalTwig.lib.boolval !== 'function') {
|
|
50
|
+
InternalTwig.lib.boolval = phpBoolval;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return Twig;
|
|
55
|
+
}
|
|
56
|
+
|
|
16
57
|
/**
|
|
17
58
|
* Register native Emulsify Twig functions and logic tags with Twig.js.
|
|
18
59
|
*
|
|
@@ -32,6 +73,8 @@ export function registerTwigExtensions(Twig) {
|
|
|
32
73
|
);
|
|
33
74
|
}
|
|
34
75
|
|
|
76
|
+
ensureTwigBoolval(Twig);
|
|
77
|
+
|
|
35
78
|
if (registeredTwigInstances.has(Twig)) {
|
|
36
79
|
return Twig;
|
|
37
80
|
}
|