Skip to content

Instantly share code, notes, and snippets.

View kavitshah8's full-sized avatar

Kavit Shah kavitshah8

  • Adobe
  • Bay Area, CA
View GitHub Profile
// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
this.lastName = ko.observable("Bertington");
this.capitalizeLastName = function() {
var currentVal = this.lastName(); // Read the current value
this.lastName(currentVal.toUpperCase()); // Write back a modified value
};
}
// Activates knockout.js
var parts = ['shoulders', 'knees'];
var parts2 = ['head', 'and', 'toes'];
// ES 5
var lyrics = parts2.slice(0, 1).concat(parts).concat(parts2.slice(1, 3));
console.log(lyrics); // ["head", "shoulders", "knees", "and", "toes"]
// ES 6
var lyrics = [parts2[0], ...parts, parts2[1], parts2[2]];
console.log(lyrics); // ["head", "shoulders", "knees", "and", "toes"]
// Mutated Version
const combineReducers = reducers => {
return (state = {}, action) => {
return Object.keys(reducers).reduce((nextState, key) => {
nextState[key] = reducers[key](state[key], action)
return nextState
}, {})
}
};
@kavitshah8
kavitshah8 / uuid.js
Created February 28, 2016 07:58
Node
// npm i node-uuid
var uuid = require('node-uuid');
// Generate a v1 (time-based) id
uuid.v1();
// Generate a v4 (random) id
uuid.v4();
@kavitshah8
kavitshah8 / arrow.js
Created February 22, 2016 18:40
arrow functions
function ViewSomething() {
this.a = 42
this.click = function() => {
alert(this.a) // undefined
}
this.click1 = () => {
alert(this.a) // 42
@kavitshah8
kavitshah8 / chain-1.js
Last active May 24, 2018 05:59
Functional Programming in Javascript
'use strict';
function chain() {
var newReleases = [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0
}, {
"id": 654356453,
"title": "Bad Boys",
@kavitshah8
kavitshah8 / map.js
Last active March 18, 2017 20:09
Closures
'use strict';
function mul(x) {
return x * x;
}
function mulWVariance(variance, x) {
return x * x + variance;
}
'use strict';
var express = require('express');
var compression = require('compression');
var app = express();
var oneYear;
// gzip the static resources before seding to browser, if the browser supports gzip compression
@kavitshah8
kavitshah8 / debounce.js
Last active March 14, 2017 05:53
Debounce
// 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.
function debounce (cb, wait) {
var timeoutID
return function () {
clearTimeout(timeoutID)
timeoutID = setTimeout(cb, wait)
}
@kavitshah8
kavitshah8 / short-circuit-operator.js
Last active December 13, 2015 03:28
Logical operators
'use strict'
function f1 () {
console.log('f1 called');
return false;
}
function f2 () {
console.log('f2 called');