Skip to content

Instantly share code, notes, and snippets.

View niksumeiko's full-sized avatar

Nik Sumeiko niksumeiko

View GitHub Profile
@niksumeiko
niksumeiko / handlebars.helpers.ifEquals.js
Created October 1, 2013 12:02
Handlebars.js templates engine custom IF condition helper. Allows to compare values one to each other like you are used to in programming.
// Compares first value to the second one allowing entering IF clouse if true.
// Otherwise entering ELSE clause if exist.
Handlebars.registerHelper('ifEquals', function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
});
@niksumeiko
niksumeiko / utils.getLocalStorageUsedSpace.js
Last active December 24, 2015 15:49
JavaScript function that calculates allocated localStorage memory. Also applicable to get sessionStorage used memory.
/**
* Returns localStorage (or its particular key) allocated memory
* in Megabytes (MB).
* @param {string=} opt_key inside the storage to calculate
* used space for.
* @return {number} with 2 decimal points.
*/
function getLocalStorageUsedSpace(opt_key) {
var allocatedMemory = 0,
@niksumeiko
niksumeiko / utils.numberToDay.js
Last active February 7, 2020 18:29
JavaScript function that converts a number into the 2 digits day of the month with leading zeros (01-31). Very useful when working with the output that holds days of the month without leading zeros (1-31).
/**
* Turns a number/string to 2 digits day of the month with leading zero.
* @param {number|string} day to turn into day.
* @return {string}
*/
function numberToDay(j) {
return ('0' + j).slice(-2);
}
// Examples:
@niksumeiko
niksumeiko / git.color
Last active August 29, 2015 13:55
Git command to enable colors for all git commands in Mac OSX Terminal.
# By executing this command in your Mac OSX Terminal, all git commands will have
# colors, so you're more comfortable reading git produced output.
git config --global color.ui true
#!/bin/bash
#
# This script will make a webcam snapshot every commit. The jpg file will have
# the commit id as the filename.
#
# This script requires imagesnap. Install with: 'brew install imagesnap'
#
# Put this file in the '.git/hooks/' name it 'post-commit' and chmod it by:
# 'chmod +x .git/hooks/post-commit'
#
@niksumeiko
niksumeiko / git.migrate
Last active April 10, 2025 01:10
Moving git repository and all its branches, tags to a new remote repository keeping commits history
#!/bin/bash
# Sometimes you need to move your existing git repository
# to a new remote repository (/new remote origin).
# Here are a simple and quick steps that does exactly this.
#
# Let's assume we call "old repo" the repository you wish
# to move, and "new repo" the one you wish to move to.
#
### Step 1. Make sure you have a local copy of all "old repo"
### branches and tags.
@niksumeiko
niksumeiko / gslint.conf
Created April 7, 2014 10:53
Sample Closure Linter configuration file used within WebStorm IDE
--strict
--jsdoc
--summary
--beep
--check_html
--nomultiprocess
--debug_indentation
--time
@niksumeiko
niksumeiko / exercises.creditcard.js
Created April 22, 2014 13:12
JavaScript exercise that requires to create credit card input fields validation functions.
/**
* @fileoverview Utils Interface credit card validation methods.
* @author (Wind Tammer)
*/
utils = {};
/** @type {Object<Function>} */
utils.creditcard = {};
@niksumeiko
niksumeiko / catching-promise-errors.js
Last active July 15, 2017 13:32
Catching errors thrown inside Nodejs promise
'use strict';
var Promise = require('promise');
/** @desc A function that returns a promise with an error-prone code. */
function build() {
return new Promise(function(fulfill, reject) {
@niksumeiko
niksumeiko / promises-chaining.js
Last active August 29, 2015 14:23
Promises chaining that solves "callbacks hell"
'use strict';
var Promise = require('promise');
function build() {
return new Promise(function(fulfill, reject) {
fulfill('zero');
});