Skip to content

Instantly share code, notes, and snippets.

View geraldyeo's full-sized avatar
🎯
Focusing

Gerald Yeo geraldyeo

🎯
Focusing
View GitHub Profile
@geraldyeo
geraldyeo / gist:544154d7be7569bd54517f26b9868329
Created October 25, 2017 03:14
NGINX feature deployment
# default branch to serve
set $branch "master";
# Serve features on *.app.dev subdomain
if ($host ~* ^([^.]+)\.app\.dev\.company\.com$) {
set $branch $1;
}
# All branches are hosted from a subdirectory
root /var/www/branches/$branch;
@geraldyeo
geraldyeo / gist:d9a9fda43c3066fb49507f850dfd82dd
Created April 28, 2017 02:25
disable git hooks temporarily
gitdir="$(git rev-parse --git-dir)"
hook="$gitdir/hooks/post-commit"
# disable post-commit hook temporarily
[ -x $hook ] && chmod -x $hook
git commit -a -m "Commit all changes for testing. Will be reverted automatically"
# enable it again
chmod +x $hook
@geraldyeo
geraldyeo / clamp.js
Created December 27, 2016 05:48
Clamp number between min and max
function clamp(val, min, max) {
return Math.min(Math.max(min, val), max)
}
@geraldyeo
geraldyeo / box-sizing-reset.css
Created September 20, 2016 09:42
Inheriting box-sizing as best practice
/*
* This will give you the same result, and make
* it easier to change the box-sizing in plugins
* or other components that leverage other behavior.
* @see https://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/
*/
html {
box-sizing: border-box;
}
*, *:before, *:after {
@geraldyeo
geraldyeo / index-of.js
Last active August 3, 2016 04:43
Write a function that takes two strings, and returns the index of the first occurrence of one string in the other.
// Write a function that takes two strings, and returns the index of the first occurrence of one string in the other
// Do not use built-in functions: indexOf, substr, substring, array.split
function indexof(strToFind, str) {
var len1 = str.length;
var len2 = strToFind.length;
var i, j, numCorrect;
for (i=0; i<len1; i++) {
numCorrect = 0;
@geraldyeo
geraldyeo / string-reverse.js
Created July 31, 2016 02:22
Reverse a string with recursion
function reverse(str) {
if (str === '')
return '';
else
return reverse(str.substr(1)) + str.charAt(0);
}
reverse("hello, world");
@geraldyeo
geraldyeo / max-len-sequence.js
Created July 31, 2016 01:33
Find the length of maximum number of consecutive numbers jumbled up in an array.
// Find the length of maximum number of consecutive numbers jumbled up in an array.
// e.g.: 1, 94, 93, 1000, 2, 92, 1001 should return 3 for 92, 93, 94
var numbers = [1, 94, 93, 1000, 2, 92, 1001];
var sequences = [];
var count = 1;
var diff;
var largest;
// sort
numbers.sort(function(a, b){
@geraldyeo
geraldyeo / interweave.js
Created July 28, 2016 12:28
Interwave letters
// Given two strings, print all the inter-leavings
// of the Strings in which characters from two strings
// should be in same order as they were in original strings.
//
// e.g.
// for "abc", "de", print all of these:
// adebc, abdec, adbce, deabc, dabce, etc, etc
var s1 = 'abc'
var s2 = 'de'
@geraldyeo
geraldyeo / redis.sh
Created July 21, 2016 06:21
Install and config Redis on Mac OS X via Homebrew
# Launch Redis on computer starts.
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
# Start Redis server via “launchctl”.
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
# Start Redis server using configuration file.
redis-server /usr/local/etc/redis.conf
# Stop Redis on autostart on computer start.
@geraldyeo
geraldyeo / hex-rgb-hsl.js
Created June 29, 2016 03:31
Regex for HEX/RGB(A)/HSL(A)
// Use simple one, if you need to find a definitely correct HEX/RGB(A)/HSL(A)
// color value among random text (limitations are set by its initial correctness):
/(#[\d\w]+|\w+\((?:\d+%?(?:,\s)*){3}(?:\d*\.?\d+)?\))/i
// …or use complex one instead, if you need to find (or not) one (or several)
// HEX/RGB(A)/HSL(A) color value(s) that has to be verified:
/(#(?:[\da-f]{3}){1,2}|rgb\((?:\d{1,3},\s*){2}\d{1,3}\)|rgba\((?:\d{1,3},\s*){3}\d*\.?\d+\)|hsl\(\d{1,3}(?:,\s*\d{1,3}%){2}\)|hsla\(\d{1,3}(?:,\s*\d{1,3}%){2},\s*\d*\.?\d+\))/gi