Skip to content

Instantly share code, notes, and snippets.

View zachlysobey's full-sized avatar

Zachary Lysobey zachlysobey

View GitHub Profile
@zachlysobey
zachlysobey / obj-shorthand.md
Last active August 7, 2023 01:39
Thoguhts on JS object shorthand vs longhand

I appreciate the brevity of the shorthand 🤷

const foo = () => ({ a: 1, b: 2 });

compare that to:

const foo = () => {
@zachlysobey
zachlysobey / partition.ts
Last active April 6, 2023 16:33
TypeScript array `partition` utility
/**
* Takes a predicate and a list of values and returns a a tuple (2-item array),
* with each item containing the subset of the list that matches the predicate
* and the complement of the predicate respectively
*
* @sig (T -> Boolean, T[]) -> [T[], T[]]
*
* @param {Function} predicate A predicate to determine which side the element belongs to.
* @param {Array} arr The list to partition
*
@zachlysobey
zachlysobey / flatten-array.js
Created October 15, 2019 00:31
JS function to flatten an arbitrarily nested array
// creates a new array with all sub-array elements concatenated into it recursively
// equivalent to array.prototype.flat with Infinity passed for depth
const flattenArray = (arr) =>
arr.reduce(
(result, value) => result.concat(
Array.isArray(value)
? flattenArray(value)
: value
),
[],
@zachlysobey
zachlysobey / keybase.md
Last active August 5, 2019 14:48
keybase.md

Keybase proof

I hereby claim:

  * I am zachlysobey on github.   * I am zlysobey (https://keybase.io/zlysobey) on keybase.   * I have a public key ASBv6iUdNuzND9RnzxBV99QvwKlaMOM-ywxoPJ7YFV1eHgo

To claim this, I am signing this object:

@zachlysobey
zachlysobey / abi-helper.js
Last active January 22, 2019 18:59
Solidity Testing Patterns
class AbiHelper {
constructor (Contract) {
this._abi = Contract.toJSON().abi
}
abi() {
return this._abi
}
publicMethodNames() {
@zachlysobey
zachlysobey / uibModalExample.js
Last active May 29, 2018 12:07
UIB Modal Example
(function() {
'use strict';
angular
.module('dkProject.quota')
.factory('sourceListModal', sourceListModal);
const modalTemplate = `
<div class="source-list-modal">
@zachlysobey
zachlysobey / angular-multi-selects.md
Last active August 8, 2017 23:10
Angular Multi-Select Revue
@zachlysobey
zachlysobey / q-advice.md
Last active June 8, 2016 19:52
breakingthings's $q advice

@breakingthings's promise advice

How not to suck at $q

  1. success / error are not promise flow. They're pseudopromise demons. Use then and catch instead.
  2. $q.defer is satan. You should basically never use it. There is an alternative syntax that is superior, $q(function(resolve, reject) {}) but chances are that what you’re working with already returns a promise, and if it does there is absolutely no need for either of these.
  3. Don’t use the promiseFn().then(successFn, errorFn) pattern, as errorFn will only catch errors caused by promiseFn, but not by successFn. Use then(successFn).catch(errorFn) instead. Also note that you can chain several thenables and catch all of them this way, ala then(a).then(b).then(c).catch(errorFn), in which errorFn will handle errors that happen for any of a, b, or c.
  4. Whatever you return from a then-able is turned into a resolving promise. Whatever you throw is turned into a rejecting one. For instance, `.catch
@zachlysobey
zachlysobey / ng-directive-inline-multi-line-template-string.es6.js
Last active March 3, 2016 11:17
Angular 1 Directive with ES6 Multi-line string template inline.
(function() {
'use strict';
angular
.module('myModule')
.directive('myDirective', myDirective);
const template = `
<div class="my-directive-template">
<h1>Using multi-line Strings for templates</h1>
@zachlysobey
zachlysobey / randomFbPhoto.html
Last active December 15, 2015 20:48
Function which grabs a random photo from a specified public facebook (page) album with optional size parameter.
<!-- make sure to include jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
// optional 3rd parameter is the "size" (between 0-9). Default is 5
// Facebook returns several sizes;
// experiment with these and fine-tune with css.
function randomFbPhoto (el, albumId, size) {
var $el = $(el); // create jQuery element from css selector
var dataUrl = "https://graph.facebook.com/fql?q=SELECT+images,+caption,+link,+created+FROM+photo+WHERE+album_object_id+=+" + albumId + "+ORDER+BY+rand()+LIMIT+1";