Skip to content

Instantly share code, notes, and snippets.

View willwright82's full-sized avatar
👾
parsleybox.com

Will Wright willwright82

👾
parsleybox.com
View GitHub Profile
@willwright82
willwright82 / randomSHA1.js
Created October 26, 2017 11:55
Generate a random SHA1 token in browser
// str byteToHex(uint8 byte)
// converts a single byte to a hex string
function byteToHex(byte) {
return ('0' + byte.toString(16)).slice(-2);
}
// str generateId(int len);
// len - must be an even number (default: 40)
function generateId(len) {
var arr = new Uint8Array((len || 40) / 2);
@willwright82
willwright82 / sakamoto-wday.rb
Created August 28, 2017 11:13
Sakamoto Weekday
def calc_wday(year, month, day)
a = (14 - month) / 12
y = year + 4800 - a
m = month + 12 * a - 3
wday = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 + 2
wday = wday % 7
return wday
end
@willwright82
willwright82 / upgradePostgreSQL.sh
Last active August 2, 2017 10:59 — forked from giannisp/gist:b53a76047b07751ed3ade3c1db1d2c51
Upgrade PostgreSQL 9.5.1 to 9.6.3 using Homebrew (macOS)
After automatically updating Postgres to 9.6.1 via Homebrew, the pg_ctl start command didn't work.
The error was something like "database files are incompatible with server".
Database files have to be updated before starting the server, here are the steps that had to be followed:
# need to have both 9.6.1 and latest 9.5.x installed, and keep 9.6.1 as default
brew unlink postgresql
brew install postgresql95
brew unlink postgresql95
brew link postgresql
@willwright82
willwright82 / bogosort.js
Created July 13, 2017 16:48
Bogosort algorithm implementation in Javascript
shuffle = function(v) {
for (var j, x, i = v.length; i; j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
return v;
};
isSorted = function(v) {
for (var i = 1; i < v.length; i++) {
if (v[i - 1] > v[i]) {
return false;
}
@willwright82
willwright82 / pandocConversions.sh
Last active June 1, 2017 12:00
Example pandoc conversions
# From docx to markdown
pandoc -s 'word-file.docx' --wrap=none --reference-links -t markdown -o 'markdown-file.md'
# From markdown to docx
pandoc 'markdown-file.md' -o 'word-file.docx'
# From web page to markdown:
pandoc -s -r html http://www.gnu.org/software/make/ -o 'example.md'
# From markdown to PDF:
@willwright82
willwright82 / deepNest.js
Created May 15, 2017 09:34
Using Javascript to access deeply nested properties
// Access "hello" in:
var state = { prop1: { prop2: "hello" } };
// Simple solution creating temporary variables:
var a = state && state.prop1 && state.prop1.prop2
// a = "hello"
// Elegant solution:
var a = ((state || {}).prop1 || {}).prop2
// a = "hello"
@willwright82
willwright82 / international_phone_regex.sh
Created May 2, 2017 09:41
Regex for checking International phone number formats
\+?\d+(?:\-|\.)?\(?\d+(?:\)|\.)?(?:(?:\-|\.)?\d{3,})+
@willwright82
willwright82 / gitRepoCreate.sh
Created April 23, 2017 20:40
Create a repo using Git API
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
# Remember replace USER with your username and REPO with your repository/application name!
git remote add origin git@github.com:USER/REPO.git
git push origin master
@willwright82
willwright82 / lowercaseMatch.rb
Created April 19, 2017 10:22
Match lowercase postgresql query
user.email = "UsERname@DOMain.com"
User.where("LOWER(email) = LOWER(?)", user.email).first