Skip to content

Instantly share code, notes, and snippets.

@brianmcallister
brianmcallister / uniqueByKey.ts
Created June 17, 2020 15:53
Unique list of JavaScript objects.
interface Item {
type: string;
}
const listA: Item[] = [
{ type: 'a' },
{ type: 'b' },
{ type: 'c' },
];
const listB: Item[] = [
\.(
[a-z]+
(-(?!-))?)+
(?:
__
([a-z]+(-(?!-))?)+
)?
(?:
@brianmcallister
brianmcallister / scale-range.js
Created October 17, 2016 15:40
Scale an input number between an input range proportionally down to be in an output range.
// @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 August 15, 2016 20:15
Get the position of a point on a circle.
/**
* 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 August 25, 2021 16:55
VWAP (Volume Weighted Average Price) function.
/**
* 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 March 30, 2016 19:03
Override React's `isRequired` prop type validation. Allows you to express "If not X, than A, B, C".
// 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 March 30, 2016 18:17
Sass mixin to convert a ratio to a percentage.
// 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 August 29, 2015 14:20
CoffeeScript natural sort. Ported from Dave Koelle's Alphanum Algorithm.
# 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 August 29, 2015 14:16
Highlight words in text. Still need to remove the underscore dependency.
# 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 September 12, 2014 15:11
Parse query parameters.
# 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}