Skip to content

Instantly share code, notes, and snippets.

View dschnare's full-sized avatar

Darren Schnare dschnare

View GitHub Profile
@dschnare
dschnare / aop.js
Last active February 28, 2023 01:58
AopJS - A lightweight aspect oriented programming (AOP) API for JavaScript.
// Author: Darren Schnare
// Keywords: aop,aspect,oriented,programming,javascript,pointcut
// License: MIT ( http://www.opensource.org/licenses/mit-license.php )
// Repo: https://gist.github.com/1235559
// Inspiration: http://karlagius.com/2008/04/25/aspect-oriented-programming-in-javascript/
// Reference: http://docs.jboss.org/jbossaop/docs/2.0.0.GA/docs/aspect-framework/reference/en/html/advices.html
// Reference: http://static.springsource.org/spring/docs/2.0.x/reference/aop.html
// Appends the aop namespace to the specified scope (defaults to global).
@dschnare
dschnare / equals.js
Created September 29, 2011 15:31
EqualsJS - Deep equality testing for JavaScript.
// Author: Darren Schnare
// Keywords: javascript,equality,testing,equals,object
// License: MIT ( http://www.opensource.org/licenses/mit-license.php )
// Repo: https://gist.github.com/1251001
// Creates an equals function within the specified scope.
(function(scope) {
// Determines if two objects have the same values. This is a
// recursive test that tests all objects through out the object
// tree.
@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 / 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 / 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 / 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 / cachetext.correct.js
Last active January 18, 2020 03:12
HTML5 Canvas Tricks: Text Caching and Registration Point
// Create our stage canvas.
var stage = document.createElement('canvas');
stage.style.border = '1px solid #000';
document.body.appendChild(stage);
// Create offscreen buffer for our text rendering.
// This way all we have to do is draw our buffer to
// the main canvas rather than drawing text each frame.
var textBuffer = document.createElement('canvas');
@dschnare
dschnare / BusinessObject.js
Last active November 18, 2019 04:01
Object hierarchy inspired by Smalltalk and OOD
/**
* Business Object class object that acts as the root of an object hierarchy.
* Creates object instances that have methods marked as read-only and the object
* sealed, meaning no new properties can be added and behaviour cannot be changed
* without extending the class object (i.e. tamperproof).
*
* @example
* const Box = BObject.subClass({
* className: 'Box',
* new ($base, width, height) {
@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
}