Skip to content

Instantly share code, notes, and snippets.

View isaaclyman's full-sized avatar

Isaac Lyman isaaclyman

View GitHub Profile
@isaaclyman
isaaclyman / Gemfile
Last active February 7, 2025 18:37
RSA key generation, encryption, and decryption in Ruby (NOT SECURE, EDUCATIONAL PURPOSES ONLY)
source 'https://rubygems.org'
ruby '3.3.4'
gem 'prime'
gem 'rspec'
@isaaclyman
isaaclyman / sort.js
Created August 23, 2021 15:17
JavaScript sort by multiple fields
rows.sort((a, b) => {
const amountTypeSort = a.amountType.localeCompare(b.amountType);
if (amountTypeSort !== 0) {
return amountTypeSort;
}
const laborCostTypeDisplayNameSort = a.costTypeDisplayName.localeCompare(b.costTypeDisplayName);
if (laborCostTypeDisplayNameSort !== 0) {
return laborCostTypeDisplayNameSort;
}
@isaaclyman
isaaclyman / _Messy XML builder.linq
Last active February 25, 2021 20:06
Zero-validation XML builder for when everything else won't do what you want
string Tag(string name, IEnumerable<Tuple<string, string>> attributes, string contents = "")
{
var attributeString = attributes.Any() ? " " + string.Join(' ', attributes.Select(a => @$"{a.Item1}=""{a.Item2}""")) : string.Empty;
var hasContents = !string.IsNullOrEmpty(contents);
if (!hasContents) {
return
@$"<{name}{attributeString}/>";
}
@isaaclyman
isaaclyman / Subscribable.js
Last active July 26, 2020 03:02
Simple class for subscribable objects with state and multiple subscribers
export class Subscribable {
constructor(name) {
this._name = name
this._subscribers = []
this._state = undefined
}
getState() {
return this._state
}
@isaaclyman
isaaclyman / !ToastInfoService
Last active May 1, 2019 18:03
A service for Angular 7+ that provides plug-and-play status toasts for Observables that users care about.
--- TITLE FILE ---
@isaaclyman
isaaclyman / performance.ts
Last active February 7, 2019 17:51
A TypeScript annotation for Angular that tracks the execution time of any class method and logs slow methods to the console
// Created by Anders Lyman, uploaded with permission
// Usage: class MyMethods { @LogPerformance() slowMethod() { ... } }
// If slowMethod exceeds 50 invocations or 30ms total run time, a statement like the following will be logged:
// 35ms | total: 35ms | calls: 1 | MyMethods:slowMethod
import {isDevMode} from '@angular/core';
export function LogPerformance(whenExceedsTimeInMs: number = 30, whenExceedsCalls: number = 50): any {
return function<T>(target: T, key: keyof T, descriptor?): any {
descriptor =
descriptor || Object.getOwnPropertyDescriptor(target, key) || Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), key);
@isaaclyman
isaaclyman / testSpeed.js
Created January 11, 2019 21:53
A function to test how long it takes to do 15,000 function calls and array assignments by index in JavaScript
function testSpeed() {
const stack = [];
function assignValue(index) {
stack[index] = 'bob ' + index;
}
const t0 = performance.now()
for (let ix = 0; ix < 15000; ix++) {
assignValue(ix);
@isaaclyman
isaaclyman / orderPromises.js
Last active March 22, 2019 20:03
Accept an array of functions that each return a Promise and execute them in order, waiting for each returned Promise to resolve before continuing to the next function
function orderPromises (promiseFns) {
if (!Array.isArray(promiseFns) || (promiseFns.length && typeof promiseFns[0] !== 'function')) {
throw new TypeError('orderPromises expects an array of functions. Received: ' + JSON.stringify(promiseFns))
}
if (!promiseFns.length) {
return Promise.resolve()
}
const promise = promiseFns[0]()
@isaaclyman
isaaclyman / gist:79e66b3e1e9e36a9d295b1fca1061707
Last active February 20, 2018 13:50
`cloc` command for a JavaScript project
cloc . --exclude-dir=node_modules,dist --not-match-f=package-lock
{
"editor.tabSize": 2,
"explorer.autoReveal": false,
"workbench.editor.enablePreview": false,
"workbench.editor.enablePreviewFromQuickOpen": false
}