Skip to content

Instantly share code, notes, and snippets.

View nyteshade's full-sized avatar

Brielle Harrison nyteshade

View GitHub Profile
@nyteshade
nyteshade / strnumconvert.c
Last active January 3, 2024 09:27
strnumconvert - C89 parseInt, strtoul, itoa with base 62 support
// strnumconvert.c
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include "strnumconvert.h"
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."
echo " --help or -h: Display this help message."
}
globalThis.Descriptor = class Descriptor {
static base(enumerable = true, configurable = true) {
return { enumerable, configurable }
}
static data(value, writable = true, { configurable, writable } = this.base()) {
return { value, enumerable, configurable, writable }
}
static accessor( getter, setter, store, { configurable, writable } = this.base()) {
/**
* The Walker class provides a mechanism for iterating through a string and
* counting occurrences of specified opening and closing characters. It's designed
* to start counting when the first instance of the 'open' character is encountered
* and stops when the count drops back to zero.
*
* @example
* const walker = new Walker("{a{b}c}", {when: () => {}, searchFor: Walker.squigly});
* walker.until(); // Process the string
*
/**
* A simple Hasher implementation in JavaScript. This mimics the behavior of Swift's
* Hasher to some extent. It uses the djb2 algorithm for hashing.
*/
class Hasher {
/**
* The constructor function takes in multiple values, flattens them into a single
* array, and then calls the combine method on each value.
*
* @param values - The "values" parameter is a rest parameter that allows you to
@nyteshade
nyteshade / Descriptors.js
Created December 11, 2023 19:09
Another go at baseline JavaScript additions such as work with Descriptors. Need to compare this with code in ne-reflection and decide which I like best.
Object.assign(Object, {
validKey(value) {
return (typeof value === 'string' || typeof value === 'symbol')
},
isObject(value) {
return value && (value instanceof Object || typeof value === 'object')
},
/**
@nyteshade
nyteshade / usePseudoState.js
Created December 11, 2023 19:08
Experimentation with a non-react specific useState approximation
/**
* The function `usePseudoState` is a custom hook that allows for the creation of
* pseudo state variables in React. When `useState` becomes available, it should be
* applied to this object output by calling the `init` method with the `useState`
* function.
*
* The object returned from this function invocation
*
* @param initialValue - The `initialValue` parameter is the initial value that
* will be assigned to the pseudo state.
import { useState } from "react";
function usePropertyState(obj, properties) {
const [_, triggerUpdate] = useState({});
const [__, setObject] = useState(obj);
const props = properties && Array.isArray(properties) ? properties : Reflect.ownKeys(properties);
const propertySet = new Set(properties);
// Creating a proxy for the object
@nyteshade
nyteshade / Tokens.js
Created December 10, 2023 07:58
Relies on Tickers
/* eslint-disable eqeqeq */
/* eslint-disable no-new-func */
import { Count, Strings, Ticker } from './ticker';
/**
* Finds the closing token for a given opening token in a string, starting from a
* specified index.
*
* @param {string} str - The string in which to find the closing token.
* @param {number} startIndex - The index in the string from which to start the search.
@nyteshade
nyteshade / Count.js
Last active December 10, 2023 08:02
Tickers are small state objects that are designed to increment or decrement values within themselves
import { Ticker } from './ticker.js'
/**
* The `Count` class extends the `Ticker` class and provides additional methods
* for arithmetic operations on the count value, like adding, subtracting,
* multiplying, and dividing by a specified delta value.
*/
class Count extends Ticker {
/**
* Adds a specified delta value to the current count value and returns the