Skip to content

Instantly share code, notes, and snippets.

View donbrae's full-sized avatar

donbrae

View GitHub Profile
@donbrae
donbrae / Useful regular expressions.md
Last active April 8, 2024 10:58
Some regular expressions for finding patterns in text.
  • Find all references to filenames ending in .js: [^ ]+\.js
  • Find all blank lines: ^\r?\n\r?
  • Wildcard (any character and newlines): (.|\n)+?
    • Eg find all <td>s, regardless of content: <td>(.|\n)+?</td>
      • Include attribute(s): <td(.|\n)+?>(.|\n)+?</td>
  • Convert getComputedStyle(foot1).left to foot1.getBoundingClientRect().x: find getComputedStyle\((.+?)\).left and replace it with $1.getBoundingClientRect().x
  • Remove all <a> tags, leaving just the anchor tag text: find <a[^>]*>(.*?)</a> and replace with $1
    • Only links that start with the string /foo: <a href="/foo[^>]*>(.*?)</a>
  • Find all URLs: \b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]
    • All URLs ending in .jpg: \b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|](\.jpg)
@donbrae
donbrae / similarText.js
Last active January 16, 2024 12:58
JavaScript function which mimics PHP’s `similar_text()`.
// Source: ChatGPT 4
function similarText(first, second) {
// Check for null, undefined, or empty string inputs
if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined' || first.trim().length === 0 || second.trim().length === 0) {
return { matchingCharacters: 0, similarityPercentage: 0 };
}
// Type coercion to ensure inputs are treated as strings
first += '';
second += '';
@donbrae
donbrae / Squiz Matrix cheat sheet.md
Last active May 16, 2023 12:58
This cheat sheet documents various shortcuts, tips and tricks, and productivity boosters. Tested on Matrix v5.4.0.1 on macOS.

Keyboard shortcuts

  • Ctrl + + S to Save
  • Ctrl + + A to Acquire Locks
  • Ctrl + + R to Release Locks
  • Ctrl + + N to invoke the Next button
  • Delete to move selected assets in the Asset Map to the Trash

Releasing all your locks

@donbrae
donbrae / css-snippets.md
Last active May 2, 2023 14:18
Random CSS snippets

Subtle box shadows

These work best with light (~#fff) elements (eg a code snippet or ‘card’ component) on light backgrounds.

Standard

box-shadow: 0 1px 15px rgba(27, 31, 35, .15);
@donbrae
donbrae / research-ai.md
Last active April 15, 2023 14:45
AI tools for researchers, scholars etc.
  • Scholarcy “Scholarcy and Scite, among other A.I. tools[,] find, aggregate and summarize relevant papers.” (NYT)
  • scite
  • Elicit “It lets him ask questions of the paper itself. It helps him find out, without having to read the whole thing, whether the paper touches on the question he’s asking.” (NYT)
@donbrae
donbrae / Reactive UI with Proxy object.html
Last active March 8, 2023 08:24
Reactive UI with Proxy object
<!DOCTYPE html>
<html lang="en">
<head>
<title>Reactive UI with Proxy object</title>
</head>
<body>
<!-- Comment which got me thinking: https://news.ycombinator.com/item?id=35062021 -->
<p>Count:
<span id="count"></span>
</p>
@donbrae
donbrae / fetch.js
Last active August 1, 2022 13:50
Example: `fetch` JSON file.
// Fetch JSON data
fetch('https://jsonplaceholder.typicode.com/todos/').then(response => {
// Success
if (response.ok)
return response.json(); // Returns to then()
// Error
return Promise.reject(response);
@donbrae
donbrae / loop.js
Last active July 17, 2022 12:25
Basic looping in JavaScript.
// Loop array
const foo = ["ane", "twa", "three"];
foo.forEach(item => console.log(item));
// Loop DOM elements
document.querySelectorAll('.foo').forEach(el => {
el.classList.add('bar'); // Add class, for example
});
@donbrae
donbrae / js-getelements-perf-test.txt
Last active July 17, 2022 12:25
`getElementById` vs `querySelector` vs `getElementsByClassName` vs `getElementsByName` vs `getElementsByTagName`.
https://measurethat.net/Benchmarks/ShowResult/170533
Test name | Executions per second
-----------------------------------------------
getElementById | 9,737,553 ops/sec
getElementsByName | 6,090,861 ops/sec
querySelector | 5,577,396 ops/sec
getElementsByTagName | 4,900,635 ops/sec
getElementsByClassName | 4,736,003 ops/sec
@donbrae
donbrae / scrollTo.js
Last active July 17, 2022 12:24
JavaScript `scrollTo`.
window.scrollTo({
top: document.getElementById('foo').offsetTop,
left: 0,
behavior: 'smooth'
});