diff options
-rw-r--r-- | lib/valueLimits.js | 17 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/valueLimits.js | 66 |
3 files changed, 84 insertions, 1 deletions
diff --git a/lib/valueLimits.js b/lib/valueLimits.js new file mode 100644 index 0000000..c902883 --- /dev/null +++ b/lib/valueLimits.js @@ -0,0 +1,17 @@ +/** + * In OSM tag values are limited to 255 characters. + * Search for addr:flats beyond that limit and wrap into addr:flats2 addr:flats3 etc. + * + * @param {Object} feature + * @returns {boolean} + */ +module.exports = (feature) => { + if ('addr:flats' in feature.properties && feature.properties['addr:flats'].length > 255) { + // need to wrap + const value = feature.properties['addr:flats'] + for (let i = 0; i < value.length; i += 255) { + feature.properties[`addr:flats${i === 0 ? '' : i / 255 + 1}`] = value.slice(i, i + 255) + } + } + return feature +} diff --git a/package.json b/package.json index 825e344..388427e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "author": "Andrew Harvey <andrew@alantgeo.com.au>", "license": "MIT", "scripts": { - "test": "./node_modules/.bin/tape test/toOSM.js test/cluster.js test/unitsToRanges.js test/withinRange.js" + "test": "./node_modules/.bin/tape test/toOSM.js test/cluster.js test/unitsToRanges.js test/withinRange.js test/valueLimits.js" }, "dependencies": { "capital-case": "^1.0.4", diff --git a/test/valueLimits.js b/test/valueLimits.js new file mode 100644 index 0000000..a7f7fd7 --- /dev/null +++ b/test/valueLimits.js @@ -0,0 +1,66 @@ +const test = require('tape') + +const valueLimits = require('../lib/valueLimits.js') + +test('less than limit', t => { + t.same( + valueLimits({ + properties: { + 'addr:flats': '' + } + }), + { + properties: { + 'addr:flats': '' + } + }, + 'less than limit' + ) + + t.same( + valueLimits({ + properties: { + 'addr:flats': '#'.repeat(255) + } + }), + { + properties: { + 'addr:flats': '#'.repeat(255) + } + }, + 'exactly at limit' + ) + + t.same( + valueLimits({ + properties: { + 'addr:flats': '#'.repeat(256) + } + }), + { + properties: { + 'addr:flats': '#'.repeat(255), + 'addr:flats2': '#'.repeat(1) + } + }, + 'one over limit' + ) + + t.same( + valueLimits({ + properties: { + 'addr:flats': '#'.repeat(255 + 255 + 100) + } + }), + { + properties: { + 'addr:flats': '#'.repeat(255), + 'addr:flats2': '#'.repeat(255), + 'addr:flats3': '#'.repeat(100) + } + }, + 'split into three' + ) + + t.end() +}) |