Skip to content

Instantly share code, notes, and snippets.

View nathansmith's full-sized avatar
😎
Ask me about free high fives!

Nathan Smith nathansmith

😎
Ask me about free high fives!
View GitHub Profile
@nathansmith
nathansmith / try-catch-promise-examples.js
Created December 8, 2022 18:04
Promise snippets I put together, to share with coworkers.
// ============
// Helper: log.
// ============
const log = (error) => {
console.log(error?.message || error);
}
// ===============
// Promise reject.
@nathansmith
nathansmith / [1] test-setup.js
Last active September 27, 2022 13:37
Jest test setup
require('@testing-library/jest-dom');
// Override.
Object.defineProperty(window, 'requestAnimationFrame', {
writable: true,
value: (f) => {
if (typeof f === 'function') {
f();
}
},
@nathansmith
nathansmith / [1] SlotWrapper.svelte
Last active September 8, 2022 14:58
Svelte component, to help test "slot" content
<script lang="ts">
// ======
// Props.
// ======
const { Component, slot, ...otherProps } = $$props;
</script>
<!--
=======
@nathansmith
nathansmith / generator-code-from-tweet.js
Last active July 1, 2022 15:14
Silly JS generator example
/*
Code snippet from this tweet.
https://twitter.com/nathansmith/status/1542887226803101697
*/
// Generator function.
function* dealWithIt() {
// Step 1.
yield '🙂';
@nathansmith
nathansmith / sleep.js
Last active July 20, 2023 15:58
Sleep function in JavaScript
// ===============
// Sleep function.
// ===============
const sleep = async (seconds = 0) => {
// Expose promise.
return new Promise((resolve) => {
// Set timer.
setTimeout(() => {
// Resolve promise.
@nathansmith
nathansmith / flex-layout-example.html
Last active February 24, 2022 17:48
Vertically centered layout example, using flexbox
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<meta
content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1"
name="viewport"
/>
<title>Flex layout example</title>
<style>
@nathansmith
nathansmith / requestAnimationFrame.test.js
Created August 12, 2021 23:03
Example of how to test `requestAnimationFrame` with Jest.
import React from 'react';
import { render } from 'react-dom';
// Test subject.
import MyComponent from './MyComponent';
// ====================
// Describe `fileName`.
// ====================
@nathansmith
nathansmith / [1] json-verbose.d.ts
Last active May 2, 2024 18:19
Example of JSON in TypeScript.
export type IJson = IJsonArray | IJsonObject | boolean | number | null | string;
export type IJsonArray = IJson[];
/*
=====
NOTE:
=====
This is an `interface` because the reference
@nathansmith
nathansmith / _zip-code-regex.md
Last active April 29, 2021 21:20
Regex for valid zip codes.
@nathansmith
nathansmith / [1] _rkv-map-merge.scss
Last active August 14, 2020 15:40
How to create Sass feature flags
@function rkv-map-merge($maps...) {
$collection: ();
@each $map in $maps {
$collection: map-merge($collection, $map);
}
@return $collection;
}