Skip to content

Instantly share code, notes, and snippets.

@nnarhinen
nnarhinen / component.js
Last active March 15, 2016 09:46
OR validation for React props
const Component = React.createClass({
propTypes: {
file: requireThisOr('files', React.PropTypes.instanceOf(File).isRequired),
files: requireThisOr('file', React.PropTypes.arrayOf(React.PropTypes.instanceOf(File)).isRequired)
}
})
@nnarhinen
nnarhinen / express-server-generator.js
Last active February 28, 2016 17:48
Example files on how to use ES6 generators in your code to reduce callback nesting
'use strict';
/*
* Express server using ES6 generators
* Use node 0.11
* run with node --harmony express-server-generator.js
*/
var Q = require('q'),
HTTP = require('q-io/http'),
express = require('express'),
http = require('http');
class BaseVideoListView extends Backbone.View
initialize: (opts) ->
@collection.selectedCount.assign @subNav.find('.selected_count'), 'text'
@collection.selectedCount.onValue (val) =>
@subNav.find('.library-nav').toggleClass 'singular-selection', val == 1
this.$el.toggleClass 'multiple-selection', val > 1
@collection.hasSelected.assign @subNav.find('.visible-selected'), 'toggle'
@collection.hasSelected.not().assign @subNav.find('.hidden-selected'), 'toggle'
function calculate (objects) {
return _.chain(objects)
.map(function(p) { return [p.vat, p.price]; })
.reduce(function(memo, pair) {
var o = {};
o[pair[0]] = (memo[pair[0]] || 0) + pair[1];
return _.extend({}, memo, o);
}, {}).value();
};
@nnarhinen
nnarhinen / deploy.bash
Last active December 31, 2015 12:28
My deployment script to pull and run a docker-contained clojure web application
id=$(docker ps | grep myimage:latest | awk '{ print $1 }')
docker pull quay.io/nnarhinen/myimage
new_id=$(docker run -d -p 3000 \
-e ENVIRONMENT=PRODUCTION quay.io/nnarhinen/myimage)
id=$new_id ./update-nginx-port.bash
docker stop $id
@nnarhinen
nnarhinen / foo-view.js
Last active December 30, 2015 18:39
My view definition not using Backbone views. Simple and easy, no magic involved. Using plain javascript objects as models and transparency.js for rendering
var fs = require('fs'),
fooTemplate = fs.readFileSync(__dirname + '/templates/foo-template.html'),
_ = require('underscore'),
app = require('../app');
var FooView = module.exports = function FooView(model) {
var self = this;
self.model = model;
self.element = $(fooTemplate);
grunt.initConfig({
///blabla
browserify: {
basic: {
src: ['<%= yeoman.app %>/scripts/main.js'],
options: {
transform: ['brfs']
},
dest: '.tmp/scripts/application.js'
}
@nnarhinen
nnarhinen / some.html
Created November 26, 2013 21:26
Make your plain javascript objects and arrays observable with just jquery, jsfiddle: http://jsfiddle.net/EeGkU/3/
<p>Observable plain javascript objects with jQuery (rendered with transparency.js)</p>
<ul class="items">
<li class="title"></li>
</ul>
<p>Wait for a few seconds and watch the last item in list</p>
@nnarhinen
nnarhinen / foo.conf
Created November 8, 2013 08:00
Upstart script for running node as non-root service
description "Foo application Node.js service"
author "Niklas Närhinen"
setuid foo
start on (local-filesystems and net-device-up)
stop on shutdown
respawn
respawn limit 5 60
@nnarhinen
nnarhinen / scan.bash
Created October 9, 2013 17:57
Scan documents as pdf with gamma correction
#!/bin/bash
#find out scanner model with scanimage -L
scanimage -d hp3900:libusb:003:005 --resolution=600 --mode=Gray > raw.pnm
#gamma correction (fiddle around with the value suitable for your printer)
convert raw.pnm -gamma 0.5 out.pnm
#convert to pdf
gm convert out.pnm out.pdf