Skip to content

Instantly share code, notes, and snippets.

View source-c's full-sized avatar
🛡️
I definitely will be slow to respond.

A I source-c

🛡️
I definitely will be slow to respond.
View GitHub Profile
@source-c
source-c / battery-level-mac
Last active November 6, 2022 16:20
Simple battery level fetch script for MacOS
#!/usr/bin/env bash
current_level=$(pmset -g batt|grep InternalBattery-0|sed -n 's#.*[[:space:]]\([[:digit:]]\{1,\}\)%.*#\1#p')
[[ -z ${current_level} ]] && current_level='--'
echo ${current_level}
@source-c
source-c / utility-types.d.ts
Created October 24, 2024 10:41
Typescript Utility Types
type OptionalTypeOrNull<T> = T | undefined | null;
type Nullish = undefined | null;
type Falsy = false | "" | 0 | 0n | Nullish;
type Truthy<T> = T extends Falsy ? never : T;
type AnyCollection = ArrayLike<unknown>;
type AnyObject = { [p: string]: AnyObject | AnyCollection | unknown };
type Maybe<T> = T | Nullish;
type MaybePromise<T> = Promise<T> | T;
type Immutable<T> = {
readonly [K in keyof T]: Immutable<T[K]>;
@source-c
source-c / generic-helpers.ts
Last active October 24, 2024 11:00
Typescript Generic Helpers
export const isString = (obj: unknown) =>
obj !== undefined
&& obj !== null
&& (obj instanceof String || typeof obj === 'string');
export const isNone = (v?: unknown): boolean => v === undefined || v === null;
export const isUndefined = (v?: unknown): boolean => v === undefined;
export const isBool = val => 'boolean' === typeof val;
export const asBool = (v?: unknown): boolean =>
@source-c
source-c / time-helpers.ts
Created October 24, 2024 10:54
Typescript generic time helpers
import {format as dateFmt} from 'date-fns'
export const getRelativeTime = (locale: string, d1: Date, d2: Date = new Date()): string => {
// in milliseconds
const units = {
year: 24 * 60 * 60 * 1000 * 365,
month: 24 * 60 * 60 * 1000 * 365 / 12,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000,