Skip to content

Instantly share code, notes, and snippets.

View kl0tl's full-sized avatar

Cyril Sobierajewicz kl0tl

View GitHub Profile
@kl0tl
kl0tl / annotation.macro.sjs
Last active August 29, 2015 14:02
`@Annotation` déclaration
/*
Annotations can only occur immediately before a variable, a function declaration or an assignment
```
@Component('position')
module.exports = function (x, y) {
return {x: x || 0, y: y || 0};
}
@kl0tl
kl0tl / existential-operator.macro.sjs
Last active August 29, 2015 14:02
Existential operator `?`
/*
`a?()`
Call its operand only if it is defined and a `function`, returns `undefined` otherwise
`a?.b` or `a?['b']`
Access a property on its operand only if it is defined and not `null` nor `undefined`, returns `undefined` otherwise
@kl0tl
kl0tl / lazy.macro.sjs
Last active August 29, 2015 14:02
Lazy initialization
/*
```
var foo = {
lazy computed() {
return [];
}
};
```
@kl0tl
kl0tl / bound.macro.sjs
Last active August 29, 2015 14:02
Bound functions déclaration (aka methods)
/*
```
var foo = {
bound method() {
return this;
}
};
var method = foo.method;
@kl0tl
kl0tl / do.macro.sjs
Last active August 29, 2015 14:02
Coffeescript like `do` expression
/*
Immediately invoke a block with scopped variables
```
do (foo = 'bar') {
...
}
```
@kl0tl
kl0tl / forEach.macro.sjs
Last active August 29, 2015 14:02
Inline `Array#forEach`
macro statement {
rule { return $expression:expr } => { $expression; continue }
rule { $expression:expr ... }
}
macro inline_forEach {
rule { ($list:expr, function ($value:ident, $index:ident, $ref:ident) {
$body:statement ...
}, $context:expr) } => {
(function ($ref) {
@kl0tl
kl0tl / map.macro.sjs
Last active August 29, 2015 14:02
Inline `Array#map`
macro inline_map {
rule { ($list:expr, function ($value:ident, $index:ident, $ref:ident) {
$return:expression
}, $context:expr) } => {
(function ($ref) {
var length = $ref.length, res = new Array(length);
for (var $index = 0; $index < length; $index += 1) {
var $value = $ref[$index];
@kl0tl
kl0tl / indexOf.macro.sjs
Created June 7, 2014 22:02
Inline `Array#indexOf`
macro inline_indexOf {
rule { ($list:expr, $needle:expr) } => {
(function (ref, needle) {
var length = ref.length;
for (var i = 0; i < length; i += 1) {
if (ref[i] === needle) return i;
}
return -1;
}($list, $needle));
@kl0tl
kl0tl / lastIndexOf.macro.sjs
Created June 7, 2014 22:03
Inline `Array#lastIndexOf`
macro inline_lastIndexOf {
rule { ($list:expr, $needle:expr) } => {
(function (ref, needle) {
for (var i = ref.length - 1; i > -1; i -= 1) {
if (ref[i] === needle) return i;
}
return -1;
}($list, $needle));
}
@kl0tl
kl0tl / keys.macro.sjs
Created June 8, 2014 14:44
Inline `Object.keys` (seems slower with Firefox)
let keys = macro {
rule infix { Object. | () } => { Object.keys() }
rule infix { Object. | ($object:expr) } => {
(function (object) {
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
var keys = [], proto = object.__proto__;