@dra2020/baseclient 1.0.0 → 1.0.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.
package/README.md CHANGED
@@ -22,4 +22,5 @@ These libraries are used across both client and server:
22
22
  - [LogAbstract](./docs/logabstract.md): An abstract logging interface. Different implementations are used on client and server.
23
23
  - [LogClient](./docs/logclient.md): A client implementation of the logging abstract interface.
24
24
  - [OT](./docs/ot-js.md): An Operational Transformation implementation, used as the basis for all content editing.
25
- - [OTE](.docs/ot-editutil.md): A set of utility functions for creating OT edits.
25
+ - [OTE](./docs/ot-editutil.md): A set of utility functions for creating OT edits.
26
+ - [FilterExpr](./docs/filterexpr.md): A class for parsing and executing a filter expression against an array of objects.
package/dist/all/all.d.ts CHANGED
@@ -16,3 +16,5 @@ import * as OTE from '../ot-editutil/all';
16
16
  export { OTE };
17
17
  import { FilterExpr } from '../filterexpr/all';
18
18
  export { FilterExpr };
19
+ import * as G from '../geo/all';
20
+ export { G };
@@ -39,7 +39,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
39
39
  return result;
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", ({ value: true }));
42
- exports.FilterExpr = exports.OTE = exports.OT = exports.LogClient = exports.LogAbstract = exports.Poly = exports.FSM = exports.Context = exports.Util = void 0;
42
+ exports.G = exports.FilterExpr = exports.OTE = exports.OT = exports.LogClient = exports.LogAbstract = exports.Poly = exports.FSM = exports.Context = exports.Util = void 0;
43
43
  // Client and Server
44
44
  const Util = __importStar(__webpack_require__(/*! ../util/all */ "./lib/util/all.ts"));
45
45
  exports.Util = Util;
@@ -59,6 +59,8 @@ const OTE = __importStar(__webpack_require__(/*! ../ot-editutil/all */ "./lib/ot
59
59
  exports.OTE = OTE;
60
60
  const all_1 = __webpack_require__(/*! ../filterexpr/all */ "./lib/filterexpr/all.ts");
61
61
  Object.defineProperty(exports, "FilterExpr", ({ enumerable: true, get: function () { return all_1.FilterExpr; } }));
62
+ const G = __importStar(__webpack_require__(/*! ../geo/all */ "./lib/geo/all.ts"));
63
+ exports.G = G;
62
64
 
63
65
 
64
66
  /***/ }),
