Skip to content

Instantly share code, notes, and snippets.

View kl0tl's full-sized avatar

Cyril Sobierajewicz kl0tl

View GitHub Profile
@kl0tl
kl0tl / gl-matrix.swizzle.js
Last active August 29, 2015 14:01
Swizzle operator for the awesome https://github.com/toji/gl-matrix
var $each, $every, aliases;
$each = Function.prototype.call.bind(Array.prototype.forEach);
$every = Function.prototype.call.bind(Array.prototype.every);
aliases = {
x: 'r', y: 'g', z: 'b', w: 'a'
};
function f(dest, channels, aliases) {
@kl0tl
kl0tl / lazy.js
Last active July 11, 2019 05:55
Lazy initialization with Object.defineProperty
var accessorDescriptor, valueDescriptor;
accessorDescriptor = {
__proto__: null,
get: null, set: null,
enumerable: true,
configurable: true
};
valueDescriptor = {
@kl0tl
kl0tl / private.js
Last active November 2, 2022 10:59
Private properties using Object.defineProperty and the .caller property of functions
function $private(object, property, descriptor) {
var getter = descriptor.get;
var setter = descriptor.set;
var value = descriptor.default;
var enumerable = Boolean(descriptor.enumerable) || false;
var configurable = Boolean(descriptor.configurable) || false;
var $descriptor = {
@kl0tl
kl0tl / k.js
Last active August 29, 2015 14:02
Very simple class alternative
/*
var Foo = {
__proto__: K,
method: function () {}
};
var Bar = {
__proto__: Foo
};
@kl0tl
kl0tl / if.macro.sjs
Last active August 29, 2015 14:02
Parens free `if` statement
/*
`if cond {}`
Brackets are mandatory, except when `if` is followed by specials keywords (return, break and continue)
```
if cond return
if cond break [label]
if cond continue [label]
@kl0tl
kl0tl / unless.macro.sjs
Last active August 29, 2015 14:02
Parens free `unless` statement, the opposite of `if`
/*
`unless cond {}` is equivalent to `if !(cond) {}`
Brackets are mandatory, except when `unless` is followed by specials keywords (return, break and continue)
```
unless cond return
unless cond break [label]
unless cond continue [label]
@kl0tl
kl0tl / while.macro.sjs
Last active August 29, 2015 14:02
Parens free `while` statement
/*
`while cond {}`
Brackets are mandatory
The following syntaxes are also valid
`do {} while cond`
`while cond;`
@kl0tl
kl0tl / until.macro.sjs
Last active August 29, 2015 14:02
Parens free `until` statement, the opposite of `while`
/*
`until cond {}` is equivalent to `while !(cond) {}`
Brackets are mandatory
The following syntaxes are also valid
`do {} until cond`
`until cond;`
@kl0tl
kl0tl / for.macro.sjs
Last active August 29, 2015 14:02
Parens free `for`, `for in` and `for own` statements
/*
```
for var i = 0; i < length; i += 1 {
...
}
for var key in object {
...
}
@kl0tl
kl0tl / switch.macro.sjs
Last active August 29, 2015 14:02
Parens free `switch` expression
/*
Multiple expressions per case are allowed
The result of the matching case, or the optional default, is automatically returned
```
var x = switch os {
case 'osx', 'ios': 'apple'
default: os