Skip to content

Instantly share code, notes, and snippets.

@raohmaru
raohmaru / parseTemplate.js
Last active May 31, 2025 10:19
Mini template engine in JavaScript
/**
* Mini template engine. Parses variables (w/o dot notation) and does not delete undefined variable expressions.
* @param {string} expr - String expression with {{variables}} to interpolate.
* @param {RegExp} [regex] - Custom regex to match variables. By default it matches double curly braces `{{variables}}`.
* @returns {(data:Object|string[]) => void} - A function that accepts an object or an array as argument to interpolate the template variables.
* @example
* const templateObj = parseTemplate('{{a}} {{b.c}}');
* template({a: 'hello', b: {c:'world'}}); // "hello world"
*
* const templateArr = parseTemplate('{{}} {{}}');
@raohmaru
raohmaru / pre-commit
Last active April 27, 2023 09:46
Git pre-comit hook to lint files using npm
#!/usr/bin/env sh
# This loads nvm
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# Bypass if a merge is happening
function skipIfMerging() {
local isMerge="$(git rev-parse -q --verify MERGE_HEAD)"
# If isMerge is a hash, a merge is ongoing
@raohmaru
raohmaru / requestAnimationFrame.js
Last active June 23, 2025 21:34
requestAnimationFrame() in node.js
const callbacks = [];
const fpsInterval = 1000 / 60;
let time = performance.now();
function requestAnimationFrameLoop() {
const now = performance.now();
const delta = now - time;
if (delta >= fpsInterval) {
// Adjust next execution time in case this loop took longer to execute
time = now - (delta % fpsInterval);
@raohmaru
raohmaru / create_symlinks.bat
Created January 31, 2016 09:52
Create symbolic links in Windows from the subdirectories of a given directory