Skip to content

Instantly share code, notes, and snippets.

View danew's full-sized avatar

Dane Wilson danew

View GitHub Profile
@danew
danew / time-set.ts
Created May 8, 2024 10:43
Set with timestamps of when values were added with a purge by age helper
export class TimeSet {
private items: Map<string, number>;
constructor() {
this.items = new Map();
}
add(item: string) {
const timestamp = Date.now();
this.items.set(item, timestamp);
@danew
danew / snippet.js
Created April 19, 2024 10:27
Print all elements and their z-index to console in descending order
(() => {
const elements = Array.from(document.querySelectorAll('*'));
const zIndexedElements = elements.map(element => ({
element,
zIndex: window.getComputedStyle(element).zIndex
}))
.filter(item => item.zIndex !== 'auto')
.sort((a, b) => b.zIndex - a.zIndex);
zIndexedElements.forEach(({ element, zIndex }) => {
@danew
danew / replace-alias-with-relative-imports.ts
Created February 23, 2024 11:11
A quick script to replace all the alias module imports with relative imports.
import ts from 'typescript';
import fs from 'fs';
import path from 'path';
const projectRoot = process.cwd();
const tsConfigPath = path.join(projectRoot, 'tsconfig.json');
const tsConfig = ts.readConfigFile(tsConfigPath, ts.sys.readFile).config;
const parsedTsConfig = ts.parseJsonConfigFileContent(tsConfig, ts.sys, projectRoot);
@danew
danew / broadcast-channel.ts
Created December 6, 2022 17:56
BroadcastChannel polyfill for Jest
import { EventEmitter } from 'node:events';
const channels: Map<string, EventEmitter> = new Map();
class MockBroadcastChannel implements BroadcastChannel {
private emitter: EventEmitter;
constructor(public name: string) {
if (channels.has(name)) {
this.emitter = channels.get(name);
@danew
danew / lintr
Last active October 6, 2020 19:25
ESlint pre-commit hook
#!/bin/bash
FILE_LIST=$(git diff --staged --diff-filter=ACMTUXB --name-only | grep -E \\.[tj]sx?$)
if [ -z "$FILE_LIST" ]; then
echo "Nothing to lint"
exit 0
fi
if ! npx eslint --fix --max-warnings 0 $FILE_LIST; then
@danew
danew / intro.md
Last active July 21, 2020 11:53
Headless Chrome lighthouse with a subset of audits

This is a simple script to run a few performance audits with Chrome's Lighthouse audit tool to help verify if you are making any performance improvements while coding.

It will print out an audit if it is outside of the given threshold; in green if it's improved or red if it hasn't.

Getting started

Run npm i -D lighthouse pretty-ms chalk
Run node test

@danew
danew / logg.js
Last active February 13, 2020 01:29
Console log with colours and can filter by /\*/
window.log = function(category, ...attr) {
const str = `%c* %c${category} %c[%c ${attr.join(', ')} %c]`;
console.log(
str,
'color:white',
'color:red; font-weight:bold',
'font-weight:bold',
'color:blue',
'font-weight:bold'
);
@danew
danew / Logger.js
Created October 24, 2019 20:36
Simple logger for browser
import axios from 'axios';
let active = true;
const save = (message, level = 'info') => {
if (process.env.NODE_ENV !== 'production') {
if (active) {
axios.post('http://localhost:9999/log', { level, message }).catch(() => {
active = false;
});
@danew
danew / verifierAndChallenge.js
Created March 21, 2019 16:03
PKCE in the browser
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_3_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
const base64 = require('base64-js');
const urlSafe = (buffer) => {
const bytes = new Uint8Array(buffer);
return base64.fromByteArray(bytes)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
};
@danew
danew / Mastermind.js
Created October 8, 2018 00:33
Small sample for the Mastermind problem solving game
class Mastermind {
constructor() {
this.answer = [];
this.blocks = [
'green',
'red',
'blue',
'yellow',
];
}