Skip to content

Instantly share code, notes, and snippets.

@sidisinsane
sidisinsane / format-phone-number.ts
Last active July 17, 2023 12:54
Formats a phone number using the libphonenumber-js library.
import parsePhoneNumber from "libphonenumber-js";
/**
* The available formatting properties for formatting a phone number.
*/
type FormatPhoneNumberProperty = "NATIONAL" | "INTERNATIONAL" | "RFC3966";
/**
* Represents the arguments for formatting a phone number.
*/
@sidisinsane
sidisinsane / i18n-format-display-names.ts
Last active July 16, 2023 10:25
Formats a display name (currency, language, region, script) with internationalization.
/**
* Represents the arguments for formatting a currency display name with internationalization.
*/
export interface I18nFormatDisplayNamesArgs {
/**
* The locale to use for formatting the display name. Defaults to "en-US".
*/
locale?: keyof typeof Intl.Locale;
/**
* The currency code for which to retrieve the display name.
@sidisinsane
sidisinsane / i18n-format-date.ts
Last active June 18, 2023 13:18
Formats a date with internationalization.
/**
* Represents the arguments for formatting a date with internationalization.
*/
export interface I18nFormatDateArgs {
/**
* The locale to use for formatting the date. Defaults to "en-US".
*/
locale?: keyof typeof Intl.Locale;
/**
* The string representation of the date to be formatted.
@sidisinsane
sidisinsane / get-object-fit-data.js
Created April 26, 2020 09:23
Calculates Dimensions and Ratio for an Object to fit in a Container
const getObjectFitData = (obj, container, cover=true) => {
let result = {};
const ratioX = container.width / obj.width;
const ratioY = container.height / obj.height;
const ratioFit = cover ? Math.max(ratioX, ratioY) : Math.min(ratioX, ratioY);
result.ratio = Math.ceil(ratioFit * 100) / 100;
result.width = Math.ceil(obj.width * result.ratio);
result.height = Math.ceil(obj.height * result.ratio);
@sidisinsane
sidisinsane / poster.command
Last active April 15, 2020 05:35
Creates a poster from a given mp4 frame.
#!/usr/bin/env bash
# Creates a poster from a given mp4 frame.
# 1. Get FFmpeg (https://evermeet.cx/ffmpeg/) and install it
# 2. Place this script within your mp4 folder
# 3. Make it executable: chmod +x path/to/poster.command
# 4. Execute it by doubleclicking this file
dir=$(dirname $(realpath -s $0))
echo "Enter filename without extension (i.e. my-awesome-video)."
read name
echo "Enter timestamp (HH:MM:SS.MM) of frame to create poster from."
@sidisinsane
sidisinsane / git-branch-to-favicon.js
Created April 4, 2020 10:31
Creates a favicon.svg from the current Git Branch
const { execSync } = require('child_process')
const { createHash } = require('crypto')
const invertColor = require('invert-color')
const fs = require('fs')
const path = require('path')
/**
* Returns numeronym string
* @param str
* @returns {*}
@sidisinsane
sidisinsane / create-webm.sh
Created April 4, 2020 10:20
Create WebM Videos
#!/usr/bin/env bash
shopt -s nullglob nocaseglob extglob
for file in src/assets/videos/*.mp4; do
name="${file%%.*}"
if [ -f "$name-1280x720-1500k.webm" ]; then
echo "$name-1280x720-1500k.webm exists"
else
ffmpeg \
-i "$file" -vn -acodec libvorbis -ab 128k -dash 1 "$name-audio.webm" && \
ffmpeg \
@sidisinsane
sidisinsane / create-webp.sh
Created April 4, 2020 10:19
Create WebP Images
#!/usr/bin/env bash
quality=80
shopt -s nullglob nocaseglob extglob
for file in src/assets/img/**/*.@(jpg|jpeg|png); do
if [ -f "${file%.*}.webp" ]; then
echo "${file%.*}.webp exists"
else
cwebp -q $quality "$file" -o "${file%.*}.webp"
fi
done
@sidisinsane
sidisinsane / compress-mp4.sh
Created April 2, 2020 21:05
Compress MP4 with FFmpeg
ffmpeg -i source.mp4 -vcodec h264 -b:v 1000k -acodec mp3 target.mp4
@sidisinsane
sidisinsane / convert-mp4-to-ogv.sh
Created March 30, 2020 06:24
Convert MP4 to OGV with FFmpeg
ffmpeg -i source.mp4 -c:v libtheora -q:v 7 -c:a libvorbis -q:a 4 target.ogv