@myrmidon/cadmus-core 0.2.0 → 0.3.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.
@@ -39,7 +39,8 @@ class Datation {
39
39
  * Create a new datation, optionally setting its data.
40
40
  * @param datation The optional data to be copied into the newly created datation.
41
41
  */
42
- constructor(datation = null) {
42
+ constructor(datation) {
43
+ this.value = 0;
43
44
  if (datation) {
44
45
  this.copyFrom(datation);
45
46
  }
@@ -119,13 +120,13 @@ class Datation {
119
120
  */
120
121
  static parse(text, options = DATATION_FORMAT_OPTIONS) {
121
122
  if (!text) {
122
- return undefined;
123
+ return null;
123
124
  }
124
125
  const datation = new Datation();
125
126
  const datationRegex = this.getParserRegex(options);
126
127
  const m = datationRegex.exec(text);
127
128
  if (!m) {
128
- return undefined;
129
+ return null;
129
130
  }
130
131
  // about
131
132
  if (m[1]) {
@@ -236,10 +237,10 @@ class Datation {
236
237
  if (this.isSpan) {
237
238
  result += 0.5;
238
239
  }
239
- if (this.month > 0 && this.month <= 12) {
240
+ if (this.month && this.month > 0 && this.month <= 12) {
240
241
  result += this.month / 12;
241
242
  }
242
- if (this.day > 0 && this.day <= 31) {
243
+ if (this.day && this.day > 0 && this.day <= 31) {
243
244
  result += this.day / (12 * 31);
244
245
  }
245
246
  return result;
@@ -447,34 +448,34 @@ class Datation {
447
448
  }
448
449
  break;
449
450
  case 'd':
450
- if (this.day > 0) {
451
+ if (this.day) {
451
452
  sb.push(this.day.toString());
452
453
  }
453
454
  break;
454
455
  case 'dd':
455
- if (this.day > 0) {
456
+ if (this.day) {
456
457
  sb.push(this.day.toString().padStart(2, '0'));
457
458
  }
458
459
  break;
459
460
  case 'ddd':
460
- if (this.day > 0) {
461
+ if (this.month && this.day) {
461
462
  const date = new Date(this.value, this.month, this.day);
462
463
  const dow = date.getDay();
463
464
  sb.push(options.dayNames[dow]);
464
465
  }
465
466
  break;
466
467
  case 'M':
467
- if (this.month > 0) {
468
+ if (this.month) {
468
469
  sb.push(this.month.toString());
469
470
  }
470
471
  break;
471
472
  case 'MM':
472
- if (this.month > 0) {
473
+ if (this.month) {
473
474
  sb.push(this.month.toString().padStart(2, '0'));
474
475
  }
475
476
  break;
476
477
  case 'MMM':
477
- if (this.month > 0) {
478
+ if (this.month) {
478
479
  sb.push(options.monthNames[this.month - 1]);
479
480
  }
480
481
  break;
@@ -539,7 +540,7 @@ var HistoricalDateType;
539
540
  * A historical date, including 0, 1, or 2 datation points.
540
541
  */
541
542
  class HistoricalDate {
542
- constructor(date = null) {
543
+ constructor(date) {
543
544
  if (!date) {
544
545
  this.a = new Datation();
545
546
  // B=undefined for a single point
@@ -555,7 +556,7 @@ class HistoricalDate {
555
556
  * Parse the specified text representing a historical datation.
556
557
  * @param text The text to be parsed.
557
558
  * @param options The datation formatter options.
558
- * @return The datation, or null if invalid.
559
+ * @return The datation, or undefined if invalid.
559
560
  */
560
561
  static parse(text, options = DATATION_FORMAT_OPTIONS) {
561
562
  if (!text) {
@@ -582,7 +583,7 @@ class HistoricalDate {
582
583
  '(?:\\s*\\{?:([^)]+)\\})?\\s*$', 'gi');
583
584
  const m1 = tailRegexp.exec(s1);
584
585
  // if 1st has no era try integration from 2nd
585
- if (m1 && m1[1]) {
586
+ if (m1 && m1[1] && s2) {
586
587
  // get era from 2nd (if 2nd hasn't it too, give up and AD will be assumed)
587
588
  const m2 = tailRegexp.exec(s2);
588
589
  if (m2 && m2[1]) {
@@ -603,7 +604,9 @@ class HistoricalDate {
603
604
  }
604
605
  }
605
606
  date.setStartPoint(Datation.parse(s1, options));
606
- date.setEndPoint(Datation.parse(s2, options));
607
+ if (s2) {
608
+ date.setEndPoint(Datation.parse(s2, options));
609
+ }
607
610
  }
608
611
  else {
609
612
  // here we have a s2, even if empty. When empty, it represents
@@ -665,7 +668,7 @@ class HistoricalDate {
665
668
  }
666
669
  if (this.getDateType() === HistoricalDateType.range) {
667
670
  const av = this.a.getSortValue();
668
- const bv = this.b.getSortValue();
671
+ const bv = this.b?.getSortValue() || 0;
669
672
  if (av && bv && av >= bv) {
670
673
  return 'Point A is past point B';
671
674
  }
@@ -683,7 +686,7 @@ class HistoricalDate {
683
686
  if (this.a.isUndefined()) {
684
687
  return this.b.getSortValue() - APPROX_DELTA;
685
688
  }
686
- if (this.b.isUndefined()) {
689
+ if (!this.b || this.b.isUndefined()) {
687
690
  return this.a.getSortValue() + APPROX_DELTA;
688
691
  }
689
692
  return (this.a.getSortValue() + this.b.getSortValue()) / 2;
@@ -749,7 +752,7 @@ class HistoricalDate {
749
752
  break;
750
753
  case HistoricalDateType.range:
751
754
  // min is missing: terminus ante
752
- if (this.a.isUndefined()) {
755
+ if (this.a.isUndefined() && this.b) {
753
756
  year = Math.trunc((this.b.isCentury
754
757
  ? this.centuryToYear(this.b.value)
755
758
  : this.b.value) - (useTerminusSpan ? APPROX_DELTA : 0));
@@ -791,12 +794,12 @@ class HistoricalDate {
791
794
  about = this.a.isApproximate;
792
795
  break;
793
796
  }
794
- if (!this.b.isUndefined()) {
797
+ if (this.b && !this.b.isUndefined()) {
795
798
  about = this.b.isApproximate;
796
799
  }
797
800
  break;
798
801
  }
799
- return about;
802
+ return about ? true : false;
800
803
  }
801
804
  /**
802
805
  * True if any value (either in point or in range) has an about flag.
@@ -812,12 +815,12 @@ class HistoricalDate {
812
815
  dubious = this.a.isDubious;
813
816
  break;
814
817
  }
815
- if (!this.b.isUndefined()) {
818
+ if (this.b && !this.b.isUndefined()) {
816
819
  dubious = this.b.isDubious;
817
820
  }
818
821
  break;
819
822
  }
820
- return dubious;
823
+ return dubious ? true : false;
821
824
  }
822
825
  }
823
826
 
@@ -924,7 +927,7 @@ class TokenPoint {
924
927
  }
925
928
  toString() {
926
929
  let s = `${this.y}.${this.x}`;
927
- if (this.at > 0 && this.run > 0) {
930
+ if (this.at && this.run) {
928
931
  s += `@${this.at}`;
929
932
  if (this.run !== 1) {
930
933
  s += `x${this.run}`;
@@ -956,7 +959,7 @@ class TokenPoint {
956
959
  }
957
960
 
958
961
  class TokenLocation {
959
- constructor(primary, secondary = null) {
962
+ constructor(primary, secondary) {
960
963
  this.primary = primary;
961
964
  this.secondary = secondary;
962
965
  }
@@ -975,7 +978,7 @@ class TokenLocation {
975
978
  if (pair.length === 0) {
976
979
  return null;
977
980
  }
978
- return new TokenLocation(TokenPoint.parse(pair[0]), pair.length > 1 ? TokenPoint.parse(pair[1]) : null);
981
+ return new TokenLocation(TokenPoint.parse(pair[0]), pair.length > 1 ? TokenPoint.parse(pair[1]) || undefined : undefined);
979
982
  }
980
983
  isRange() {
981
984
  // note that != checks for both null and undefined, whereas !== does not
@@ -1046,7 +1049,7 @@ PendingChangesGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0",
1046
1049
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: PendingChangesGuard, decorators: [{
1047
1050
  type: Injectable,
1048
1051
  args: [{
1049
- providedIn: 'root'
1052
+ providedIn: 'root',
1050
1053
  }]
1051
1054
  }] });
1052
1055
 
@@ -1077,6 +1080,7 @@ class SortPipe {
1077
1080
  else if (a[propName] > b[propName]) {
1078
1081
  return 1;
1079
1082
  }
1083
+ return 0;
1080
1084
  });
1081
1085
  }
1082
1086
  }
@@ -1100,7 +1104,7 @@ class GravatarService {
1100
1104
  */
1101
1105
  buildGravatarUrl(email, size = 80) {
1102
1106
  if (!email) {
1103
- return null;
1107
+ return '';
1104
1108
  }
1105
1109
  return gravatar.url(email, { s: size.toString() });
1106
1110
  // const hash = this._hash.getMd5(email.trim().toLowerCase());
@@ -1173,13 +1177,13 @@ class LibraryRouteService {
1173
1177
  if (i > -1) {
1174
1178
  return {
1175
1179
  frTypeId: roleId.substring(0, i),
1176
- frRoleId: roleId.substring(i + 1)
1180
+ frRoleId: roleId.substring(i + 1),
1177
1181
  };
1178
1182
  }
1179
1183
  else {
1180
1184
  return {
1181
1185
  frTypeId: roleId,
1182
- frRoleId: null
1186
+ frRoleId: undefined,
1183
1187
  };
1184
1188
  }
1185
1189
  }
@@ -1190,22 +1194,23 @@ class LibraryRouteService {
1190
1194
  * @param partDefs The parts definitions.
1191
1195
  * @param typeId The type ID.
1192
1196
  * @param roleId The role ID or null.
1193
- * @returns Part definition or null if not found.
1197
+ * @returns Part definition or undefined if not found.
1194
1198
  */
1195
1199
  findPartDefinition(partDefs, typeId, roleId = null) {
1196
1200
  if (!partDefs) {
1197
- return null;
1201
+ return undefined;
1198
1202
  }
1199
1203
  // if role ID is specified, find both type and role
1200
1204
  if (roleId) {
1201
1205
  const reqRoleId = this.stripFragmentRoleId(roleId);
1202
- return partDefs.find(d => {
1206
+ return partDefs.find((d) => {
1203
1207
  return (d.typeId === typeId &&
1208
+ d.roleId &&
1204
1209
  this.stripFragmentRoleId(d.roleId) === reqRoleId);
1205
1210
  });
1206
1211
  }
1207
1212
  // else just find type ID
1208
- return partDefs.find(d => {
1213
+ return partDefs.find((d) => {
1209
1214
  return d.typeId === typeId;
1210
1215
  });
1211
1216
  }
@@ -1216,12 +1221,12 @@ class LibraryRouteService {
1216
1221
  * @param typeId The part's type ID.
1217
1222
  * @param roleId The part's role ID.
1218
1223
  */
1219
- getEditorKeyFromPartType(typeId, roleId = null) {
1224
+ getEditorKeyFromPartType(typeId, roleId) {
1220
1225
  // find the part type ID
1221
1226
  const partGroupKey = this._partEditorKeys[typeId];
1222
1227
  if (!partGroupKey) {
1223
1228
  return {
1224
- partKey: 'default'
1229
+ partKey: 'default',
1225
1230
  };
1226
1231
  }
1227
1232
  // additionally find the role ID if specified
@@ -1229,11 +1234,11 @@ class LibraryRouteService {
1229
1234
  const reqRoleId = this.stripFragmentRoleId(roleId);
1230
1235
  return {
1231
1236
  partKey: partGroupKey.part,
1232
- frKey: partGroupKey.fragments[reqRoleId]
1237
+ frKey: partGroupKey.fragments[reqRoleId],
1233
1238
  };
1234
1239
  }
1235
1240
  return {
1236
- partKey: partGroupKey.part
1241
+ partKey: partGroupKey.part,
1237
1242
  };
1238
1243
  }
1239
1244
  /**
@@ -1247,13 +1252,13 @@ class LibraryRouteService {
1247
1252
  if (i > -1) {
1248
1253
  return {
1249
1254
  partKey: key.substr(0, i),
1250
- frKey: key.substr(i + 1)
1255
+ frKey: key.substr(i + 1),
1251
1256
  };
1252
1257
  }
1253
1258
  else {
1254
1259
  return {
1255
1260
  partKey: key,
1256
- frKey: key
1261
+ frKey: key,
1257
1262
  };
1258
1263
  }
1259
1264
  }
@@ -1285,7 +1290,7 @@ class LibraryRouteService {
1285
1290
  route = `/items/${itemId}/${editorKey.partKey}/${typeId}/${partId}`;
1286
1291
  return {
1287
1292
  route: route,
1288
- rid: roleId
1293
+ rid: roleId,
1289
1294
  };
1290
1295
  }
1291
1296
  /**
@@ -1308,7 +1313,7 @@ class LibraryRouteService {
1308
1313
  route = `/items/${itemId}/${editorKey.frKey}/fragment/${partId}/${frTypeId}/${loc}`;
1309
1314
  return {
1310
1315
  route: route,
1311
- rid: frRoleId
1316
+ rid: frRoleId,
1312
1317
  };
1313
1318
  }
1314
1319
  }
@@ -1317,7 +1322,7 @@ LibraryRouteService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0",
1317
1322
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: LibraryRouteService, decorators: [{
1318
1323
  type: Injectable,
1319
1324
  args: [{
1320
- providedIn: 'root'
1325
+ providedIn: 'root',
1321
1326
  }]
1322
1327
  }], ctorParameters: function () { return [{ type: undefined, decorators: [{
1323
1328
  type: Inject,
@@ -1406,13 +1411,13 @@ class TextLayerService {
1406
1411
  * @param sb The target array.
1407
1412
  */
1408
1413
  renderTokenAtEnd(pt, token, sb) {
1409
- if (pt.at > 0) {
1414
+ if (pt.at) {
1410
1415
  // ...to]
1411
1416
  sb.push(token.substr(pt.at - 1, pt.run));
1412
1417
  sb.push('</span>');
1413
1418
  // ...ken
1414
- if (pt.at - 1 + pt.run < token.length) {
1415
- sb.push(token.substr(pt.at - 1 + pt.run));
1419
+ if (pt.at - 1 + (pt.run || 0) < token.length) {
1420
+ sb.push(token.substr(pt.at - 1 + (pt.run || 0)));
1416
1421
  }
1417
1422
  }
1418
1423
  else {
@@ -1431,7 +1436,7 @@ class TextLayerService {
1431
1436
  */
1432
1437
  renderTokenAtPointStart(loc, token, sb, isSelected) {
1433
1438
  // 1.token's left-part
1434
- if (loc.primary.at > 0) {
1439
+ if (loc.primary.at) {
1435
1440
  // to...
1436
1441
  sb.push(token.substr(0, loc.primary.at - 1));
1437
1442
  }
@@ -1452,14 +1457,14 @@ class TextLayerService {
1452
1457
  */
1453
1458
  renderTokenAtRangeStart(loc, token, sb, isSelected) {
1454
1459
  // 1.token's left-part (=before at)
1455
- if (loc.primary.at > 0) {
1460
+ if (loc.primary.at) {
1456
1461
  // to...
1457
1462
  sb.push(token.substr(0, loc.primary.at - 1));
1458
1463
  }
1459
1464
  // 2.span: [
1460
1465
  this.renderOpeningSpan(loc, 0, sb, isSelected);
1461
1466
  // 3.token's right-part (...ken or ...token)
1462
- if (loc.primary.at > 0) {
1467
+ if (loc.primary.at) {
1463
1468
  // ...ken
1464
1469
  sb.push(token.substr(loc.primary.at - 1));
1465
1470
  }
@@ -1489,14 +1494,14 @@ class TextLayerService {
1489
1494
  * fragment if any. The selected fragment gets decorated with an additional
1490
1495
  * class value.
1491
1496
  */
1492
- render(text, locations, selectedLoc = null) {
1497
+ render(text, locations, selectedLoc) {
1493
1498
  if (!text) {
1494
1499
  return '<div></div>';
1495
1500
  }
1496
1501
  // get lines from text
1497
1502
  const sb = [];
1498
1503
  const lines = this.getLines(text);
1499
- let loc = null;
1504
+ let loc = undefined;
1500
1505
  let tokenSelected = false;
1501
1506
  if (!locations) {
1502
1507
  locations = [];
@@ -1521,7 +1526,7 @@ class TextLayerService {
1521
1526
  if (loc && loc.secondary) {
1522
1527
  if (loc.secondary.y === y + 1 && loc.secondary.x === x + 1) {
1523
1528
  this.renderTokenAtEnd(loc.secondary, token, sb);
1524
- loc = null;
1529
+ loc = undefined;
1525
1530
  }
1526
1531
  else {
1527
1532
  sb.push(token);
@@ -1535,7 +1540,7 @@ class TextLayerService {
1535
1540
  if (loc) {
1536
1541
  tokenSelected = selectedLoc ? loc.overlaps(selectedLoc) : false;
1537
1542
  if (!this.renderTokenAtStart(loc, token, sb, tokenSelected)) {
1538
- loc = null;
1543
+ loc = undefined;
1539
1544
  }
1540
1545
  }
1541
1546
  else {
@@ -1565,7 +1570,7 @@ class TextLayerService {
1565
1570
  * Get the Y bounds from the specified selection.
1566
1571
  * @param range The selection range.
1567
1572
  * @param forNew True if getting bounds for a new fragment.
1568
- * @returns Range, inclusive.
1573
+ * @returns Range, inclusive, or undefined.
1569
1574
  */
1570
1575
  getYBoundsFromRange(range, forNew) {
1571
1576
  let p = null;
@@ -1573,10 +1578,10 @@ class TextLayerService {
1573
1578
  case Node.TEXT_NODE:
1574
1579
  // single-p selection, parent=p
1575
1580
  p = range.commonAncestorContainer.parentElement;
1576
- if (forNew && (p.nodeName !== 'P' || !p.hasAttribute('id'))) {
1581
+ if (forNew && (p?.nodeName !== 'P' || !p.hasAttribute('id'))) {
1577
1582
  return null;
1578
1583
  }
1579
- if (p.nodeName === 'SPAN') {
1584
+ if (p?.nodeName === 'SPAN') {
1580
1585
  p = p.parentElement;
1581
1586
  }
1582
1587
  const y = parseInt(p.getAttribute('id').substr(1), 10);
@@ -1620,6 +1625,7 @@ class TextLayerService {
1620
1625
  end: yMax,
1621
1626
  };
1622
1627
  }
1628
+ return null;
1623
1629
  }
1624
1630
  locatePrevSpace(line, index) {
1625
1631
  while (index > -1 && line.charAt(index) !== ' ') {
@@ -1807,13 +1813,14 @@ class TextLayerService {
1807
1813
  */
1808
1814
  getNodeBaseOffset(node) {
1809
1815
  let offset = 0;
1810
- node = node.previousSibling;
1811
- while (node) {
1816
+ let n = node;
1817
+ n = node.previousSibling;
1818
+ while (n) {
1812
1819
  offset +=
1813
- node.nodeType === Node.ELEMENT_NODE
1814
- ? node.innerText?.length || 0
1815
- : node.textContent?.length || 0;
1816
- node = node.previousSibling;
1820
+ n.nodeType === Node.ELEMENT_NODE
1821
+ ? n.innerText?.length || 0
1822
+ : n.textContent?.length || 0;
1823
+ n = n.previousSibling;
1817
1824
  }
1818
1825
  return offset;
1819
1826
  }
@@ -1861,7 +1868,7 @@ class TextLayerService {
1861
1868
  // single token: check for at,run
1862
1869
  if (start.at) {
1863
1870
  sb.push(`@${start.at}`);
1864
- if (start.run > 1) {
1871
+ if (start.run && start.run > 1) {
1865
1872
  sb.push(`x${start.run}`);
1866
1873
  }
1867
1874
  }
@@ -1914,8 +1921,8 @@ class TextLayerService {
1914
1921
  let inside = false;
1915
1922
  if (
1916
1923
  // the selected text node must not be child of a span
1917
- range.startContainer.parentElement.tagName === 'SPAN' ||
1918
- range.startContainer.parentElement.tagName === 'SPAN' ||
1924
+ range.startContainer.parentElement?.tagName === 'SPAN' ||
1925
+ range.startContainer.parentElement?.tagName === 'SPAN' ||
1919
1926
  // the nodes tree from the common ancestor, from the start node
1920
1927
  // up to the end node, must not include any span
1921
1928
  this.findNode(range.commonAncestorContainer, (node) => {
@@ -1947,7 +1954,7 @@ class TextLayerService {
1947
1954
  * @param range The selected range. You can get it from the
1948
1955
  * current selection using getSelectedRange().
1949
1956
  * @param text The full base text.
1950
- * @returns TokenLocation The token location or null.
1957
+ * @returns TokenLocation The token location or undefined.
1951
1958
  */
1952
1959
  getSelectedLocationForNew(range, text) {
1953
1960
  if (!range || this.selHasAnySpan(range)) {
@@ -2068,27 +2075,27 @@ class PasswordValidator {
2068
2075
  }
2069
2076
  if (control.value.length < 8) {
2070
2077
  return {
2071
- passwordTooShort: true
2078
+ passwordTooShort: true,
2072
2079
  };
2073
2080
  }
2074
2081
  if (!/.*[A-Z].*/.test(control.value)) {
2075
2082
  return {
2076
- noUpperInPassword: true
2083
+ noUpperInPassword: true,
2077
2084
  };
2078
2085
  }
2079
2086
  if (!/.*[a-z].*/.test(control.value)) {
2080
2087
  return {
2081
- noLowerInPassword: true
2088
+ noLowerInPassword: true,
2082
2089
  };
2083
2090
  }
2084
2091
  if (!/.*[A-Z].*/.test(control.value)) {
2085
2092
  return {
2086
- noUpperInPassword: true
2093
+ noUpperInPassword: true,
2087
2094
  };
2088
2095
  }
2089
2096
  if (!/.*[-`~!@#$%^&*()_+=\[\]{};:'",.<>/?|\\].*/.test(control.value)) {
2090
2097
  return {
2091
- noSymbolInPassword: true
2098
+ noSymbolInPassword: true,
2092
2099
  };
2093
2100
  }
2094
2101
  return null;