Skip to content

Instantly share code, notes, and snippets.

View tanyagupta's full-sized avatar

tanyagupta tanyagupta

  • Washington DC
View GitHub Profile
@tanyagupta
tanyagupta / tester_eff_ineff_noob.js
Last active November 4, 2016 17:21
Tester function for efficient and inefficient function
function get_data_set(which){
var all_data_set={
basic:[
{make: "Jeep",model: "Cherokee",trim:"Sport 4dr Front-wheel Drive",year:"2016",wheel:{wheel_size:"17 x 7.5",
wheel_metal:"Aluminum"}},
{make: "Jeep",model: "Compass",trim:"Latitude 4dr Front-wheel Drive",year:"2014",
wheel:{wheel_metal:"Steel"}},
{make: "Jeep",model: "Patriot",trim:"Sport 4dr 4x4"},
{make: "Jeep",model: "Wrangler",trim:"Sahara 2dr 4x4",year: "2015"}
]
@tanyagupta
tanyagupta / and_example_json_data.js
Last active October 28, 2017 11:23
Accessing heterogenous JSON data (using &&) - Part 4
function test_and(){
var cars=[
{make: "Jeep",model: "Cherokee",trim:"Sport 4dr Front-wheel Drive",year:"2016",wheel:{wheel_size:"17 x 7.5",wheel_metal:"Aluminum"}},
{make: "Jeep",model: "Wrangler",trim:"Sahara 2dr 4x4",year: "2015"}
]
for (var i in cars){
if (cars[i]["wheel"] && cars[i]["wheel"]["wheel_size"]){
@tanyagupta
tanyagupta / or_example_json_data.js
Last active November 4, 2016 17:17
Accessing heterogenous JSON data (using or) - Part 3
function test_or(){
var cars=[
{make: "Jeep",model: "Cherokee",trim:"Sport 4dr Front-wheel Drive",year:"2016",
wheel:{wheel_size:"17 x 7.5",wheel_metal:"Aluminum"}},
{make: "Jeep",model: "Wrangler",trim:"Sahara 2dr 4x4",year: "2015"}
]
for (var i in basic){
Logger.log(cars[i]["wheel"] || "") // Logger.log() is Google App Script's console.log
@tanyagupta
tanyagupta / smarter_noob_way_json_data_efficient.js
Last active November 4, 2016 17:19
Accessing heterogenous JSON data (smarter way) - Part 2
//Smarter way to access the data
function noob_smart_way(){
var cars=get_data_set("basic");
var format_string = "%s %s %s %s %s";
for (var i in cars){
Logger.log(format_string, cars[i]["model"] || "",cars[i]["trim"] || "",cars[i]["year"] || "",
cars[i]["wheel"] && cars[i]["wheel"]["wheel_size"] || "",
cars[i]["wheel"] && cars[i]["wheel"]["wheel_metal"] || "")
@tanyagupta
tanyagupta / noob_way_json_data_inefficient.js
Last active November 4, 2016 17:20
Accessing heterogenous JSON data (inefficient way) - Part 1
//Note that this is an INEFFICIENT way to solve the problem
function noob_way() {
// get test data from a test data utility
var cars=get_data_set("basic");
for (var i in cars){
if(cars[i]["model"]!==undefined) {
Logger.log(cars[i]["model"]); // Logger.log() is Google App Script's console.log
@tanyagupta
tanyagupta / client_id_in_ga.js
Last active October 2, 2016 19:04
Tracking users using Google Analytics
/*
The most common way to track users in Google Analytics is by using cookies. But sometimes
there are platform and security limitations that will not allow cookies to be set or
stored for multiple sessions. The following code uses the localStorage property (available
now in most browsers) to provide an alternate way to the cookie-based solution. The code
is heavily commented to help even the most casual users understand the overall workings
of the code. Feel free to ignore this part if you know what you are doing.
This work is based on a Google example:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id
@tanyagupta
tanyagupta / http_collect.js
Created December 6, 2015 21:48
HTTP COLLECT (Exercise 8 of 13)
var bl = require('bl');
var http = require('http');
var url = process.argv[2];
http.get(url, function(res)
{
res.setEncoding("utf8");
res.pipe(bl(function (err,data)
{
if(err)
@tanyagupta
tanyagupta / http_test.js
Created December 5, 2015 23:04
HTTP CLIENT (Exercise 7 of 13)
var http = require('http')
http.get(process.argv[2], function (response) {
response.setEncoding('utf8')
response.on('data', console.log)
response.on('error', console.error)
})
@tanyagupta
tanyagupta / main_modular.js
Created December 5, 2015 22:15
MAKE IT MODULAR (Exercise 6 of 13)
var mod = require('./module1');
mod(process.argv[2], process.argv[3], function (err, listf) {
if (err) {
console.log('Error!')
} else {
for (var i = 0; i < listf.length; i++) {
console.log(listf[i]);
}
}
@tanyagupta
tanyagupta / program.js
Created December 5, 2015 21:47
FILTERED LS (Exercise 5 of 13)
var fs = require('fs')
var path = require('path')
fs.readdir(process.argv[2], function (err, list) {
list.forEach(function (file) {
if (path.extname(file) === '.' + process.argv[3])
console.log(file)
})
})