Skip to content

Instantly share code, notes, and snippets.

View typerandom's full-sized avatar

Robin Orheden typerandom

View GitHub Profile
class LocalStorageSet {
constructor(key, fifoMaxEntries = null) {
this.key = key;
this.fifoMaxEntries = fifoMaxEntries;
this.entries = this.load();
}
load() {
const rawValue = window.localStorage.getItem(
this.key,
function getQueryParams() {
const queryString = document.location.search;
if (!queryString) {
return {};
}
return (
// Strip the initial ?
queryString.substring(1)
@typerandom
typerandom / build_trie.py
Created September 3, 2018 13:51
Python function to create a trie from a list of words.
def create_trie(words, tokenize_fn=None, count_key='__count__', is_word_key='__is_word__'):
top_node = {}
if not tokenize_fn:
tokenize_fn = lambda word: list(word)
for word in words:
cursor = top_node
tokens = tokenize_fn(word)
tokens_length = len(tokens)
@typerandom
typerandom / gist:18308e43783e5ade6c5c
Created July 16, 2014 05:21
userapp-facebook-find-connection
$client = User::getClient();
$facebook_id = null;
$connection_result = $client->oauth->connection->search(array(
"user_id" => "self"
));
foreach($connection_result->items as $connection){
if($connection->provider_id == 'facebook'){
@typerandom
typerandom / python-convert-dictionary-to-object
Created November 22, 2013 23:48
Convert a dictionary to an object (recursive).
class DictionaryUtility:
"""
Utility methods for dealing with dictionaries.
"""
@staticmethod
def to_object(item):
"""
Convert a dictionary to an object (recursive).
"""
def convert(item):
@typerandom
typerandom / alphamail-node-js-example.js
Created March 29, 2013 23:24
Send email with Node.js using AlphaMail
var alphamail = require('alphamail');
var emailService = new alphamail.EmailService("API-TOKEN");
var data = {name:"Joe", pwns:true, likesCats:"certainly"};
var payload = new alphamail.EmailMessagePayload()
.setProjectId(1235) // ID of your AlphaMail project
.setSender(new alphamail.EmailContact("My Company", "[email protected]"))
.setReceiver(new alphamail.EmailContact("Some dude", "[email protected]"))
.setBodyObject(data);
@typerandom
typerandom / alphamail-comlang-template.htm
Created November 21, 2012 21:31
AlphaMail - Comlang Template
<html>
<body>
<h2>Hi <# capitalize(payload.user.name) #>!</h2><br><br>
<# capitalize(payload.sender.name) #> (<# payload.sender.id #>) sent you a message.<br>
<p>
<# shorten(payload.message.body, 25) #>
<# if(payload.message.body.length > 25){ #>
<a href="http://www.awesome.com/msg/<# urlencode(payload.message.id) #>/read/">login and read the full message</a>
@typerandom
typerandom / alphamail-simple.php
Created November 21, 2012 20:57
AlphaMail Example
<?php
include_once("comfirm.alphamail.client/emailservice.class.php");
$email_service = AlphaMailEmailService::create()
->setServiceUrl("http://api.amail.io/v1")
->setApiToken("YOUR-ACCOUNT-API-TOKEN-HERE");
$notification = array(
"user" => array(
"name" => "John"
@typerandom
typerandom / swiftmailer-simple.php
Created November 21, 2012 20:39
Swift Mailer - Simple example
<?php
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
@typerandom
typerandom / php-mail-simple.php
Created November 21, 2012 17:28
PHP mail-function - Simple example
<?php
// to, subject, body
mail('[email protected]', 'My Subject', "My awesome body text");
?>