Skip to content

Instantly share code, notes, and snippets.

View kn9ts's full-sized avatar
🌠
I am one with the force!

Eugene Mutai kn9ts

🌠
I am one with the force!
View GitHub Profile
@kn9ts
kn9ts / cors.js
Created May 14, 2016 13:51
for CORS in expressjs
// CORS Support in my Node.js web app written with Express
// http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD');
next();
});
@kn9ts
kn9ts / uuid.js
Created April 10, 2016 16:26
a light UUID generator (written in JS)
export default class UUID {
constructor() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
}
@kn9ts
kn9ts / basicServiceWorker.js
Created March 28, 2016 12:51 — forked from adactio/basicServiceWorker.js
A basic Service Worker, for use on, say, a blog.
'use strict';
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// http://creativecommons.org/publicdomain/zero/1.0/
(function() {
// Update 'version' if you need to refresh the cache
var staticCacheName = 'static';
var version = 'v1::';
@kn9ts
kn9ts / base64encode-image.js
Created March 23, 2016 14:18
Base64 encoding of an image in node.js
var fs = require("fs");
var data = fs.readFileSync("../images/html5logos/black.png", "utf8");
console.log(new Buffer(data).toString('base64'));
@kn9ts
kn9ts / inAppBrowserExample.js
Last active August 6, 2020 02:35
An InAppBrowser instance example
var browser = cordova.InAppBrowser.open('http://amazon.com?vitumob_session_id=13279njke12hjhuidhiush124hu', '_blank', 'location=no');
browser.addEventListener('loadstart', function(event) {
alert('Began loading this page: ' + JSON.stringify(event));
});
// event fires when the InAppBrowser finishes loading a URL.
browser.addEventListener('loadstop', function(event) {
// event => {type: 'loadstop', url: 'URL navigated to'}
// alert('InAppBrowser Event: ' + JSON.stringify(event));
@kn9ts
kn9ts / postgres_where_query_builder.py
Created February 16, 2016 18:16
Using a tokenizer and custom function to build complex regex WHERE queries for my search engine
import collections
import re
class Parser(object):
Token = collections.namedtuple('Token', ['typ', 'value'])
def tokenize(self, statement):
token_specification = [
('NUMBER', r'\d+(\.\d*)?'), # Integer or decimal number
# To Freckle work time
function freckle() {
# Set the project ID here
ProjectID=378484 # change it to desired project ID (no spaces please)
adjust="1"
# if a third argument has been provided
if [ $3 ]; then
if [ "${#3}" -eq 6 ]; then
var insert = function(array, rightIndex, value) {
for(var j = rightIndex;
j >= 0 && array[j] > value;
j--) {
array[j + 1] = array[j];
}
array[j + 1] = value;
};
var insertionSort = function(array) {
@kn9ts
kn9ts / selectionSort.js
Created February 3, 2016 12:35
Selection sort loops over positions in the array. For each position, it finds the index of the minimum value in the subarray starting at that position. Then it swaps the values at the position and at the minimum index. Write selection sort, making use of the swap and indexOfMinimum functions.
var swap = function(array, firstIndex, secondIndex) {
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};
var indexOfMinimum = function(array, startIndex) {
var minValue = array[startIndex];
var minIndex = startIndex;
@kn9ts
kn9ts / app.html
Created January 15, 2016 21:06
Flask example for Treehouse workstation
{% extends "layout.html" %}
{% block content %}
<!--Enter Name -->
<div class="enter-name">
<div class="grid-100">
<img src="/static/img/bear_avatar.svg" class="bear-avatar" />
</div>
<div class="grid-100">
<form action="{{ url_for('save') }}" method="POST">
<label>Name your bear</label>