mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-06-24 13:45:11 +00:00
41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
//
|
|
// GENERAL UTILITY FUNCTIONS
|
|
// OBject, string manipulation etc.
|
|
//
|
|
|
|
// Utility function to escape HTML characters from a string.
|
|
function escapeHtml(str) {
|
|
if (typeof str !== 'string') {
|
|
return '';
|
|
}
|
|
|
|
const escapeCharacter = (match) => {
|
|
switch (match) {
|
|
case '&':
|
|
return '&';
|
|
case '<':
|
|
return '<';
|
|
case '>':
|
|
return '>';
|
|
case '"':
|
|
return '"';
|
|
case '\'':
|
|
return ''';
|
|
case '`':
|
|
return '`';
|
|
default:
|
|
return match;
|
|
}
|
|
};
|
|
|
|
return str.replace(/[&<>"'`]/g, escapeCharacter);
|
|
}
|
|
|
|
// Converts an HTML hex colour to an array of [R, G, B] where each is 0-255.
|
|
function hexToRGB(hex) {
|
|
return hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i
|
|
, (m, r, g, b) => '#' + r + r + g + g + b + b)
|
|
.substring(1).match(/.{2}/g)
|
|
.map(x => parseInt(x, 16));
|
|
}
|