@@ -1156,6 +1158,454 @@ class FsmArray extends Fsm {
1156
1158
  exports.FsmArray = FsmArray;
1157
1159
 
1158
1160
 
1161
+ /***/ }),
1162
+
1163
+ /***/ "./lib/geo/all.ts":
1164
+ /*!************************!*\
1165
+ !*** ./lib/geo/all.ts ***!
1166
+ \************************/
1167
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
1168
+
1169
+
1170
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
1171
+ if (k2 === undefined) k2 = k;
1172
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
1173
+ }) : (function(o, m, k, k2) {
1174
+ if (k2 === undefined) k2 = k;
1175
+ o[k2] = m[k];
1176
+ }));
1177
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
1178
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
1179
+ };
1180
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
1181
+ __exportStar(__webpack_require__(/*! ./geo */ "./lib/geo/geo.ts"), exports);
1182
+ __exportStar(__webpack_require__(/*! ./vfeature */ "./lib/geo/vfeature.ts"), exports);
1183
+
1184
+
1185
+ /***/ }),
1186
+
1187
+ /***/ "./lib/geo/geo.ts":
1188
+ /*!************************!*\
1189
+ !*** ./lib/geo/geo.ts ***!
1190
+ \************************/
1191
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
1192
+
1193
+
1194
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
1195
+ if (k2 === undefined) k2 = k;
1196
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
1197
+ }) : (function(o, m, k, k2) {
1198
+ if (k2 === undefined) k2 = k;
1199
+ o[k2] = m[k];
1200
+ }));
1201
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
1202
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
1203
+ }) : function(o, v) {
1204
+ o["default"] = v;
1205
+ });
1206
+ var __importStar = (this && this.__importStar) || function (mod) {
1207
+ if (mod && mod.__esModule) return mod;
1208
+ var result = {};
1209
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
1210
+ __setModuleDefault(result, mod);
1211
+ return result;
1212
+ };
1213
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
1214
+ exports.geoIntersect = exports.geoIntersectOptions = exports.GeoMultiCollection = exports.geoMapEqual = exports.geoEqual = exports.geoTopoToCollection = exports.geoCollectionToTopo = exports.geoMapToCollection = exports.geoCollectionToMap = void 0;
1215
+ const Util = __importStar(__webpack_require__(/*! ../util/all */ "./lib/util/all.ts"));
1216
+ const Poly = __importStar(__webpack_require__(/*! ../poly/all */ "./lib/poly/all.ts"));
1217
+ function geoCollectionToMap(col) {
1218
+ if (col == null)
1219
+ return null;
1220
+ let map = {};
1221
+ col.features.forEach((f) => { map[String(f.properties.id)] = f; });
1222
+ return map;
1223
+ }
1224
+ exports.geoCollectionToMap = geoCollectionToMap;
1225
+ function geoMapToCollection(map) {
1226
+ if (map == null || Util.countKeys(map) == 0)
1227
+ return null;
1228
+ let col = { type: 'FeatureCollection', features: [] };
1229
+ Object.keys(map).forEach((geoid) => { col.features.push(map[geoid]); });
1230
+ return col;
1231
+ }
1232
+ exports.geoMapToCollection = geoMapToCollection;
1233
+ function geoCollectionToTopo(col) {
1234
+ let topo = Poly.topoFromCollection(col);
1235
+ Poly.topoPack(topo);
1236
+ return topo;
1237
+ }
1238
+ exports.geoCollectionToTopo = geoCollectionToTopo;
1239
+ function geoTopoToCollection(topo) {
1240
+ let col = Poly.topoToCollection(topo);
1241
+ Poly.featurePack(col);
1242
+ return col;
1243
+ }
1244
+ exports.geoTopoToCollection = geoTopoToCollection;
1245
+ function geoEqual(m1, m2) {
1246
+ let n1 = m1 ? m1.length : 0;
1247
+ let n2 = m2 ? m2.length : 0;
1248
+ if (n1 != n2)
1249
+ return false;
1250
+ if (n1 == 0)
1251
+ return true;
1252
+ let n = 0;
1253
+ let eq = true;
1254
+ m1.forEach(f => { if (eq && !m2.find(f.properties.id))
1255
+ eq = false; });
1256
+ return eq;
1257
+ }
1258
+ exports.geoEqual = geoEqual;
1259
+ function geoMapEqual(m1, m2) {
1260
+ if (m1 == null)
1261
+ return Util.isEmpty(m2);
1262
+ if (m2 == null)
1263
+ return Util.isEmpty(m1);
1264
+ let p;
1265
+ for (p in m1)
1266
+ if (m1.hasOwnProperty(p))
1267
+ if (m1[p] !== m2[p])
1268
+ return false;
1269
+ for (p in m2)
1270
+ if (m2.hasOwnProperty(p))
1271
+ if (m1[p] === undefined)
1272
+ return false;
1273
+ return true;
1274
+ }
1275
+ exports.geoMapEqual = geoMapEqual;
1276
+ class GeoMultiCollection {
1277
+ constructor(tag, topo, col, map) {
1278
+ this.stamp = Math.trunc(Math.random() * Number.MAX_SAFE_INTEGER / 2);
1279
+ this.empty();
1280
+ if (tag)
1281
+ this.add(tag, topo, col, map);
1282
+ }
1283
+ empty() {
1284
+ this.entries = {};
1285
+ this.hidden = {};
1286
+ this._onChange();
1287
+ }
1288
+ get nEntries() { return Util.countKeys(this.entries); }
1289
+ nthEntry(n) {
1290
+ return Util.nthProperty(this.entries, n);
1291
+ }
1292
+ add(tag, topo, col, map) {
1293
+ let entry = this.entries[tag];
1294
+ if (entry === undefined)
1295
+ entry = { tag: tag }, this.entries[tag] = entry;
1296
+ if ((topo && entry.topo !== topo) || (col && entry.col !== col) || (map && entry.map !== map)) {
1297
+ entry.topo = topo;
1298
+ entry.col = col;
1299
+ entry.map = map;
1300
+ this._onChange();
1301
+ }
1302
+ else if (topo == null && col == null && map == null)
1303
+ this.remove(tag);
1304
+ }
1305
+ addMulti(multi) {
1306
+ multi.forEachEntry(e => {
1307
+ this.add(e.tag, e.topo, e.col, e.map);
1308
+ });
1309
+ for (let p in multi.hidden)
1310
+ if (multi.hidden.hasOwnProperty(p)) {
1311
+ this.hidden[p] = true;
1312
+ this._onChange();
1313
+ }
1314
+ }
1315
+ remove(tag) {
1316
+ let entry = this.entries[tag];
1317
+ if (entry) {
1318
+ if (entry.topo || entry.col || entry.map)
1319
+ this._onChange();
1320
+ delete this.entries[tag];
1321
+ }
1322
+ }
1323
+ _onChange() {
1324
+ this.all = { tag: 'all' };
1325
+ this.stamp++;
1326
+ }
1327
+ _col(e) {
1328
+ if (e == null)
1329
+ return null;
1330
+ if (!e.col) {
1331
+ if (e.map)
1332
+ e.col = geoMapToCollection(e.map);
1333
+ else if (e.topo)
1334
+ e.col = geoTopoToCollection(e.topo);
1335
+ }
1336
+ return e.col;
1337
+ }
1338
+ _map(e) {
1339
+ if (e == null)
1340
+ return null;
1341
+ if (!e.map) {
1342
+ if (e.col)
1343
+ e.map = geoCollectionToMap(e.col);
1344
+ else if (e.topo) {
1345
+ e.col = geoTopoToCollection(e.topo);
1346
+ e.map = geoCollectionToMap(e.col);
1347
+ }
1348
+ }
1349
+ return e.map;
1350
+ }
1351
+ _topo(e) {
1352
+ if (e == null)
1353
+ return null;
1354
+ if (!e.topo) {
1355
+ if (e.col)
1356
+ e.topo = geoCollectionToTopo(e.col);
1357
+ else if (e.map) {
1358
+ e.col = geoMapToCollection(e.map);
1359
+ e.topo = geoCollectionToTopo(e.col);
1360
+ }
1361
+ }
1362
+ return e.topo;
1363
+ }
1364
+ colOf(tag) { return this._col(this.entries[tag]); }
1365
+ mapOf(tag) { return this._map(this.entries[tag]); }
1366
+ topoOf(tag) { return this._topo(this.entries[tag]); }
1367
+ forEachEntry(cb) {
1368
+ Object.values(this.entries).forEach(cb);
1369
+ }
1370
+ allCol() {
1371
+ if (this.nEntries == 0)
1372
+ return null;
1373
+ if (!this.all.col) {
1374
+ // optimise case where one entry
1375
+ let n = this.nEntries;
1376
+ if (n == 1)
1377
+ this.all.col = this._col(this.nthEntry(0));
1378
+ else {
1379
+ this.all.col = { type: 'FeatureCollection', features: [] };
1380
+ this.forEach(f => { this.all.col.features.push(f); });
1381
+ }
1382
+ }
1383
+ return this.all.col;
1384
+ }
1385
+ allMap() {
1386
+ if (this.nEntries == 0)
1387
+ return null;
1388
+ if (!this.all.map) {
1389
+ // optimise case where one entry
1390
+ let n = this.nEntries;
1391
+ if (n == 1)
1392
+ this.all.map = this._map(this.nthEntry(0));
1393
+ else
1394
+ this.all.map = geoCollectionToMap(this.allCol());
1395
+ }
1396
+ return this.all.map;
1397
+ }
1398
+ allTopo() {
1399
+ if (this.nEntries == 0)
1400
+ return null;
1401
+ if (!this.all.topo) {
1402
+ // optimise case where one entry
1403
+ let n = this.nEntries;
1404
+ if (n == 1)
1405
+ this.all.topo = this._topo(this.nthEntry(0));
1406
+ else
1407
+ this.all.topo = geoCollectionToTopo(this.allCol());
1408
+ }
1409
+ return this.all.topo;
1410
+ }
1411
+ hide(id) {
1412
+ if (id) {
1413
+ if (typeof id === 'string')
1414
+ this.hidden[id] = true;
1415
+ else if (Array.isArray(id))
1416
+ id.forEach((i) => { this.hidden[i] = true; });
1417
+ else if (typeof id === 'object')
1418
+ for (let p in id)
1419
+ if (id.hasOwnProperty(p))
1420
+ this.hidden[p] = true;
1421
+ this._onChange();
1422
+ }
1423
+ }
1424
+ show(id) {
1425
+ if (id) {
1426
+ if (typeof id === 'string')
1427
+ delete this.hidden[id];
1428
+ else if (Array.isArray(id))
1429
+ id.forEach((i) => { delete this.hidden[i]; });
1430
+ else if (typeof id === 'object')
1431
+ for (let p in id)
1432
+ if (id.hasOwnProperty(p))
1433
+ delete this.hidden[p];
1434
+ this._onChange();
1435
+ }
1436
+ }
1437
+ showAll() {
1438
+ if (!Util.isEmpty(this.hidden)) {
1439
+ this.hidden = {};
1440
+ this._onChange();
1441
+ }
1442
+ }
1443
+ get length() {
1444
+ let n = 0;
1445
+ this.forEachEntry(e => {
1446
+ if (e.col)
1447
+ n += e.col.features.length;
1448
+ else if (e.map)
1449
+ n += Util.countKeys(e.map);
1450
+ else if (e.topo)
1451
+ n += Util.countKeys(e.topo.objects);
1452
+ });
1453
+ return n;
1454
+ }
1455
+ // Use forEach in preference to iteration using this function
1456
+ nthFeature(n) {
1457
+ let found;
1458
+ if (n >= 0)
1459
+ this.forEachEntry(e => {
1460
+ if (found)
1461
+ return;
1462
+ let col = this._col(e);
1463
+ if (col)
1464
+ if (n > col.features.length)
1465
+ n -= col.features.length;
1466
+ else
1467
+ found = col.features[n];
1468
+ });
1469
+ return found;
1470
+ }
1471
+ forEach(cb) {
1472
+ this.forEachEntry(e => {
1473
+ let col = this._col(e);
1474
+ if (col)
1475
+ col.features.forEach(f => { if (this.hidden[f.properties.id] === undefined)
1476
+ cb(f); });
1477
+ });
1478
+ }
1479
+ map(cb) {
1480
+ let features = [];
1481
+ this.forEach((f) => { features.push(cb(f)); });
1482
+ return features;
1483
+ }
1484
+ isHidden(id) {
1485
+ return this.hidden[id] !== undefined;
1486
+ }
1487
+ find(id) {
1488
+ if (this.hidden[id] !== undefined)
1489
+ return undefined;
1490
+ let entries = Object.values(this.entries);
1491
+ for (let i = 0; i < entries.length; i++) {
1492
+ let map = this._map(entries[i]);
1493
+ if (map[id])
1494
+ return map[id];
1495
+ }
1496
+ return undefined;
1497
+ }
1498
+ }
1499
+ exports.GeoMultiCollection = GeoMultiCollection;
1500
+ var geoIntersectOptions;
1501
+ (function (geoIntersectOptions) {
1502
+ geoIntersectOptions[geoIntersectOptions["Intersects"] = 0] = "Intersects";
1503
+ geoIntersectOptions[geoIntersectOptions["Bounds"] = 1] = "Bounds";
1504
+ geoIntersectOptions[geoIntersectOptions["BoundsCenter"] = 2] = "BoundsCenter";
1505
+ })(geoIntersectOptions = exports.geoIntersectOptions || (exports.geoIntersectOptions = {}));
1506
+ ;
1507
+ function geoBoxIntersect(x1, x2, opt) {
1508
+ if (x1.left === undefined || x2.left === undefined)
1509
+ return false;
1510
+ let l1 = x1.left;
1511
+ let l2 = x2.left;
1512
+ let r1 = x1.right;
1513
+ let r2 = x2.right;
1514
+ let b1 = x1.top; // flip
1515
+ let b2 = x2.top; // flip
1516
+ let t1 = x1.bottom; // flip
1517
+ let t2 = x2.bottom; // flip
1518
+ let cx2 = l2 + (r2 - l2) / 2;
1519
+ let cy2 = t2 + (b2 - t2) / 2;
1520
+ // Note I flipped top and bottom above when extracting,
1521
+ // in order to make below logic work for normal y axis alignment (0 at top).
1522
+ switch (opt) {
1523
+ case geoIntersectOptions.Intersects:
1524
+ return !(l2 > r1 || r2 < l1 || t2 > b1 || b2 < t1);
1525
+ case geoIntersectOptions.Bounds:
1526
+ return l1 <= l2 && t1 <= t2 && r1 >= r2 && b1 >= b2;
1527
+ case geoIntersectOptions.BoundsCenter:
1528
+ return l1 <= cx2 && t1 <= cy2 && r1 >= cx2 && b1 >= cy2;
1529
+ }
1530
+ }
1531
+ function geoIntersect(multi, bbox, opt) {
1532
+ let m = {};
1533
+ let bboxPoly = Poly.boundboxPoly(bbox);
1534
+ multi.forEach((f) => {
1535
+ let box = Poly.boundbox(f);
1536
+ if (geoBoxIntersect(bbox, box, opt)) {
1537
+ if (opt !== geoIntersectOptions.Intersects || Poly.polyIntersects(bboxPoly, f))
1538
+ m[f.properties.id] = f;
1539
+ }
1540
+ });
1541
+ let result = new GeoMultiCollection();
1542
+ result.add('result', null, null, m);
1543
+ return result;
1544
+ }
1545
+ exports.geoIntersect = geoIntersect;
1546
+
1547
+
1548
+ /***/ }),
1549
+
1550
+ /***/ "./lib/geo/vfeature.ts":
1551
+ /*!*****************************!*\
1552
+ !*** ./lib/geo/vfeature.ts ***!
1553
+ \*****************************/
1554
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
1555
+
1556
+
1557
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
1558
+ if (k2 === undefined) k2 = k;
1559
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
1560
+ }) : (function(o, m, k, k2) {
1561
+ if (k2 === undefined) k2 = k;
1562
+ o[k2] = m[k];
1563
+ }));
1564
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
1565
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
1566
+ }) : function(o, v) {
1567
+ o["default"] = v;
1568
+ });
1569
+ var __importStar = (this && this.__importStar) || function (mod) {
1570
+ if (mod && mod.__esModule) return mod;
1571
+ var result = {};
1572
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
1573
+ __setModuleDefault(result, mod);
1574
+ return result;
1575
+ };
1576
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
1577
+ exports.computeVFeature = void 0;
1578
+ const Util = __importStar(__webpack_require__(/*! ../util/all */ "./lib/util/all.ts"));
1579
+ const Poly = __importStar(__webpack_require__(/*! ../poly/all */ "./lib/poly/all.ts"));
1580
+ // Given the topology for a precinct, the bintrie mapping and the list of blocks, construct the
1581
+ // feature data for the virtual feature.
1582
+ //
1583
+ function computeVFeature(topoPrecinct, bintrie, blocks) {
1584
+ let contiguity = new Util.IndexedArray();
1585
+ let block_contiguity = new Util.IndexedArray();
1586
+ let f = Poly.topoMerge(topoPrecinct, blocks);
1587
+ f.properties.datasets = {};
1588
+ blocks.forEach(blockid => {
1589
+ let b = topoPrecinct.objects[blockid];
1590
+ if (b.properties.datasets)
1591
+ Util.deepAccum(f.properties.datasets, b.properties.datasets);
1592
+ if (b.properties.contiguity) {
1593
+ b.properties.contiguity.forEach((id) => {
1594
+ contiguity.set(id === 'OUT_OF_STATE' ? id : bintrie.get(id));
1595
+ });
1596
+ b.properties.contiguity.forEach((id) => {
1597
+ block_contiguity.set(id);
1598
+ });
1599
+ }
1600
+ });
1601
+ f.properties.contiguity = contiguity.asArray();
1602
+ f.properties.block_contiguity = block_contiguity.asArray();
1603
+ f.properties.blocks = blocks;
1604
+ return f;
1605
+ }
1606
+ exports.computeVFeature = computeVFeature;
1607
+
1608
+
1159
1609
  /***/ }),
