Skip to content

Instantly share code, notes, and snippets.

@barmatz
barmatz / dumpObject.js
Last active August 29, 2015 13:56
This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
function dumpObject(object/*[, depth, name, objectHistory]*/) {
var type = typeof object,
isArray = object instanceof Array,
isObject = type === 'object',
isString = type === 'string',
isFunction = type === 'function',
isUndefined = type === 'undefined',
isNull = object === null,
prefix = ((isUndefined || isNull) ? '' : isString ? '"' : isArray ? '[' : isObject ? '{' : ''),
posfix = ((isUndefined || isNull) ? '' : isString ? '"' : isArray ? ']' : isObject ? '}' : ''),
@barmatz
barmatz / .jshintrc
Created February 9, 2014 00:19
Configuration file for JSHint
{
/* Enforcing options */
"bitwise": false, // This option prohibits the use of bitwise operators such as ^ (XOR), | (OR) and others. Bitwise operators are very rare in JavaScript programs and quite often & is simply a mistyped &&.
"camelcase": true, // This option allows you to force all variable names to use either camelCase style or UPPER_CASE with underscores.
"curly": true, // This option requires you to always put curly braces around blocks in loops and conditionals.
"eqeqeq": true, // This options prohibits the use of == and != in favor of === and !==.
"es3": false, // This option tells JSHint that your code needs to adhere to ECMAScript 3 specification.
"forin": true, // This option requires all for in loops to filter object's items.
"freeze": true, // This options prohibits overwriting prototypes of native objects such as Array, Date and so on.
"immed": true, // This option prohibits the use of immediate function invocations without wrapping them in parentheses.
@barmatz
barmatz / initIE8Checkbox.js
Created February 11, 2014 16:31
A jQuery dependant snippet that adds support to styled checkbox/radio elements in IE8
function initIE8Checkbox() {
jQuery('body').find('input[type="checkbox"] + label, input[type="radio"] + label').
on('click', function (event) {
var element = document.getElementById(event.target.getAttribute('for'));
jQuery('body').find('input[name="' + element.name + '"]').removeClass('checked');
jQuery(element).toggleClass('checked');
}).each(function (index, element) {
element = document.getElementById(element.getAttribute('for'));
@barmatz
barmatz / jshint-jasmine-globals
Last active August 29, 2015 14:01
JSHint Jasmine globals
Inline JavaScript comment:
/*global jasmine, describe, xdescribe, it, xit, expect, beforeEach, afterEach, pending, spyOn*/
Configuration file:
{
"globals": {
"jasmine": true,
"describe": true,
"xdescribe": true,
@barmatz
barmatz / EventSubPubManager.js
Created July 7, 2014 14:43
A subscribe/publish event manager class
window.EventSubPubManager = window.EventSubPubManager || (function() {
function EventSubPubManager() {
this.__ = {}
}
EventSubPubManager.prototype.subscribe = function(topic, callback) {
var callbacks = this.__[topic];
if (!callbacks) {
callbacks = this.__[topic] = [];
@barmatz
barmatz / Gruntfile.js
Last active August 29, 2015 14:05
Gruntfile template
'use strict';
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
usebanner: {
options: {
banner: '/*!\n * <%= pkg.title || pkg.name %>\n * <%= pkg.description%>\n * Version <%= pkg.version%>\n * Compiled <%= grunt.template.today("dddd, mmmm dS, yyyy, h:MM:ss TT") %>\n */\n',
},
@barmatz
barmatz / CutomError.js
Last active August 29, 2015 14:08
NoseJS custom error
'use strict';
var inherits = require('util').inherits;
function CustomError(message) {
Error.captureStackTrace(this, CustomError);
this.message = message;
}
@barmatz
barmatz / singleton.js
Created December 10, 2014 12:12
Node.js Singleton pattern
'use strict';
var singletonLock = {},
instance;
function MySingleton(lock) {
if (!(lock instanceof singletonLock)) {
throw new Error('This is a singleton, do not initiate!');
}
}
@barmatz
barmatz / logger.js
Last active January 23, 2018 14:47
Basic Express server
const { Logger, transports: { Console } } = require('winston');
module.exports = new ]Logger({
transports: [
new Console({
colorize: true,
level: process.env.NODE_ENV === 'development' ? 'silly' : 'info'
})
]
});
@barmatz
barmatz / Gruntfile.js
Created July 9, 2015 09:32
Node ES6 Ember Project
'use strict';
module.exports = function (grunt) {
grunt.initConfig({
clean: {
app: '.tmp/**'
},
compass: {
app: {
options: {