Skip to content

Instantly share code, notes, and snippets.

View freekrai's full-sized avatar

Roger Stringer freekrai

View GitHub Profile
@freekrai
freekrai / time
Created October 12, 2016 22:19
convert timestamps
function toUserTimeZone(utc_date, offset){
if (utc_date.slice(-3) !== "UTC") {
utc_date += " UTC";
}
var local_date = new Date(utc_date);
var local_offset = local_date.getTimezoneOffset() * 60000;
var utc_time = local_date.getTime() + local_offset;
return new Date(utc_time + (3600000 * offset));
}
@freekrai
freekrai / todo.html
Last active June 25, 2016 06:20
reactfly
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React Tutorial</title>
<link rel="stylesheet" href="css/base.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.16/browser.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
@freekrai
freekrai / queue.js
Last active October 10, 2016 16:29
JS queues
/* Creates a new queue. A queue is a first-in-first-out (FIFO) data structure -
* items are added to the end of the queue and removed from the front.
*/
function Queue(){
// initialise the queue and offset
var queue = [];
var offset = 0;
// Returns the length of the queue.
@freekrai
freekrai / leftpad.js
Last active March 24, 2016 03:00
various utils
function leftpad (str, len, ch) {
str = String(str);
var i = -1;
if (!ch && ch !== 0) ch = ' ';
len = len - str.length;
while (++i < len) {
str = ch + str;
}
return str;
}
@freekrai
freekrai / notify.html
Created March 14, 2016 17:50
push notifications in browser
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/notification -->
<script src="notify.js"></script>
<button onclick="notifyMe()">Notify me!</button>
@freekrai
freekrai / gist:5cf5e85456494f5265ce
Created March 9, 2016 00:59 — forked from vladimirtsyupko/gist:10964772
Git force pull to overwrite local files
git fetch --all
git reset --hard origin/master
git pull origin master
@freekrai
freekrai / 0_reuse_code.js
Created March 8, 2016 04:43
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@freekrai
freekrai / install_ruby_1.9.3
Last active February 14, 2016 18:00 — forked from slouma2000/install_ruby_1.9.3
Install Ruby 1.9.3 on CentOS, RedHat using RVM
Step 1: Upgrade Packages
# yum update
# yum -ygroupinstall "Development Tools"
Step 2: Installing Recommended Packages
# yum -y install gcc-c++ patch readline readline-devel zlib zlib-devel
# yum -y install libyaml-devel libffi-devel openssl-devel make
# yum -y install bzip2 autoconf automake libtool bison iconv-devel
Step 3: Install RVM ( Ruby Version Manager )
@freekrai
freekrai / geo.js
Last active February 1, 2016 07:12
collection of functions for calculating distances
function CalculateDistance(lat1, long1, lat2, long2) {
// Translate to a distance
var distance =
Math.sin(lat1 * Math.PI) * Math.sin(lat2 * Math.PI) +
Math.cos(lat1 * Math.PI) * Math.cos(lat2 * Math.PI) * Math.cos(Math.abs(long1 - long2) * Math.PI);
// Return the distance in miles
//return Math.acos(distance) * 3958.754;
// Return the distance in meters
@freekrai
freekrai / not-bad-code.js
Created January 30, 2016 17:06 — forked from domenic/not-bad-code.js
Avoiding explicit promise construction antipattern
function getUserDetail(username) {
if (userCache[username]) {
return Promise.resolve(userCache[username]);
}
// Use the fetch API to get the information
return fetch('users/' + username + '.json')
.then(function(result) {
userCache[username] = result;
return result;