Skip to content

Instantly share code, notes, and snippets.

View mfaridzia's full-sized avatar
📒
storyteller

Muhammad Farid Zia mfaridzia

📒
storyteller
View GitHub Profile
@sshrpe
sshrpe / FileUpload.html
Created September 20, 2011 10:44
Javascript File upload preview
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>File Upload Preview Test</title>
<script type="text/javascript">
if (window.FileReader) {
var reader = new FileReader(), rFilter = /^(image\/bmp|image\/cis-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x-cmu-raster|image\/x-cmx|image\/x-icon|image\/x-portable-anymap|image\/x-portable-bitmap|image\/x-portable-graymap|image\/x-portable-pixmap|image\/x-rgb|image\/x-xbitmap|image\/x-xpixmap|image\/x-xwindowdump)$/i;
@CristinaSolana
CristinaSolana / gist:1885435
Created February 22, 2012 14:56
Keeping a fork up to date

1. Clone your fork:

git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git

2. Add remote from original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream
@cjohansen
cjohansen / gist:4135065
Created November 23, 2012 10:43
Naming this in nested functions

tl;dr

If you must nest functions in a way that requires access to multiple this', alias outer this to something meaningful - describe the value it's holding. Treat this as the invisible first argument.

In general though, avoiding the situation (nested functions and frivolous use of this) will frequently produce clearer results.

Naming this in nested functions

I was accidentally included in a discussion on how to best name this in nested functions in JavaScript. +1's were given to this suggestion of using _this.

Giving style advice on naming nested this without a meaningful context isn't too helpful in my opinion. Examples below have been altered to have at least some context, although a completely contrived and stupid one.

@eerohele
eerohele / swaparrayelements.js
Last active November 2, 2021 16:01
Swap two array elements (JavaScript)
/**
* Swap the elements in an array at indexes x and y.
*
* @param (a) The array.
* @param (x) The index of the first element to swap.
* @param (y) The index of the second element to swap.
* @return {Array} The input array with the elements swapped.
*/
var swapArrayElements = function (a, x, y) {
if (a.length === 1) return a;
@getify
getify / ex1-prototype-style.js
Last active January 7, 2024 11:58
OLOO (objects linked to other objects) pattern explored (with comparison to the prototype style of the same code)
function Foo(who) {
this.me = who;
}
Foo.prototype.identify = function() {
return "I am " + this.me;
};
function Bar(who) {
Foo.call(this,"Bar:" + who);
@anandsunderraman
anandsunderraman / gist:5852834
Created June 24, 2013 19:34
Regular Expression to Format Currency In Javascript
function formatCurrency(amount)
{
//truncate the amount to 0 decimals
//for every digit that is followed by 3 digits and a word boundary
//add a comma
amount = amount.toFixed(0).replace(/(\d)(?=(\d{3})+\b)/g, "$1,");
return amount;
}
@emre
emre / golang_binary_search.go
Last active October 13, 2020 23:10
binary search implementation on golang slices
package main
import (
"fmt"
)
func BinarySearch(target_map []int, value int) int {
start_index := 0
end_index := len(target_map) - 1
@fjaguero
fjaguero / numberWithThousands.js
Created October 11, 2013 09:28
JS Regex: Adds thousands separator to a number.
// Adds the thousands separator
function numberWithThousands(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
@iwek
iwek / csv-to-json.js
Last active April 24, 2024 19:05
CSV to JSON Conversion in JavaScript
//var csv is the CSV file with headers
function csvJSON(csv){
var lines=csv.split("\n");
var result = [];
var headers=lines[0].split(",");
for(var i=1;i<lines.length;i++){
@miguelmota
miguelmota / getDates.js
Last active February 7, 2024 23:43
Get dates in between two dates with JavaScript.
// Returns an array of dates between the two dates
function getDates (startDate, endDate) {
const dates = []
let currentDate = startDate
const addDays = function (days) {
const date = new Date(this.valueOf())
date.setDate(date.getDate() + days)
return date
}
while (currentDate <= endDate) {