UNPKG

1.22 kBJavaScriptView 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 * @param {Object} options
8 * @param {boolean} [options.noDuplicateMembers=true] forbid repeated
9 * properties. This is only available for string input, becaused parsed
10 * Objects cannot have duplicate properties.
11 * @returns {Array<Object>} an array of errors
12 */
13function hint(str, options) {
14
15 var gj, errors = [];
16
17 if (typeof str === 'object') {
18 gj = str;
19 } else if (typeof str === 'string') {
20 try {
21 gj = jsonlint.parse(str);
22 } catch(e) {
23 var match = e.message.match(/line (\d+)/),
24 lineNumber = 0;
25 if (match) { lineNumber = parseInt(match[1], 10); }
26 return [{
27 line: lineNumber - 1,
28 message: e.message,
29 error: e
30 }];
31 }
32 } else {
33 return [{
34 message: 'Expected string or object as input',
35 line: 0
36 }];
37 }
38
39 errors = errors.concat(geojsonHintObject.hint(gj, options));
40
41 return errors;
42}
43
44module.exports.hint = hint;