Skip to content

Instantly share code, notes, and snippets.

@josephg
josephg / README.md
Created January 14, 2019 08:17
Bad comment script in JS

This is a port of this blog post to javascript. The one annoying issue with it is that you need to install node-fetch somehow (or put this script somewhere that has access to node-fetch). I could adapt this to use no deps, but it'd be longer and uglier.

@josephg
josephg / transformJSPosition.js
Last active January 8, 2019 01:34
transform raw string positions with text ops
const {strPosToUni, uniToStrPos} = require('unicount')
// The cursor position (input and output) are specified in JS string
// offsets here.
function transformJSPosition(cursor /*: number*/, doc /*: string*/, op /*: TextOp*/) {
let prePos = 0, postPos = 0 // string index in doc.
for (let i = 0; i < op.length && cursor > postPos; i++) {
const c = op[i]
diff --git a/src/main.rs b/src/main.rs
index 19c571c..3cf9380 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,10 +3,7 @@
#[macro_use]
extern crate serde_json;
-use sodiumoxide::crypto::{box_, sign, auth, scalarmult, secretbox};
-use sodiumoxide::crypto::hash::sha256;
@josephg
josephg / main.rs
Last active October 9, 2018 23:20
Rust allocations
// This is an example of making a c-style struct ending in a dynamically sized array in stable rust.
use std::alloc::{GlobalAlloc, Layout, System};
struct Blah {
height: usize,
arr: [u8; 0],
}
impl Blah {
fn get_layout_at(height: usize) -> Layout {
@josephg
josephg / README.md
Last active March 7, 2024 06:58
Getting Zig compiling to WASM

In case anyone else wants to play with Zig on webassembly, here's what you need to do to make it work on a mac today.

1. Get LLVM 7 compiled with webassembly support.

You'll need LLVM to output to the WASM target. This has just been added by default in trunk, so if LLVM >7 is available, you might be able to just brew install llvm.

If you have wasm support already you should see:

$ llc --version
@josephg
josephg / foundationdb.conf
Created June 7, 2018 16:43
Foundationdb freebsd installation first pass
## foundationdb.conf
##
## Configuration file for FoundationDB server processes
## Full documentation is available at
## https://www.foundationdb.org/documentation/configuration.html#foundationdb-conf
[general]
restart_delay = 60
## by default, restart_backoff = restart_delay_reset_interval = restart_delay
# initial_restart_delay = 0
@josephg
josephg / bt.js
Last active April 17, 2018 06:00
Behaviour trees with generators example 2
// 2 entities, George and Phil will meet on this fateful night
const e1 = {
name: 'George',
health: 400,
target: null,
}
const e2 = {
name: 'Phil',
health: 400,
@josephg
josephg / bt.js
Created April 17, 2018 05:10
Behaviour trees with generators example 2
const e1 = {
name: 'George',
health: 1000,
target: null,
}
const e2 = {
name: 'Phil',
health: 1000,
@josephg
josephg / bt.js
Last active April 17, 2018 03:25
Behaviour trees using generators
// Some example behaviours
// Helper to wait X frames
function *wait(framecount) {
while(--framecount) yield // Each call to yield will wait 1 frame
}
// Dance function will
function *dance() {
console.log('\\_o_\\')
@josephg
josephg / client.js
Created January 3, 2018 03:44
Cryptopia API from nodejs
const request = require('request')
const crypto = require('crypto')
const API_KEY = 'API KEY GOES HERE'
const API_SECRET = Buffer.from('SECRET GOES HERE', 'base64')
const md5 = str => crypto.createHash('md5').update(str).digest().toString('base64')
const hmac = str => crypto.createHmac('sha256', API_SECRET).update(str).digest().toString('base64')
const getBalance = (currency) => {