Skip to content

Instantly share code, notes, and snippets.

View roboncode's full-sized avatar

Rob Taylor roboncode

View GitHub Profile
@roboncode
roboncode / diff.js
Created March 21, 2014 11:21
A simple diff function in JavaScript. Does not try to diff arrays. Requires "underscore" or "lodash"
// Requires either "underscore" or "lodash"
function difference(source, compare) {
var ret = {}, dateStr;
for (var name in compare) {
if (name in source) {
if (_.isDate(compare[name])) {
dateStr = _.isDate(source[name]) ? source[name].toISOString() : source[name];
if (compare[name].toISOString() !== dateStr) {
ret[name] = compare[name];
}
@roboncode
roboncode / JsonUtil.js
Last active August 29, 2015 14:07
Provides instructions from an API based on simple rules and converts it to Mongo instructions for setting object in database
var JsonUtil = (function (exports) {
exports = exports || {};
var reserved = 'userId name'.split(' ');
var dates = 'updateLastRequestOn'.split(' ');
var incs = 'newSession'.split(' ');
var e;
var isObject;
@roboncode
roboncode / basic-js-framework.js
Created October 3, 2014 14:40
Training - Basic JavaScript Framework Structure & Concepts
// This represents an external JS file
var example = (function(exports){
'use strict';
exports = exports || {};
var frameworks = {};
function getInstance(name) {
@roboncode
roboncode / forEach
Created June 23, 2015 15:11
Asynchronous forEach function
function forEach(list, fn, done, options) {
if (typeof fn !== 'function') {
throw new Error('Second param expected type "function"');
}
var fnDesc = fn.toString();
var next;
var len = list.length;
var index = 0;
@roboncode
roboncode / supplant.js
Last active July 12, 2016 13:58 — forked from pbroschwitz/supplant.js
supplant - Crockford
/**
* supplant() does variable substitution on the string. It scans through the string looking for
* expressions enclosed in { } braces. If an expression is found, use it as a key on the object,
* and if the key has a string value or number value, it is substituted for the bracket expression
* and it repeats.
*
* Updated by Rob Taylor
* http://roboncode.com
*
* Originally written by Douglas Crockford
@roboncode
roboncode / git_tips.md
Last active July 27, 2016 20:05
GIT Tips & Tricks
@roboncode
roboncode / match_rules.js
Created July 31, 2016 16:51
Match Rules
// http://stackoverflow.com/questions/26246601/wildcard-string-comparison-in-javascript
// Short code
function matchRuleShort(str, rule) {
return new RegExp("^" + rule.split("*").join(".*") + "$").test(str);
}
// Explanation code
function matchRuleExpl(str, rule) {
// "." => Find a single character, except newline or line terminator
@roboncode
roboncode / waterfall-example.js
Last active September 7, 2016 20:59
Waterfall calls function async preventing deep nesting
// usage example
var wf = waterfall({count: 0})
wf([
function(data) {
data.count++;
},
function(data) {
data.count++;
// to stop the cascade, return anything
// return 'Stop cascade';
@roboncode
roboncode / permissions.js
Created August 27, 2016 16:30
Permissions using Binary
function Permissions() {
}
Permissions.createRules = function(options) {
var permissions = {};
var num = 1;
var len = options.length;
for(var i=0;i<len;i++) {
if(i === 0) {
permissions[options[i]] = num;
@roboncode
roboncode / permissions-usage.js
Last active August 27, 2016 16:32
Permissions.js
var rules = Permissions.createRules(['view', 'modify', 'create', 'delete']);
var permissions = Permissions.createPermissions([rules.modify, rules.delete]);
console.log('#view', Permissions.checkForPermission(permissions, rules.view)); // false
console.log('#modify', Permissions.checkForPermission(permissions, rules.modify)); // true
console.log('#create', Permissions.checkForPermission(permissions, rules.create)); // false
console.log('#delete', Permissions.checkForPermission(permissions, rules.delete)); // true