Skip to content

Instantly share code, notes, and snippets.

View signalwerk's full-sized avatar
💫
on the r͢oad

Stefan Huber signalwerk

💫
on the r͢oad
View GitHub Profile
@signalwerk
signalwerk / html-yaml-cli.js
Last active July 12, 2024 14:52
A tool to extract content from HTML, convert it to YAML, and update HTML content from YAML files.
// HTML to YAML Extractor and Updater
/**
* A tool to extract content from HTML, convert it to YAML, and update HTML content from YAML files.
*/
import cheerio from "cheerio";
import { promises as fs } from "fs";
import path from "path";
import yaml from "js-yaml";
@signalwerk
signalwerk / ChatGPT.mjs
Created April 29, 2023 18:24
Minimal ChatGPT request to the OpenAI-API
// This code sends a prompt to the OpenAI API and then outputs the response
// to the terminal and to a file.
// Setup:
// npm init -y && npm i dotenv node-fetch
// echo "OPENAI_API_KEY=sk-XXX" > .env
import fs from "fs";
import fetch from "node-fetch";
import * as dotenv from "dotenv";
@signalwerk
signalwerk / ip-print.js
Created April 29, 2023 18:10
Alternative IP notations
// alternative ip notations
// https://ma.ttias.be/theres-more-than-one-way-to-write-an-ip-address/
// https://twitter.com/h43z/status/1618220318023364608
dotNotationToDec({ ip: "185.15.230.26" }); // liip.ch;
dotNotationToDec({ prefix: "185.", ip: "15.230.26" });
/*
output:
dec http://3104826906/
@signalwerk
signalwerk / record.js
Last active March 31, 2024 16:00
Record from a canvas element to a webm file
// convert .webm to .mp4 and remove audio
// ffmpeg -i in.webm -vcodec h264 -an out.mp4
function dateString() {
const now = new Date();
return (
now.getUTCFullYear() +
"-" +
("0" + (now.getUTCMonth() + 1)).slice(-2) +
@signalwerk
signalwerk / template.js
Last active April 29, 2023 18:31
Replace {{ values }} in String
// This is a basic templating function. It replaces any {{key}} in the
// string with the corresponding value in data. If no data is provided,
// then the template is returned as-is.
//
// Example:
// template("Hello, {{name}}!", { name: "John" }) → "Hello, John!"
// template("Hello, {{name}}!") → "Hello, {{name}}!"
export function template(string, data = {}) {
let out = string;
@signalwerk
signalwerk / at.js
Created November 3, 2021 23:50
get an element from an array at a specific position (index) – the index can be negative and exceed the length of the array (Modulo wrapping)
const at = (arr, n) => {
const offset = Math.abs(n) % arr.length;
if (n >= 0) {
return arr[n % arr.length];
} else {
return arr[(arr.length - offset) % arr.length];
}
};
@signalwerk
signalwerk / obj2html.js
Created October 31, 2021 23:05
Create html-string from object notation
export function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}
export function style(obj) {
return Object.entries(obj)
.map(
@signalwerk
signalwerk / vue-splitter.js
Last active November 7, 2022 16:05
It is a tool to split a folder with .vue files into three files (.split.js/.split.scss/.split.vue) and merge them back together. Ideal for refactoring code with external tools that don't know .vue files.
const doc = `
## Vue-Splitter
It is a tool to split a folder with .vue files into three files (.split.js/.split.scss/.split.vue)
and merge them back together. Ideal for refactoring code with external tools that don't know .vue files.
## ⚠️ CAUTION
⚠️⚠️⚠️ Make sure you have a proper copy (git) of your code before you run this tool.
@signalwerk
signalwerk / get.js
Last active September 19, 2021 22:31
reduced lodash get() function in vanilla javascript
/**
* https://gist.github.com/signalwerk/eadabea1fc42795ed8af2882693d20e1
*
* Return the value at `path` in `object`
* @param {Object} object
* @param {string|array} path
* @returns {*} value if found otherwise undefined
*/
export const get = (object, path) => {
let parts = path;
@signalwerk
signalwerk / node-fetch.js
Last active April 26, 2024 14:52
A fetch like helper in node
// get an url in node without additional packages
const https = require("https");
function httpRequest(method = "get", url, body = null) {
if (!["GET", "POST", "HEAD"].includes(method.toUpperCase())) {
throw new Error(`Invalid method: ${method}`);
}
let urlObject;