Skip to content

Instantly share code, notes, and snippets.

View zgover's full-sized avatar
Hiring for React + Typescript Developer

Zach Gover zgover

Hiring for React + Typescript Developer
View GitHub Profile
@zgover
zgover / roman_year.md
Last active April 28, 2021 10:39
[Year to Roman] Convert 4 digit year into Roman Numeral #date #convert #helper

lang: PHP

function roman_year(int $year = null): string {
    $year = $year ?? date('Y');
    $romanNumerals = [
        'M' => 1000,
        'CM' => 900,
 'D' => 500,
@zgover
zgover / mitt-emitter.ts
Last active August 15, 2021 05:49
[TS/JS MittEmitter] Typed Event Emitter Function Factory #javascript #typescript #mitt #eventemitter #emit #events
// This file is based on https://github.com/developit/mitt/blob/v1.1.3/src/index.js
// It's been edited for the needs of this script
// See the LICENSE at the top of the file
// Event handler callback function
type EventHandler = (...events: any[]) => void
// Wild card handler function receiving the first parameter as the event name
type WildCardEventHandler = (name: string, ...events: any[]) => void
@zgover
zgover / word-wrap.js
Last active April 28, 2021 10:48
[JS WordWrap] Strip and ellipsize text characters exceeding the max
/**
* This utility function shortens passed text to desired length
*
* e.g. wordWrap(str, { maxWidth: 150, moreIndication: '...' })
*
* @param {String} str text that requires shortening
* @param {Object} opt the wrapping options
* {Number} maxWidth the max width of the text
* {string} moreIndication indicator to append to the end
*/
/**
* @license
* Copyright Gover Construction LLC. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://gist.github.com/zgover/678d51ffc2477d2e714052d7154f784b
*/
/**
* Removes stop words (e.g. the, we, you, did, a, etc.) from
@zgover
zgover / fib.ts
Created December 3, 2020 02:45
Fibonacci Sequence
class Fib implements IterableIterator<number> {
protected fn1 = 0;
protected fn2 = 1;
constructor(protected maxValue?: number) {}
public next(): IteratorResult<number> {
var current = this.fn1;
this.fn1 = this.fn2;
@zgover
zgover / base36Id.ts
Last active December 12, 2020 20:48
Base36 Random UID Generator
/**
* Generate a random ID string
*
* Math.random should be unique because of its seeding algorithm.
* Convert it to base 36 (numbers + letters) and grab the first 9 characters
* after the decimal.
*
* @param config {RandomIdConfig}
*
* @return {string}
@zgover
zgover / JSONAble.js
Created December 16, 2020 21:10
Class abstraction for automatic JSON generation
export default abstract class {
toJSON () {
const proto = Object.getPrototypeOf(this)
const jsonObj: any = Object.assign({}, this)
Object.entries(Object.getOwnPropertyDescriptors(proto))
// .filter(([_, descriptor]) => typeof descriptor.get === 'function')
.map(([key, descriptor]) => {
if (descriptor && key[0] !== '_') {
@zgover
zgover / guards.ts
Last active June 10, 2021 14:44
Quick TypeScript Guard and Helper Utility Functions
/**
* Is literal type 'boolean'
*
* @export
* @param {*} val
* @returns {val is boolean}
*/
export function _isBool(val: unknown): val is boolean {
return typeof val === 'boolean'
}
@zgover
zgover / fibonacci-sequence.md
Last active August 15, 2021 05:55
[Common Design Patterns] Simple, common and quick algorithms #algorithm #typescript #javascript #dart #sequence #patterns #effecient #math #facorial

lang: TypeScript

/**
 * Recursion simple
 */
const fibonacciRecursion = (n: number, fibRecusor?: typeof fibonacciRecursion): number => {
  const recurse = fibRecusor ?? fibonacciRecursion
  return (n < 2) ? n : (recurse.call(null, n - 1) + recurse.call(null, n - 2))
}
console.log('fibonacciRecursion', `${fibonacciRecursion(20)}`) // [LOG]: "fibonacciRecursion",  "6765" 
@zgover
zgover / clearable-weak-map.md
Last active April 23, 2021 16:57
[JS/TS Clearable WeakMap] JS/TS – Implementing a clearable WeakMap-like class #javascript #weakmap #class #constructor #garbagecollection #memory #references #private #typescript
class ClearableWeakMap<T = unknown, K extends object = object> {
  private _: WeakMap<K,T>;
  constructor(init: Iterable<[K,T]>) {
    this._ = new WeakMap(init)
  }
  clear() {
    this._ = new WeakMap()
    return this
 }