Skip to content

Instantly share code, notes, and snippets.

View davidsharp's full-sized avatar

David Sharp davidsharp

View GitHub Profile
@davidsharp
davidsharp / circDepReplacer.js
Created November 22, 2023 09:58
A quick little little circular dependancy remover for JSON.stringify
View circDepReplacer.js
function circDepReplacer(k,v){return k&&v==this?null:v}
// used like JSON.stringify(obj,circDepReplacer)
// only replaces references to main object, not sub-objects
@davidsharp
davidsharp / bluetooth.10s.sh
Last active November 15, 2023 15:45 — forked from ieatfood/Connect Airpods.applescript
A xbar/bitbar wrapper around an Applescript to connect bluetooth devices, such as Airpods.
View bluetooth.10s.sh
#!/bin/bash
function pair(){
osascript <<'END'
use framework "IOBluetooth"
use scripting additions
set blueToothDevice to "Buds Pro"
on getFirstMatchingDevice(deviceName)
@davidsharp
davidsharp / getBytes.js
Created April 6, 2023 10:36
turns a byte count into an object which can be used to compose KB/MB/GB/TB/PB values
View getBytes.js
const getBytes = (bytes,{unit=null,binary=false,toFixed=1} = {}) => {
const divisor = binary?1024:1000;
let value = bytes;
let sizeLevel=-1;
const sizeArray=['KB','MB','GB','TB','PB'];
if(unit){
sizeLevel=sizeArray.indexOf(unit);
value=(bytes/Math.pow(divisor,sizeLevel+1)).toFixed(toFixed)
}
else while(value>=divisor){
@davidsharp
davidsharp / kofi-widget.jsx
Created March 20, 2023 12:27
A Preact component for displaying Ko-fi embed
View kofi-widget.jsx
import {useEffect} from 'preact/hooks';
export default function Kofi({name='davidsharp',text='Tip Me',backgroundColor='#fcbf47',textColor='#323842'}){
const widgetScript = (`console.log('Donate @ ko-fi.com/${name}')
kofiWidgetOverlay.draw('${name}', {
'type': 'floating-chat',
'floating-chat.donateButton.text': '${text}',
'floating-chat.donateButton.background-color': '${backgroundColor}',
'floating-chat.donateButton.text-color': '${textColor}'
});`)
@davidsharp
davidsharp / nvm-symlink.sh
Created July 12, 2022 16:14
symlinking the node binary with whatever nvm has set it as
View nvm-symlink.sh
@davidsharp
davidsharp / autolink.jsx
Created June 24, 2022 09:08
Ever so slightly shorter version of the Autolink component found in 30 Seconds of Knowledge
View autolink.jsx
@davidsharp
davidsharp / force-xhr-status-with-proxy.js
Created April 22, 2022 10:27
Proxying a XMLHttpRequest to force the status received
View force-xhr-status-with-proxy.js
XMLHttpRequest = new Proxy(XMLHttpRequest, {
construct:function(t,a){
const req = new t();
return new Proxy(req, {
get:function(o,p){
if(p=='status')return 9001
return typeof o[p] == 'function'?o[p].bind(o):o[p]
},
set: function(target, prop, value) {
Reflect.set(target, prop, value) // or target[prop] = value
@davidsharp
davidsharp / rot13.js
Created February 19, 2022 17:10
Dirty little ROT13 function
View rot13.js
const rot13 = str => str.replace(/[a-z]/gi,x=>{
const c=x.charCodeAt(0)
const a=c>=97?97:65
return String.fromCharCode(
a+((c-a+13)%26)
)
})
@davidsharp
davidsharp / triangular-number.js
Created December 17, 2021 10:23
Finds triangular number (or x + x-1 + x-2, etc down to 0)
View triangular-number.js
const fn = x => x + (x * ((x-1)/2))
@davidsharp
davidsharp / remark-star-wipe.css
Last active November 26, 2021 10:20
A neat little star wipe for Remark.js presentations
View remark-star-wipe.css
@-webkit-keyframes wipe {
0% {
-webkit-mask-size: 0% 0%;
}
100% {
-webkit-mask-size: 550% 550%;
}
}
.remark-visible .remark-slide-content {