Skip to content

Instantly share code, notes, and snippets.

View rla's full-sized avatar

Raivo Laanemets rla

View GitHub Profile
var control = require('../control');
var checkbox = control.checkbox;
var text = control.text;
var select = control.select;
// Adds cabriolet properties to the vehicle.
exports.mixin = function(vehicle) {
var flatten = (a, r, cb) => {
if (typeof a.length === 'undefined') {
r.push(a);
return cb;
} else if (a.length === 0) {
return cb;
} else {
return flatten(a[0], r, () => flatten(a.slice(1), r, cb));
}
};
@rla
rla / code.js
Created May 1, 2016 09:35
API and MySQL
app.get('/matches', function(req, res, next) {
fixture.withTip().then(function(matches) {
res.render('matches', { matches: matches });
}).catch(next);
});
// database.js
exports.withTip = function() {
var sql = 'SELECT * FROM `fixture`';
@rla
rla / code.js
Created May 1, 2016 09:32
MySQL pool and Promises
var mysql = require('mysql');
var config = require('../config.json');
var pool = mysql.createPool(config.mysql);
// Runs MySQL query.
// Returns a promise.
exports.query = function(query, params) {
return new Promise(function(resolve, reject) {
@rla
rla / code.js
Last active April 22, 2016 05:14
function create_warehouse(t, account_id) {
var warehouse = {
account_id: account_id,
name: 'Default warehouse',
type: 'local',
is_default: true
};
var sql = 'insert into warehouse(account_id, name, type, is_default)' +
' values (${account_id}, ${name}, ${type}, ${is_default}) returning id';
return t.one(sql, warehouse);
@rla
rla / code.js
Created April 11, 2016 18:53
Extraction Expression Evaluator
var _eee = (function() {
// Guard against Prototype manipulations.
// Later this needs proper workaround for some
// libraries that override native objects.
if (window.Prototype) {
delete Object.prototype.toJSON;
delete Array.prototype.toJSON;
@rla
rla / results.json
Last active April 11, 2016 15:50
Extraction Expression Evaluator (EEE) for http://stackoverflow.com/questions/tagged/prolog
{
"questions": [
{
"title":"binary search tree in prolog",
"url":"/questions/35714742/binary-search-tree-in-prolog"
},
{
"title":"Just can't import files to my Racket program",
"url":"/questions/35704143/just-cant-import-files-to-my-racket-program"
},
// Moves step upwards.
model.click.up = function(step) {
move(step, -1);
};
// Moves step downwards.
model.click.down = function(step) {
@rla
rla / code.prolog
Created March 17, 2016 13:32
Facebook group posts extraction
posts(GroupUrl, Posts):-
Query = _{
wait: 5300,
url: GroupUrl,
extract: _{
posts: _{
type: collection,
selector: '.userContentWrapper',
extract: _{
title: _{ selector: 'h5' },
prolog_questions(Questions):-
Query = _{
url: 'http://stackoverflow.com/questions/tagged/prolog',
extract: _{
questions: _{
type: collection,
selector: '#questions > div',
extract: _{
url: _{ selector: 'h3 > a', attribute: 'href' },
title: _{ selector: 'h3 > a' }