Skip to content

Instantly share code, notes, and snippets.

Avatar
😎
🐹 Go 🦀 Rust 📦 Node 🐧 Linux

AJ ONeal coolaj86

😎
🐹 Go 🦀 Rust 📦 Node 🐧 Linux
View GitHub Profile
View escape.js
function escapeHtml(str) {
// TODO more
return str
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&lt;')
.replace(/'/g, '&#39;')
.replace(/"/g, '&quot;')
;
}
@coolaj86
coolaj86 / node-javascript-utahjs.js
Last active Apr 12, 2022
AJ's Code Snippets for Thumbnail Backgrounds
View node-javascript-utahjs.js
// XPub to Public Parts
wallet.addrFromXPubKey = async function (xpubKey, index) {
let derivedRoot = HdKey.fromExtendedKey(xpubKey);
return await wallet.addrFromXPubRoot(derivedRoot, index);
};
wallet.addrFromXPubRoot = async function (derivedRoot, index) {
let derivedChild = derivedRoot.deriveChild(index);
let addrFromXPubKey = await b58c.encode({
version: myCoin.pubKeyHashVersion,
pubKeyHash: derivedChild.pubKeyHash.toString("hex"),
View README.md

BogoSort Wins!

The for loop outperforms the binary search.
(well, the forEach did, until we changed the caching in an unrelated function)

And the optimizations have (almost) nothing to do with the algorithms.
(changing any code can massively change the performance of any benchmark)

See the video: https://www.youtube.com/watch?v=0mmi44ZB2C0

View allman-vs-k-r-braces.md

K&R Style

  • Required (but not enforced) for JavaScript
  • Required AND enforced for Go
function kr() {
  // foo
}
View arrow-functions-are-evil.md

Why is this bad?

const something = (params) => value;

TL;DR Because this ISN'T bad

View js-pyramids-of-doom.md

Pyramids of Doom in JavaScript

  • callbacks
  • promises
  • try/catch

solution: await.catch

async function doLotsOfStuff(params) {
View dash-tx.json
{
"hash": "716dac449e7f7c02a69cb0ec37ea3b905f3f01aac707c1db2dd38bfa9deedb20",
"version": 1,
"inputs": [
{
"prevTxId": "ea5ff9b5bdcce85e3bbdf7c0d84a63681265cf630481bc48e6009d7b5dbad069",
"outputIndex": 1,
"sequenceNumber": 4294967295,
"script": "47304402207a88c7eb79b5307288e73bce404963cb3d8f76db7a3fa3924ab633cfbaf9f49d02204de2d008ce5b38383ad9dbfccaa99bed880fe949166ce2d7ce0c6034b52177070121038081fa1a40e680a1e7ea5be15895669b1a6fb5ea884dabbfa688e7097d0d9011",
"scriptString": "71 0x304402207a88c7eb79b5307288e73bce404963cb3d8f76db7a3fa3924ab633cfbaf9f49d02204de2d008ce5b38383ad9dbfccaa99bed880fe949166ce2d7ce0c6034b521770701 33 0x038081fa1a40e680a1e7ea5be15895669b1a6fb5ea884dabbfa688e7097d0d9011"
View Abuses of Reduces.md

(Ab)uses of Reduce

Watch: https://youtu.be/X85QdHe1tDw?t=409 (Starts at 6:50)

reduce is one of the least understood Array methods, and probably the most abused.

Use #0 Actually Reducing

Reducing, as the good functional programmer intended:

View mid-level-programmer-litmus-test.md

Can you convert a Value Object (i.e. JSON) to YAML?

My Expectation

My expectation is that a mid-senior level developer should be able to accomplish this task in under 4 hours:

  • write a basic solution in 30-60 minutes
  • debug / test / fine-tune for another 60-90 minutes
  • document, package, and publish within the remaining time

I could see it reasonable even if a developer takes 2 days to solve this for the first time - possibly because they needed to take a break but didn't, such as feeling pressure and getting brain freeze, taking an initial wrong approach, over-thinking the problem, and then just keeping beating the dead horse, etc - especially if they've never had to work independently before.

@coolaj86
coolaj86 / json-to-yaml-litmus-test.md
Last active Mar 14, 2022
"Can you write code?" litmus test.
View json-to-yaml-litmus-test.md

The Developer Litmus Test

I used to think that fizzbuzz was a stupid way to test if someone is a developer.

Screen Shot 2022-01-24 at 3 06 33 AM

Now I think that it's a good litmus test.

However, I think I've found a better one...