Skip to content

Instantly share code, notes, and snippets.

View jlevy's full-sized avatar

Joshua Levy jlevy

View GitHub Profile
@jlevy
jlevy / friendlyTruncate.spec.ts
Last active July 12, 2023 21:29
Human-friendly truncation of strings in TypeScript, by character, word, and paragraph, with Markdown compatibility
import {
MARKDOWN_FORMAT_CHARS,
truncate,
truncateCondensed,
truncateOnParagraphs,
truncateOnWords,
} from './friendlyTruncate';
describe('truncate', () => {
it('returns the input if it is null or shorter than the limit', () => {
@jlevy
jlevy / logPerf.ts
Last active June 16, 2023 17:59
Client-side convenience wrapper function to log when a (sync or async) function takes a while to run
/**
* Convenience for timing specific function calls and logging to console.
* Works with both synchronous and asynchronous functions and also logs
* if the function exited or had an exception. minMs is the minimum number
* of milliseconds that the function must take to be logged.
*
* Usage example: To log when an expression like
*
* const result = someFunction(arg1, arg2);
*
@jlevy
jlevy / comparisonChain.spec.ts
Last active June 10, 2023 22:11
Comparison chain for advanced sorting in TypeScript, inspired by Guava
import { comparisonChain, ordering } from './comparisonChain';
interface Item {
title: string | null;
url: string | null;
sequenceNum: number | null;
}
// Setup a sample data
const items: Item[] = [
@jlevy
jlevy / simple-hash.js
Last active April 12, 2024 16:06
Fast and simple insecure string hash for JavaScript
// These hashes are for algorithmic use cases, such as bucketing in hashtables, where security isn't
// needed and 32 or 64 bits is enough (that is, rare collisions are acceptable). These are way simpler
// than sha1 (and all its deps) or similar, and with a short, clean (base 36 alphanumeric) result.
// A simple, *insecure* 32-bit hash that's short, fast, and has no dependencies.
// Output is always 7 characters.
// Loosely based on the Java version; see
// https://stackoverflow.com/questions/6122571/simple-non-secure-hash-function-for-javascript
const simpleHash = str => {
let hash = 0;
@jlevy
jlevy / pjson
Created May 28, 2019 01:21
Command-line JSON pretty-printer
#!/bin/bash
# Convenience script to pretty-print and browse colored JSON without remembering flags.
# Read from files or stdin.
jq -C . "$@" | less -R -m
@jlevy
jlevy / diffjson
Created May 28, 2019 01:16
Command-line JSON diff (colorized and with normalized key ordering)
#!/bin/bash
set -euo pipefail
file1="${1:?
Usage: $(basename $0) file1.json file2.json
Show diff of JSON, normalizing key ordering.
}"
@jlevy
jlevy / SecConfig.py
Last active November 4, 2019 18:11
SecConfig.py from AWS Re-Invent 2013 (export AWS security configurations)
#! /usr/bin/python
# This script is taken (unmodified except for this comment) from: https://s3.amazonaws.com/reinvent2013-sec402/SecConfig.py
# Talk: http://www.slideshare.net/AmazonWebServices/intrusion-detection-in-the-cloud-sec402-aws-reinvent-2013
# Example code to output account security config
__author__ = 'Greg Roth'
import boto
import urllib