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 / interpolation.js
Last active May 26, 2016 18:01
Ruby-style string interpolation for JavaScript.
// Author: Darren Schnare
// Keywords: javascript,interpolation,string,ruby
// License: MIT ( http://www.opensource.org/licenses/mit-license.php )
// Repo: https://gist.github.com/gists/3886395
String.prototype.interpolate = function (o) {
return this.replace(/#\{(.+?)\}/g, function ($0, $1) {
with (o) {
return eval($1);
}
});
@dschnare
dschnare / index.correct.jade
Last active December 18, 2015 12:49
Sharing Variables with Jade Templates: Examples of how and how not to share variables across a template and layout.
extends layout
prepend page
- title = "Home"
block body
p This is the \#{title} page
@dschnare
dschnare / validate.js
Last active December 21, 2015 16:59
jQuery Validate File Fields Quirkiness - Example of how to manually update the valid state of a file field when using jQuery Validate.
// jQuery Validate setup.
$('form').validate({
rules: {
"cover-letter": {
required: true,
accept: "application/pdf|application/msword|application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}
},
messages: {
"cover-letter": "Please upload your cover letter (PDF, DOC or DOCX)."
@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 / index.html
Last active December 23, 2015 04:59
Applies several stretch strategies to a viewport's contents following the examples here: http://msdn.microsoft.com/en-us/library/system.windows.media.stretch.aspx. This snippet requires jQuery.
<!DOCTYPE html>
<html>
<head>
<title>TITLE</title>
<style type="text/css">
html,
body {
margin: 0;
}
.viewport {
@dschnare
dschnare / bs-carousel-nesting.js
Created March 27, 2015 17:42
Adds nesting support to Twitter Bootstrap carousels.
@dschnare
dschnare / collate.js
Last active October 7, 2017 13:51
groupBy and collate
/**
* Collates all properties with varying values or the specified properties in
* the property map into a single object.
*
* If the propMap is an Object then the keys are the properties to collate and
* the values are the target property name to created on the collated object. If
* the key in the propMap is a comma delimited string then the target property
* will be set to an object with all the properties in the comma delimited
* key.
*
@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) {