ha-nunjucks 1.2.3 → 1.2.4-alpha.2

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/README.md CHANGED
@@ -52,7 +52,7 @@ const renderedString = renderTemplate(this.hass, templateString, context);
52
52
 
53
53
  `renderTemplate` will return a string unless the result is `true` or `false` (*not* case sensitive), in which case it will return a boolean.
54
54
 
55
- When the return type is expected to be a number, end users should cast these values using the nunjucks `int` or `float` filters to prevent undesired behavior caused by JavaScript forcing operations between disparate variable types. Numbers are not returned by default to prevent leading and zeroes from being truncated from numerical strings.
55
+ When the return type is expected to be a number, end users should cast these values using the nunjucks `int` or `float` filters to prevent undesired behavior caused by JavaScript forcing operations between disparate variable types. Numbers are not returned by default to prevent leading and trailing zeroes from being truncated from numerical strings.
56
56
 
57
57
  `renderTemplate` will return an empty string for strings that may have been cast from nullish non-numerical values, such as `undefined`, `null`, and `None` (case sensitive).
58
58
 
package/dist/index.d.ts CHANGED
@@ -1 +1,9 @@
1
- export * from './renderTemplate';
1
+ import { HomeAssistant } from 'custom-card-helpers';
2
+ /**
3
+ * Render a Home Assistant template string using nunjucks
4
+ * @param {HomeAssistant} hass The Home Assistant object
5
+ * @param {string} str The template string to render
6
+ * @param {object} [context] Additional context to expose to nunjucks
7
+ * @returns {string | boolean} The rendered template string if a string was provided, otherwise the unaltered input
8
+ */
9
+ export declare function renderTemplate(hass: HomeAssistant, str: string, context?: object): string | boolean;
package/dist/index.js CHANGED
@@ -1,17 +1,30 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./renderTemplate"), exports);
3
+ exports.renderTemplate = void 0;
4
+ const nunjucks_1 = require("nunjucks");
5
+ const context_1 = require("./context");
6
+ /**
7
+ * Render a Home Assistant template string using nunjucks
8
+ * @param {HomeAssistant} hass The Home Assistant object
9
+ * @param {string} str The template string to render
10
+ * @param {object} [context] Additional context to expose to nunjucks
11
+ * @returns {string | boolean} The rendered template string if a string was provided, otherwise the unaltered input
12
+ */
13
+ function renderTemplate(hass, str, context) {
14
+ if (typeof str == 'string' &&
15
+ ((str.includes('{{') && str.includes('}}')) ||
16
+ (str.includes('{%') && str.includes('%}')))) {
17
+ str = (0, nunjucks_1.renderString)(structuredClone(str), Object.assign(Object.assign({}, (0, context_1.CONTEXT)(hass)), context)).trim();
18
+ if ([undefined, null, 'undefined', 'null', 'None'].includes(str)) {
19
+ return '';
20
+ }
21
+ if (str.toLowerCase() == 'true') {
22
+ return true;
23
+ }
24
+ if (str.toLowerCase() == 'false') {
25
+ return false;
26
+ }
27
+ }
28
+ return str;
29
+ }
30
+ exports.renderTemplate = renderTemplate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ha-nunjucks",
3
- "version": "1.2.3",
3
+ "version": "1.2.4-alpha.2",
4
4
  "description": "Wrapper for nunjucks for use with Home Assistant frontend custom components to render templates",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -31,20 +31,20 @@
31
31
  "README.md"
32
32
  ],
33
33
  "dependencies": {
34
- "@types/nunjucks": "^3.2.6",
35
- "custom-card-helpers": "^1.9.0",
36
- "nunjucks": "^3.2.4"
34
+ "@types/nunjucks": "latest",
35
+ "custom-card-helpers": "latest",
36
+ "nunjucks": "latest"
37
37
  },
38
38
  "devDependencies": {
39
- "@types/jest": "^29.5.10",
40
- "@typescript-eslint/eslint-plugin": "^6.13.2",
41
- "@typescript-eslint/parser": "^6.13.2",
42
- "eslint": "^8.55.0",
43
- "husky": "^8.0.3",
44
- "prettier": "^3.1.0",
45
- "pretty-quick": "^3.1.3",
46
- "ts-jest": "^29.1.1",
47
- "ts-loader": "^9.5.1",
48
- "typescript": "^5.3.2"
39
+ "@types/jest": "latest",
40
+ "@typescript-eslint/eslint-plugin": "latest",
41
+ "@typescript-eslint/parser": "latest",
42
+ "eslint": "latest",
43
+ "husky": "latest",
44
+ "prettier": "latest",
45
+ "pretty-quick": "latest",
46
+ "ts-jest": "latest",
47
+ "ts-loader": "latest",
48
+ "typescript": "latest"
49
49
  }
50
50
  }