Skip to content

Instantly share code, notes, and snippets.

View ryankshaw's full-sized avatar

Ryan Shaw ryankshaw

  • Instructure
  • Utah
View GitHub Profile
@ryankshaw
ryankshaw / foobar.ts
Created August 15, 2023 00:33
which of the three of these is more readable / has less mental burden to grok?
// Which of the 3 of these options is more readable / is easier to understand?
// OPTION 1: .forEach with .push
const subqueries = []
Object.entries(SPECIAL_SUBQUERY_FIELDS).forEach(([field, subquery]) => {
if (fieldRequested(field, info))
subqueries.push(`(${subquery} LIMIT 1) AS ${field}`)
})
// OPTION 2: .map with .filter
@ryankshaw
ryankshaw / typescript-object-helpers.ts
Last active February 2, 2023 19:51
a couple typescript type-aware helpers for iterating objects
type PickByValue<T, V> = Pick<
T,
{ [K in keyof T]: T[K] extends V ? K : never }[keyof T]
>
type Entries<T> = {
[K in keyof T]: [keyof PickByValue<T, T[K]>, T[K]]
}[keyof T][]
/**
* A better Object.entries _BUT ONLY FOR OBJECTS YOU KNOW DON'T HAVE EXTRA PROPERTIES_,
blueprint:
name: Lamp Follows Swicth
description: Turn on or aff a lamp every time a switch state changes
domain: switch
input:
master_switch:
name: the Master Switch
description: This sensor will be synchronized with the light.
selector:
entity:
@ryankshaw
ryankshaw / largestSum.js
Created December 1, 2021 00:31
largestSum
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0)
}
function largestSum(array) {
let largestSumSoFar
for (let i = 0; i < array.length; i++) {
for (let j = i; j < array.length; j++) {
let subArray = array.slice(i, j + 1)
let test = sum(...subArray)
!(function(t) {
var e = {};
function n(r) {
if (e[r]) return e[r].exports;
var o = (e[r] = { i: r, l: !1, exports: {} });
return t[r].call(o.exports, o, o.exports, n), (o.l = !0), o.exports;
}
(n.m = t),
(n.c = e),
(n.d = function(t, e, r) {
@ryankshaw
ryankshaw / index.js
Last active March 8, 2019 20:26
here is the difference for @instructure/ui-buttons/lib/components/Button/index.js before/after this change
var styles = {
componentId: 'bavIU',
template: function template(theme) {
return "/* imported from styles.css */\n\n.bavIU_922a,a.bavIU_922a,button.bavIU_922a,button.bavIU_922a[type=button],button.bavIU_922a[type=reset],button.bavIU_922a[type=submit]{-moz-appearance:none;-moz-user-select:none;-ms-user-select:none;-webkit-appearance:none;-webkit-user-select:none;appearance:none;background:none;border-style:".concat(theme.borderStyle || 'none', ";border-width:").concat(theme.borderWidth || 'none', ";box-sizing:border-box;direction:inherit;display:inline-block;font-family:").concat(theme.fontFamily || 'none', ";font-weight:").concat(theme.fontWeight || 'none', ";height:auto;letter-spacing:").concat(theme.letterSpacing || 'none', ";margin:0;max-width:100%;overflow:visible;padding:0;position:relative;text-decoration:none;text-transform:").concat(theme.textTransform || 'none', ";touch-action:manipulation;transform:").concat(theme.transform || 'none', ";transition:background 0.2s,transform 0.2s;user-select:n
@ryankshaw
ryankshaw / example.js
Last active May 16, 2018 20:09
how should I handle binding event listeners in react components?
// bad: manual binding in constructor
class Foo extends React.Component {
// this is a lot of boilerplate that does not really give us any insight into
// what we are intending to do with our code
constructor(props) {
super(props)
this.updateCounter = this.updateCounter.bind(this)
this.state = {
counter:0
@ryankshaw
ryankshaw / custom_customer_js_file.js
Created February 13, 2017 21:11
wait for rcs service to be loaded
function initCustomTinyMCEStuff (tinyMCE) {
console.log('here is where you would run your callback that does custom tinyMCE stuff. in the case of your minified code, it is the function called "r"', tinyMCE)
}
//this will keep checking for the rceModule to be ready and call your
// callback that does all of your customizations once it is.
var waitForRCSServiceToBeReadyPollInterval = setInterval(function(){
if (typeof RceModule === 'undefined') return
initCustomTinyMCEStuff(tinyRCE)
clearTimeout(waitForRCSServiceToBeReadyPollInterval);
@ryankshaw
ryankshaw / timezone.js
Last active October 7, 2016 17:31
how would I accomplish this same thing in webpack?
// if someone requires 'timezone', I want it to get this file
// and then based on some info in a global variable 'window.ENV'
// at runtime fetch a json file with a bunch of data specific to
// that timezone and then export the 'timezone' object, with
// that zone's data preloaded. in requireJS, I had a loader that
// looked something like this
// in timezone.js
define(['timezone_core'], function(timezone) {
//see: http://requirejs.org/docs/plugins.html#apiload for the specifics of how this api works
require 'net/http'
require 'uri'
require 'digest'
TEMP_DIR = '/tmp/customer_js_that_uses_require'
UNIQUE_JS_FILES = {}
def collect_js_urls
Account.active.joins(:brand_config).find_each do |acct|
url = acct.brand_config.try(:js_overrides)