Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / downloads-table.md
Created November 30, 2021 03:00
Nice format for packages table with downloads

Packages

Package Badges Details
typedoc-plugin-markdown typedoc-plugin-markdown Downloads [Readm
@DavidWells
DavidWells / query-by-id-shortcut.html
Created November 26, 2021 06:06
Shortcut for manipulating DOM for code golfing
/* https://news.ycombinator.com/item?id=29346918 */
<div id=result></div>
<script>
document.getElementById("result").textContent = "Why do it this way—";
document.querySelector("result").textContent = "—or even this way—";
result.textContent = "—when you can do it this way?";
</script>
setInterval(function() {
var heading = Array.from(document.querySelectorAll('h2[role="heading"]')).find((header) => {
return header.innerText === "What’s happening"
})
var whatsHappening = heading && heading.parentNode && heading.parentNode.parentNode && heading.parentNode.parentNode.parentNode
// Kill Whats happening garbage
if (whatsHappening) {
whatsHappening.style.display = 'none'
@DavidWells
DavidWells / sort-on-object-key.js
Created November 14, 2021 04:32
Sort an array by key of object in array
// https://github.com/softvar/js/blob/master/packages/util-array/src/index.ts#L97
function sortOnKey(arr, key, direction = 'asc' ) {
if (!arr || !arr.length || !key) {
return [];
}
const dir = direction === 'asc' ? 1 : -1
arr.sort((a, b) => {
return b[key] > a[key] ? -1 : a[key] > b[key] ? dir : 0;
});
@DavidWells
DavidWells / get-lastest-repo-sha.bash
Created October 16, 2021 06:27
Get Latest repo SHA in bash
# via https://github.com/getsentry/sentry-docs/blob/ee13e55eac4a0fa946a6c16b43d302ff18552556/scripts/bump-version.sh
LATEST_SHA="$(curl -sSL 'https://api.github.com/repos/getsentry/sentry-api-schema/commits/main' | awk 'BEGIN { RS=",|:{\n"; FS="\""; } $2 == "sha" { print $4 }')";
echo $LATEST_SHA;
@DavidWells
DavidWells / conditional.bash
Created October 14, 2021 05:32
Conditional variables in bash
# https://stackoverflow.com/questions/2440947/how-to-build-a-conditional-assignment-in-bash
(condition) \
&& variable=true \
|| variable=false
e.g as in
[[ $variableToCheck == "$othervariable, string or number to match" ]] \
&& variable="$valueIfTrue" \
|| variable="$valueIfFalse"
or to get 1 in a positive check, and 0 upon failure (like in the question's example):
@DavidWells
DavidWells / persist-page-views.js
Created October 10, 2021 01:21
Example of persisting page views via analytics package
import Analytics from 'analytics'
import onRouteChange from '@analytics/router-utils'
const persistPageViewsPlugin = {
name: 'persist-page-data-plugin',
page: ({ payload }) => {
const { properties } = payload
const pageView = {
path: properties.path,
title: properties.title,
// via https://yeun.github.io/open-color/
const palette = {
dark: [
'#d5d7e0',
'#acaebf',
'#8c8fa3',
'#666980',
'#4d4f66',
'#34354a',
@DavidWells
DavidWells / package.json
Created September 12, 2021 22:11
Symlink a directory for package.json scripts
{
"if-not-ci": "pnpm ts support/scripts/run-if-not-ci.ts",
}
@DavidWells
DavidWells / javascript-highlight-text.js
Created September 10, 2021 20:01
Highlight Search match in JS
function highlightMatch(string, regexp) {
return escapeString(string).replace(regexp, (match) => `<mark>${match}</mark>`)
}
// Used to match HTML entities and HTML characters.
const unescapedHtml = /[&<>"']/g
const hasUnescapedHtml = RegExp(unescapedHtml.source)
const htmlEscapes = {
'&': '&amp;',