From a74109cf7f487fb7a4c09ed7af765720e1542a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Desboeufs?= Date: Fri, 14 Nov 2014 17:04:46 +0100 Subject: [PATCH] First implementation --- .gitignore | 2 ++ api.js | 8 ++++++++ import-data.js | 20 ++++++++++++++++++++ package.json | 11 ++++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 api.js create mode 100644 import-data.js diff --git a/.gitignore b/.gitignore index 59d842b..ba41a74 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ node_modules # Users Environment Variables .lock-wscript + +codes-postaux.json diff --git a/api.js b/api.js new file mode 100644 index 0000000..f2b696a --- /dev/null +++ b/api.js @@ -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] : []; +}; diff --git a/import-data.js b/import-data.js new file mode 100644 index 0000000..c40d44f --- /dev/null +++ b/import-data.js @@ -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')); diff --git a/package.json b/package.json index 91b0e6a..e995481 100644 --- a/package.json +++ b/package.json @@ -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" + } }