Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / no-deps-url-parse.js
Created January 2, 2022 07:33
Parse URL fallback
// https://github.com/shm-open/utilities/blob/master/src/url.ts?cool#L52
function parseURL(url, base) {
let rest = url;
const reProtocol = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\S\s]*)/i;
// extract protocol
const protocolMatch = reProtocol.exec(url);
let protocol = protocolMatch[1]?.toLowerCase() ?? '';
const hasSlashes = !!protocolMatch[2];
// eslint-disable-next-line prefer-destructuring
rest = protocolMatch[3];
@DavidWells
DavidWells / How.md
Created December 11, 2021 23:31
Async to sync messaging for webworkers

How Does It Work?

How Partytown's Sync Communication Works

Partytown relies on Web Workers, Service Workers, JavaScript Proxies, and a communication layer between them all.

  1. Scripts are disabled from running on the main thread by using the type="text/partytown" attribute on the <script/> tag.
  2. Service worker creates an onfetch handler to intercept specific requests.
  3. Web worker is given the scripts to execute within the worker thread.
  4. Web worker creates JavaScript Proxies to replicate and forward calls to the main thread APIs (such as DOM operations).
@DavidWells
DavidWells / warn-once.js
Created December 2, 2021 05:06
Show warning just once via new Set()
// https://github.com/satya164/warn-once/blob/main/index.js
const DEV = process.env.NODE_ENV !== "production";
const warnings = new Set();
function warnOnce(condition, ...rest) {
if (DEV && condition) {
const key = rest.join(" ");
if (warnings.has(key)) {
@DavidWells
DavidWells / forbidden-usernames.js
Created December 1, 2021 05:36
Block confusing usernames
// https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/utilities.ts#L188
/**
* List of forbidden usernames. Note that usernames can be used as apify.com/username,
* so we need to prohibit any username that might be part of our website or confusing in anyway.
*/
const FORBIDDEN_USERNAMES_REGEXPS = [
// Meteor app routes
'page-not-found', 'docs', 'terms-of-use', 'about', 'pricing', 'privacy-policy', 'customers',
'request-form', 'request-solution', 'release-notes', 'jobs', 'api-reference', 'video-tutorials',
'acts', 'key-value-stores', 'schedules', 'account', 'sign-up', 'sign-in-discourse', 'admin',
@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):