Skip to content

Instantly share code, notes, and snippets.

View sagiavinash's full-sized avatar

Sagi Avinash Varma sagiavinash

View GitHub Profile
@sagiavinash
sagiavinash / debounce-snippet.js
Created April 28, 2015 17:16
Debounce Function Snippet : Plain JS
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
@sagiavinash
sagiavinash / image-center-fill.css
Created April 28, 2015 17:16
image centering - fill configuration
/** crop vertical portions **/
.outer-wrap{
position:relative;
width:100px;
height:100px; /* @OH */
background:#ccc;
overflow:hidden;
}
.inner-wrap{
@sagiavinash
sagiavinash / image-center-fit.css
Created April 28, 2015 17:17
image centering with "fit" configuration
/* Solution 1 (best) */
.wrap{
position:relative;
}
img{
position:abosulute;
top:0;
right:0;
bottom:0;
left:0;
@sagiavinash
sagiavinash / anchorContainer-remove-space.css
Created April 28, 2015 17:18
remove space after image which is inside anchor tag
a{
line-height:0;
/*font-size:0; will not work only line-height*/
}
@sagiavinash
sagiavinash / inline-block-fix.css
Created April 28, 2015 17:18
fix to remove space between inline-block elements due to line-breaks in code
/* Solution 0:
* Avoiding line breaks in code or using multi-line comments as placeholders for line-breaks.
* Its best to adopt this solution if its part of the html build process to remove whitespaces.
*/
/* Solution 1:
* use width=device-width (needed for iphone).
*/
.list-wrapper {
letter-spacing: -0.31em; /* -4px doesnt work in zoomed states */
@sagiavinash
sagiavinash / FunctionProperties.js
Created April 28, 2015 17:21
Function Properties (instead of global variables to store invokation values).
/*
instead of declaring global variables.
var counter = 0;
$(document).on("click", function(){
console.log(counter);
counter++;
});
*/
$(document).on("click", function handler(){
@sagiavinash
sagiavinash / Immediately_invoked_setInterval.js
Last active August 29, 2015 14:20
Immediately invoked setInterval
/* Instead of
interval();
setInterval(interval, 1000);
*/
// -1. Invocation function already defined.
// Never use this even if it looks awesome implicit eval is slow because of an additional parse step.
function interval(){
console.log("Invoked");
}
@sagiavinash
sagiavinash / literallyEqual.js
Last active August 29, 2015 14:20
Check if two values are literally equal
function literallyEqual(value1, value2) {
var _fn = literallyEqual,
result = 1;
_fn.propLength = {};
// getLength method is used to get simplify equality condition from (a subsetOf b && b subsetOf a) to (a subsetOf b && a.length = b.length).
_fn.getLength = function (object) {
if(Object.hasOwnProperty("keys")) return Object.keys(object).length;
// fallback for Object.keys.
var _len = 0, _key;
for (_key in object) { _len++; }
@sagiavinash
sagiavinash / Copying_Arrays.js
Last active August 29, 2015 14:26
Copying an Array (Referenced & Non-Referenced/Snapshot)
// generally for in loops are used to save a non-referenced copy/snapshot of an array. this is a far simpler method to acheive the same
var arr = [1, 2, 3];
// referenced copy of an array.
var arr_ref_copy = arr;
// non-referenced copy or snapshot of an array.
var arr_snapshot = arr.slice(0);
@sagiavinash
sagiavinash / memoize.js
Last active October 22, 2015 08:14
Memoize functions (aync also)
/**
* memoize(fn[, options]) -> returns a new function which memoizes return values for given args.
* Arguments:
* 1. fn: -> function to be memoized. (pass function's promise if it is async).
* 2. options: {
* isAsync -> (boolean), if function to be memoized is async.
* cacheLimit -> (integer), max no. of results that can be stored in cache.
* }
*/