Skip to content

Instantly share code, notes, and snippets.

View kbjr's full-sized avatar

James Brumond kbjr

View GitHub Profile
@kbjr
kbjr / function-bind.ts
Created June 23, 2024 02:56
`Function.bind` in TypeScript with typing
export interface Fn<T, P extends any[], R> {
(this: T, ...args: P) : R;
}
export type UnboundParams<F extends Fn<any, [ ...BindP, ...any[] ], any>, BindP extends any[]>
= F extends Fn<any, [ ...BindP, ...infer CallP ], any> ? CallP : never;
export type BoundFn<F extends Fn<any, [ ...BindP, ...any[] ], any>, BindP extends any[]>
= F extends Fn<infer T, [ ...BindP, ...infer CallP ], infer R>
? Fn<T, CallP, R>
@kbjr
kbjr / prism-wasm.ts
Created March 20, 2023 00:15
Extend PrismJS highlighting for WASM with SIMD/Vector, `table`, `memory`, `ref` instructions
import { languages } from 'prismjs';
const extra_keywords = [
{
pattern: /\b(?:memory\.(?:init|copy|fill)|data\.(?:drop)|table\.(?:init|copy|grow|size|fill|get|set)|elem\.(?:drop)|ref\.(?:null|is_null|func))\b/,
inside: {
'punctuation': /\./
}
},
@kbjr
kbjr / description-list.ts
Last active May 21, 2022 05:01
Marked.js Extensions
import { marked } from 'marked';
export interface DescriptionListToken extends marked.Tokens.Generic {
items: (DescriptionTermToken | DescriptionDetailToken)[];
}
export interface DescriptionTermToken extends marked.Tokens.Generic {
text: string;
}
import { randomBytes } from 'crypto';
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
const randomString = (length) => {
return new Promise((resolve, reject) => {
const chars = new Array(length);
randomBytes(length, (error, bytes) => {
if (error) {
@kbjr
kbjr / sum-of-squares.js
Last active May 28, 2019 01:05
Simple benchmark showing performance regression in recent versions of Node.js
const size = 32000000;
const rand = () => Math.random() * 100;
console.log('Generating test data...');
let data = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = rand();
@kbjr
kbjr / model.js
Created June 13, 2015 20:58
Model With Change History
(function() {
//
var isInheriting = false;
//
//
//
function Model(originalDTO) {
if (! isInheriting) {
@kbjr
kbjr / findObjectScope.js
Created June 13, 2015 20:25
Find Object Scope
//
// Finds a specified property path in a nested object structure
//
// @param {obj} the object to search
// @param {path} the property path (eg. "foo.bar.[0].{baz:qux}.property")
// @return object
//
function findObjectScope(obj, path) {
var scope = obj;
var steps = path.split('.');
@kbjr
kbjr / promises.js
Created August 31, 2013 16:52
An extension to oath that would simplify creating/returning/passing promises.
function promises(func) {
var promiseIndex = func.length - 1;
return function() {
var scope = this;
var args = Array.prototype.slice.call(arguments);
var promise = args[promiseIndex] = args[promiseIndex] || new oath();
process.nextTick(function() {
func.apply(scope, args);
@kbjr
kbjr / gist:5485117
Last active December 16, 2015 19:29
<?php
$mysql = mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno($mysql)){echo "Failed to connect to database";}
$user = "rebel24";
$input = mysqli_query($mysql,"SELECT * FROM `userdata` WHERE `Username` = '$user';");
//while ($row = mysqli_fetch_array($input)){
@kbjr
kbjr / gist:5210793
Last active December 15, 2015 05:39
<?php while (have_posts()) : the_post(); $count++; ?>
<?php $video = get_field('video'); ?>
<a href="#" onclick="switchVideo(this, '<?php echo $video; ?>'); return false;" class="vidtn">
<img src="<?php echo the_field('vidthumbnail'); ?>" />
</a>
<?php endwhile; ?>
<script type="text/javascript">
//
// @param original the DOM element of the existing video to be replaced