ropegeo-common 1.20.1 → 1.20.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.
@@ -17,6 +17,11 @@ export declare class TimeMeasurement extends Measurement {
17
17
  constructor(value: number, unit: TimeMeasurementUnit);
18
18
  toMeasurementSystem(system: TimeMeasurementSystem, unitName?: string): TimeMeasurement;
19
19
  protected abbreviate(): string;
20
+ /**
21
+ * Renders the value in a human-friendly unit derived from the stored unit and magnitude
22
+ * (e.g. 90 minutes → `1.5h`) without changing {@link unit}.
23
+ */
24
+ toString(showUnitName?: boolean): string;
20
25
  private toFreedom;
21
26
  static fromResult(json: unknown): TimeMeasurement;
22
27
  static coerce(value: unknown, defaultUnit: TimeMeasurementUnit): TimeMeasurement | null;
@@ -1 +1 @@
1
- {"version":3,"file":"timeMeasurement.d.ts","sourceRoot":"","sources":["../../../src/models/measurements/timeMeasurement.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,WAAW,EAAE,MAAM,eAAe,CAAC;AACxE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,MAAM,MAAM,cAAc,GAAG;IACzB,iBAAiB,EAAE,UAAU,CAAC;IAC9B,IAAI,EAAE,SAAS,GAAG,OAAO,GAAG,MAAM,CAAC;CACtC,CAAC;AACF,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAAG,aAAa,CAAC;AACjE,MAAM,MAAM,qBAAqB,GAAG,UAAU,GAAG,SAAS,CAAC;AAE3D,2EAA2E;AAC3E,eAAO,MAAM,OAAO,EAAE,cAAmE,CAAC;AAC1F,eAAO,MAAM,KAAK,EAAE,cAAiE,CAAC;AACtF,eAAO,MAAM,IAAI,EAAE,cAAgE,CAAC;AA8CpF,0FAA0F;AAC1F,qBAAa,eAAgB,SAAQ,WAAW;IAC5C,QAAQ,CAAC,eAAe,EAAG,MAAM,CAAU;IAC3C,SAAiB,IAAI,EAAE,mBAAmB,CAAC;gBAE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gBtF,SAAS,CAAC,UAAU,IAAI,MAAM;IAI9B;;;OAGG;IACH,QAAQ,CAAC,YAAY,UAAO,GAAG,MAAM;IAYrC,OAAO,CAAC,SAAS;IAcjB,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,eAAe;IAkBjD,MAAM,CAAC,MAAM,CACT,KAAK,EAAE,OAAO,EACd,WAAW,EAAE,mBAAmB,GACjC,eAAe,GAAG,IAAI;CAiB5B"}
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TimeMeasurement = exports.DAYS = exports.HOURS = exports.MINUTES = void 0;
4
4
  const measurement_1 = require("./measurement");
5
+ const measurementValue_1 = require("./measurementValue");
5
6
  /** Convenience unit descriptors for the units RopeWiki stores times in. */
6
7
  exports.MINUTES = { measurementSystem: 'Standard', name: 'minutes' };
7
8
  exports.HOURS = { measurementSystem: 'Standard', name: 'hours' };
@@ -61,16 +62,22 @@ class TimeMeasurement extends measurement_1.Measurement {
61
62
  return new TimeMeasurement(value, { measurementSystem: 'Standard', name: targetName });
62
63
  }
63
64
  abbreviate() {
64
- switch (this.unit.name) {
65
- case 'minutes':
66
- return 'min';
67
- case 'hours':
68
- return 'h';
69
- case 'days':
70
- return 'd';
71
- default:
72
- return this.unit.name;
65
+ return abbreviateStandardUnit(this.unit.name);
66
+ }
67
+ /**
68
+ * Renders the value in a human-friendly unit derived from the stored unit and magnitude
69
+ * (e.g. 90 minutes → `1.5h`) without changing {@link unit}.
70
+ */
71
+ toString(showUnitName = true) {
72
+ if (this.unit.measurementSystem === measurement_1.FREEDOM_MEASUREMENT_SYSTEM) {
73
+ return super.toString(showUnitName);
74
+ }
75
+ const { value, unit } = displayUnitAndValue(this.value, this.unit.name);
76
+ const formatted = (0, measurementValue_1.formatMeasurementValue)(value);
77
+ if (!showUnitName) {
78
+ return formatted;
73
79
  }
80
+ return `${formatted}${abbreviateStandardUnit(unit)}`;
74
81
  }
75
82
  toFreedom(unit, unitName) {
76
83
  const seconds = this.value * SECONDS_PER_UNIT[unit.name];
@@ -115,6 +122,45 @@ class TimeMeasurement extends measurement_1.Measurement {
115
122
  }
116
123
  }
117
124
  exports.TimeMeasurement = TimeMeasurement;
125
+ function abbreviateStandardUnit(name) {
126
+ switch (name) {
127
+ case 'minutes':
128
+ return 'min';
129
+ case 'hours':
130
+ return 'h';
131
+ case 'days':
132
+ return 'd';
133
+ }
134
+ }
135
+ /** Picks a display unit and scaled value for {@link TimeMeasurement.toString}. */
136
+ function displayUnitAndValue(value, baseUnit) {
137
+ switch (baseUnit) {
138
+ case 'minutes':
139
+ if (value >= 60 * 24) {
140
+ return { value: value / (60 * 24), unit: 'days' };
141
+ }
142
+ if (value >= 60) {
143
+ return { value: value / 60, unit: 'hours' };
144
+ }
145
+ return { value, unit: 'minutes' };
146
+ case 'hours':
147
+ if (value >= 24) {
148
+ return { value: value / 24, unit: 'days' };
149
+ }
150
+ if (value < 1) {
151
+ return { value: value * 60, unit: 'minutes' };
152
+ }
153
+ return { value, unit: 'hours' };
154
+ case 'days':
155
+ if (value < 1 / 24) {
156
+ return { value: value * 24 * 60, unit: 'minutes' };
157
+ }
158
+ if (value < 1) {
159
+ return { value: value * 24, unit: 'hours' };
160
+ }
161
+ return { value, unit: 'days' };
162
+ }
163
+ }
118
164
  function assertStandardUnit(name) {
119
165
  if (!STANDARD_NAMES.includes(name)) {
120
166
  throw new Error(`TimeMeasurement: "${name}" is not a valid Standard unit (expected one of [${STANDARD_NAMES.join(', ')}])`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ropegeo-common",
3
- "version": "1.20.1",
3
+ "version": "1.20.2",
4
4
  "description": "Shared domain models and helpers for RopeGeo and WebScraper",
5
5
  "license": "ISC",
6
6
  "repository": {