Skip to content

Instantly share code, notes, and snippets.

View sorenlouv's full-sized avatar

Søren Louv-Jansen sorenlouv

View GitHub Profile
@sorenlouv
sorenlouv / range.js
Created December 4, 2015 10:38
Optimized range function for angular
// Extended answer: http://stackoverflow.com/questions/11873570/angularjs-for-loop-with-numbers-ranges/17124017#17124017
// By caching the function result, it can become orders of magnitudes more efficient (depending on how big the range is)
// jsPerf: http://jsperf.com/memoizer-range/9
$scope.range = (function() {
var cache = {};
return function(min, max, step) {
var isCacheUseful = (max - min) > 70;
var cacheKey;
@sorenlouv
sorenlouv / getTime.js
Created December 15, 2015 00:18
Measure time since start
var getTime = (function() {
var start = new Date().getTime();
return function () {
var end = new Date().getTime();
var time = end - start;
return time;
};
})();
@sorenlouv
sorenlouv / signed_request.js
Created January 3, 2016 00:31
Parse signed request from Facebook cookie, and exchange code to access token
var request = require('request-promise');
var crypto = require('crypto');
var config = {...};
function getAccessToken(cookies) {
var cookieName = 'fbsr_' + config.client_id;
var signedRequest = cookies[cookieName];
var code = getCode(signedRequest);
return exchangeCodeForAccessToken(code);
};
@sorenlouv
sorenlouv / facebook-cookie.MD
Last active December 21, 2023 03:48
Get "xs" value from Facebook cookie
@sorenlouv
sorenlouv / facebook-local-app.MD
Last active March 21, 2017 15:49
Getting a Facebook app id to use for fb-sleep-stats

Choose either option A or option B.

Option A: Fast way

Facebook App Id: 435522656639081

Update: Facebook has closed the app above, and you must setup your own Facebook app. Follow the instructions in option B below.

Option B: Setup your own Facebook app

Use this options, if you want to run the service from any other domain than localhost

@sorenlouv
sorenlouv / triangle.js
Last active February 27, 2016 19:33
Pascal's Triangle Example
pascalTriangle(10);
function pascalTriangle(levels) {
return _.reduce(_.range(levels), function(triangle, i) {
var row = _.reduce(_.range(i + 1), function(row, j) {
var isFirst = j === 0;
var isLast = j === i;
if (isFirst || isLast) {
row.push(1);
} else {
@sorenlouv
sorenlouv / batch-promise.js
Last active June 29, 2022 14:31
Execute promises sequentially in batches
var Q = require('q');
function batchPromises(items, fn, options) {
var results = [];
var index = (options.batchSize - 1);
function getNextItem() {
index++;
if (items.length > index) {
var nextItem = items[index];
@sorenlouv
sorenlouv / simple-port-forwarding.js
Last active February 19, 2023 22:06
Simple Port forwarding with Node.js
// npm install http-proxy
var httpProxy = require('http-proxy');
var targetHost = '192.168.99.100';
var port = 8489;
httpProxy.createProxyServer({target:'http://' + targetHost + ':' + port}).listen(port);
```
git reset --hard origin/master@{1}
```
@sorenlouv
sorenlouv / eslint+prettier.MD
Last active April 16, 2018 00:46
ESLint + prettier setup

Install packages

yarn add eslint prettier eslint-{config,plugin}-prettier eslint-plugin-react  --dev --exact

package.json

{
 "scripts": {