Skip to content

Instantly share code, notes, and snippets.

🌌
I must go, the galaxy needs me!

Klemen Slavič KrofDrakula

🌌
I must go, the galaxy needs me!
Block or report user

Report or block KrofDrakula

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View regexp.ts
const SPECIAL_CHARS = /[-\/\\^$*+?.()|[\]{}]/g;
const ESCAPED_CHAR = "\\$&";
const escapeRegExp = (s: string) => s.replace(SPECIAL_CHARS, ESCAPED_CHAR);
const regexp = (flags?: string) =>
(strings: string[], ...values: string[]) =>
new RegExp(strings[0] + values.map((value, index) => escapeRegExp(value) + strings[index + 1]).join(""), flags);
@KrofDrakula
KrofDrakula / clone.ts
Created Apr 3, 2019
Just stashing a proof-of-concept
View clone.ts
const isPrimitive = (obj) => {
if (typeof obj === 'number' || typeof obj === 'boolean' || typeof obj === 'string') return true;
if (Number.isNaN(obj) || obj === null || obj === undefined) return true;
return false;
}
const isIterable = (obj) => !isPrimitive(obj);
const clone = (obj) => {
if (isIterable(obj)) {
@KrofDrakula
KrofDrakula / stateful.js
Created May 18, 2018
A stateful HOC for pure components
View stateful.js
import { h, Component } from 'preact';
const withState = Wrapped => class extends Component {
constructor(props) {
super(props);
this.state = props;
}
componentWillReceiveProps(nextProps) {
this.setState(nextProps);
View auto_classnames.jsx
import { options } from 'preact';
import cx from 'classnames';
let old = options.vnode;
options.vnode = vnode => {
let isElement = typeof vnode.nodeName==='string',
props = vnode.attributes || {},
key = props.class ? 'class' : 'className';
if (isElement && props[key] && typeof props[key]==='object') {
props[key] = cx(props[key]);
@KrofDrakula
KrofDrakula / vec2d.js
Created Jul 23, 2016
A mutable 2D vector class, made for real-time graphics in JS
View vec2d.js
class Vec2d {
constructor(x, y) { this.reset(x, y); }
get mag() { return Math.sqrt(this.dot(this)); }
clone() { return new Vec2d(this.x, this.y); }
reset(x, y) { this.x = x; this.y = y; return this; }
neg() { return this.scale(-1); }
norm() {
let m = this.mag;
if (m > 0) return this.scale(1 / m);
return this;
@KrofDrakula
KrofDrakula / tmpl.js
Created Jun 5, 2015
Small templating function
View tmpl.js
// Use `<% ... %>` to execute blocks of JavaScript, `<%= ... %>` to write
// out the result of the embedded expression.
function tmpl(str, params) {
if (!str) return '';
function generateOutput(str) {
return " p.push('" + str.replace(/'/g, "\\'").split(/\r?\n/g).join("\\n');\n p.push('") + "');\n";
}
var fn;
View keybase.md

Keybase proof

I hereby claim:

  • I am krofdrakula on github.
  • I am krofdrakula (https://keybase.io/krofdrakula) on keybase.
  • I have a public key whose fingerprint is A5B2 D08D 4333 E167 DB37 A7C0 6444 7954 106D 1D39

To claim this, I am signing this object:

@KrofDrakula
KrofDrakula / curves.js
Created Dec 28, 2014
CSS Animation Bézier curve generator
View curves.js
(function(global) {
function extend(obj) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var name in source) if (source.hasOwnProperty(name))
obj[name] = source[name];
}
return obj;
}
@KrofDrakula
KrofDrakula / dither.js
Created Nov 22, 2014
Drag image example
View dither.js
(function(global) {
function Dither(container, width, height) {
this.container = container;
this.width = width;
this.height = height;
this.canvas = null;
this.image = null;
this.draw = this.draw.bind(this);
You can’t perform that action at this time.