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 / imperative-cycle.js
Last active January 3, 2017 18:20
Imperative-style programming in Javascript
const array = [1, 2, 3, 4, 5];
for (let i=0; i < array.length; i++) {
array[i] = array[i] * 2;
}
console.log(array); // [ 2, 4, 6, 8, 10 ]
@pavel-lens
pavel-lens / declarative-cycle.js
Created January 3, 2017 18:20
Declarative (functional) style programming example
const array = [1, 2, 3, 4, 5];
const array2 = array.map((value) => value * 2);
console.log(array2); // [ 2, 4, 6, 8, 10 ]
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;
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
@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>
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) {
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!
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 })
}
}
import React from 'react'
import classnames from 'classnames'
import './revert-apply.scss'
const getClassNames = (props) => classnames(
'revert-apply',
{
bordered: props.bordered,
},
@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';