Skip to content

Instantly share code, notes, and snippets.

@hattmarris
hattmarris / linux.sh
Last active September 25, 2015 08:43
Linux Commands
# ubuntu tree
sudo apt-get install tree
# print to terminal in tree format
$ tree [DIRECTORY]
# example [csv] directory outputs:
├── csv
│   ├── CVS
│   │   ├── Entries
@hattmarris
hattmarris / array-filter.js
Created August 19, 2015 22:06
Array Filter
// Array.prototype.filter
function containsTest(value) {
return value.indexOf('test') != -1;
}
var a = ['a', 'b', 'c', 'a_test'];
var filtered = a.filter(containsTest);
// filtered = ['a_test'];
@hattmarris
hattmarris / php-cli.php
Last active November 10, 2015 20:30
Command Line PHP
<?php
/**
* Run PHP in terminal / command line:
* $ php -a
*/
// Get input from stdin command line
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
@hattmarris
hattmarris / nodejs.sh
Last active April 25, 2016 18:01
Nodejs
# nvm - node version manager
$ nvm use 0.0.1 # select 0.0.1 from installed version
$ nvm ls # list versions installed
$ nvm ls-remote # list versions available
$ nvm alias default 0.0.1 # set 0.0.1 version as default
$ nvm use default
$ nvm help
# node - node js package (nodejs ubuntu 14.04)
$ node -v # show version
@hattmarris
hattmarris / netstat.sh
Last active September 13, 2015 01:26
netstat
# netstat
$ netstat -tulpn # [--tcp|-t] [--udp|-u] [--listening|-l] [--numeric|-n] [--program|-p]
# Active Internet connections (only servers)
# Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
# tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 22535/mysqld
# tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 16902/sshd
# tcp6 0 0 :::80 :::* LISTEN 4969/apache2
# tcp6 0 0 :::22 :::* LISTEN 16902/sshd
# udp 0 0 0.0.0.0:15213 0.0.0.0:* 580/dhclient
// Log charCodes: string from keyboard events
for(i=0; i<222; i++){
console.log(i + ': ' + String.fromCharCode(i));
}
@hattmarris
hattmarris / cake.php
Last active November 4, 2015 21:43
Cake Helpful Functions
<?php
/**
* Get default data source and log last run SQL query
*/
function getLastQuery() {
$dbo = ConnectionManager::getDataSource('default');
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
@hattmarris
hattmarris / time.php
Created November 10, 2015 20:33
PHP Time
<?php
/**
* Time difference logging function
*
* $start time from PHP microtime(true) // returns seconds as float
* $end time from PHP microtime(true)
*/
function logTimeDiff($start, $end) {
$seconds = $end - $start;
$milli = $seconds / 1000;
@hattmarris
hattmarris / parseJson.js
Last active December 22, 2015 00:12
parse JSON
/**
* Parse JSON using JavaScript
*/
// arbitrary string "bunch o' junk"
var string = '{"menu":{"id":"file","value":"File","popup":{"menuitem":[{"value":"New","onclick":"CreateNewDoc()"},{"value":"Open","onclick":"OpenDoc()"},{"value":"Close","onclick":"CloseDoc()"}]}}}';
JSON.parse(string);
// parsed and printed in a more "human readable" format
{
@hattmarris
hattmarris / mysql.sql
Created December 22, 2015 21:59
MySQL
# tabular view of data with scrolling
mysql> pager less -SFX
mysql> SELECT * FROM sometable;