Skip to content

Instantly share code, notes, and snippets.

@chrisross
chrisross / flat.js
Created July 11, 2020 13:47
Simple object flattener.
/**
* Converts an object or array to an 1 dimensional object
* with dot delimited key paths as keys
*
* @param {(Object|Array)} oldObject
* @returns {Object}
*/
const flat = oldObject => {
// This scope is just to create the new object, to avoid mutating state
const newObj = {};
@chrisross
chrisross / basic-fibonacci.rb
Last active February 23, 2023 23:25
Simple Fibonacci sequence generator using Ruby
def fibonacci(limit = 10)
limit.times.each_with_object([0,1]) do |elem, accum|
accum << accum[-1] + accum[-2]
end
end
# Examples
fibonacci
#=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
@chrisross
chrisross / batch_ffplay.sh
Created March 4, 2016 12:16
Loop through directory of videos, playing each one. Useful for scrubbing through a massive collection of video using ffplay.
#!/usr/bin/env bash
# Hotkeys (While playing):
# @see http://linux.die.net/man/1/ffplay
# q, ESC Quit
# f Toggle full screen
# p, SPC Pause
# a Cycle audio channel
# v Cycle video channel
# t Cycle subtitle channel
@chrisross
chrisross / autoexec.py
Last active November 26, 2018 16:51
This script scans for connected usb media, selects the first one and creates a playlist from the USB's top-level directory. Connect to the same network as the raspberry pi and scp the script to /home/osmc/.kodi/userdata (this may vary depending on OS). This is meant for a Raspberry Pi used as a media player, with OSMC installed.
# Autoplay videodirectory
import os, xbmc, time
usb_dir = "/media" # path of external media
delay = 10 # seconds to delay media player
# Collect directories (USBs) in usb directory
usb_list = []
for fname in os.listdir(usb_dir):
fpath = os.path.join(usb_dir, fname)
@chrisross
chrisross / sass_index_guardfile.rb
Last active March 3, 2016 12:10
A simple guardfile for a compass project, that updates each directory within the `sass_dir` with an index file called `_all.scss` in which there are imports for every other scss file in that folder making importation of all files simple. update sass_dir per project.
# Example Directory structure
# /path/to/project
# |───scss
# | |───library
# | | |───_all.scss <------- creates/updates this file
# | | |───_typography.scss
# | | |───_structure.scss
# | | └───_responsive.scss
# | |───modules
# | | |───_all.scss <------- and this file
@chrisross
chrisross / git_diff_tags
Created July 25, 2013 19:23
A Bash alias for a script to list files that have changed or been added between the last two tags.
alias gdt="git tag | sort -bt. -k1,1 -k2,2n -k3,3n -k4,4n -k5,5n | tail -n 2 | xargs git diff --name-only"
@chrisross
chrisross / units_sass_extensions.rb
Last active March 3, 2016 11:53
This Sass extension adds the ability to strip or append units to a number with ease, which can be annoying in Sass. Strip units works on all unit values, eg. 12in, 0.56pt, 800px, 34em, except % but that is already easy to add to unitless numbers.
#!/usr/bin/env ruby
# To implement extension, run this command in a shell:
# $ sass -i -r ./units_sass_extension.rb
# Or require it in compass config
# require './units_sass_extension'
module Sass::Script::Functions
#
# Strips units from a value, e.g. '10px' or '10in' becomes 10
@chrisross
chrisross / gcd_sass_extension.rb
Last active March 3, 2016 11:55
This sass extension utilises the Greated Common Divisor (gcd) algorithm to reduce duplicate styles, especially when making your own grid system.
#!/usr/bin/env ruby
# To implement extension, run this command in a shell:
# $ sass -i -r ./gcd_sass_extension.rb
# Or require it in compass config
# require './gcd_sass_extension'
module Sass::Script::Functions
#
# Uses the 'greatest common divisor' algorithm to work out if a fraction
@chrisross
chrisross / gcd_sass_script.scss
Last active February 23, 2023 23:27
This Sass (SCSS) function utilises the Greated Common Divisor (gcd) to reduce duplicate styles.
// Algorithm to calculate the Lowest Common Denominator
@function gcd($a, $b){
@if $b == 0 {
@return $a;
} @else {
@return gcd($b, $a%$b);
}
}
// Example Use-case:
module Colors
def colorize(text, color_code)
"\033[#{color_code}m#{text}\033[0m"
end
{
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,