Skip to content

Instantly share code, notes, and snippets.

View sagiavinash's full-sized avatar

Sagi Avinash Varma sagiavinash

View GitHub Profile
@sagiavinash
sagiavinash / frontend-guidelines.md
Created April 21, 2017 18:17
Frontend Guidelines

HTML

form inputs

  • use implicit labels instead of explicit labels
<!-- instead of explicit labels -->
<label for="formField">
<input id="formField" type="text" />

<!-- use of implicit labels -->
@sagiavinash
sagiavinash / webpack-config.js
Created April 17, 2017 10:39
webpack-config
const autoprefixer = require('autoprefixer');
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const BundleTracker = require('webpack-bundle-tracker');
const ENV_VARS = {
@sagiavinash
sagiavinash / unit_testing_guidelines.md
Created April 13, 2017 16:18
Unit Testing Guidelines

UNIT TESTING GUIDELINES

File structure

  • All unit test files should be placed under /__tests__
  • Every test file's name should be suffixed with .spec.js

Test Codebase

Every test should convey the following information

  • Unit of work which is being tested
@sagiavinash
sagiavinash / arrow_function_vs_bind.js
Last active April 3, 2016 19:51
Use Arrow functions for event callbacks instead of binded functions
/* preferred way */
handleClick={() => self.handleClick(index)}
//(or)
handleClick={e => self.handleClick(e, index)}
/* current way */
handleClick={this.handleClick.bind(this, index)}
//(or)
handleClick={this.handleClick.bind(null, index)}
@sagiavinash
sagiavinash / binding_namespaced_methods.js
Last active April 2, 2016 12:32
Binding namespaced methods.
/* general way of writing a this bound method */
class SampleComponent extends React.Component {
method = () => {
let props = this.props; // props can be accessed
}
}
/* binding namespaced methods */
// Doesnt Work
@sagiavinash
sagiavinash / trackableSetInterval.js
Last active December 11, 2015 18:27
Utility function to create a setInterval with its running status available
/**
* problem:: there is no way to know if a setInterval is cleared or still running.
* with the following utility function you can create a trackable setInterval.
* http://jsbin.com/dakewamihi/edit?js,console
*/
/* definition */
function setTrackableSetInterval(logicFn, delay, trackerObj) {
var trackerIndex = 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.
* }
*/
@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 / 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 / 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");
}