Sunday, December 28, 2014

Simple JavaScript function to compare and sort localized strings

A recurring problem in JavaScript is sorting localized strings, that is, strings having characters from outside of the ASCII [A-Za-z] range, like for example the Spanish characters áéíóúüñ (and their uppercase equivalents).

For this problem I wrote the JavaScript function getLocaleStrCmpFn, which returns a function to compare and sort strings depending on the order given as parameter, for example, to sort an array of strings having Spanish characters you can do:

var ordES = ['AaÁá','Bb','Cc','Dd','EeÉé','Ff','Gg','Hh',
'IiÍí','Jj','Kk','Ll','Mm','Nn','Ññ','OoÓó','Pp','Qq',
'Rr','Ss','Tt','UuÚúÜü','Vv','Ww','Xx','Yy','Zz'];
var fnES = getLocaleStrCmpFn(ordES);

var arr = ['Álamo','abedul','Arce','abeto','Castaño','acacia'];
arr.sort(fnES);

Other ways to do it are the localeCompare function with additional non-standard arguments or the future JavaScript Internationalization API, but both are not supported in many versions of major browsers, so the locale-str-cmp.js solution is an easy way to sort strings for a specific locale or to create custom comparison functions. What do you think?

Thursday, November 13, 2014

Program to draw Tangram figures in SVG

The Tangram is an ancient Chinese game consisting in 7 geometrical flat pieces which must be put together to form a given figure, usually printed in paper. The solution for a given problem can be in another page, for example, this is how you form a square with the Tangram pieces:


I wrote a program in Perl called draw-tangram to create images of Tangram figures. To create a figure you need to write a description of the position of the pieces in a text file, in a simple format created for the program. Then you can choose to create images for the problem or the solution. For example:


To make this figure I first wrote the following instructions in a text file:

<3/4> a-b-c-d-a <0,6,4,2> [1,1,1,1];
<3/4> b-e-f-c-b <7,6,3,2> [0:1,1,0:1,1];
<3/4> f-g-h-f <3,6,1> [1,0:1,1];
<1/2> h-i-j-h <4,7,2> [2,0:2,2];
<1/2> i-k <7> [1];
<1/2> k-l-m-k <7,5,2> [1,1,0:1];
<1/2> k-o-n-k <3,5,0> [2,2,0:2];
<1/2> n-q <1> [0:1];
<1/2> q-s-r-q <1,4,7> [0:1,2,0:1];

Numbers between < and > are angles and numbers between [ and ] are lengths. The program helps you to create this because without parameters it prints the names of the points of the figure. To run the program you need Perl installed, and then you can write, for example:

perl draw-tangram.pl figure.txt

Then you can open the generated SVG file in a web browser to see the result. After that, you can use a program for editing vector graphics like Inkscape if you want a PNG or JPEG image. Enjoy it!

Sunday, June 15, 2014

CSV calendar generator

csv-cal.c is a command-line calendar program written in C to generate files in format CSV (Comma-Separated Values). The output of the program can be redirected to a .csv file ready to be opened in a spreadsheet application like LibreOffice Calc in order to format and print the calendar as you like.
The program allows to choose the first month to show, how many months and how many columns to show. The first day of the week can be set to Sunday or Monday and the names of the months and the days of the week can be changed. For example, to generate a calendar for the year 2014 starting the weeks on Monday and using Spanish names, you can type:
csv-cal -m 2014 "do,lu,ma,mi,ju,vi,sá" "Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre,Diciembre" >2014.csv
To run it you need first a C compiler to generate the executable file, and then you need to know how to execute it from the command-line console. Executing the program without parameters will print a help for the different options that it supports. Enjoy!