Skip to content

Instantly share code, notes, and snippets.

@brianmcallister
brianmcallister / uniqueByKey.ts
Created Jun 17, 2020
Unique list of JavaScript objects.
View uniqueByKey.ts
interface Item {
type: string;
}
const listA: Item[] = [
{ type: 'a' },
{ type: 'b' },
{ type: 'c' },
];
const listB: Item[] = [
@brianmcallister
brianmcallister / scale-range.js
Created Oct 17, 2016
Scale an input number between an input range proportionally down to be in an output range.
View scale-range.js
// @flow
/**
* Scale an input number between an input range proportionally down to be
* in an output range.
*
* @param {number} input - Input number.
* @param {Array<number, number>} inputRange - The input range.
* @param {Array<number, number>} outputRange - The output range.
*
@brianmcallister
brianmcallister / coords.js
Created Aug 15, 2016
Get the position of a point on a circle.
View coords.js
/**
* Get the position of a point on a circle.
*
* @param {number} radius - Radius of the circle.
* @param {number} angle - Angle of the point to calculate.
* @param {number} offset - How far outside the circle the point should fall.
* @param {number} dotSize - Adjust for the size of the dot.
*
* @returns {object} results for top and left coords.
*/
@brianmcallister
brianmcallister / vwap.js
Last active Aug 25, 2021
VWAP (Volume Weighted Average Price) function.
View vwap.js
/**
* Calculate VWAP (volume weighted average price) for a list of bids.
*
* @param {number} total - Total shares bought.
* @param {Array<number, number>} positions - Tuples of position data. The first
* index is the amount of shares, the
* second index is the price of the
* shares.
*
* @example
@brianmcallister
brianmcallister / overridePropValidation.js
Created Mar 30, 2016
Override React's `isRequired` prop type validation. Allows you to express "If not X, than A, B, C".
View overridePropValidation.js
// Override the `isRequired` validation of a list of props for another, single
// prop. For example, if you had a number of props used to generate a URL path,
// you could require all of them *unless* a full path was passed as a prop.
//
// @param {array} toOverride - Array of strings to 'override', or ignore.
// @param {function} [type] - PropType validation function. Optional.
// @param {boolean} [required] - Validate the prop as required. Optional.
//
// @returns {function} Callback function for custom prop validation.
// See the `customProp` in http://bit.ly/1nhwVQ3.
@brianmcallister
brianmcallister / _ratio-as-percentage.scss
Created Mar 30, 2016
Sass mixin to convert a ratio to a percentage.
View _ratio-as-percentage.scss
// Convert a ratio into a percentage. Useful for responsive images and videos.
// https://gist.github.com/brianmcallister/03b9038b45c02dabba3a779efc6c4e38
//
// $ratio - 2 item list of integers. Represents a ratio. Defaults to 1:1.
//
// Examples
//
// ratio-as-percentage();
// #=> 100%
//
@brianmcallister
brianmcallister / natural-sort.coffee
Last active Aug 29, 2015
CoffeeScript natural sort. Ported from Dave Koelle's Alphanum Algorithm.
View natural-sort.coffee
# Public: Natural sort an array.
# https://gist.github.com/brianmcallister/f0e77c185f4a0d6f7172
# Ported to CoffeeScript from: http://www.davekoelle.com/files/alphanum.js
# See: http://www.davekoelle.com/alphanum.html
#
# This function has been modified to support passing in an array as an
# argument, as opposed to attaching this function to the Array prototype.
#
# array - Array to natural sort.
#
@brianmcallister
brianmcallister / highlight.coffee
Last active Aug 29, 2015
Highlight words in text. Still need to remove the underscore dependency.
View highlight.coffee
# Private: Highlight individual words within the message text.
#
# text - Text to format.
# words - Array of words to highlight.
#
# Returns the formatted text.
highlightWords = (text, words = []) ->
return text if words.length is 0
# Create a DOM element to contain the text so we can detect and handle
@brianmcallister
brianmcallister / params.coffee
Created Sep 12, 2014
Parse query parameters.
View params.coffee
# Parse the query string params.
# https://gist.github.com/brianmcallister/94d89b3c50b2b9e98f6d
#
# Examples
#
# If the query string is:
# test=test&foo&a=truee&bool=false&num=10
#
# parseQueryParams()
# #=> {"test": "test", "foo": "", "a": "truee", "bool": false, "num": 10}