1160
1610
 
1161
1611
  /***/ "./lib/logabstract/all.ts":
@@ -7744,7 +8194,7 @@ class FsmIncrementalUnion extends FSM.Fsm {
7744
8194
  let values = Object.values(map);
7745
8195
  this.work = { nUnion: values.length, nDifference: 0, ms: 0 };
7746
8196
  let elapsed = new Util.Elapsed();
7747
- this.result = topoMergeFeatures(this.key.topo, values);
8197
+ this.result = topoMergeFeatures(this.key.multi.allTopo(), values);
7748
8198
  this.work.ms = elapsed.ms();
7749
8199
  this.map = map;
7750
8200
  }
@@ -7870,7 +8320,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
7870
8320
  return result;
7871
8321
  };
7872
8322
  Object.defineProperty(exports, "__esModule", ({ value: true }));
7873
- exports.FsmUnion = exports.polyIntersects = void 0;
8323
+ exports.FsmUnion = exports.polyDifference = exports.polyIntersects = void 0;
7874
8324
  const PC = __importStar(__webpack_require__(/*! polygon-clipping */ "polygon-clipping"));
7875
8325
  const Util = __importStar(__webpack_require__(/*! ../util/all */ "./lib/util/all.ts"));
7876
8326
  const FSM = __importStar(__webpack_require__(/*! ../fsm/all */ "./lib/fsm/all.ts"));
@@ -7929,6 +8379,20 @@ function polyIntersects(p1, p2) {
7929
8379
  return bIntersects;
7930
8380
  }
7931
8381
  exports.polyIntersects = polyIntersects;
8382
+ function polyDifference(main, parts) {
8383
+ main = PP.polyUnpack(coords(main));
8384
+ // need to explode multipolygon so that "exploded" is a valid multipolygon input to underlying difference routine
8385
+ let exploded = [];
8386
+ parts.forEach((p) => {
8387
+ p = PP.polyUnpack(coords(p));
8388
+ if (Util.depthof(p) == 5)
8389
+ p.forEach((poly) => { exploded.push(poly); });
8390
+ else
8391
+ exploded.push(p);
8392
+ });
8393
+ return PR.polyRound(_difference(main, exploded));
8394
+ }
8395
+ exports.polyDifference = polyDifference;
7932
8396
  class FsmDifference extends FSM.Fsm {
7933
8397
  constructor(env, accum, polys) {
7934
8398
  super(env);