Skip to content

Commit

Permalink
First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesboeufs committed Nov 14, 2014
1 parent 702088b commit a74109c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ node_modules

# Users Environment Variables
.lock-wscript

codes-postaux.json
8 changes: 8 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var _ = require('lodash');
var data = require('./codes-postaux.json');
var index = _.groupBy(data, 'codePostal');

exports.listByCodePostal = function(codePostal) {
var key = codePostal.toString();
return (key in index) ? index[key] : [];
};
20 changes: 20 additions & 0 deletions import-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var request = require('request');
var parse = require('csv-parse');
var JSONStream = require('JSONStream');
var fs = require('fs');

var sourceFile = 'https://www.data.gouv.fr/s/resources/base-officielle-des-codes-postaux/20141106-120608/code_postaux_v201410.csv';

// Override first line
var columns = function() {
return ['codeInsee', 'nomCommune', 'codePostal', 'libelleAcheminement'];
};

// Request source file
request(sourceFile)
// Parse CSV as Object
.pipe(parse({ delimiter: ';', trim: true, columns: columns }))
// Turn into JSON Array String
.pipe(JSONStream.stringify())
// Output
.pipe(fs.createWriteStream(__dirname + '/codes-postaux.json'));
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@
"bugs": {
"url": "https://github.com/sgmap/codes-postaux/issues"
},
"homepage": "https://github.com/sgmap/codes-postaux"
"homepage": "https://github.com/sgmap/codes-postaux",
"main": "api",
"dependencies": {
"lodash": "^2.4.1"
},
"devDependencies": {
"JSONStream": "^0.9.0",
"csv-parse": "0.0.6",
"request": "^2.48.0"
}
}

0 comments on commit a74109c

Please sign in to comment.