Skip to content

Instantly share code, notes, and snippets.

View gnab's full-sized avatar

Ole Petter Bang gnab

  • Onyx CenterSource
  • Tønsberg, Norway
View GitHub Profile
@gnab
gnab / random.sjs
Last active January 3, 2016 20:38
Sweet.js random expression macro
macro random {
case { _ ($expr:expr (,) ...) } => {
var matches = match.patternEnv.$expr.match;
var randomIndex = Math.floor(Math.random() * matches.length);
var randomMatch = matches[randomIndex].match;
return randomMatch
}
}
random(console.log(1), console.log(2), console.log(3))
@gnab
gnab / server.js
Created December 11, 2013 18:34
Syntax highlighting Markdown code blocks with Harp.
require('harp').server(__dirname + '/public', { port: process.env.PORT || 5000 });
var marked = require('./node_modules/harp/node_modules/terraform/node_modules/marked');
var highlight = require('highlight.js');
marked.setOptions({
langPrefix: '',
highlight: function (code, language) {
if (language) {
return highlight.highlight(language, code).value;
@gnab
gnab / generators.js
Created December 10, 2013 12:10
Generators.
function* fib () {
var [a, b, n] = [1, 1, 1];
while (true) {
yield [a, n];
[a, b, n] = [b, a + b, n + 1];
}
}
for (let [n, i] of fib()) {
@gnab
gnab / destructuring.js
Last active December 30, 2015 21:48
Destructuring.
var [first, second, ...rest] = [1, 2, 3, 4, 5];
console.log('first =', first);
console.log('second =', second);
console.log('rest =', rest);
// => first = 1
// => second = 2
// => rest = [3, 4, 5]
function printPoint({x: x, y: y}) {
@gnab
gnab / spread.js
Created December 10, 2013 11:58
Spread operator.
function sum (...args) {
return args.reduce((a, b) => a + b, 0);
}
var nums = [1,2,3];
// ES5
console.log('sum.apply(null, nums) =', sum.apply(null, nums));
// => sum.apply(null, nums) = 6
@gnab
gnab / block-bindings.js
Last active December 30, 2015 21:50
Block binding.
var nums = [1, 2, 3];
for (var i in nums) {
setTimeout(() => {
console.log('var i =', nums[i]);
}, 1000 * i);
}
for (let i in nums) {
setTimeout(() => {
@gnab
gnab / bindings.js
Last active December 28, 2015 23:29
KO numeric extension.
ko.bindingHandlers.number = {
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var value = ko.unwrap(valueAccessor());
valueAccessor = function () {
return value.toString().replace('.', ',');
};
ko.bindingHandlers.text.update(element, valueAccessor, allBindings, viewModel, bindingContext);
}
};
@gnab
gnab / demo.js
Created October 11, 2013 13:05
RequireJS factory plugin. Uses temporarily unique resource names to trick Require into calling plugin#load every time the resource is needed, and returns a new instance of the resource every time.
define('itemFactory', function () {
function Item() {
this.value = Math.ceil(Math.random() * 1000);
};
return Item;
});
define('firstView', ['factory!itemFactory'], function (item) {
console.log('First view; ' + JSON.stringify(item));
@gnab
gnab / fizzbuzz.clj
Created June 24, 2013 19:58
FizzBuzz solved using condp function
(defn FizzBuzz
"Returns 'Fizz', 'Buzz' or 'FizzBuzz' if n is divisible by
3, 5 or both, respectively, or else just n."
[n]
(condp #(zero? (mod %2 %1)) n
15 "FizzBuzz"
3 "Fizz"
5 "Buzz"
(str n)))
@gnab
gnab / whenCacheInSync.js
Last active December 18, 2015 07:39
Reload until application cache is in sync (noupdate). If you don't reload after app cache is updated, you'll still be using old html/css/js until the next time you reload. Should be called initially with a callback that loads your application like you normally would.
function whenCacheInSync(callback) {
// 1. Checking
var onChecking = function () {
window.applicationCache.removeEventListener('checking', onChecking);
window.applicationCache.addEventListener('noupdate', onNoUpdate);
window.applicationCache.addEventListener('downloading', onDownloading);
};
// 1A. No updates, load app