Skip to content

Instantly share code, notes, and snippets.

View dschnare's full-sized avatar

Darren Schnare dschnare

View GitHub Profile
@dschnare
dschnare / typer.js
Last active August 14, 2020 18:30
No frills, JavaScript type checking library
/*
Copyright 2020 Darren Schnare
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
@dschnare
dschnare / type.js
Last active February 19, 2020 14:58
Functional runtime type checking functions
/**
* Get the type name for a value.
*
* @example type(null) // 'Null'
* @example type(undefined) // 'Undefined'
* @example type(45) // 'Number'
* @example type('Hello') // 'String'
* @example type({}) // 'Object'
* @example type(/hi/) // 'RegExp'
* @example type(new Date()) // 'Date'
@dschnare
dschnare / check.js
Created January 31, 2020 20:55
A type checking function for JS
const check = (type, value) => {
if (typeof type !== 'string') {
throw new TypeError('Argument "type" must be a string')
}
const optional = /!\??$/.test(type)
const nullable = /\?!?$/.test(type)
type = type.replace(/!$|\?$/g, '').trim()
if (['null', 'undefined', ''].includes(type.toLowerCase())) {
@dschnare
dschnare / validate.js
Last active October 5, 2019 04:01
Small validation library
function validate (value, validators, message = 'Invalid value') {
validators = [].concat(validators)
if (typeof message !== 'string') {
throw Object.assign(
new Error('Argument "message" must be a string'),
{
name: 'ArgumentError',
propertyName: 'message',
propertyValue: message
}
@dschnare
dschnare / t.js
Created September 2, 2019 20:43
Type checking at runtime
/**
* Checks the type of a value. If the value is invalid then throws.
*
* Errors:
*
* TypeError
*
* Thrown whenever a value has an invalid type
*
* Properties:
@dschnare
dschnare / template.js
Last active April 5, 2021 15:56
Simple EcmaScript template string-based templating system
/*
* Simple EcmaScript templating system.
*
* Templates are just functions that accept an object of
* properties and return a string.
*
* const Hello = (props = {}) => `
* Hello ${props.message || 'World'}!
* `
* console.log(Hello({ message: 'Mom' }))
@dschnare
dschnare / service-container.js
Last active September 21, 2019 00:45
Simple service container
/**
* A simple service container.
*
* @example
* const app = express()
*
* const appContainer = new ServiceContainer()
* const config = Object.freeze({ })
* appContainer.constant('Config', config)
* appContainer.singleton('Db', () => new Db())
@dschnare
dschnare / json-pointer.js
Last active September 13, 2019 18:43
JSONPointer and walkObject APIs
const walkObject = (function () {
function walkObjectRec (obj, visit, { pointer = '', key = '', visited = new WeakMap(), parent = undefined } = {}) {
if (obj === null || obj === undefined) {
visit(obj, key, pointer, parent)
} else if (typeof obj === 'object') {
if (visited.has(obj)) {
visited.set(obj, visited.get(obj) + 1)
} else {
visited.set(obj, 1)
visit(obj, key, pointer, parent)
@dschnare
dschnare / read-option.js
Last active April 2, 2020 21:27
Read a command line option
/**
* Read a command line option. Option names must start with '-'.
*
* Options:
*
* `required` Determines if the option must appear on the command line.
*
* `multi` Determines if the option can be specified more than once on the
* command line. Causes the return value to be an array.
*
@dschnare
dschnare / lists.js
Last active May 30, 2019 03:30
Sorted and priority list factories
/**
* @param {any[]} items
* @param {any} item
* @param {{ (a: any, b: any) => number }} compare
* @param {number} low
* @param {number} high
* @return {number}
*/
function binarySearch (items, item, compare, low = 0, high = items.length) {
if (high <= low) {