Skip to content

Instantly share code, notes, and snippets.

View drowzy's full-sized avatar
🤠
Focusing

Simon Thörnqvist drowzy

🤠
Focusing
View GitHub Profile
@drowzy
drowzy / titlecase.js
Last active December 17, 2015 14:59
javascript titlecase
function toTitleCase(str) {
return str.replace(/^\s+|\s$/g, '').replace(/\b([a-z])/g, function (_, txt) {
return txt.toUpperCase();
});
};
@drowzy
drowzy / DispatchBeforeRoute.jsx
Last active February 25, 2018 12:07
React router execute action before routing
import React, { Component } from 'react';
import { Route } from 'react-router-dom';
export class DispatchWrapper extends Component {
componentDidMount() {
this.props.action();
}
render() {
let { component: Component, action, ...rest } = this.props;
return <Component {...rest} />;
@drowzy
drowzy / SSHt.ex
Last active March 21, 2018 22:45
ssh connect
defmodule SSHt do
def connect(opts \\ []) do
host = Keyword.get(opts, :host, "127.0.0.1")
port = Keyword.get(opts, :port, 22)
ssh_config = defaults(opts)
:ssh.connect(String.to_charlist(host), port, ssh_config)
end
defp defaults(opts) do
defmodule SSHt do
@ini_window_size 1024 * 1024
@max_packet_size 32 * 1024
@direct_tcpip String.to_charlist("direct-tcpip")
def connect(opts \\ []) do
host = Keyword.get(opts, :host, "127.0.0.1")
port = Keyword.get(opts, :port, 22)
ssh_config = defaults(opts)
defmodule SSHt.Application do
@moduledoc """
Application module
"""
use Application
def start(_type, _args) do
children = [
{DynamicSupervisor, name: SSHt.TunnelSupervisor, strategy: :one_for_one}
]
defmodule SSHt.Tunnel do
def start_link(ref, to) do
DynamicSupervisor.start_child(
SSHt.TunnelSupervisor,
{SSHt.Tunnel.TCPServer, worker_opts(ref, to)}
)
end
defp worker_opts(ref, {:tcpip, {port, _}} = to),
do: basic_opts(ref, base_name(port), to)
defmodule SSHt.Tunnel.TCPHandler do
use GenServer
require Logger
def start_link(ref, socket, transport, opts) do
pid = :proc_lib.spawn_link(__MODULE__, :init, [{ref, socket, transport, opts}])
{:ok, pid}
end
def init({ref, socket, transport, opts}) do
defmodule SSHt.Tunnel do
@type to :: {:tcpip, tuple()} | {:local, String.t}
@spec start_link(pid(), to) :: {:ok, pid()} | {:error, term()}
def start_link(ref, to) do
DynamicSupervisor.start_child(
SSHt.TunnelSupervisor,
worker_spec(worker_opts(ref, to))
)
end
@drowzy
drowzy / without.js
Created April 11, 2018 11:13
es6 dynamic key without
const obj = { foo: 'foo', bar: 'bar' };
const key = 'foo';
const { [key]: _, ...newObj } = obj;