From 422a1895cad4dcee2a82eba1aad86513268ed0ce Mon Sep 17 00:00:00 2001 From: slomo Date: Fri, 28 Jan 2011 17:21:42 +0100 Subject: url parsing --- src/nodejs/main.js | 64 +++++++++++++++++++++++++++++++++++++++++ src/nodejs/parse.js | 48 +++++++++++++++++++++++++++++++ src/nodejs/tests/urlToResObj.js | 4 +-- 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/nodejs/parse.js diff --git a/src/nodejs/main.js b/src/nodejs/main.js index 4411081..1c01b78 100644 --- a/src/nodejs/main.js +++ b/src/nodejs/main.js @@ -80,6 +80,70 @@ function rowToWay(row){ // #################### MAY be put to different module later + +// #################### my little clutch replacments + + +var urlToXpathObj = function urlToXpathObj(url){ + + // FIXME: more validaiton + // filter stars in keys + // filter no enough arguments + + var parseKeyList = function(string){ + result = /(.+)(:?\|(.+))/.exec(string); + result.shift(); + return result; + } + + var parseBboxList = function(string){ + + result = /(.+)(:?,(.+)){3}/.exec(string): + + if(result.length != 4){ + throw "error"; + } + + result.shift(); + + return { + 'left' : result[0]; + 'bottom' : result[1]; + 'right' : result[2]; + 'top' : result[3]; + } + + var xp = {}; + + result = /\/(*|node|way|relation)(:?\[(.*)=(.*)\])*/.exec(url); + + xp.object=result[1]; + + for(i=2;i<=result.length();i++){ + if(result[i]==="bbox"){ + xp.bbox = parseBboxValues(result[i+1]); + } else { + xp.tag ={}; + xp.tag.keys = parseKeyList(result[i]); + xp.tag.values = parseKeyList(result[i+1]); + } + i++; + } +} + + + + + + + +// ################## end my little clutch replacments + + + + + + var options = [ { short : 'c', long : 'config', diff --git a/src/nodejs/parse.js b/src/nodejs/parse.js new file mode 100644 index 0000000..0338954 --- /dev/null +++ b/src/nodejs/parse.js @@ -0,0 +1,48 @@ +exports.urlToXpathObj = function urlToXpathObj(url){ + + // FIXMEresult.shift(): more validaiton + // filter stars in keys + // filter no enough arguments + + var parseKeyList = function(string){ + result = /(.+)(:?\|(.+))/.exec(string); + result.shift(); + return result; + } + + var parseBboxList = function(string){ + + result = /(.+)(:?,(.+)){3}/.exec(string); + + if(result.length != 4){ + throw "error"; + } + + result.shift(); + + return { + 'left' : result[0], + 'bottom' : result[1], + 'right' : result[2], + 'top' : result[3] + } + } + + var xp = {}; + + result = /\/(*|node|way|relation)(:?\[(.*)=(.*)\])*/.exec(url); + + xp.object=result[1]; + + for(i=2;i<=result.length;i++){ + if(result[i]==="bbox"){ + xp.bbox = parseBboxValues(result[i+1]); + } else { + xp.tag ={}; + xp.tag.keys = parseKeyList(result[i]); + xp.tag.values = parseKeyList(result[i+1]); + } + i++; + } +} + diff --git a/src/nodejs/tests/urlToResObj.js b/src/nodejs/tests/urlToResObj.js index 3740745..a29fb3f 100644 --- a/src/nodejs/tests/urlToResObj.js +++ b/src/nodejs/tests/urlToResObj.js @@ -1,7 +1,7 @@ var assert = require('assert'); -//var test = require(''); +var test = require('../parse.js'); //TODO function does not exist yet -var toTest = new function(id) { return id; }; +var toTest = test.urlToXpathObj; // test all simple objects { node, way, relation } -- cgit v1.2.3