UNPKG

983 BJavaScriptView Raw
1var jsonlint = require('jsonlint-lines'),
2 geojsonHintObject = require('./object');
3
4/**
5 * @alias geojsonhint
6 * @param {(string|object)} GeoJSON given as a string or as an object
7 * @returns {Array<Object>} an array of errors
8 */
9function hint(str) {
10
11 var gj, errors = [];
12
13 if (typeof str === 'object') {
14 gj = str;
15 } else if (typeof str === 'string') {
16 try {
17 gj = jsonlint.parse(str);
18 } catch(e) {
19 var match = e.message.match(/line (\d+)/),
20 lineNumber = 0;
21 if (match) { lineNumber = parseInt(match[1], 10); }
22 return [{
23 line: lineNumber - 1,
24 message: e.message,
25 error: e
26 }];
27 }
28 } else {
29 return [{
30 message: 'Expected string or object as input',
31 line: 0
32 }];
33 }
34
35 errors = errors.concat(geojsonHintObject.hint(gj));
36
37 return errors;
38}
39
40module.exports.hint = hint;