Skip to content

Instantly share code, notes, and snippets.

Søren Louv-Jansen sqren

Block or report user

Report or block sqren

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@sqren
sqren / determine-changed-props.js
Last active Feb 11, 2020
Determine which props causes React components to re-render
View determine-changed-props.js
import React, { Component } from 'react';
export default function withPropsChecker(WrappedComponent) {
return class PropsChecker extends Component {
componentWillReceiveProps(nextProps) {
Object.keys(nextProps)
.filter(key => {
return nextProps[key] !== this.props[key];
})
.map(key => {
@sqren
sqren / batch-promise.js
Last active Feb 5, 2020
Execute promises sequentially in batches
View batch-promise.js
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];
@sqren
sqren / useComponentId.js
Last active Jan 17, 2020
React hook for getting a unique identifier for a component
View useComponentId.js
import { useRef } from 'react';
let uniqueId = 0;
const getUniqueId = () => uniqueId++;
export function useComponentId() {
const idRef = useRef(getUniqueId());
return idRef.current;
}
@sqren
sqren / cpu-intensive.js
Last active Jan 2, 2020
A CPU intensive operation. Use to test imitate blocking code, test WebWorkers etc.
View cpu-intensive.js
function mySlowFunction(baseNumber) {
console.time('mySlowFunction');
let result = 0;
for (var i = Math.pow(baseNumber, 7); i >= 0; i--) {
result += Math.atan(i) * Math.tan(i);
};
console.timeEnd('mySlowFunction');
}
mySlowFunction(8); // higher number => more iterations => slower
@sqren
sqren / gist:c948678c090fb6906208545d6a36196d
Last active Dec 17, 2019
Map tilde sign (`) to section sign (§) on MacOS
View gist:c948678c090fb6906208545d6a36196d
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035},{"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064}]}'
@sqren
sqren / .gitconfig
Created Apr 21, 2019
Example .gitconfig file
View .gitconfig
[user]
name = John Doe
email = johndoe@gmail.com
@sqren
sqren / facebook-cookie.MD
Last active Mar 31, 2019
Get "xs" value from Facebook cookie
View facebook-cookie.MD
@sqren
sqren / replace-webpack-alias-with-relative-path.js
Created Apr 29, 2018
Replace webpack aliases with relative paths
View replace-webpack-alias-with-relative-path.js
// 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();
}
@sqren
sqren / read-local-file.js
Created Feb 24, 2019
Bookmark to read local file
View read-local-file.js
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;
@sqren
sqren / github-changed-files-filter.js
Last active Nov 23, 2018
Count lines-of-code changed on Github PR excluding certain files
View github-changed-files-filter.js
/*
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'];
You can’t perform that action at this time.