Skip to content

Instantly share code, notes, and snippets.

View nyteshade's full-sized avatar

Brielle Harrison nyteshade

View GitHub Profile
@nyteshade
nyteshade / promisify_observable.js
Last active October 26, 2022 04:34
Zen-Observable (Used by ApolloClient) to Promise
/**
* Observables can contain multiple values over time. In order to retrieve
* the values as a single promise, one must promisify the observable by
* reducing each subsequent value into a new array. This shows the order
* of the items as they are returned. Secondly, the promises resolve method
* must be fired from the subscribe() function of the observable.
*
* @param {Observable} o the observable instance to work with
* @return {Promise} a promise that can be used with async/await
*/
@nyteshade
nyteshade / deferredpromise.js
Created October 26, 2022 18:47
Deferred Promises, a useful tidbit that jQuery devised
export class DeferredPromise {
// A definition of this.resolve. If null, this object is uninitialized
resolve = null;
// A definition of this.reject. If null, this object is uninitialized
reject = null;
// A definition of this.promise. If null, this object is uninitialized
promise = null;
@nyteshade
nyteshade / json.js
Last active November 2, 2022 20:47
json cli tool
#!/usr/bin/env node
const FS = require('fs')
const { inspect } = require('util')
const { EOL } = require('os')
const { dirname, basename } = require('path')
const version = {
major: 1,
minor: 1,
@nyteshade
nyteshade / sideBySide.mjs
Last active November 10, 2022 21:48
SideBySide
const version = {
major: 1,
minor: 2,
patch: 0,
toString() { return `${this.major}.${this.minor}.${this.patch}` },
compare(to) {
if (this.major < to.major) return -1
if (this.major > to.major) return 1
if (this.minor < to.minor) return -1
if (this.minor > to.minor) return 1
@nyteshade
nyteshade / Deferred.ts
Last active November 10, 2022 23:21
TypeScript Deferred Function
type DeferredResult<DT> = {
promise: Promise<DT>;
resolve: (value?: DT | PromiseLike<DT>) => void;
reject: (reason?: any) => void;
};
type DeferredOptionalResult<DT> = {
promise?: Promise<DT>;
resolve?: (value?: DT | PromiseLike<DT>) => void;
reject?: (reason?: any) => void;
@nyteshade
nyteshade / typeOf.js
Last active June 9, 2023 18:20
2023 Quick Revamp of typeOf.js/isA.js
/// Grab the underlying type of a value. This will return the true type of
/// a value, whereas typeof will return 'object' for arrays, null, and
/// other non-primitive types.
///
/// @param {any} o - The value to get the type of
/// @returns {string} - The type of the value
const typeOf = o => /\[object (\w+)\]/.exec(Object.prototype.toString.call(o))[1];
/// Determines if the supplied function is actually an ES6+ class or just
/// a regular function by checking its toString() values.
@nyteshade
nyteshade / extensions.mjs
Last active July 5, 2023 18:36
Global Extensions for Classes
// Priavte database for applied extensions
const extensionDB = new Map()
// A constant defining the default set name
export const kDefaultSet = "Default JavaScript Extension Set"
// A constant defining the keys of a descriptor object used with
// Object.defineProperties and Object.defineProperty
export const kDescriptorKeys = [
'value', 'writable', 'get', 'set', 'enumerable', 'configurable',
@nyteshade
nyteshade / reindent_script.py
Created August 15, 2023 03:53
Python Codemod to Modify All Indentation from 4 to 2 Spaces, Recursively, In a Directory
import os
import sys
def modify_indentation(code):
new_code_lines = []
for line in code.splitlines():
leading_spaces = 0
# Count the leading spaces in increments of 4
while leading_spaces + 4 <= len(line) and line[leading_spaces:leading_spaces + 4] == ' ':
@nyteshade
nyteshade / .zsh.fns
Created October 28, 2023 01:26
JSON Helper in ZSH
#!/usr/bin/env zsh
json() {
usage() {
echo "Usage: json [property] [file]"
echo " property: (optional) The property of the JSON object to retrieve."
echo " file: (optional) The file containing the JSON object. Defaults to 'package.json'."
echo ""
echo "Special values for property:"
echo " *: Retrieve the whole object."
@nyteshade
nyteshade / type.js
Last active November 11, 2023 16:23
Vanilla Type Checker
export class Type {
/**
* Creates an instance of the Type class.
*
* @param {string} typeName - The name of the type.
* @param {Object} format - An object representing the format for the type.
* @param {Function} constructor - The constructor function for the type.
*/
constructor(typeName, format, constructor = null) {
this.typeName = typeName