Skip to content

Instantly share code, notes, and snippets.

View wkronemeijer's full-sized avatar
📡

Wessel Kronemeijer wkronemeijer

📡
  • 18:52 (UTC +01:00)
View GitHub Profile
@wkronemeijer
wkronemeijer / Scourge.cjs
Created September 4, 2023 22:49
Moves the normal layout of a Path of Exile hideout to the scourged version, and vice versa.
const { readFileSync, writeFileSync } = require("fs");
const { resolve } = require("path");
const FlippedBit = 0b1000_0000;
const ScourgedBit = 0b0100_0000;
const VariationBitMask = 0b0011_1111;
function check(condition, message = "assertion failed") {
if (!condition) {
console.error(message);
@wkronemeijer
wkronemeijer / almost-adt.ts
Last active June 18, 2016 17:53
ES6 Module for creating what can be considered **almost** ADTs.
export interface InstanceConstructor<
/**Enum */ E,
/**Shared properties */ S,
/**Unique properties */ T
> {
/**Create new instance */
(diffs?: S & T): S & T;
/**Type guard for destructuring */
typecheck(object: {}): object is S & T;
/**For introspection, and to make IntelliSense work */
@wkronemeijer
wkronemeijer / accessor.js
Last active June 18, 2016 17:52
A convenience function when you have a list of records and need only a member; index $1 with the property of choice and map it over the collection to obtain a list of members.
const $1 = new Proxy({}, {
get(target, key) {
return x => x[key];
}
});
@wkronemeijer
wkronemeijer / columnindex.ts
Created November 14, 2015 18:28
Turning a column name into a number for indexing in the range [1, →)
function parseHeader(letters: string): number {
function letterValue(letter: string): number {
return parseInt(letter, 36) - 9
}
let result = 0
let factor = 1
if (letters.length === 1) {
result = letterValue(letters[0])
@wkronemeijer
wkronemeijer / iter.js
Last active August 29, 2015 14:25
Python-like iterators for using .next() in a sequence
this.iter = function iter(array) {
var copy = array.slice();
var i = 0;
var max = copy.length - 1;
var next = function next() {
var index = i
i += 1
if (index > max) {
@wkronemeijer
wkronemeijer / value
Created June 13, 2015 15:15
Creating value types with minimal boilerplate
// Sometimes you'll need a value type; for representing a point, or other values
// which are fully defined after construction. If we take the example of a point:
class Point {
constructor(public x: number, public y: number) {
return Object.freeze(this);
}
}
let p = new Point(3, 4);
@wkronemeijer
wkronemeijer / Object#extend
Created November 2, 2013 22:39
extend arbitrary classes with your prepoerties; this works just like the Backbone.js variant, but is now situated under Object.
Object.defineProperty(Object.prototype, 'extend', {value: function (proto_props) {
var child = proto_props.hasOwnProperty('constructor') ?
proto_props.constructor :
function () {child.__super__.constructor.apply(this, arguments);};
child.prototype = Object.create(this.prototype);
child.prototype.constructor = child;
Object.keys(proto_props).forEach(function (key) {
@wkronemeijer
wkronemeijer / gist:7281274
Created November 2, 2013 17:22
custom EventTargets à la the DOM variant.
CustomEventTarget = (function () {
function CustomEventTarget() {
if (!(this instanceof CustomEventTarget)) {
return new CustomEventTarget();
};
Object.defineProperty(this, '_listeners_', {
value: {}
});
};
method(CustomEventTarget, 'addEventListener', function (type, func) {
@wkronemeijer
wkronemeijer / method
Last active December 27, 2015 06:28
save three keystrokes every time
function method(obj, func) {
if (func.name !== "" && func.name !== 'anonymous') {
obj.prototype[func.name] = func;
};
};
@wkronemeijer
wkronemeijer / __module
Created November 2, 2013 16:51
simple augmentation module system for javascript. usage:
function __module(base, name, func) {
if (typeof base[name] === 'undefined') {
base[name] = {};
}
func(base[name]);
}
//usage:
// __module(window.MyModule, 'namespace', function (exports) {
// exports.sayHello = function () {