Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Gunar Gessner gunar

🏠
Working from home
Block or report user

Report or block gunar

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
@gunar
gunar / p2pkhFromXpub.js
Created Jun 14, 2019
Safely Generate Electrum-valid P2PKH Addresses From Your HD Wallet's XPUB key
View p2pkhFromXpub.js
'use strict'
const { HDPublicKey, PublicKey, Address, Networks } = require('bitcore-lib')
const xpubkey = 'xpub...'
const hdPublicKey = HDPublicKey(xpubkey);
// We'll generate the first 20 addresses (default number of addresses that Electrum shows you)
for (let i = 0; i < 20; i++) {
const orderPublicKey = hdPublicKey.deriveChild(`m/0/${i}`)
@gunar
gunar / uncontrolled.tsx
Created Apr 3, 2019
Reset Uncontrolled Forms by Forcing React to Rebuild the DOM.
View uncontrolled.tsx
import React, { useState, createRef } from "react";
const Form = () => {
const emailRef = createRef<HTMLInputElement>()
return (
<div>
Email:
<input type="text" ref={emailRef}/>
<button
onClick={() => {
@gunar
gunar / form.tsx
Created Apr 3, 2019
How to Reset Forms in React.
View form.tsx
import React, { useState } from 'react'
const Form = () => {
const [key, setKey] = useState(0)
return (
<div key={key}>
<input type='text' />
<button onClick={/* ... */}>Submit</button>
<button onClick={() => { setKey(key + 1) }}>Reset</button>
</div>
View tests.js
// Property-based test
describe('properties', () => {
it('is a bijection', () => {
fc.assert(fc.property(
fc.integer(1, Number.MAX_SAFE_INTEGER),
seed => decode(encode(seed)) === seed
))
});
})
View redeclaring_types.ts
// Proof: https://www.typescriptlang.org/play/#src=type%20Woman%20%3D%20%7B%0D%0A%20%20id%3A%20Number%0D%0A%20%20husband%3F%3A%20string%0D%0A%20%20%2F%2F%20Anytime%20you%20need%20to%20add%20a%20new%20field%20here%0D%0A%7D%0D%0A%0D%0A%2F%2F%20You%20have%20to%20update%20this%20vvvvvvvvvvvvvvvvvvvvvvvvvvv%0D%0Aconst%20processMrs%20%3D%20(mrs%3A%20%7B%20id%3A%20Number%2C%20husband%3A%20string%20%7D)%20%3D>%20%7B%0D%0A%20%20%20%20const%20%7B%20husband%20%7D%20%3D%20mrs%0D%0A%20%20%20%20console.log(%60I%20now%20for%20a%20fact%20%24%7Bmrs%7D%20is%20married.%60)%0D%0A%7D%0D%0AprocessMrs(%7B%20id%3A%201%2C%20husband%3A%20'John'%20%7D)%20%20%20%20%2F%2F%20passes%0D%0AprocessMrs(%7B%20id%3A%202%2C%20husband%3A%20undefined%20%7D)%20%2F%2F%20fails%20(with%20strictNullChecks%3A%20true)%0D%0AprocessMrs(%7B%20id%3A%203%20%7D)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20fails%20(with%20strictNullChecks%3A%20true)%0D%0A%0D%0A%2F%2F%20And%20this%20%20%20%20%20%20%20%20%20%20%20%20%20%20vvvvvvvvvv%0D%0
View with_or_without_you.ts
// Proof: https://www.typescriptlang.org/play/#src=type%20Woman%20%3D%20%7B%0D%0A%20%20id%3A%20Number%0D%0A%20%20husband%3F%3A%20string%0D%0A%7D%0D%0A%0D%0Atype%20With%3CT%2C%20K%20extends%20keyof%20T%3E%20%3D%20(Required%3CPick%3CT%2C%20K%3E%3E%20%26%20Exclude%3CT%2C%20K%3E)%0D%0Atype%20Without%3CT%2C%20K%20extends%20keyof%20T%3E%20%3D%20Pick%3CT%2C%20Exclude%3Ckeyof%20T%2C%20K%3E%3E%0D%0A%0D%0Aconst%20processMrs%20%3D%20(mrs%3A%20With%3CWoman%2C%20'husband'%3E)%20%3D%3E%20%7B%0D%0A%20%20%20%20const%20%7B%20husband%20%7D%20%3D%20mrs%0D%0A%20%20%20%20console.log(%60I%20now%20for%20a%20fact%20%24%7Bmrs%7D%20is%20married.%60)%0D%0A%7D%0D%0AprocessMrs(%7B%20id%3A%201%2C%20husband%3A%20'John'%20%7D)%20%20%20%20%2F%2F%20passes%0D%0AprocessMrs(%7B%20id%3A%202%2C%20husband%3A%20undefined%20%7D)%20%2F%2F%20fails%20(with%20strictNullChecks%3A%20true)%0D%0AprocessMrs(%7B%20id%3A%203%20%7D)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20fails%20(with%20strictNullChecks%3A%20true)%0D%0A%0D%0Aconst
View index.js
import { mySort } from './mySort'
console.log(mySort([3, 1, 2]));
// [1, 2, 3]
@gunar
gunar / sort.spec.js
Last active Mar 6, 2019
Standardized Test example.
View sort.spec.js
// Example: Standardized Testing.
assert.deepEqual(mySort([3, 1, 2]), [1, 2, 3]))
assert.deepEqual(mySort([1, -5, 0, 10]) , [-5, 0, 1, 10]))
@gunar
gunar / sort.spec.js
Last active Mar 7, 2019
Property-based Testing example (pseudo-code).
View sort.spec.js
// Property-based Testing example (pseudo-code).
test([Number], anArrayOfNumbers => {
// ↑↑↑↑↑↑↑↑↑↑ ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
// The schema. The generated test-data.
assert.deepEqual(
mySort([anArrayOfNumbers]),
Array.prototype.sort.call(anArrayOfNumbers)
)
})
@gunar
gunar / sort.spec.js
Last active Mar 6, 2019
Example of Property-based Testing using the fast-check library.
View sort.spec.js
import * as fc from 'fast-check';
import { sort } from '../src/sort';
test('should contain the same items', () => {
const count = (tab, element) => tab.filter(v => v === element).length;
fc.assert(
fc.property(fc.array(fc.integer()), data => {
const sorted = sort(data);
assert(sorted.length === data.length);
for (const item of data) {
You can’t perform that action at this time.