node-powertools
Advanced tools
Comparing version
@@ -18,4 +18,8 @@ # CHANGELOG | ||
--- | ||
## [2.0.0] - 2025-01-04 | ||
### Breaking | ||
- Option in `.template()` to specify whether to escape html or not, which now defaults to `true`. | ||
## [1.0.0] - 2024-06-19 | ||
### Added | ||
- Initial release of the project 🚀 |
@@ -307,3 +307,21 @@ (function (root, factory) { | ||
// Replace instances of the settings in the input string | ||
Powertools.template = function (input, settings) { | ||
// Powertools.template = function (input, settings) { | ||
// if (typeof input !== 'string') { | ||
// throw new Error('No string provided'); | ||
// } | ||
// return input.replace(/\{\s*([\w\s\.]*)\s*\}/g, function (match, key) { | ||
// var trimmed = key.trim(); | ||
// var value = getNestedValue(settings, trimmed); | ||
// // If object, return JSON | ||
// if (typeof value === 'object') { | ||
// return JSON.stringify(value); | ||
// } else { | ||
// return value !== undefined ? value : match; | ||
// } | ||
// }); | ||
// }; | ||
Powertools.template = function (input, settings, options) { | ||
// Ensure input is a string | ||
if (typeof input !== 'string') { | ||
@@ -313,2 +331,7 @@ throw new Error('No string provided'); | ||
// Default options | ||
options = options || {}; | ||
options.escape = typeof options.escape === 'undefined' ? true : options.escape; | ||
// Replace the settings in the input string | ||
return input.replace(/\{\s*([\w\s\.]*)\s*\}/g, function (match, key) { | ||
@@ -318,8 +341,14 @@ var trimmed = key.trim(); | ||
// Escape HTML | ||
if (options.escape && typeof value === 'string') { | ||
value = escapeHTML(value); | ||
} | ||
// If object, return JSON | ||
if (typeof value === 'object') { | ||
return JSON.stringify(value); | ||
} else { | ||
return value !== undefined ? value : match; | ||
value = JSON.stringify(value); | ||
} | ||
// Return the value or the match | ||
return value !== undefined ? value : match; | ||
}); | ||
@@ -490,13 +519,13 @@ }; | ||
// Build the result object | ||
const result = {}; | ||
var result = {}; | ||
// Loop through the keys | ||
keys.forEach((key) => { | ||
const keyParts = key.split('.'); | ||
let currentObj = obj; | ||
let currentResult = result; | ||
keys.forEach(function (key) { | ||
var keyParts = key.split('.'); | ||
var currentObj = obj; | ||
var currentResult = result; | ||
// Loop through the parts of the key | ||
for (let i = 0; i < keyParts.length; i++) { | ||
const part = keyParts[i]; | ||
for (var i = 0; i < keyParts.length; i++) { | ||
var part = keyParts[i]; | ||
@@ -758,2 +787,35 @@ // If the key is not in the object, break | ||
// Escape HTML | ||
// function escapeHTML(str) { | ||
// return str.replace(/[&<>"']/g, function (match) { | ||
// const escapeMap = { | ||
// '&': '&', | ||
// '<': '<', | ||
// '>': '>', | ||
// '"': '"', | ||
// "'": ''' | ||
// }; | ||
// return escapeMap[match]; | ||
// }); | ||
// } | ||
function escapeHTML(str) { | ||
// Escape HTML entities | ||
return str.replace(/[<>&"']/g, function (m) { | ||
switch (m) { | ||
case '<': | ||
return '<'; | ||
case '>': | ||
return '>'; | ||
case '&': | ||
return '&'; | ||
case '"': | ||
return '"'; | ||
case "'": | ||
return '''; | ||
default: | ||
return m; | ||
} | ||
}); | ||
}; | ||
// FunctionQueue.js | ||
@@ -760,0 +822,0 @@ function FunctionQueue(options) { |
{ | ||
"name": "node-powertools", | ||
"version": "1.7.0", | ||
"version": "2.0.0", | ||
"description": "Powerful assistive functions for Node and Browser environments.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -252,4 +252,4 @@ <p align="center"> | ||
### powertools.template(str, data) | ||
Replace all instances of `{key}` in `str` with the corresponding value in `data`. | ||
### powertools.template(str, data, options) | ||
Replace all instances of `{key}` in `str` with the corresponding value in `data`. You can use `options` to customize the template. | ||
```js | ||
@@ -264,2 +264,7 @@ powertools.template( | ||
); // Output: 'Ian\'s favorite color is purple' | ||
powertools.template( | ||
'My favorite color is {color}', | ||
{color: '<b>purple</b>'}, | ||
{escape: true} | ||
); // Output: 'My favorite color is <b>purple</b>' | ||
``` | ||
@@ -266,0 +271,0 @@ |
48654
4.29%858
6.98%361
1.4%