Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Building

David Murdoch davidmurdoch

💭
Building
View GitHub Profile
@davidmurdoch
davidmurdoch / hex-to-dec.ts
Created Sep 11, 2020
Convert hexadecimal string into decimal array in typescript
View hex-to-dec.ts
// requires the types from: https://gist.github.com/davidmurdoch/3145903f3a267543568225c68dcc325b
// this is *very* limited in string size at the moment
type Data<T extends string> = {
value: DATA<T>,
// @ts-ignore: Ignore type instantiation depth errors as we have already limited our depth
toBuffer: () => ToUInt8<Hex<Trim<T, "0x">>>
}
type HexLookUp = {"00": 0, "01": 1, "02": 2, "03": 3, "04": 4, "05": 5, "06": 6, "07": 7, "08": 8, "09": 9, "0a": 10, "0b": 11, "0c": 12, "0d": 13, "0e": 14, "0f": 15, "10": 16, "11": 17, "12": 18, "13": 19, "14": 20, "15": 21, "16": 22, "17": 23, "18": 24, "19": 25, "1a": 26, "1b": 27, "1c": 28, "1d": 29, "1e": 30, "1f": 31, "20": 32, "21": 33, "22": 34, "23": 35, "24": 36, "25": 37, "26": 38, "27": 39, "28": 40, "29": 41, "2a": 42, "2b": 43, "2c": 44, "2d": 45, "2e": 46, "2f": 47, "30": 48, "31": 49, "32": 50, "33": 51, "34": 52, "35": 53, "36": 54, "37": 55, "38": 56, "39": 57, "3a": 58, "3b": 59, "3c": 60, "3d": 61, "3e": 62, "3f": 63, "40": 64, "41": 65, "42"
@davidmurdoch
davidmurdoch / typesafe-hex-strings.ts
Last active Oct 13, 2020
Typesafe, `0x` prefixed, hex strings up to 32 bytes long
View typesafe-hex-strings.ts
type Cast<T, U> = T extends U ? T : U;
type Prop<T, K> = K extends keyof T ? T[K] : never;
type HexChars = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "A" | "B" | "C" | "D" | "E" | "F" | "a" | "b" | "c" | "d" | "e" | "f";
type HexPairs = `${HexChars}${HexChars}`;
/**
* returns a Tuple of hex pairs (bytes), or `never` if the input doesn't contain an even number of hex characters
*/
type Hex<S extends string> =
S extends "" ? [] :
@davidmurdoch
davidmurdoch / test.ts
Last active Sep 10, 2020
Typesafe Ethereum Transaction hash
View test.ts
type Cast<T, U> = T extends U ? T : U;
type Prop<T, K> = K extends keyof T ? T[K] : never;
type HexChars = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "A" | "B" | "C" | "D" | "E" | "F" | "a" | "b" | "c" | "d" | "e" | "f";
type HexPairs = `${HexChars}${HexChars}`;
type Hex<S extends string> =
S extends "" ? [] :
S extends `${HexPairs}${infer R}` ? S extends `${infer C}${R}`
? [C, ...(Hex<R> extends infer X ? Cast<X, string[]> : never)]
: never : never;
@davidmurdoch
davidmurdoch / using.js
Last active Jul 11, 2020
C#'s using in JS, ish
View using.js
const fs = require('fs');
const {promisify} = require('util');
const open = promisify(fs.open.bind(fs));
async function using(closable, fn) {
const fd = await closable;
try {
await fn(fd);
} finally {
closable && typeof closable.close === "function" && closable.close();
@davidmurdoch
davidmurdoch / HackyBigDecimal.js
Created May 3, 2019
Hacked together an implementation of big decimals using `BigInt`. Doesn't do division very well. Lots of issues.
View HackyBigDecimal.js
function getDecimalSeparator(locale) {
const numberWithDecimalSeparator = 1.1;
return Intl.NumberFormat(locale)
.formatToParts(numberWithDecimalSeparator)
.find(part => part.type === 'decimal')
.value;
}
getDecimalSeparator();
const RADIX_PREFIX = {
@davidmurdoch
davidmurdoch / snippet-deflator.aspx.cs
Created Oct 9, 2013
The code behind my snippet deflator (http://www.vervestudios.co/projects/compression-tests/snippet-deflator). Original written sometime in 2009.
View snippet-deflator.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form != null && Request.Form.Count > 0 && Request.Form["text"] != null)
{
var text = Request.Form["text"];
byte[] byteArray = Encoding.ASCII.GetBytes(text);
using (var stream = new MemoryStream())
{
using (var strm = new zlib.ZOutputStream(stream, zlib.zlibConst.Z_BEST_COMPRESSION, false))
View jsmem.md

JavaScript Code

var str = "hi";

Memory allocation:

Address Value Description
...... ...
@davidmurdoch
davidmurdoch / chromeless\modules\lib\chromeless-policy.js
Created May 27, 2011
How do I register this so I can use nsIContentPolicy shouldLoad method?
View chromeless\modules\lib\chromeless-policy.js
const {Cc, Ci, Cu, Cm, Cr} = require("chrome");
const xpcom = require("xpcom");
/***********************************************************
class definition
***********************************************************/
var description = "Chromeless Policy XPCOM Component";
/* UID generated by http://www.famkruithof.net/uuid/uuidgen */
View frame-buster-buster
(function(){
var preventBust = 0;
window.onbeforeunload = function () {
preventBust++;
};
setInterval(function () {
if (preventBust > 0) {
preventBust -= 2;
window.top.location = "/statuscode/204";
}
@davidmurdoch
davidmurdoch / css30.xml
Created Mar 24, 2011 — forked from KrofDrakula/css30.xml
CSS 3 validation for Visual Studio 20(08|10)
View css30.xml
<?xml version="1.0" encoding="utf-8" ?>
<cssmd:cssmd xmlns:cssmd="http://schemas.microsoft.com/Visual-Studio-Intellisense/css" version="VS8" description="CSS3 validation data">
<cssmd:property-set>
<!-- Based on the original css21.xml from Visual Studio 2010 -->
<!-- Font Properties -->
<cssmd:property-def _locID="color" _locAttrData="description,syntax" type="color"
description="Color of an element's text"
syntax="#RRGGBB | Named Color | rgb(R, G, B) | rgba(R, G, B, A) | inherit"
enum="inherit Aqua Black Blue Fuchsia Gray Green Lime Maroon Navy Olive Orange Purple Red Silver Teal White Yellow ActiveBorder ActiveCaption AppWorkspace Background ButtonFace ButtonHighlight ButtonShadow ButtonText CaptionText GrayText Highlight HighlightText InactiveBorder InactiveCaption InactiveCaptionText InfoBackground InfoText Menu MenuText Scrollbar ThreeDDarkShadow ThreeDFace ThreeDHighlight ThreeDLighShadow ThreeDShadow Window WindowFrame WindowText"/>
You can’t perform that action at this time.