Category Archives: dev

SELECT latestversion FROM versionedRows: a comparative approach

Because there’s no “the best way to do things” but only the best way to do THIS thing (to read with Yoda voice).

Leave a comment

Posted by on 2017/09/07 in dev


Tags: , ,

GZip a file in memory (csharp)

It is a thing I ALWAYS forget: what’s the right piping sequence? How avoid errors? ..and so on…

Here’s the snippet

Leave a comment

Posted by on 2017/08/23 in dev


Tags: , ,

POST a file, read a JSON response

Here’s a trick to send a file to a server AND read a response from it, in the same context.
I used it with a Node.js server (Express + busboy) and it worked like a charm.

When using HTML forms, you can submit stuff to a server but you can’t read a response from it.
If you need some kind of “send data” → (do something server side) → “read response” it can be painful.

I wrote a workaround to manage this kind of situation. It is pretty simple:
– prepare a form as usual
– block the original event
– build an ajax request as a POST method

Here’s the code

Leave a comment

Posted by on 2017/07/14 in dev, Uncategorized


Tags: , ,

Promises rejection (..but with style)

I find handy define this kind of function in my code:

function if_error(reject, err) {
  if (err) {

Now when I’ll need to handle a rejection, my code will be

something.can('go-wrong', (err) => {
  if_error(reject, err);

It’s something between pure functional programming and very readable code.

Leave a comment

Posted by on 2017/05/16 in dev


Tags: ,

Arduino colors!

Last night I was playing with a Neopixel Ring controlled by a Wemos Mini, when I decided to attach a potentiometer to manually handle the colors.
Now, when you read from an analog source you’ll get a value in an interval, like [0, 1024]. The problem with this kind of value is that it is a single number that needs to be mapped in three r,g,b values (ring.setPixelColor(n, red, green, blue);)!

After trying a good way to map [0,1024] => {[0,255], [0,255], [0,255]}, I remembered there are MANY ways to define colors: RGB, CMYC, HSV, …Wait! HSV? It’s perfect!
It’s like handling polar coordinates where you have an angle that describes the Hue and it is a number between 0 and 360. The other parameters are two vectors and describe the Saturation and the Brightness. These values can be set constant to 1, in my case.

First let’s map the analog value to a Hue value:

int val = analogRead(A0); // 0 - 1024
float h = 360.0 * ((float)val / (float)1024); // 0 - 360

then use this function to convert. Note that r,g and b are by ref.

* HSV to RGB color conversion
* The hue value H runs from 0 to 360 degrees.
* The saturation S is the degree of strength or purity and is from 0 to 1. Purity is how much white is added to the color, so S=1 makes the purest color (no white).
* Brightness V also ranges from 0 to 1, where 0 is the black.
* R,G,B are between 0 and 255.
void HSVtoRGB(float h, float s, float v, float *r, float *g, float *b)
    int i;
    float f, p, q, t;

    if (s == 0) {
        // achromatic (grey)
        *r = *g = *b = v;
    else {
        h /= 60;      // sector 0 to 5
        i = floor(h);
        f = h - i;      // factorial part of h
        p = v * ( 1 - s );
        q = v * ( 1 - s * f );
        t = v * ( 1 - s * ( 1 - f ) );

        switch( i ) {
            case 0:
                *r = v;
                *g = t;
                *b = p;
            case 1:
                *r = q;
                *g = v;
                *b = p;
            case 2:
                *r = p;
                *g = v;
                *b = t;
            case 3:
                *r = p;
                *g = q;
                *b = v;
            case 4:
                *r = t;
                *g = p;
                *b = v;
            default:    // case 5:
                *r = v;
                *g = p;
                *b = q;
        *r *= 255;
        *g *= 255;
        *b *= 255;
Leave a comment

Posted by on 2017/03/24 in dev


Tags: ,

Extract the XPath of every node from an XML. Uh, in SQL Server

Disclaimer: this is SQL Server 2005 compliant! =O

Ok, we’re on a database and we want tables:

EXEC sp_xml_preparedocument @idoc OUTPUT, @xml
    ISNULL(id,'') AS id
    , parentid
	PARTITION BY parentid,localname
    ) AS idx
	PARTITION BY parentid
    ) AS position
    , nodetype
    , CASE
	WHEN (nodetype = 3) THEN 'text()'
	WHEN (nodetype = 2) THEN '@' + localname
	ELSE localname
    END as localname
    , text
  INTO #nodetree
  FROM OPENXML(@idoc, '/', 3)
EXEC sp_xml_removedocument @idoc

SELECT * FROM #nodetree

Now let’s use a recursive CTE to build the XPath of every text node

;WITH cte AS (
    , id
    , N'/' + localname + '[' + cast(idx as nvarchar(100)) + ']' AS xpath
    , text
  FROM #nodetree WHERE nodetype = 3
    , N'/' + localname + '[' + cast(idx as nvarchar(100)) + ']' + xpath
    , node.text
  FROM cte AS node
  INNER JOIN #nodetree parent on = node.parentid
  REPLACE(xpath, '/text()[1]', '') as xpath, text
  FROM cte
  WHERE parentid IS NULL

Finally check the XPath built:

  node.value(N'insert here one xpath row from the CTE', nvarchar(1000))
  from @xml.nodes(N'.') as T(node)
Leave a comment

Posted by on 2016/12/23 in dev


Tags: , , ,

js timestamp

Sometimes while logging js I need to have a timestamp in the beggining of the row. There are libraries to manage timestamps and to print wonderful logs, but sometimes the needs are only to make some console.log while debugging.

This is my one-line way to handle it:

function timestamp() {
  return (new Date).toISOString().match(/([0-9]{2}:[0-9]{2}:[0-9]?[0-9].[0-9]{3})/g)[0];

or “keep only the time part of an ISO formatted timestamp”.

Leave a comment

Posted by on 2016/12/16 in dev


Tags: , ,