It's a simple api rest of users, GET, POST, DELETE over http://
jueves, 14 de enero de 2016
Playing with express and mongodb in nodejs
Today I've finished a little toy.
It's a simple api rest of users, GET, POST, DELETE over http://localhost:8080/api/users
The code lives here
I'm studing the ecosystem of node-javascript. I think that it's
very interesting. But I need a debugger, because a little project is
fine, but in a huge one, it becomes a horrible monster.
A new song in my life by Ludovico Einaudi.
Happy crafting :-)
It's a simple api rest of users, GET, POST, DELETE over http://
viernes, 25 de diciembre de 2015
functional programming in javascript
Since summer I've been studing functional programming, first of all I started with clojure, later continued to javascript. Actually I studied some functional programming concepts and I applied into javascript, like map, reduce, foreach, monads, ...
Of course been doing tdd, althoguh the tests are not very expressives.
The code lives here.
Some examples of tests
Of course been doing tdd, althoguh the tests are not very expressives.
The code lives here.
Some examples of tests
describe("combine filter, map & reduce", function(){
it("return the result of combination of filter, mal and reduce", function(){
var data = [1,2,3,4,5,6];
var resultExpected = 15;
var result = reduce(
map(
filter(
data, functionForFilter
),
functionForMap
),
0, functionForReduce
);
expect(resultExpected).to.eql(result);
});
function functionForMap(y){
return y + 1;
}
function functionForFilter(x){
return x % 2 == 0;
}
function functionForReduce(acumulative, z){
return acumulative + z;
}
});
describe("PipeLine object", function(){
it("make pipe line through different functions", function(){
var arrayOfData = [1,2,3];
var expectedResult = 9;
var handlerOne = function(data){
return map(data, function(number){
return number +1;
});
}
var handlerTwo = function(data){
var result = 0;
return reduce(data, result, function(result, number){
return result + number;
});
}
var pipeLine = new PipeLine();
var pipeLineResult = pipeLine.pipe(handlerOne)
.pipe(handlerTwo)
.execute(arrayOfData);
expect(expectedResult).to.eql(pipeLineResult);
});
});
describe("curry function", function(){
var expectedResult = 10;
var add = function(a, b, c, d){
return a + b + c + d;
}
var curriedFunction = curry(add);
beforeEach(function(){});
it("curry function with all parameters as partial", function(){
var result = curriedFunction(1)(2)(3)(4);
expect(expectedResult).to.eql(result);
});
it("curry function with two parameters as partial", function(){
var result = curriedFunction(1, 2)(3)(4);
expect(expectedResult).to.eql(result);
});
it("curry function with one parameters as partial", function(){
var result = curriedFunction(1, 2, 3)(4);
expect(expectedResult).to.eql(result);
});
it("curry function with parameters as partial in two pairs", function(){
var result = curriedFunction(1, 2)(3, 4);
expect(expectedResult).to.eql(result);
});
it("curry function with parameters as usual", function(){
var result = curriedFunction(1, 2, 3, 4);
expect(expectedResult).to.eql(result);
});
});
Playing with promises in javascript
Reading about callbacks, monads, ... I've tried q library.
Interesting reading about monads and promises, source
This the code.
Interesting reading about monads and promises, source
This the code.
var fs = require('fs');
var Q = require('q');
function readFile(file){
var deferred = Q.defer();
fs.readFile(file, 'utf8', function (err, data) {
if (err){
deferred.reject(err);
}
else{
deferred.resolve(data);
}
});
return deferred.promise;
}
var textFilteContent = readFile('notes.txt');
textFilteContent.then(function(data){
console.log(data);
}).fail(function(err){
console.log('error reading the file: ', err);
});
console.log('end');
jueves, 3 de diciembre de 2015
Lean Code Kata
Yesterday I was in an event at Software Craftsmanship Madrid. It was about lean code. The result of the exercise lives in my repo at Github.com. The code could be refactored. But I am learning that early refactoring could be a wrong decision for the future.
jueves, 26 de noviembre de 2015
bind in js
miércoles, 4 de noviembre de 2015
Playing with socket.io
More and more javascript is the language that the most of the time I'm writting. Some time ago I've discovered web sockets, so I've been playing with it. The result is a simple chat with socket.io library.
The source of the example code
The code of this example lives in github
Happy hacking
The source of the example code
The code of this example lives in github
Happy hacking
jueves, 8 de octubre de 2015
Setup the current number format
I´ve discovered the new way for setting up numbers in the current country format. I´m from Spain, so the numbers are like 12.345,67.
We can use.
We can use.
var result = parseInt(simpleNumber).toLocaleString('es-ES');
Suscribirse a:
Entradas (Atom)