Skip to content

Instantly share code, notes, and snippets.

@fiznool
fiznool / fz.restsource.js
Created April 17, 2014 14:27
Patch for angularjs $resource to send POST or PUT depending on ID field
// This patches the $resource.$save function
// to make a PUT when an ID is present.
// Inspired by http://kirkbushell.me/angular-js-using-ng-resource-in-a-more-restful-manner/
'use strict';
angular.module( 'fz.restsource', [ 'ngResource' ] )
.factory('$restsource', function($resource) {
var idField = '_id';
@fiznool
fiznool / app.js
Last active August 29, 2015 14:01 — forked from silas/app.js
var express = require('express'),
app = express();
app.configure(function() {
app.use(express.bodyParser());
app.use(app.router);
});
app.get('/', function(req, res) {
res.send(500);
@fiznool
fiznool / convert-json-to-js.js
Created September 9, 2014 11:19
Converts a directory of JSON files to JS ones.
'use strict';
var util = require('util'),
fs = require('fs');
var path = './server/services/defs';
var dirs = fs.readdirSync(path);
dirs.forEach(function(dir) {
var servicePath = path + '/' + dir;
@fiznool
fiznool / q-extra.decorator.js
Created September 25, 2014 10:48
Add extra functions to the $q Angular service.
'use strict';
angular.module('q-extra', [])
.config(['$provide', function($provide) {
$provide.decorator('$q', ['$delegate', function($delegate) {
if(angular.isUndefined($delegate.resolve)) {
$delegate.resolve = function($q) {
return function(val){
var dfd = $q.defer();
dfd.resolve(val);
@fiznool
fiznool / index.js
Created December 18, 2014 08:19
request basic auth
var request = require('request');
// The `auth` object will take precedence
// over the `Authorization` header
var options = {
url: 'http://requestb.in/1jt9nmk1',
method: 'GET',
headers: {
Authorization: 'Basic 1234'
},
@fiznool
fiznool / date-duration.js
Created February 5, 2015 10:27
Date Duration
Date.prototype.duration = function(granularity) {
if(!granularity) {
granularity = 's';
}
var units = [{
enabled: /^[wdhms]$/.test(granularity),
singular: 'week',
plural: 'weeks',
inMillis: 7 * 24 * 60 * 60 * 1000
@fiznool
fiznool / Underscore-Mustache
Created March 23, 2012 10:44
Underscore Mustache-like templating
// Change underscore's templating from ERB-style to Mustache-style
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
@fiznool
fiznool / flotr2-amd.js
Created July 3, 2012 14:37
Flotr2 AMD
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['bean', 'underscore'], function (bean, _) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
return (root.Flotr2 = factory(bean, _));
});
} else {
@fiznool
fiznool / emailRegex.js
Last active December 18, 2015 00:09
HTML5 Email validation regex
var EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$/;
var isValidEmailAddress = function(email) {
return EMAIL_REGEX.test(email);
};
@fiznool
fiznool / serializeJSON.js
Created June 2, 2013 19:54
Serialize a Backbone Form to JSON.
return _.reduce($el.serializeArray(), function (hash, pair) {
hash[pair.name] = pair.value;
return hash;
}, {});