Skip to content

Instantly share code, notes, and snippets.

View andrit's full-sized avatar

Andrew Ritter andrit

View GitHub Profile
@andrit
andrit / reactContextPattern.md
Created August 2, 2018 13:40
context api in react

Context Consumer Component

 <BasicContextConsumer >
                    {({state, actions}) => (
                    <React.Fragment>
                        <div className="row">
                            <FormGroup
                                controlId="desc-field"
                            >
                                <h3>Description</h3>

function findbyport() { sudo lsof -P -iTCP:$1 -sTCP:LISTEN }

@andrit
andrit / object2ArrayJS.md
Last active July 27, 2018 16:28
convert object to an array in js
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map(function(key) {
  return [Number(key), obj[key]];
});

console.log(result);

or

@andrit
andrit / phprandomString.md
Created July 27, 2018 14:55
simple php random string generator
 public function randomString($length) {
        $key = '';
        $keys = array_merge(range(0, 9), range('a', 'z'));
    
        for ($i = 0; $i < $length; $i++) {
            $key .= $keys[array_rand($keys)];
        }
    
 return $key;
@andrit
andrit / jsRouterSmall.md
Created July 26, 2018 14:50
js router with pushState from http://krasimirtsonev.com/blog/article/A-modern-JavaScript-router-in-100-lines-history-api-pushState-hash-url . There is no Github of this so Im saving it here so I can check it out further/use it
var Router = {
    routes: [],
    mode: null,
    root: '/',
    config: function(options) {
        this.mode = options && options.mode && options.mode == 'history' 
                    && !!(history.pushState) ? 'history' : 'hash';
        this.root = options && options.root ? '/' + this.clearSlashes(options.root) + '/' : '/';
        return this;
const axios = require('axios');
const FormData = require('form-data');

const form = new FormData();
// Second argument  can take Buffer or Stream (lazily read during the request) too.
// Third argument is filename if you want to simulate a file upload. Otherwise omit.
form.append('field', 'a,b,c', 'blah.csv');
axios.post('http://example.org/endpoint', form, {
 headers: form.getHeaders(),
function getBase64(file) {
   var reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = function () {
     console.log(reader.result);
   };
   reader.onerror = function (error) {
     console.log('Error: ', error);
 };

React component form field update of nested state for any text field in form

this.state={
user:{
  name: '',
  age: '',
  
}
}

use apply to spread out arrays of values as parameters to a function call

function foo(a, b){
console.log( "a:" + a ", b:" + b );
}

foo.apply(null, [2, 3]);  //a:2, b:3
function logAndReturn(func) {
  return function() {
    var args = Array.prototype.slice.call(arguments);
    var result = func.apply(null, args);
    console.log('Result', result);
    return result;
  }
}