Skip to content

Instantly share code, notes, and snippets.

View thejhh's full-sized avatar
💭
Excellence isn't believing you're best; It's about always striving to get better

Jaakko Heusala thejhh

💭
Excellence isn't believing you're best; It's about always striving to get better
View GitHub Profile
@thejhh
thejhh / test.js
Last active December 11, 2015 20:28
Building equations for multiple systems from JavaScript presentation by calling a function
"use strict";
/** Query builder */
function build_query(type, schema) {
// Metadata for builder
var _converters = {
js:{
or: function(x, y) { return '(' + x + ') || (' + y + ')'; },
and: function(x, y) { return '(' + x + ') && (' + y + ')'; },
@thejhh
thejhh / client.js
Created January 13, 2013 22:19
Test of a reverse HTTP request. Server will send data first, then wait for input from the client based on the data. Rude ugly code but it actually works.
var http = require('http');
var options = {
hostname: '192.168.56.101',
port: 1337,
path: '/',
method: 'POST'
};
var i = 0;
@thejhh
thejhh / package.json
Last active December 10, 2015 02:38
Promise test
{
"name": "promise-test",
"version": "0.0.1",
"description": "Promise test",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"author": "Jaakko-Heikki Heusala <jheusala@iki.fi>",
@thejhh
thejhh / .gitignore
Last active December 10, 2015 01:38
node_modules/
@thejhh
thejhh / example.js
Last active December 10, 2015 00:39
URI implementation
var uri_builder = require('./uri.js');
var service_uri = uri_builder('http://api.example.com');
var collection_uri = service_uri('/users');
console.log(require('url').format(collection_uri));
@thejhh
thejhh / gist:4316742
Created December 17, 2012 08:40
Example use of databases with Promises
var Q = require('q');
var DB = require('db');
// These DB initializations would probably not be synchronous in a real
// app. You could use Q.spread(), etc. See sample #2.
var db1 = DB.openSync("db1");
var db2 = DB.openSync("db2");
var db3 = DB.openSync("db3");
@thejhh
thejhh / gist:4077250
Created November 15, 2012 07:44
Usage of Neo4j query with promise support to list all nodes a user has read access
/** Get available objects for current user */
server.get('/', function(req, res, next) {
var username = req.authorization.basic.username;
db_query([
'START root=node(0)',
'MATCH root-[:USERS_REF]->uroot-[:USER]->user-[:ACCEPT_READ]->n',
'WHERE user.name = {username}',
'RETURN n'
], {'username': username}).then(function(results) {
var nodes = [];
@thejhh
thejhh / gist:2818014
Created May 28, 2012 08:38
Example source code for a command 'account' in Sendanor's CCD Server
/* $Id: account.js 13872 2010-08-23 11:42:51Z jheusala $ */
command.help.addSVN({
url:"$URL: https://svn.sendanor.fi/svn/sendanor/projects/ccd/server/trunk/commands/account.js $",
author:"$Author: jheusala $",
revision:"$Revision: 13872 $",
date:"$Date: 2010-08-23 14:42:51 +0300 (Mon, 23 Aug 2010) $",
id:"$Id: account.js 13872 2010-08-23 11:42:51Z jheusala $"
});

Funny thing with json_decode...

$ php
<?php echo var_export(json_decode("true ", true), true) ?>
NULL

(Press ctrl-d after you write "?>".)

The same using node.js:

mysql> SELECT * FROM freeciv_client_unitlog;
+------------+---------------------+-----------+--------+--------+------------------+---------+--------------+---------+
| unitlog_id | created | unit_name | unit_x | unit_y | player_name | unit_hp | unit_veteran | unit_id |
+------------+---------------------+-----------+--------+--------+------------------+---------+--------------+---------+
| 1 | 0000-00-00 00:00:00 | Trireme | 60 | 1 | Domenico Fattori | 10 | 0 | 164 |
| 2 | 0000-00-00 00:00:00 | Trireme | 67 | 14 | Domenico Fattori | 10 | 1 | 397 |
| 3 | 0000-00-00 00:00:00 | Trireme | 66 | 15 | Domenico Fattori | 10 | 1 | 397 |
| 4 | 0000-00-00 00:00:00 | Trireme | 65 | 16 | Domenico Fattori | 10 | 1 | 397 |
+------------+---------------------+-----------+--------+--------+------------------+---------+--------------+---------+
4 rows in s