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.
@@ -1,140 +1,181 @@
1
1
  function conditioned(evt: string): boolean {
2
- return evt.match(/^(.+?)\[(.+?)\]>$/g) != null;
2
+ return evt.match(/^(.+?)\[(.+?)\]>$/g) !== null;
3
3
  }
4
4
 
5
- function parseEvent(evt: string): {[k: string]: any} {
6
- let match = /^(.+?)\[(.+?)\]>$/g.exec(evt);
7
- if (match)
8
- return {evt: match[1] + '>', exp: match[2]};
9
- return {evt: evt};
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): Function {
13
- let body = 'return ' + cond.replace(/(\$.+?)(?=[=!.])/g, 'node.$&') + ';';
14
- return new Function('node', body);
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
- queryParent: boolean;
19
- progressive: boolean;
20
- parentMap: WeakMap<any, any>;
21
- evtListeners: {[k: string]: any};
22
-
23
- constructor(opts: any) {
24
- if (opts) {
25
- this.queryParent = opts.queryParent? true : false;
26
- this.progressive = opts.progressive;
27
- if (this.queryParent) this.parentMap = new WeakMap();
28
- }
29
- this.evtListeners = {};
30
- }
31
-
32
- parse(xml: string, parent?: any, dir?: string) {
33
- dir = dir? dir + '.' : '';
34
- let nodeRegEx = /<([^ >\/]+)(.*?)>/mg, nodeMatch: any = null, nodes: any[] = [];
35
- while (nodeMatch = nodeRegEx.exec(xml)) {
36
- let tag = nodeMatch[1], node: {[k: string]: any} = {$tag: tag}, fullTag = dir + tag;
37
-
38
- let attrText = nodeMatch[2].trim(), closed = false;
39
- if (attrText.endsWith('/') || tag.startsWith('?') || tag.startsWith('!')) {
40
- closed = true;
41
- }
42
-
43
- let attRegEx1 = /([^ ]+?)="(.+?)"/g, attRegEx2 = /([^ ]+?)='(.+?)'/g;
44
- let attMatch: any = null, hasAttrs: boolean = false;
45
- while (attMatch = attRegEx1.exec(attrText)) {
46
- hasAttrs = true;
47
- node[attMatch[1]] = attMatch[2];
48
- }
49
- if (!hasAttrs)
50
- while (attMatch = attRegEx2.exec(attrText)) {
51
- hasAttrs = true;
52
- node[attMatch[1]] = attMatch[2];
53
- }
54
-
55
- if (!hasAttrs && attrText !== '') node.text = attrText;
56
- if (this.progressive) this.emit(`<${fullTag}>`, node, parent);
57
-
58
-
59
- if (!closed) {
60
- let innerRegEx = new RegExp(`([^]+?)<\/${tag}>`, 'g');
61
- innerRegEx.lastIndex = nodeRegEx.lastIndex;
62
- let innerMatch = innerRegEx.exec(xml);
63
- if (innerMatch && innerMatch[1]) {
64
- nodeRegEx.lastIndex = innerRegEx.lastIndex;
65
- let innerNodes = this.parse(innerMatch[1], node, fullTag);
66
- if (innerNodes.length > 0) node.$innerNodes = innerNodes;
67
- else node.$innerText = innerMatch[1];
68
- }
69
- }
70
- if (this.queryParent && parent) {
71
- this.parentMap.set(node, parent);
72
- }
73
-
74
- if (this.progressive) this.emit(`</${fullTag}>`, node, parent);
75
-
76
- nodes.push(node);
77
- }
78
-
79
- return nodes;
80
- }
81
-
82
- getParent(node: any): any {
83
- if (this.queryParent)
84
- return this.parentMap.get(node);
85
- return null;
86
- }
87
-
88
- $addListener(evt: string, func: Function) {
89
- let funcs = this.evtListeners[evt];
90
- if (funcs) funcs.push(func);
91
- else this.evtListeners[evt] = [func];
92
- }
93
-
94
- // support javascript condition for the last tag
95
- addListener(evt: string, func: Function) {
96
- if (conditioned(evt)) {
97
- // func.prototype = evt;
98
- let ev = parseEvent(evt);
99
- (<{[k: string]: any}>func).condition = genConditionFunc(ev.exp);
100
- evt = ev.evt;
101
- }
102
- this.$addListener(evt, func);
103
- }
104
-
105
- $removeListener(evt: string, func: Function) {
106
- let funcs = this.evtListeners[evt];
107
- let idx = null;
108
- if (funcs && (idx = funcs.indexOf(func)) >= 0)
109
- funcs.splice(idx, 1);
110
- }
111
-
112
- removeListener(evt: string, func: Function) {
113
- if (conditioned(evt)) {
114
- let ev = parseEvent(evt);
115
- evt = ev.evt;
116
- }
117
- this.$removeListener(evt, func);
118
- }
119
-
120
- emit(evt: string, ...args) {
121
- let funcs = this.evtListeners[evt];
122
- if (funcs) {
123
- for (let func of funcs) {
124
- if (func.condition) {
125
- if (func.condition.apply(null, args) === true)
126
- func.apply(null, args);
127
- } else
128
- func.apply(null, args);
129
- }
130
- }
131
- }
132
-
133
- on(evt: string, func: Function) {
134
- this.addListener(evt, func);
135
- }
136
-
137
- off(evt: string, func: Function) {
138
- this.removeListener(evt, func);
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
@@ -8,6 +8,10 @@
8
8
  "lib": ["es2017"]
9
9
  },
10
10
 
11
+ "typeRoots": [
12
+ "node_modules/@types"
13
+ ],
14
+
11
15
  "include": [
12
16
  "src/*"
13
17
  ],
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";