osm2geojson-lite 0.8.13 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Makefile +1 -1
- package/index.html +7 -3
- package/lib/index.d.ts +21 -0
- package/lib/index.js +51 -59
- package/lib/osmobjs.js +1 -1
- package/lib/xmlparser.js +1 -1
- package/osm2geojson-lite +10 -0
- package/package.json +12 -2
- package/ts/release/index.js +195 -0
- package/ts/release/osmobjs.js +348 -0
- package/ts/release/utils.js +213 -0
- package/ts/release/xmlparser.js +157 -0
- package/ts/src/index.d.ts +17 -0
- package/ts/src/index.ts +217 -169
- package/ts/src/osmobjs.ts +400 -365
- package/ts/src/typings.d.ts +1 -1
- package/ts/src/utils.ts +207 -170
- package/ts/src/xmlparser.ts +173 -132
- package/ts/tsconfig.json +4 -0
- package/ts/tslint.json +24 -0
- package/ts/types/imports.d.ts +1 -0
package/ts/src/xmlparser.ts
CHANGED
|
@@ -1,140 +1,181 @@
|
|
|
1
1
|
function conditioned(evt: string): boolean {
|
|
2
|
-
|
|
2
|
+
return evt.match(/^(.+?)\[(.+?)\]>$/g) !== null;
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
-
function parseEvent(evt: string): {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
function parseEvent(evt: string): {evt: string, exp?: string} {
|
|
6
|
+
const match = /^(.+?)\[(.+?)\]>$/g.exec(evt);
|
|
7
|
+
if (match) {
|
|
8
|
+
return {evt: match[1] + '>', exp: match[2]};
|
|
9
|
+
}
|
|
10
|
+
return {evt};
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
function genConditionFunc(cond: string):
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
function genConditionFunc(cond: string): (node: {[k: string]: any}) => boolean {
|
|
14
|
+
const body = 'return ' + cond.replace(/(\$.+?)(?=[=!.])/g, 'node.$&') + ';';
|
|
15
|
+
return new Function('node', body) as (node: {[k: string]: any}) => boolean;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export default class {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
19
|
+
private queryParent: boolean;
|
|
20
|
+
private progressive: boolean;
|
|
21
|
+
private parentMap: WeakMap<any, any>;
|
|
22
|
+
private evtListeners: {[k: string]: any};
|
|
23
|
+
|
|
24
|
+
constructor(opts: any) {
|
|
25
|
+
if (opts) {
|
|
26
|
+
this.queryParent = opts.queryParent ? true : false;
|
|
27
|
+
this.progressive = opts.progressive;
|
|
28
|
+
if (this.queryParent) {
|
|
29
|
+
this.parentMap = new WeakMap();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
this.evtListeners = {};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public parse(xml: string, parent?: any, dir?: string) {
|
|
36
|
+
dir = dir ? dir + '.' : '';
|
|
37
|
+
|
|
38
|
+
const nodeRegEx = /<([^ >\/]+)(.*?)>/mg;
|
|
39
|
+
const nodes: any[] = [];
|
|
40
|
+
|
|
41
|
+
let nodeMatch = nodeRegEx.exec(xml);
|
|
42
|
+
while (nodeMatch) {
|
|
43
|
+
const tag = nodeMatch[1];
|
|
44
|
+
const node: {[k: string]: any} = {$tag: tag};
|
|
45
|
+
const fullTag = dir + tag;
|
|
46
|
+
|
|
47
|
+
const attrText = nodeMatch[2].trim();
|
|
48
|
+
let closed = false;
|
|
49
|
+
|
|
50
|
+
if (attrText.endsWith('/') || tag.startsWith('?') || tag.startsWith('!')) {
|
|
51
|
+
closed = true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const attRegEx1 = /([^ ]+?)="(.+?)"/g;
|
|
55
|
+
const attRegEx2 = /([^ ]+?)='(.+?)'/g;
|
|
56
|
+
let attMatch = attRegEx1.exec(attrText);
|
|
57
|
+
let hasAttrs = false;
|
|
58
|
+
|
|
59
|
+
while (attMatch) {
|
|
60
|
+
hasAttrs = true;
|
|
61
|
+
node[attMatch[1]] = attMatch[2];
|
|
62
|
+
attMatch = attRegEx1.exec(attrText);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!hasAttrs) {
|
|
66
|
+
attMatch = attRegEx2.exec(attrText);
|
|
67
|
+
while (attMatch) {
|
|
68
|
+
hasAttrs = true;
|
|
69
|
+
node[attMatch[1]] = attMatch[2];
|
|
70
|
+
attMatch = attRegEx2.exec(attrText);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!hasAttrs && attrText !== '') {
|
|
75
|
+
node.text = attrText;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (this.progressive) {
|
|
79
|
+
this.emit(`<${fullTag}>`, node, parent);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!closed) {
|
|
83
|
+
const innerRegEx = new RegExp(`([^]+?)<\/${tag}>`, 'g');
|
|
84
|
+
innerRegEx.lastIndex = nodeRegEx.lastIndex;
|
|
85
|
+
const innerMatch = innerRegEx.exec(xml);
|
|
86
|
+
if (innerMatch && innerMatch[1]) {
|
|
87
|
+
nodeRegEx.lastIndex = innerRegEx.lastIndex;
|
|
88
|
+
const innerNodes = this.parse(innerMatch[1], node, fullTag);
|
|
89
|
+
if (innerNodes.length > 0) {
|
|
90
|
+
node.$innerNodes = innerNodes;
|
|
91
|
+
} else {
|
|
92
|
+
node.$innerText = innerMatch[1];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (this.queryParent && parent) {
|
|
97
|
+
this.parentMap.set(node, parent);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (this.progressive) {
|
|
101
|
+
this.emit(`</${fullTag}>`, node, parent);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
nodes.push(node);
|
|
105
|
+
nodeMatch = nodeRegEx.exec(xml);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return nodes;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public getParent(node: any): any {
|
|
112
|
+
if (this.queryParent) {
|
|
113
|
+
return this.parentMap.get(node);
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// support javascript condition for the last tag
|
|
119
|
+
public addListener(evt: string, func: (node: {[k: string]: any}, parent?: {[k: string]: any}) => void) {
|
|
120
|
+
if (conditioned(evt)) {
|
|
121
|
+
// func.prototype = evt;
|
|
122
|
+
const ev = parseEvent(evt);
|
|
123
|
+
if (ev.exp) {
|
|
124
|
+
(func as {[k: string]: any}).condition = genConditionFunc(ev.exp);
|
|
125
|
+
}
|
|
126
|
+
evt = ev.evt;
|
|
127
|
+
}
|
|
128
|
+
this.$addListener(evt, func);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public removeListener(evt: string, func: (node: {[k: string]: any}, parent?: {[k: string]: any}) => void) {
|
|
132
|
+
if (conditioned(evt)) {
|
|
133
|
+
const ev = parseEvent(evt);
|
|
134
|
+
evt = ev.evt;
|
|
135
|
+
}
|
|
136
|
+
this.$removeListener(evt, func);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public on(evt: string, func: (node: {[k: string]: any}, parent?: {[k: string]: any}) => void) {
|
|
140
|
+
this.addListener(evt, func);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public off(evt: string, func: (node: {[k: string]: any}, parent?: {[k: string]: any}) => void) {
|
|
144
|
+
this.removeListener(evt, func);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private $addListener(evt: string, func: (node: {[k: string]: any}, parent?: {[k: string]: any}) => void) {
|
|
148
|
+
const funcs = this.evtListeners[evt];
|
|
149
|
+
if (funcs) {
|
|
150
|
+
funcs.push(func);
|
|
151
|
+
} else {
|
|
152
|
+
this.evtListeners[evt] = [func];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private $removeListener(evt: string, func: (node: {[k: string]: any}, parent?: {[k: string]: any}) => void) {
|
|
157
|
+
const funcs = this.evtListeners[evt];
|
|
158
|
+
let idx = -1;
|
|
159
|
+
if (funcs) {
|
|
160
|
+
idx = funcs.indexOf(func);
|
|
161
|
+
}
|
|
162
|
+
if (idx >= 0) {
|
|
163
|
+
funcs.splice(idx, 1);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private emit(evt: string, ...args) {
|
|
168
|
+
const funcs = this.evtListeners[evt];
|
|
169
|
+
if (funcs) {
|
|
170
|
+
for (const func of funcs) {
|
|
171
|
+
if (func.condition) {
|
|
172
|
+
if (func.condition.apply(null, args) === true) {
|
|
173
|
+
func.apply(null, args);
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
func.apply(null, args);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
package/ts/tsconfig.json
CHANGED
package/ts/tslint.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"defaultSeverity": "error",
|
|
3
|
+
"extends": [
|
|
4
|
+
"tslint:recommended"
|
|
5
|
+
],
|
|
6
|
+
"jsRules": {},
|
|
7
|
+
"rules": {
|
|
8
|
+
"indent": [true, "spaces", 4],
|
|
9
|
+
"linebreak-style": [true, "LF"],
|
|
10
|
+
"quotemark": [true, "single"],
|
|
11
|
+
"semicolon": [true, "always"],
|
|
12
|
+
"no-console": false,
|
|
13
|
+
"curly": true,
|
|
14
|
+
"triple-equals": true,
|
|
15
|
+
"no-string-throw": true,
|
|
16
|
+
"no-var-keyword": true,
|
|
17
|
+
"no-trailing-whitespace": true,
|
|
18
|
+
"object-literal-key-quotes": [true, "as-needed"],
|
|
19
|
+
"max-line-length": false,
|
|
20
|
+
"max-classes-per-file": [true, 5, "exclude-class-expressions"],
|
|
21
|
+
"object-literal-sort-keys": false
|
|
22
|
+
},
|
|
23
|
+
"rulesDirectory": []
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module "geojson";
|