JavaScript Code Snippets

Keyboard Manager (Game Dev)

A class that provides an easy-to-use interface for using a hardware keyboard as input in a game. Specifically, it's meant to be instantiated outside of the game loop so that its functions can be called in the game loop. This way, the game can update based on the keyboard state on each tick.

Gamepad Manager (Browser)

A class that provides an easier-to-use interface for interacting with gamepads like PlayStation and XBox controllers. This class assumes the controller has a standard control mapping. Construct it outside of the game loop, and call its member functions on each tick in the loop. Only call its functions when the instance's ready() function returns true. See this, this, and this.

Check Whether the Current Browser has an Ethereum Wallet

This function returns a boolean value determining whether the current browser has a Web3 wallet available. It returns true if the wallet is available and false if it isn't.

Send Ether to an Address

A quick-and-dirty way to send Ether to an Ethereum address. It's compatible with all of the EIP-1102 implementations and older Web3 browser implementations. This function accepts the recipient address as a string and the hex-encoded amount in Wei to send as a string. It returns a promise that resolves to the transaction receipt or hash, depending on the Web3 version used.

Request Access to an Ethereum Wallet

A quick-and-dirty way to request access to a user's Ethereum wallet within the browser. It returns 'allowed' if the user gives access, 'denied' if the user denies access, and 'no-wallet' if the user doesn't have a Web3 wallet.

Promisify a Function Accepting a Callback

Turn a function that accepts a callback function as a argument into another that returns a Promise. Here, someFunction accepts a callback as the second argument. somePromisifiedFunction is the promisified version of someFunction.

Detect Dark Mode in the Browser

Detect dark mode and changes to dark mode in the browser in JS, when CSS isn't enough.

Send a Single SMS Message (Twilio)

To run this Node.js script, the Twilio JavaScript library must first be installed. Here, sendingNumber and receivingNumber include the country code. Note that the Account SID and Auth Token are provided to the script as environment variables. You can find these values in your Twilio dashboard.

Node.js Unit Testing (Jest)

The structure of a test suite in Node.js. First, Jest must be installed. This code should be in a file titled mytest.test.js, and it can be ran by executing the jest command in the terminal.

Linear Interpolation (lerp)

A simple lerp implementation. progress ranges from 0 (start) to 1 (end).

Generate list of numbers in range

Like Python's range function, this returns an array of numbers, starting from the given initial value, and increments by step, and stops before the given final value. If the optional boolean parameter inclusiveOfB is set to true, this function will include the given final value at the end of the returned array.

Slugify a String

Turn a string into a slug. Borrowed from Lulu.