Skip to content

Instantly share code, notes, and snippets.

View ryangoree's full-sized avatar
🌎

Ryan Goree ryangoree

🌎
View GitHub Profile
@ryangoree
ryangoree / url-api-polyfill.js
Last active February 11, 2025 16:23
URL Polyfill
(function() {
if (typeof URL != 'function') {
rewriteURL();
} else if (!('searchParams' in new URL(window.location))) {
rewriteURL();
}
function rewriteURL() {
// Overwrite URL if no searchParams property exists.
@ryangoree
ryangoree / sass-grid.scss
Last active October 11, 2021 09:22
Sass Grid Mixin
@mixin grid($cols, $rowGap: 0, $colGap: 0, $flexGrow: false) {
display: flex;
gap: #{$rowGap}px #{$colGap}px;
flex-wrap: wrap;
.col {
flex-basis: calc((100% - #{$colGap}px * (#{$cols} - 1)) / #{$cols});
@if $flexGrow == true {
flex-grow: 1;
}
function groupStrings(strings) {
return Object.values(strings.sort().reduce((a, str) => {
const s = str.toLowerCase();
const f = s[0];
a[f] = a[f] ? [...a[f], s] : [s];
return a;
}, {}));
}
@ryangoree
ryangoree / formatBytes.js
Last active March 13, 2023 20:00
formatBytes - Format bytes as human-readable text.
/**
* Format bytes as human-readable text.
* @param {number} bytes Number of bytes.
* @param {number} decimalPlaces Number of decimal places to display.
* @param {number} baseQuantity Number of bytes in a kilobyte.
* @param {string[]} sizes The unites to use for each power of the baseQuantity.
* @return {string} Formatted byte size.
*/
export const formatBytes = (
bytes,
@ryangoree
ryangoree / convert.ts
Last active December 5, 2024 06:27
Deep convert function with converted types
/**
* Recursively converts a type to a new type. The `predicateFn` is
* used to determine if the `converterFn` should be run on the value.
*
* The function first checks the value itself, if the `predicateFn` returns
* false and the value is an array or object, the function will recursively
* check each item in the array or object.
*
* @param value - The value to convert primitive types in.
* @param predicateFn - A function that returns true if the `converterFn` should
@ryangoree
ryangoree / DefinedWhen.ts
Last active February 24, 2025 00:47
Experiment for dynamic interfaces
/**
* Construct a type based off T where TDefinedField is defined when
* T matches TDefinedState.
*
* @example
* ```ts
* type Payload = DefinedWhen<
* {
* isFetched: boolean;
* hasError: boolean;
@ryangoree
ryangoree / binarySearch.ts
Created September 14, 2023 22:04
Conducts a binary search on a sorted array of items to find the index of a specific target.
/**
* Conducts a binary search on a sorted array of items to find the index of a
* specific target. If the target is not found, it returns the nearest index
* based on the comparator function.
*
* @param items - The sorted array of items to search within.
* @param compare - A comparator function that returns:
* - a negative number if `item` is less than the target,
* - a positive number if `item` is greater than the target,
* - and 0 if `item` is equal to the target.
@ryangoree
ryangoree / getOSConfigDir.ts
Last active December 8, 2023 21:24
Get the path to an app specific config directory based on operating system standards
import os from "node:os";
import path from "node:path";
/**
* Get the path to an app specific config directory based on operating system
* standards.
* @param projectName
* @returns
*/
export function getOSConfigDir(projectName: string): string {
@ryangoree
ryangoree / camelCase.ts
Created November 5, 2023 19:37
Generic hyphen-case to CamelCase util and type
/**
* Converts a hyphenated string to camel case.
*
* @example
* camelCase('foo-bar') // 'fooBar'
*/
export function camelCase<S>(str: S): CamelCase<S> {
return (
typeof str === 'string'
? str.toLowerCase().replace(/-+([^-])/g, (_, c) => c.toUpperCase())
@ryangoree
ryangoree / README.md
Last active November 7, 2024 21:31
Attempts to get the path to the file that called the function that called this function.

Get Caller Path

Attempts to get the path to the file that called the function that called this function.

📦 Install

npm i gist:b82256d70c5d9bed769b94a8d72bb38a