Skip to content

Instantly share code, notes, and snippets.

View jasdeepkhalsa's full-sized avatar

Jasdeep Khalsa jasdeepkhalsa

View GitHub Profile
@jasdeepkhalsa
jasdeepkhalsa / array-methods-in-javascript-filter-map-reduce.js
Last active October 29, 2024 19:57
Array Methods in JavaScript: Filter, Map, Reduce
var data = ['apple', 'pear', 'orange', 'orange', 'apple', 'lime', 'lime'];
data
.filter((item, index, array) => item !== 'pear') /* Let's filter out all pears (as they are horrible!) */
.reduce((total, item, index, array) => {
if (total.indexOf(item) === -1) total.push(item); // Only add unique items to the array (de-duplication)
return total; // Return the cumulative total
}, [] /* Initial value, can be any primitive e.g. [], boolean etc */)
.map((item, index, array) => item + ' juice') /* Add the word 'juice' to the end */
.forEach((item, index, array) => item + ' ' + index) /* Add an index */
@jasdeepkhalsa
jasdeepkhalsa / cookie.js
Created January 28, 2013 09:26
A complete cookies reader/writer framework with full unicode support by Mozilla
@jasdeepkhalsa
jasdeepkhalsa / examples.html
Created January 3, 2013 14:09
Simple JavaScript Templating by John Resig (with examples)
<!-- Example 1: Normal Template -->
<script type="text/html" id="item_tmpl">
<div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
<div class="grid_1 alpha right">
<img class="righted" src="<%=profile_image_url%>"/>
</div>
<div class="grid_6 omega contents">
<p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p>
</div>
</div>
@jasdeepkhalsa
jasdeepkhalsa / longPolling.js
Last active April 17, 2024 10:59
Simple Long Polling Example with JavaScript and jQuery by Tian Davis (@tiandavis) from Techoctave.com (http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery)
// Long Polling (Recommened Technique - Creates An Open Connection To Server ∴ Fast)
(function poll(){
$.ajax({ url: "server", success: function(data){
//Update your dashboard gauge
salesGauge.setValue(data.value);
}, dataType: "json", complete: poll, timeout: 30000 });
})();
@jasdeepkhalsa
jasdeepkhalsa / socks.txt
Last active February 14, 2024 07:48
Configuring a SOCKS proxy server in Chrome
From: http://www.chromium.org/developers/design-documents/network-stack/socks-proxy
To configure chrome to proxy traffic through the SOCKS v5 proxy server myproxy:8080, launch chrome with these two command-line flags:
--proxy-server="socks5://myproxy:8080"
--host-resolver-rules="MAP * 0.0.0.0 , EXCLUDE myproxy"
The first thing to check when debugging is look at the Proxy tab on about:net-internals, and verify what the effective proxy settings are:
chrome://net-internals/#proxy
@jasdeepkhalsa
jasdeepkhalsa / imageCropAdvanced.php
Created December 19, 2012 20:02 — forked from anonymous/imageCropAdvanced.php
Using PHP and GD to crop an image proportionally according to its aspect ratio. From: http://stackoverflow.com/questions/1855996/crop-image-in-php
<?php
$image = imagecreatefromjpeg($_GET['src']);
$filename = 'images/cropped_whatever.jpg';
$thumb_width = 200;
$thumb_height = 150;
$width = imagesx($image);
$height = imagesy($image);
@jasdeepkhalsa
jasdeepkhalsa / checkType.js
Created December 25, 2012 23:37
Check JavaScript variable type with the .toString method by ExampleJS.com
function isType(arg, type) {
type = type.charAt(0).toUpperCase() + type.substr(1);
return Object.prototype.toString.call(arg) == '[object ' + type + ']';
}
@jasdeepkhalsa
jasdeepkhalsa / JSONPath.js
Last active February 10, 2023 16:20
A useful utility for searching through a JSON object with XPath-like expressions
/* JSONPath 0.8.0 - XPath for JSON
*
* Copyright (c) 2007 Stefan Goessner (goessner.net)
* Licensed under the MIT (MIT-LICENSE.txt) licence.
*/
function jsonPath(obj, expr, arg) {
var P = {
resultType: arg && arg.resultType || "VALUE",
result: [],
normalize: function(expr) {
@jasdeepkhalsa
jasdeepkhalsa / interval.js
Created April 20, 2015 15:26
setTimeout and setInterval with pause and resume
// http://stackoverflow.com/questions/7279567/how-do-i-pause-a-window-setinterval-in-javascript
function RecurringTimer(callback, delay) {
var timerId, start, remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= new Date() - start;
};
@jasdeepkhalsa
jasdeepkhalsa / post-commit.sh
Last active September 13, 2022 17:42
Post-Commit Shell Script for Subversion (SVN) for updating only changed files
#!/bin/bash
# Before using please rename from post-commit.sh to post-commit
# Add this file to the hooks directory in the svn root folder
REPOS="$1"
REV="$2"
# A - Item added to repository
# D - Item deleted from repository
# U - File contents changed