Skip to content

Instantly share code, notes, and snippets.

View pavel-lens's full-sized avatar

pavel-lens pavel-lens

  • Ether
View GitHub Profile
@pavel-lens
pavel-lens / backoff.ts
Created April 13, 2020 10:37
Implementation of function backoff which will retry to run a function with exponentially increased delay. Useful for network operations.
interface EmptyFunction {
(): Promise<any>
}
function wait(duration: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, duration))
}
function backoffOrig(retries: number, callbackFn: EmptyFunction, delay: number = 500): Promise<any> {
return callbackFn().catch((err) =>

Keybase proof

I hereby claim:

  • I am svitekpavel on github.
  • I am m_ax (https://keybase.io/m_ax) on keybase.
  • I have a public key ASD-_gQNI_HB__SXpRyaSO9Audz5omvezbDwftuxh8fJlAo

To claim this, I am signing this object:

@pavel-lens
pavel-lens / secure-p2p-communication-using-elliptic-curve-cryptography.spec.js
Created January 31, 2019 17:11
How to setup a secure way to exchange data in insecure P2P environment using elliptic curve cryptography
const aes256 = require('aes256');
// const pbkdf2 = require('pbkdf2');
const { ec: EC } = require('elliptic');
const { expect } = require('chai');
const key = 'my passphrase';
const plaintext = 'my plaintext message';
// const MASTER_PASSWORD = 'cornflake12';
// const SALT = '2019-01-29T21:54:56.015Z';
import React from 'react'
import classnames from 'classnames'
import './revert-apply.scss'
const getClassNames = (props) => classnames(
'revert-apply',
{
bordered: props.bordered,
},
handleApplyClick() {
const { onApplyClick } = this.props
const result = onApplyClick()
// Don't update state if callback is signaling failing behaviour
if (result !== false) {
this.setState({ touched: false })
}
}
renderWrappedChildren(children) {
// Traverse through all children with pretty functional way :-)
return React.Children.map(children, (child) => {
// This is support for non-node elements (eg. pure text), they have no props
if (!child.props) {
return child
}
// If current component has additional children, traverse through them as well!
createHandleChange(callback) {
// This has to be a ES5 function, because we need to access its `arguments`
return (function() {
// don't run for children that has no onChange prop
if (!callback) {
return
}
// set touched to true so we can re-render active Apply/Revert buttons
if (!this.state.touched) {
@pavel-lens
pavel-lens / revert-apply-usage.jsx
Created April 19, 2017 16:01
Revert/Apply component in action
<RevertApply onApplyClick={this.handleApplyClick} onRevertClick={this.handleRevertClick} >
<Label title="Parameter 1">
<Textbox value={param1} onChange={e => this.handleChange('param1', e)} />
</Label>
<Label title="Parameter 2">
<Textbox value={param2} onChange={e => this.handleChange('param2', e)} />
</Label>
</RevertApply>
function sumDigPow(start, stop) {
// create array from start to stop
const arr = Array.apply(null, Array(stop-start)).map((_,i) => i+start);
function calc(num) {
const n_arr = String(num).split('').map(Number).map((n,i) => Math.pow(n,i+1));
return n_arr.reduce((acc,n) => acc+n);
}
// create new array with numbers that match criteria
function sumDigPow(a, b) {
const arr = [];
for (let i = a; i <= b; i++) {
let sum = 0;
for (let j = 0; j <= String(i).length; j++) {
sum += Math.pow(parseInt(String(i)[j]), j+1);
if (sum == i) arr.push(i);
}
}
return arr;