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 / read-local-file.js
Created February 24, 2019 22:15
Bookmark to read local file
copy(
encodeURIComponent(`(function() {
function readLocalFile(e) {
const file = e.target.files[0];
if (!file) {
alert('No file selected!')
}
const reader = new FileReader();
reader.onload = function(e) {
const contents = e.target.result;
@sorenlouv
sorenlouv / github-changed-files-filter.js
Last active November 23, 2018 14:33
Count lines-of-code changed on Github PR excluding certain files
/*
The number of changed files (additions/deletions) includes all files.
I wanted to see how many files I had changed, excluding tests and test snapshots.
This filter will help you do that.
To use it, go to the PR, navigate to "Files" and invoke the following
*/
(function() {
const excludeWords = ['test', 'mock-responses', 'typings'];
const hypercore = require('hypercore');
const discovery = require('discovery-swarm');
const multifeed = require('multifeed');
const pump = require('pump');
const suffix = process.argv[2];
const db = `./multichat-${suffix}`;
console.log(`Using db: ${db}`);
const multi = multifeed(hypercore, db, { valueEncoding: 'json' });
@sorenlouv
sorenlouv / puppeteer-random-page.js
Last active September 6, 2018 13:17
Infinitely click around page
# yarn add puppeteer
# node ./puppeteer-random-page.js
const puppeteer = require('puppeteer');
const initialUrl = process.env.url || 'http://localhost:8080';
async function init() {
const browser = await puppeteer.launch({ headless: true });
return browser.newPage();
}
@sorenlouv
sorenlouv / package.json
Last active May 6, 2018 20:25
Browserify, Babel and React
{
"scripts": {
"browserify": "browserify --standalone myModuleName ./src/index.js | uglifyjs --compress --source-map --output dist/index.min.js"
},
"browserify": {
"transform": [
[
"babelify",
{
"presets": [
@sorenlouv
sorenlouv / replace-webpack-alias-with-relative-path.js
Created April 29, 2018 17:18
Replace webpack aliases with relative paths
// Usage: jscodeshift -t replace-webpack-alias-with-relative-path.js ./kibana/x-pack/plugins ./kibana/src
const path = require('path');
const URI = require('urijs');
function getRelativePath(currentFilePath, dependencyPath) {
return URI(dependencyPath)
.relativeTo(currentFilePath)
.toString();
}
@sorenlouv
sorenlouv / demo.js
Last active April 25, 2018 19:30
Recursively Iterate a nested structure and render strings as mustache templates
const input = {
email: {
subject: 'Happy birthday {{name}} 🎂',
body: 'Hi {{name}}, you are turning {{age}} today!'
}
};
const ctx = { name: 'Søren', age: 30 };
const tmpl = renderMustache(input, ctx);
// tmpl: {"email": {"subject": "Happy birthday Søren 🎂", "body": "Hi Søren, you are turning 30 today!"}}
@sorenlouv
sorenlouv / chromeless-random.js
Last active September 6, 2018 12:52
Randomly click around a webpage
// Deprecated. Use puppeteer instead: https://gist.github.com/sqren/94ab2b9c5e88c1c119182425b19bcd59
const { Chromeless } = require('chromeless');
const initialUrl = process.env.CHROMELESS_URL || 'https://www.dr.dk';
const chromeless = new Chromeless({
launchChrome: true
});
function sleep(ms) {

Keybase proof

I hereby claim:

  • I am sqren on github.
  • I am sqren (https://keybase.io/sqren) on keybase.
  • I have a public key ASBUUQc0dmZTpHrgymt-8rvKrYuYbPx7R9WnAwNK6-F61wo

To claim this, I am signing this object:

@sorenlouv
sorenlouv / mock-router-and-redux.js
Last active January 9, 2018 21:39
Approaches to mocking React-router and Redux store
import { mount } from 'enzyme';
import React from 'react';
import { createMockStore } from 'redux-test-utils';
import { Provider } from 'react-redux';
import { Router } from 'react-router-dom';
import createHistory from 'history/createHashHistory';
import PropTypes from 'prop-types';
export const mountWithRouterAndStore = (Component, storeState = {}) => {
const store = createMockStore(storeState);