Skip to content

Instantly share code, notes, and snippets.

@renoirb
Last active October 29, 2025 16:35
Show Gist options
  • Save renoirb/fd340fa396cdcd408f38e5ac34845089 to your computer and use it in GitHub Desktop.
Save renoirb/fd340fa396cdcd408f38e5ac34845089 to your computer and use it in GitHub Desktop.
Small ECMAScript functions
/**
* Create an UTC date object based on an ISO yyyy-mm-dd string
*/
export const toUTC = (input) => {
const date = new Date(`${input}T00:00:00Z`);
if (Number.isNaN(date.getDate())) {
throw new Error(date);
}
return date;
};
/**
* Tagged function to create a UTC date object
*/
export function utc() {
const [input] = arguments;
const trimmed = String(input).trim();
return toUTC(trimmed);
}
import { __ } from '<package with gettext __ function>';
/**
* Tagged template function to normalizes whitespace for text we manage translation.
*
* - Removes newlines
* - Collapses multiple spaces to single space
* - Trims leading/trailing whitespace
* - Pass to translation
*/
export const __t = (
strings: TemplateStringsArray,
...values: unknown[]
): string => {
// Interleave strings and values
let result = strings[0];
for (let i = 0; i < values.length; i++) {
result += String(values[i]) + strings[i + 1];
}
// Normalize whitespace
result = result
// Replace newlines with space
.replace(/\n/g, ' ')
// Collapse multiple spaces
.replace(/\s+/g, ' ')
// Remove leading/trailing space
.trim();
return __(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment