Skip to content

Instantly share code, notes, and snippets.

// work in progress
// problem: you push a change to the database now your whole app needs to also update to that change
// one solution is to fetch the data back from the server just after it has changed
// this assumes acid updates, elastic search is not acid, nor are many nosql databases
// this is an impossible solution without acid
// the alternative is to retain an in memory copy of the data and update it optimistically
// if there is an error from the server roll back the in memory change
// if there is no error the update is already in memory, barring any server side transformations not accounted for
// to make this work you need to keep each object in memory over time,
@andrewluetgers
andrewluetgers / LambdaDDBChangeFeedTrigger.js
Last active March 2, 2017 17:30
DynamoDB Simple Change-Feed Lambda Trigger
/**
* @license
* Lodash (Custom Build) lodash.com/license | Underscore.js 1.8.3 underscorejs.org/LICENSE
* Build: `lodash include="isObject,isEqual"`
*/
;(function(){function t(t,e){for(var r=-1,n=null==t?0:t.length,o=0,a=[];++r<n;){var i=t[r];e(i,r,t)&&(a[o++]=i)}return a}function e(t,e){for(var r=-1,n=null==t?0:t.length;++r<n;)if(e(t[r],r,t))return true;return false}function r(t){return function(e){return t(e)}}function n(t){var e=-1,r=Array(t.size);return t.forEach(function(t,n){r[++e]=[n,t]}),r}function o(t){var e=-1,r=Array(t.size);return t.forEach(function(t){r[++e]=t}),r}function a(){}function i(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){
var n=t[e];this.set(n[0],n[1])}}function c(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function s(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function u(t){var e=-1,r=null==t?0:t.length;for(this.__data__=new s;++e<r;)this.add(t[e])}function f(t){this.size=(th
function mapper(data, setType) {
let keys = Object.keys(data),
ret = data.concat ? [] : {};
console.log("keys", keys);
while (keys.length) {
let key = keys.shift(),
obj = data[key];
@andrewluetgers
andrewluetgers / App.js
Last active March 10, 2021 17:39
Image loading with react-konva
import React, { Component } from 'react';
import DevTools from 'mobx-react-devtools';
import Konva from 'konva';
import {Stage, Layer, Rect, Line, Image} from 'react-konva';
import Img from './Img/Img.js';
import './App.css';
import pg from '../assets/scribo-doc-dia-00008061.json'
console.log(pg);
@andrewluetgers
andrewluetgers / normalizer.js
Last active March 1, 2016 23:22
Convert a number in domain A to domain B
// a normalize function constructor for mapping from one range of numbers to another
// actually it is 4 ranges, one from mid to max another from min to mid for each domain
// so given 3 input (domainA) numbers for min, mid, max
// and given 3 output (domainB) numbers or arrays of any length of numbers also for min, mid, max
// the constructor function will return a normalize function that wil convert any number in domainA to domainB
//
// example:
// converting a value from -1 to 1 to a color ramp red to green
// with a white neutral in the middle and alpha evenly interpolated
//
@andrewluetgers
andrewluetgers / Map.js
Last active August 29, 2015 14:18
Map data structure with handy deepGet and deepSet methods https://jsfiddle.net/aq93v4g7/7/
function Map(keys, values) {return new _Map(keys, values)}
function _Map(keys, values) {this.clear(keys, values);}
_Map.prototype = {
set: function(key, value) {
var i = this.indexOf(key);
this._keys[i] = key;
this._values[i] = value;
return i !== this._keys.length;
},
@andrewluetgers
andrewluetgers / forceUpdateOnState.js
Last active August 29, 2015 14:17
Omniscient / Immstruct - forceUpdateOnState
/**
* forceUpdateOnState
*
* @param state immstruct structure
* @returns component mixins {componentWillMount, componentWillUpdate, componentWillUnmount}
*
* the parent component should provide to the target component
* a map of prop names as keys for cursors and period delimited path strings
* on as 'cursors' property, e.g. <div cursors={{foo: 'bar.foo'}}></div>
* in the above example: props.foo = state.reference(['bar', 'foo']).cursor()
@andrewluetgers
andrewluetgers / gist:927fb8a8651d7a713365
Created February 5, 2015 16:38
Webpack + React + Stylus, Dev: Hotloat, Build: File Output
var webpack = require('webpack'),
path = require('path'),
ExtractTextPlugin = require("extract-text-webpack-plugin"),
isDev = process.env.NODE_ENV;
var config = {
cache: true,
resolve: {
extensions: ["", ".js", ".css", ".styl"]
@andrewluetgers
andrewluetgers / gulptfile.js
Created January 29, 2015 06:38
Gulp build for riot.js with sourcemaps
var es = require('event-stream'),
gulp = require('gulp'),
riot = require('gulp-riot'),
rimraf = require('gulp-rimraf'),
jshint = require('gulp-jshint'),
stylus = require('gulp-stylus'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
notify = require("gulp-notify"),
plumber = require('gulp-plumber'),
function jaccardIndex(s1, s2) {
// Jaccard index: http://en.wikipedia.org/wiki/Jaccard_index
// size of the intersection divided by the size of the union of the sample sets
s1 = _.isArray(s1) ? s1 : _.keys(s1);
s2 = _.isArray(s2) ? s2 : _.keys(s2);
return _.intersection(s1, s2).length / _.union(s1, s2).length
}