Last active
October 29, 2025 16:35
-
-
Save renoirb/fd340fa396cdcd408f38e5ac34845089 to your computer and use it in GitHub Desktop.
Small ECMAScript functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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