Skip to content

Instantly share code, notes, and snippets.

@sqren
sqren / useComponentId.js
Last active Oct 21, 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 / determine-changed-props.js
Last active Oct 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 / cpu-intensive.js
Last active Oct 8, 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 / mac-os-mapping-keys-uk-keyboard.md
Last active Jul 28, 2020
Map tilde sign (`) to section sign (§) on MacOS (useful for UK keyboards)
View mac-os-mapping-keys-uk-keyboard.md
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035},{"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064}]}'
@sqren
sqren / batch-promise.js
Last active Jul 27, 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 / simple-port-forwarding.js
Last active Jul 24, 2020
Simple Port forwarding with Node.js
View simple-port-forwarding.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);
@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 / facebook-cookie.MD
Last active Apr 7, 2020
Get "xs" value from Facebook cookie
View facebook-cookie.MD
@sqren
sqren / jest-run-timers-until-resolved.ts
Last active Apr 1, 2020
Simplifies testing promise-returning-functions that executes timers (setTimeout / setInterval). The helper `runTimersUntilResolved` will run the timers repeatedly until the promise resolves. Jest helper.
View jest-run-timers-until-resolved.ts
/*
* Run timers (setInterval/setTimeout) every tick continuously until the promise has been resolved
*/
async function runTimersUntilResolved(fn: () => Promise<any>) {
jest.useFakeTimers();
let isResolved = false;
const p = fn();
p.finally(() => (isResolved = true));
@sqren
sqren / .gitconfig
Created Apr 21, 2019
Example .gitconfig file
View .gitconfig
[user]
name = John Doe
email = johndoe@gmail.com
You can’t perform that action at this time.