@canboat/canboatjs 1.22.1 → 1.22.2

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.
Files changed (2) hide show
  1. package/lib/fromPgn.js +45 -10
  2. package/package.json +1 -1
package/lib/fromPgn.js CHANGED
@@ -99,7 +99,17 @@ class Parser extends EventEmitter {
99
99
  pgnList = [ ...customPgns.definitions, ...(pgnList||[]) ]
100
100
  }
101
101
 
102
- let pgnData = pgnList[0]
102
+ let pgnData
103
+ let origPGNList = pgnList
104
+
105
+ if ( pgnList.length > 1 ) {
106
+ pgnData = this.findMatchPgn(pgnList)
107
+ }
108
+
109
+ if ( !pgnData ) {
110
+ pgnData = pgnList[0]
111
+ }
112
+
103
113
  let couldBeMulti = false;
104
114
 
105
115
  if ( pgnList.length > 0 && len == 8 ) {
@@ -202,16 +212,29 @@ class Parser extends EventEmitter {
202
212
  if ( pgnList.length == 0 ) {
203
213
  //this.emit('warning', pgn, `no conversion found for pgn`)
204
214
  trace('warning no conversion found for pgn %j', pgn)
205
- const ts = _.get(pgn, 'timestamp', new Date())
206
- pgn.timestamp = _.isDate(ts) ? ts.toISOString() : ts
207
- this.emit('pgn', pgn)
208
- cb && cb(undefined, pgn)
209
- return pgn
215
+
216
+ let nonMatch = this.findNonMatchPgn(origPGNList)
217
+ if ( nonMatch ) {
218
+ pgnList = [ nonMatch ]
219
+ pgnData = pgnList[0]
220
+ fields = pgnData.Fields
221
+ var postProcessor = fieldTypePostProcessors[field.Type]
222
+ if ( postProcessor ) {
223
+ value = postProcessor(pgnData.Fields[i], value)
224
+ }
225
+ } else {
226
+ const ts = _.get(pgn, 'timestamp', new Date())
227
+ pgn.timestamp = _.isDate(ts) ? ts.toISOString() : ts
228
+ this.emit('pgn', pgn)
229
+ cb && cb(undefined, pgn)
230
+ return pgn
231
+ }
232
+ } else {
233
+ pgnData = pgnList[0]
234
+ fields = pgnData.Fields
235
+ //console.log(`using ${JSON.stringify(pgnData, null, 2)}`)
236
+ value = pgnData.Fields[i].Description
210
237
  }
211
- pgnData = pgnList[0]
212
- fields = pgnData.Fields
213
- //console.log(`using ${JSON.stringify(pgnData, null, 2)}`)
214
- value = pgnData.Fields[i].Description
215
238
  }
216
239
 
217
240
  if ( !_.isUndefined(value) && value != null ) {
@@ -258,6 +281,18 @@ class Parser extends EventEmitter {
258
281
  }
259
282
  }
260
283
 
284
+ findNonMatchPgn(pgnList) {
285
+ return pgnList.find(f => {
286
+ return !f.Fields.find(f => !_.isUndefined(f.Match))
287
+ })
288
+ }
289
+
290
+ findMatchPgn(pgnList) {
291
+ return pgnList.find(f => {
292
+ return f.Fields.find(f => !_.isUndefined(f.Match))
293
+ })
294
+ }
295
+
261
296
  parse(data, cb) {
262
297
  if (_.isString(data) ) {
263
298
  return this.parseString(data, cb)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canboat/canboatjs",
3
- "version": "1.22.1",
3
+ "version": "1.22.2",
4
4
  "description": "Native javascript version of canboat",
5
5
  "main": "index.js",
6
6
  "scripts": {