@budiauktioner/buildi-primitives 0.2.0 → 0.2.1
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/CHANGELOG.md +12 -0
- package/PARITY.md +1 -1
- package/dist/cjs/internal/scan-boundaries.js +26 -0
- package/dist/cjs/internal/scan-boundaries.js.map +7 -0
- package/dist/cjs/measurement/area.js +2 -1
- package/dist/cjs/measurement/area.js.map +2 -2
- package/dist/cjs/measurement/data-size.js +1 -1
- package/dist/cjs/measurement/data-size.js.map +1 -1
- package/dist/cjs/measurement/electric-charge.js +1 -1
- package/dist/cjs/measurement/electric-charge.js.map +1 -1
- package/dist/cjs/measurement/electric-current.js +1 -1
- package/dist/cjs/measurement/electric-current.js.map +1 -1
- package/dist/cjs/measurement/energy.js +1 -1
- package/dist/cjs/measurement/energy.js.map +1 -1
- package/dist/cjs/measurement/flow-rate.js +1 -1
- package/dist/cjs/measurement/flow-rate.js.map +1 -1
- package/dist/cjs/measurement/frequency.js +1 -1
- package/dist/cjs/measurement/frequency.js.map +1 -1
- package/dist/cjs/measurement/length.js +1 -1
- package/dist/cjs/measurement/length.js.map +1 -1
- package/dist/cjs/measurement/luminance.js +1 -1
- package/dist/cjs/measurement/luminance.js.map +1 -1
- package/dist/cjs/measurement/luminous-flux.js +4 -1
- package/dist/cjs/measurement/luminous-flux.js.map +2 -2
- package/dist/cjs/measurement/percentage.js +1 -1
- package/dist/cjs/measurement/percentage.js.map +1 -1
- package/dist/cjs/measurement/power.js +7 -3
- package/dist/cjs/measurement/power.js.map +2 -2
- package/dist/cjs/measurement/pressure.js +1 -1
- package/dist/cjs/measurement/pressure.js.map +1 -1
- package/dist/cjs/measurement/rotational-speed.js +1 -1
- package/dist/cjs/measurement/rotational-speed.js.map +1 -1
- package/dist/cjs/measurement/sound-level.js +5 -1
- package/dist/cjs/measurement/sound-level.js.map +2 -2
- package/dist/cjs/measurement/speed.js +1 -1
- package/dist/cjs/measurement/speed.js.map +1 -1
- package/dist/cjs/measurement/temperature.js +1 -1
- package/dist/cjs/measurement/temperature.js.map +2 -2
- package/dist/cjs/measurement/torque.js +1 -1
- package/dist/cjs/measurement/torque.js.map +1 -1
- package/dist/cjs/measurement/voltage.js +1 -1
- package/dist/cjs/measurement/voltage.js.map +1 -1
- package/dist/cjs/measurement/volume.js +1 -1
- package/dist/cjs/measurement/volume.js.map +1 -1
- package/dist/cjs/measurement/weight.js +1 -1
- package/dist/cjs/measurement/weight.js.map +1 -1
- package/dist/esm/internal/scan-boundaries.js +6 -0
- package/dist/esm/internal/scan-boundaries.js.map +7 -0
- package/dist/esm/measurement/area.js +2 -1
- package/dist/esm/measurement/area.js.map +2 -2
- package/dist/esm/measurement/data-size.js +1 -1
- package/dist/esm/measurement/data-size.js.map +1 -1
- package/dist/esm/measurement/electric-charge.js +1 -1
- package/dist/esm/measurement/electric-charge.js.map +1 -1
- package/dist/esm/measurement/electric-current.js +1 -1
- package/dist/esm/measurement/electric-current.js.map +1 -1
- package/dist/esm/measurement/energy.js +1 -1
- package/dist/esm/measurement/energy.js.map +1 -1
- package/dist/esm/measurement/flow-rate.js +1 -1
- package/dist/esm/measurement/flow-rate.js.map +1 -1
- package/dist/esm/measurement/frequency.js +1 -1
- package/dist/esm/measurement/frequency.js.map +1 -1
- package/dist/esm/measurement/length.js +1 -1
- package/dist/esm/measurement/length.js.map +1 -1
- package/dist/esm/measurement/luminance.js +1 -1
- package/dist/esm/measurement/luminance.js.map +1 -1
- package/dist/esm/measurement/luminous-flux.js +4 -1
- package/dist/esm/measurement/luminous-flux.js.map +2 -2
- package/dist/esm/measurement/percentage.js +1 -1
- package/dist/esm/measurement/percentage.js.map +1 -1
- package/dist/esm/measurement/power.js +7 -3
- package/dist/esm/measurement/power.js.map +2 -2
- package/dist/esm/measurement/pressure.js +1 -1
- package/dist/esm/measurement/pressure.js.map +1 -1
- package/dist/esm/measurement/rotational-speed.js +1 -1
- package/dist/esm/measurement/rotational-speed.js.map +1 -1
- package/dist/esm/measurement/sound-level.js +5 -1
- package/dist/esm/measurement/sound-level.js.map +2 -2
- package/dist/esm/measurement/speed.js +1 -1
- package/dist/esm/measurement/speed.js.map +1 -1
- package/dist/esm/measurement/temperature.js +1 -1
- package/dist/esm/measurement/temperature.js.map +2 -2
- package/dist/esm/measurement/torque.js +1 -1
- package/dist/esm/measurement/torque.js.map +1 -1
- package/dist/esm/measurement/voltage.js +1 -1
- package/dist/esm/measurement/voltage.js.map +1 -1
- package/dist/esm/measurement/volume.js +1 -1
- package/dist/esm/measurement/volume.js.map +1 -1
- package/dist/esm/measurement/weight.js +1 -1
- package/dist/esm/measurement/weight.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/pressure.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { PressureUnit } from \"./pressure-unit.js\";\n\n/** Options for {@link Pressure.format} and {@link Pressure.normalize}. */\nexport interface PressureFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: PressureUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { PressureUnit } from \"./pressure-unit.js\";\n\n/** Options for {@link Pressure.format} and {@link Pressure.normalize}. */\nexport interface PressureFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: PressureUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*(?:GPa|MPa|kPa|hPa|mbar|mmHg|mm\\s+Hg|lb/in\u00B2|atm|torr|bar|Pa|PSI)(?![\\p{L}\\p{N}_])`,\n \"giu\",\n);\n\n/**\n * A pressure stored internally in pascals,\n * parsed from any supported unit and convertible between all of them.\n */\nexport class Pressure extends Quantity<PressureUnit> {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Pressure\",\n localizedName: \"Tryck\",\n emoji: \"\uD83C\uDFAF\",\n sources: Object.freeze([\n \"https://www.bipm.org/en/measurement-units/si-derived-units\",\n \"https://en.wikipedia.org/wiki/Conversion_of_units\",\n ]),\n });\n\n protected get units(): MeasurementUnitIndex<PressureUnit> {\n return PressureUnit.index;\n }\n\n protected withBaseValue(baseValue: Decimal, unit: PressureUnit): this {\n return new Pressure(baseValue, unit) as this;\n }\n\n get pascals(): number {\n return this.in(PressureUnit.Pascal);\n }\n get hectopascals(): number {\n return this.in(PressureUnit.Hectopascal);\n }\n get kilopascals(): number {\n return this.in(PressureUnit.Kilopascal);\n }\n get megapascals(): number {\n return this.in(PressureUnit.Megapascal);\n }\n get gigapascals(): number {\n return this.in(PressureUnit.Gigapascal);\n }\n get bars(): number {\n return this.in(PressureUnit.Bar);\n }\n get millibars(): number {\n return this.in(PressureUnit.Millibar);\n }\n get psi(): number {\n return this.in(PressureUnit.Psi);\n }\n get atmospheres(): number {\n return this.in(PressureUnit.Atmosphere);\n }\n get millimetersOfMercury(): number {\n return this.in(PressureUnit.MillimeterOfMercury);\n }\n\n /** Creates a pressure from a value and unit. */\n static create(value: number | Decimal, unit: PressureUnit): Pressure {\n PressureUnit.index.assertOwns(unit);\n const amount = value instanceof Decimal ? value : Pressure.decimalOf(value);\n return new Pressure(unit.toBase(amount), unit);\n }\n\n static fromPascals(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Pascal);\n }\n static fromHectopascals(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Hectopascal);\n }\n static fromKilopascals(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Kilopascal);\n }\n static fromMegapascals(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Megapascal);\n }\n static fromGigapascals(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Gigapascal);\n }\n static fromBars(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Bar);\n }\n static fromMillibars(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Millibar);\n }\n static fromPsi(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Psi);\n }\n static fromAtmospheres(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.Atmosphere);\n }\n static fromMillimetersOfMercury(value: number | Decimal): Pressure {\n return Pressure.create(value, PressureUnit.MillimeterOfMercury);\n }\n\n static tryParse(input: string | null | undefined): Pressure | null {\n const parsed = tryParseQuantity(input, PressureUnit.index);\n return parsed === null ? null : new Pressure(parsed.baseValue, parsed.unit);\n }\n\n static parse(input: string): Pressure {\n const result = Pressure.tryParse(input);\n if (result === null) throw new Error(\"Invalid pressure.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return Pressure.tryParse(input) !== null;\n }\n\n /** Display form in the parsed unit, or `null` when the input is invalid. */\n static format(input: string | null | undefined, options?: PressureFormatOptions): string | null {\n const parsed = Pressure.tryParse(input);\n if (parsed !== null) {\n if (options?.unit !== undefined || options?.decimals !== undefined) {\n return parsed.toStringInUnit(options.unit ?? parsed.originalUnit, options.decimals);\n }\n return parsed.toString();\n }\n return Pressure.fallback(input, options);\n }\n\n /** The value in pascals, or `null` when the input is invalid. */\n static normalize(\n input: string | null | undefined,\n options?: PressureFormatOptions,\n ): string | null {\n const parsed = Pressure.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return Pressure.fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && Pressure.normalize(input) === input;\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<Pressure>[] {\n if (!text) return [];\n\n const out: TextCandidate<Pressure>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = Pressure.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<Pressure>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"Pressure\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskPressure(value),\n confidence: TextMatchConfidence.Medium,\n value,\n }),\n );\n }\n\n return out;\n }\n\n private static fallback(\n input: string | null | undefined,\n options?: PressureFormatOptions,\n ): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n }\n\n private static decimalOf(value: number): Decimal {\n const parsed = Decimal.fromNumber(value);\n if (parsed === null) throw new Error(`Value cannot be represented as a pressure: ${value}`);\n return parsed;\n }\n}\n\n/** Returns a masked pressure, e.g. `10 Pa` becomes `*** Pa`. */\nexport function maskPressure(value: Pressure): string {\n return `*** ${value.originalUnit.symbol}`;\n}\n"],
|
|
5
5
|
"mappings": "AAAA,SAAS,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AA,UACV,UAAU,sBAAsB;AAAA,UAChC,gBAAgB,MAAM,mBAAmB;AAAA,UACzC,eAAe,MAAM,SAAS;AAAA,UAC9B,YAAY,aAAa,KAAK;AAAA,UAC9B,YAAY,oBAAoB;AAAA,UAChC;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,OAAe,SACb,OACA,SACe;AACf,QAAI,SAAS,sCAAsC,KAAM,QAAO;AAChE,QAAI,SAAS,KAAM,QAAO;AAC1B,UAAM,UAAU,MAAM,KAAK;AAC3B,WAAO,QAAQ,SAAS,IAAI,UAAU;AAAA,EACxC;AAAA,EAEA,OAAe,UAAU,OAAwB;AAC/C,UAAM,SAAS,QAAQ,WAAW,KAAK;AACvC,QAAI,WAAW,KAAM,OAAM,IAAI,MAAM,8CAA8C,KAAK,EAAE;AAC1F,WAAO;AAAA,EACT;AACF;AAGO,SAAS,aAAa,OAAyB;AACpD,SAAO,OAAO,MAAM,aAAa,MAAM;AACzC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -6,7 +6,7 @@ import { TextCandidateCategory } from "../text-scanning/text-candidate-category.
|
|
|
6
6
|
import { TextMatchConfidence } from "../text-scanning/text-match-confidence.js";
|
|
7
7
|
import { RotationalSpeedUnit } from "./rotational-speed-unit.js";
|
|
8
8
|
const SCAN_PATTERN = new RegExp(
|
|
9
|
-
String.raw`(
|
|
9
|
+
String.raw`(?<![\p{L}\p{N}_])(?:[+-]\s*)?\d+[0-9 .,]*\s*(?:rpm|r/min|rev/min|varv/min|rps|r/s|rev/s|varv/s|rad/s(?:ec)?)(?![\p{L}\p{N}_])`,
|
|
10
10
|
"giu"
|
|
11
11
|
);
|
|
12
12
|
class RotationalSpeed extends Quantity {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/rotational-speed.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { tryParseNumberOnly } from \"../internal/measurement-unit-parser.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { RotationalSpeedUnit } from \"./rotational-speed-unit.js\";\n\n/** Options for {@link RotationalSpeed.format} and {@link RotationalSpeed.normalize}. */\nexport interface RotationalSpeedFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: RotationalSpeedUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { tryParseNumberOnly } from \"../internal/measurement-unit-parser.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { RotationalSpeedUnit } from \"./rotational-speed-unit.js\";\n\n/** Options for {@link RotationalSpeed.format} and {@link RotationalSpeed.normalize}. */\nexport interface RotationalSpeedFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: RotationalSpeedUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*(?:rpm|r/min|rev/min|varv/min|rps|r/s|rev/s|varv/s|rad/s(?:ec)?)(?![\\p{L}\\p{N}_])`,\n \"giu\",\n);\n\n/**\n * A rotational speed stored internally in revolutions per minute,\n * parsed from any supported unit and convertible between all of them.\n */\nexport class RotationalSpeed extends Quantity<RotationalSpeedUnit> {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Rotational Speed\",\n localizedName: \"Varvtal\",\n emoji: \"\uD83D\uDD04\",\n sources: Object.freeze([\n \"https://en.wikipedia.org/wiki/Revolutions_per_minute\",\n \"https://www.bipm.org/en/measurement-units/si-derived-units\",\n ]),\n });\n\n protected get units(): MeasurementUnitIndex<RotationalSpeedUnit> {\n return RotationalSpeedUnit.index;\n }\n\n protected withBaseValue(baseValue: Decimal, unit: RotationalSpeedUnit): this {\n return new RotationalSpeed(baseValue, unit) as this;\n }\n\n get rpm(): number {\n return this.in(RotationalSpeedUnit.Rpm);\n }\n get rps(): number {\n return this.in(RotationalSpeedUnit.Rps);\n }\n get radiansPerSecond(): number {\n return this.in(RotationalSpeedUnit.RadiansPerSecond);\n }\n\n /** Creates a rotational speed from a value and unit. */\n static create(value: number | Decimal, unit: RotationalSpeedUnit): RotationalSpeed {\n RotationalSpeedUnit.index.assertOwns(unit);\n const amount = value instanceof Decimal ? value : RotationalSpeed.decimalOf(value);\n return new RotationalSpeed(unit.toBase(amount), unit);\n }\n\n static fromRpm(value: number | Decimal): RotationalSpeed {\n return RotationalSpeed.create(value, RotationalSpeedUnit.Rpm);\n }\n static fromRps(value: number | Decimal): RotationalSpeed {\n return RotationalSpeed.create(value, RotationalSpeedUnit.Rps);\n }\n static fromRadiansPerSecond(value: number | Decimal): RotationalSpeed {\n return RotationalSpeed.create(value, RotationalSpeedUnit.RadiansPerSecond);\n }\n\n /** A bare number is read as rpm, the unit rotational speed is usually quoted in. */\n static tryParse(input: string | null | undefined): RotationalSpeed | null {\n const bare = tryParseNumberOnly(input);\n if (bare !== null) return new RotationalSpeed(bare, RotationalSpeedUnit.Rpm);\n\n const parsed = tryParseQuantity(input, RotationalSpeedUnit.index);\n return parsed === null ? null : new RotationalSpeed(parsed.baseValue, parsed.unit);\n }\n\n static parse(input: string): RotationalSpeed {\n const result = RotationalSpeed.tryParse(input);\n if (result === null) throw new Error(\"Invalid rotational speed.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return RotationalSpeed.tryParse(input) !== null;\n }\n\n /** Display form in the parsed unit, or `null` when the input is invalid. */\n static format(\n input: string | null | undefined,\n options?: RotationalSpeedFormatOptions,\n ): string | null {\n const parsed = RotationalSpeed.tryParse(input);\n if (parsed !== null) {\n if (options?.unit !== undefined || options?.decimals !== undefined) {\n return parsed.toStringInUnit(options.unit ?? parsed.originalUnit, options.decimals);\n }\n return parsed.toString();\n }\n return RotationalSpeed.fallback(input, options);\n }\n\n /** The value in revolutions per minute, or `null` when the input is invalid. */\n static normalize(\n input: string | null | undefined,\n options?: RotationalSpeedFormatOptions,\n ): string | null {\n const parsed = RotationalSpeed.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return RotationalSpeed.fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && RotationalSpeed.normalize(input) === input;\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<RotationalSpeed>[] {\n if (!text) return [];\n\n const out: TextCandidate<RotationalSpeed>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = RotationalSpeed.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<RotationalSpeed>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"RotationalSpeed\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskRotationalSpeed(value),\n confidence: TextMatchConfidence.Low,\n value,\n }),\n );\n }\n\n return out;\n }\n\n private static fallback(\n input: string | null | undefined,\n options?: RotationalSpeedFormatOptions,\n ): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n }\n\n private static decimalOf(value: number): Decimal {\n const parsed = Decimal.fromNumber(value);\n if (parsed === null)\n throw new Error(`Value cannot be represented as a rotational speed: ${value}`);\n return parsed;\n }\n}\n\n/** Returns a masked rotational speed, e.g. `10 rpm` becomes `*** rpm`. */\nexport function maskRotationalSpeed(value: RotationalSpeed): string {\n return `*** ${value.originalUnit.symbol}`;\n}\n"],
|
|
5
5
|
"mappings": "AAAA,SAAS,eAAe;AAExB,SAAS,0BAA0B;AACnC,SAAS,UAAU,wBAAwB;AAE3C,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AACpC,SAAS,2BAA2B;AAWpC,MAAM,eAAe,IAAI;AAAA,EACvB,OAAO;AAAA,EACP;AACF;AAMO,MAAM,wBAAwB,SAA8B;AAAA,EACjE,OAAgB,WAA8B,OAAO,OAAO;AAAA,IAC1D,aAAa;AAAA,IACb,eAAe;AAAA,IACf,OAAO;AAAA,IACP,SAAS,OAAO,OAAO;AAAA,MACrB;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAAA,EAED,IAAc,QAAmD;AAC/D,WAAO,oBAAoB;AAAA,EAC7B;AAAA,EAEU,cAAc,WAAoB,MAAiC;AAC3E,WAAO,IAAI,gBAAgB,WAAW,IAAI;AAAA,EAC5C;AAAA,EAEA,IAAI,MAAc;AAChB,WAAO,KAAK,GAAG,oBAAoB,GAAG;AAAA,EACxC;AAAA,EACA,IAAI,MAAc;AAChB,WAAO,KAAK,GAAG,oBAAoB,GAAG;AAAA,EACxC;AAAA,EACA,IAAI,mBAA2B;AAC7B,WAAO,KAAK,GAAG,oBAAoB,gBAAgB;AAAA,EACrD;A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AW,KAAK;AACvC,QAAI,WAAW;AACb,YAAM,IAAI,MAAM,sDAAsD,KAAK,EAAE;AAC/E,WAAO;AAAA,EACT;AACF;AAGO,SAAS,oBAAoB,OAAgC;AAClE,SAAO,OAAO,MAAM,aAAa,MAAM;AACzC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Decimal } from "../internal/decimal.js";
|
|
2
2
|
import { sanitizeInput } from "../internal/input-sanitization.js";
|
|
3
3
|
import { tryParseNumberOnly } from "../internal/measurement-unit-parser.js";
|
|
4
|
+
import { WORD_BOUNDARY } from "../internal/scan-boundaries.js";
|
|
4
5
|
import { TextCandidate } from "../text-scanning/text-candidate.js";
|
|
5
6
|
import { TextCandidateCategory } from "../text-scanning/text-candidate-category.js";
|
|
6
7
|
import { TextMatchConfidence } from "../text-scanning/text-match-confidence.js";
|
|
@@ -17,7 +18,10 @@ const SoundWeighting = {
|
|
|
17
18
|
Z: 4
|
|
18
19
|
};
|
|
19
20
|
const UNIT_PATTERN = /^\s*(?<sign>[+-])?\s*(?<num>[0-9][0-9 .,]*[0-9]|[0-9])\s*(?<unit>dB\s*(?:\(\s*(?<w1>[ABCZabcz])\s*\)|(?<w2>[ABCZabcz]))?)?\s*$/iu;
|
|
20
|
-
const SCAN_PATTERN =
|
|
21
|
+
const SCAN_PATTERN = new RegExp(
|
|
22
|
+
String.raw`(?<![\p{L}\p{N}_])(?:[+-]\s*)?\d+[0-9 .,]*\s*dB(?:\s*\(\s*[ABCZabcz]\s*\)|[ABCZabcz])?${WORD_BOUNDARY}`,
|
|
23
|
+
"giu"
|
|
24
|
+
);
|
|
21
25
|
const SUFFIXES = {
|
|
22
26
|
[SoundWeighting.Unweighted]: " dB",
|
|
23
27
|
[SoundWeighting.A]: " dB(A)",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/sound-level.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport { sanitizeInput } from \"../internal/input-sanitization.js\";\nimport { tryParseNumberOnly } from \"../internal/measurement-unit-parser.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\n\n/**\n * The frequency weighting curve applied to a sound measurement.\n */\nexport const SoundWeighting = {\n /** No weighting: a flat response. */\n Unweighted: 0,\n /** A-weighting, which approximates human hearing sensitivity. */\n A: 1,\n /** B-weighting, an intermediate curve that is rarely used. */\n B: 2,\n /** C-weighting, flat across most audible frequencies. */\n C: 3,\n /** Z-weighting, zero weighting, flat from 10 Hz to 20 kHz. */\n Z: 4,\n} as const;\n\nexport type SoundWeighting = (typeof SoundWeighting)[keyof typeof SoundWeighting];\n\n/** Options for {@link SoundLevel.format} and {@link SoundLevel.normalize}. */\nexport interface SoundLevelFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n}\n\nconst UNIT_PATTERN =\n /^\\s*(?<sign>[+-])?\\s*(?<num>[0-9][0-9 .,]*[0-9]|[0-9])\\s*(?<unit>dB\\s*(?:\\(\\s*(?<w1>[ABCZabcz])\\s*\\)|(?<w2>[ABCZabcz]))?)?\\s*$/iu;\n\nconst SCAN_PATTERN
|
|
5
|
-
"mappings": "AAAA,SAAS,eAAe;AACxB,SAAS,qBAAqB;AAC9B,SAAS,0BAA0B;
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport { sanitizeInput } from \"../internal/input-sanitization.js\";\nimport { tryParseNumberOnly } from \"../internal/measurement-unit-parser.js\";\nimport { WORD_BOUNDARY } from \"../internal/scan-boundaries.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\n\n/**\n * The frequency weighting curve applied to a sound measurement.\n */\nexport const SoundWeighting = {\n /** No weighting: a flat response. */\n Unweighted: 0,\n /** A-weighting, which approximates human hearing sensitivity. */\n A: 1,\n /** B-weighting, an intermediate curve that is rarely used. */\n B: 2,\n /** C-weighting, flat across most audible frequencies. */\n C: 3,\n /** Z-weighting, zero weighting, flat from 10 Hz to 20 kHz. */\n Z: 4,\n} as const;\n\nexport type SoundWeighting = (typeof SoundWeighting)[keyof typeof SoundWeighting];\n\n/** Options for {@link SoundLevel.format} and {@link SoundLevel.normalize}. */\nexport interface SoundLevelFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n}\n\nconst UNIT_PATTERN =\n /^\\s*(?<sign>[+-])?\\s*(?<num>[0-9][0-9 .,]*[0-9]|[0-9])\\s*(?<unit>dB\\s*(?:\\(\\s*(?<w1>[ABCZabcz])\\s*\\)|(?<w2>[ABCZabcz]))?)?\\s*$/iu;\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*dB(?:\\s*\\(\\s*[ABCZabcz]\\s*\\)|[ABCZabcz])?${WORD_BOUNDARY}`,\n \"giu\",\n);\n\nconst SUFFIXES: Record<SoundWeighting, string> = {\n [SoundWeighting.Unweighted]: \" dB\",\n [SoundWeighting.A]: \" dB(A)\",\n [SoundWeighting.B]: \" dB(B)\",\n [SoundWeighting.C]: \" dB(C)\",\n [SoundWeighting.Z]: \" dB(Z)\",\n};\n\nconst WEIGHTINGS: Record<string, SoundWeighting> = {\n A: SoundWeighting.A,\n B: SoundWeighting.B,\n C: SoundWeighting.C,\n Z: SoundWeighting.Z,\n};\n\n/**\n * A sound pressure level in decibels, optionally carrying a frequency\n * weighting \u2014 `85 dB`, `69 dB(A)`, `40 dBZ`.\n *\n * The decibel unit is missing from many inputs in practice, so a bare number is\n * accepted and read as unweighted decibels.\n */\nexport class SoundLevel {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Sound Level\",\n localizedName: \"Ljudniv\u00E5\",\n emoji: \"\uD83D\uDD0A\",\n sources: Object.freeze([\n \"https://en.wikipedia.org/wiki/A-weighting\",\n \"https://www.iso.org/standard/17426.html\",\n ]),\n });\n\n /** The display form, e.g. `69 dB(A)`. */\n readonly value: string;\n\n private constructor(\n private readonly decibelsValue: Decimal,\n /** The frequency weighting applied to this measurement. */\n readonly weighting: SoundWeighting,\n ) {\n this.value = `${decibelsValue.toTrimmedString()}${SUFFIXES[weighting]}`;\n }\n\n /** The numeric decibel value, e.g. `69`. */\n get decibels(): number {\n return this.decibelsValue.toNumber();\n }\n\n /** Creates a sound level from a value and weighting. */\n static create(value: number | Decimal, weighting: SoundWeighting): SoundLevel {\n return new SoundLevel(toDecimal(value), weighting);\n }\n\n /** Creates an unweighted sound level, e.g. `85 dB`. */\n static fromDecibels(value: number | Decimal): SoundLevel {\n return new SoundLevel(toDecimal(value), SoundWeighting.Unweighted);\n }\n\n /** Creates an A-weighted sound level, e.g. `69 dB(A)`. */\n static fromDecibelA(value: number | Decimal): SoundLevel {\n return new SoundLevel(toDecimal(value), SoundWeighting.A);\n }\n\n static tryParse(input: string | null | undefined): SoundLevel | null {\n if (input == null) return null;\n\n const trimmed = sanitizeInput(input).trim();\n if (trimmed.length === 0) return null;\n\n const match = UNIT_PATTERN.exec(trimmed);\n const number = match?.groups?.[\"num\"];\n if (number === undefined) return null;\n\n const parsed = tryParseNumberOnly(number);\n if (parsed === null) return null;\n\n const decibels = match?.groups?.[\"sign\"] === \"-\" ? parsed.negated() : parsed;\n const letter = match?.groups?.[\"w1\"] ?? match?.groups?.[\"w2\"];\n const weighting =\n letter === undefined\n ? SoundWeighting.Unweighted\n : (WEIGHTINGS[letter.toUpperCase()] ?? SoundWeighting.Unweighted);\n\n return new SoundLevel(decibels, weighting);\n }\n\n static parse(input: string): SoundLevel {\n const result = SoundLevel.tryParse(input);\n if (result === null) throw new Error(\"Invalid sound level.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return SoundLevel.tryParse(input) !== null;\n }\n\n /** Display form, e.g. `69 dB(A)`. */\n static format(\n input: string | null | undefined,\n options?: SoundLevelFormatOptions,\n ): string | null {\n const parsed = SoundLevel.tryParse(input);\n if (parsed !== null) return parsed.toString();\n return fallback(input, options);\n }\n\n /** Canonical form, e.g. `69 dB(A)`. */\n static normalize(\n input: string | null | undefined,\n options?: SoundLevelFormatOptions,\n ): string | null {\n const parsed = SoundLevel.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && SoundLevel.normalize(input) === input;\n }\n\n toNormalizedString(): string {\n return this.value;\n }\n\n toString(): string {\n return this.value;\n }\n\n compareTo(other: SoundLevel | null | undefined): number {\n if (other == null) return 1;\n return this.decibelsValue.compareTo(other.decibelsValue);\n }\n\n equals(other: SoundLevel | null | undefined): boolean {\n return (\n other != null &&\n this.decibelsValue.equals(other.decibelsValue) &&\n this.weighting === other.weighting\n );\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<SoundLevel>[] {\n if (!text) return [];\n\n const out: TextCandidate<SoundLevel>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = SoundLevel.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<SoundLevel>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"SoundLevel\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskSoundLevel(value),\n confidence: TextMatchConfidence.Low,\n value,\n }),\n );\n }\n\n return out;\n }\n}\n\n/** Returns a masked sound level, e.g. `69 dB(A)` becomes `*** dB(A)`. */\nexport function maskSoundLevel(soundLevel: SoundLevel): string {\n return `***${SUFFIXES[soundLevel.weighting]}`;\n}\n\nfunction toDecimal(value: number | Decimal): Decimal {\n if (value instanceof Decimal) return value;\n const parsed = Decimal.fromNumber(value);\n if (parsed === null) throw new Error(`Value cannot be represented as a sound level: ${value}`);\n return parsed;\n}\n\nfunction fallback(\n input: string | null | undefined,\n options?: SoundLevelFormatOptions,\n): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,eAAe;AACxB,SAAS,qBAAqB;AAC9B,SAAS,0BAA0B;AACnC,SAAS,qBAAqB;AAE9B,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AAK7B,MAAM,iBAAiB;AAAA;AAAA,EAE5B,YAAY;AAAA;AAAA,EAEZ,GAAG;AAAA;AAAA,EAEH,GAAG;AAAA;AAAA,EAEH,GAAG;AAAA;AAAA,EAEH,GAAG;AACL;AASA,MAAM,eACJ;AAEF,MAAM,eAAe,IAAI;AAAA,EACvB,OAAO,4FAA4F,aAAa;AAAA,EAChH;AACF;AAEA,MAAM,WAA2C;AAAA,EAC/C,CAAC,eAAe,UAAU,GAAG;AAAA,EAC7B,CAAC,eAAe,CAAC,GAAG;AAAA,EACpB,CAAC,eAAe,CAAC,GAAG;AAAA,EACpB,CAAC,eAAe,CAAC,GAAG;AAAA,EACpB,CAAC,eAAe,CAAC,GAAG;AACtB;AAEA,MAAM,aAA6C;AAAA,EACjD,GAAG,eAAe;AAAA,EAClB,GAAG,eAAe;AAAA,EAClB,GAAG,eAAe;AAAA,EAClB,GAAG,eAAe;AACpB;AASO,MAAM,WAAW;AAAA,EAcd,YACW,eAER,WACT;AAHiB;AAER;AAET,SAAK,QAAQ,GAAG,cAAc,gBAAgB,CAAC,GAAG,SAA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UAAI;AAAA,QACF,IAAI,cAA0B;AAAA,UAC5B,YAAY,MAAM;AAAA,UAClB,QAAQ,MAAM,CAAC,EAAE;AAAA,UACjB,cAAc,MAAM,CAAC;AAAA,UACrB,UAAU;AAAA,UACV,UAAU,sBAAsB;AAAA,UAChC,gBAAgB,MAAM,mBAAmB;AAAA,UACzC,eAAe,MAAM,SAAS;AAAA,UAC9B,YAAY,eAAe,KAAK;AAAA,UAChC,YAAY,oBAAoB;AAAA,UAChC;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;AAGO,SAAS,eAAe,YAAgC;AAC7D,SAAO,MAAM,SAAS,WAAW,SAAS,CAAC;AAC7C;AAEA,SAAS,UAAU,OAAkC;AACnD,MAAI,iBAAiB,QAAS,QAAO;AACrC,QAAM,SAAS,QAAQ,WAAW,KAAK;AACvC,MAAI,WAAW,KAAM,OAAM,IAAI,MAAM,iDAAiD,KAAK,EAAE;AAC7F,SAAO;AACT;AAEA,SAAS,SACP,OACA,SACe;AACf,MAAI,SAAS,sCAAsC,KAAM,QAAO;AAChE,MAAI,SAAS,KAAM,QAAO;AAC1B,QAAM,UAAU,MAAM,KAAK;AAC3B,SAAO,QAAQ,SAAS,IAAI,UAAU;AACxC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,7 +5,7 @@ import { TextCandidateCategory } from "../text-scanning/text-candidate-category.
|
|
|
5
5
|
import { TextMatchConfidence } from "../text-scanning/text-match-confidence.js";
|
|
6
6
|
import { SpeedUnit } from "./speed-unit.js";
|
|
7
7
|
const SCAN_PATTERN = new RegExp(
|
|
8
|
-
String.raw`(
|
|
8
|
+
String.raw`(?<![\p{L}\p{N}_])(?:[+-]\s*)?\d+[0-9 .,]*\s*(?:miles\s+per\s+hour|km/h|ft/s|m/s|kph|kmh|km/t|mph|knots?|knop|kt|kn)(?![\p{L}\p{N}_])`,
|
|
9
9
|
"giu"
|
|
10
10
|
);
|
|
11
11
|
class Speed extends Quantity {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/speed.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { SpeedUnit } from \"./speed-unit.js\";\n\n/** Options for {@link Speed.format} and {@link Speed.normalize}. */\nexport interface SpeedFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: SpeedUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { SpeedUnit } from \"./speed-unit.js\";\n\n/** Options for {@link Speed.format} and {@link Speed.normalize}. */\nexport interface SpeedFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: SpeedUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*(?:miles\\s+per\\s+hour|km/h|ft/s|m/s|kph|kmh|km/t|mph|knots?|knop|kt|kn)(?![\\p{L}\\p{N}_])`,\n \"giu\",\n);\n\n/**\n * A speed stored internally in meters per second,\n * parsed from any supported unit and convertible between all of them.\n */\nexport class Speed extends Quantity<SpeedUnit> {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Speed\",\n localizedName: \"Hastighet\",\n emoji: \"\uD83C\uDFCE\uFE0F\",\n sources: Object.freeze([\n \"https://www.bipm.org/en/measurement-units/si-base-units\",\n \"https://en.wikipedia.org/wiki/Conversion_of_units\",\n ]),\n });\n\n protected get units(): MeasurementUnitIndex<SpeedUnit> {\n return SpeedUnit.index;\n }\n\n protected withBaseValue(baseValue: Decimal, unit: SpeedUnit): this {\n return new Speed(baseValue, unit) as this;\n }\n\n get metersPerSecond(): number {\n return this.in(SpeedUnit.MetersPerSecond);\n }\n get kilometersPerHour(): number {\n return this.in(SpeedUnit.KilometersPerHour);\n }\n get milesPerHour(): number {\n return this.in(SpeedUnit.MilesPerHour);\n }\n get feetPerSecond(): number {\n return this.in(SpeedUnit.FeetPerSecond);\n }\n get knots(): number {\n return this.in(SpeedUnit.Knot);\n }\n\n /** Creates a speed from a value and unit. */\n static create(value: number | Decimal, unit: SpeedUnit): Speed {\n SpeedUnit.index.assertOwns(unit);\n const amount = value instanceof Decimal ? value : Speed.decimalOf(value);\n return new Speed(unit.toBase(amount), unit);\n }\n\n static fromMetersPerSecond(value: number | Decimal): Speed {\n return Speed.create(value, SpeedUnit.MetersPerSecond);\n }\n static fromKilometersPerHour(value: number | Decimal): Speed {\n return Speed.create(value, SpeedUnit.KilometersPerHour);\n }\n static fromMilesPerHour(value: number | Decimal): Speed {\n return Speed.create(value, SpeedUnit.MilesPerHour);\n }\n static fromFeetPerSecond(value: number | Decimal): Speed {\n return Speed.create(value, SpeedUnit.FeetPerSecond);\n }\n static fromKnots(value: number | Decimal): Speed {\n return Speed.create(value, SpeedUnit.Knot);\n }\n\n static tryParse(input: string | null | undefined): Speed | null {\n const parsed = tryParseQuantity(input, SpeedUnit.index);\n return parsed === null ? null : new Speed(parsed.baseValue, parsed.unit);\n }\n\n static parse(input: string): Speed {\n const result = Speed.tryParse(input);\n if (result === null) throw new Error(\"Invalid speed.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return Speed.tryParse(input) !== null;\n }\n\n /** Display form in the parsed unit, or `null` when the input is invalid. */\n static format(input: string | null | undefined, options?: SpeedFormatOptions): string | null {\n const parsed = Speed.tryParse(input);\n if (parsed !== null) {\n if (options?.unit !== undefined || options?.decimals !== undefined) {\n return parsed.toStringInUnit(options.unit ?? parsed.originalUnit, options.decimals);\n }\n return parsed.toString();\n }\n return Speed.fallback(input, options);\n }\n\n /** The value in meters per second, or `null` when the input is invalid. */\n static normalize(input: string | null | undefined, options?: SpeedFormatOptions): string | null {\n const parsed = Speed.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return Speed.fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && Speed.normalize(input) === input;\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<Speed>[] {\n if (!text) return [];\n\n const out: TextCandidate<Speed>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = Speed.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<Speed>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"Speed\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskSpeed(value),\n confidence: TextMatchConfidence.Medium,\n value,\n }),\n );\n }\n\n return out;\n }\n\n private static fallback(\n input: string | null | undefined,\n options?: SpeedFormatOptions,\n ): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n }\n\n private static decimalOf(value: number): Decimal {\n const parsed = Decimal.fromNumber(value);\n if (parsed === null) throw new Error(`Value cannot be represented as a speed: ${value}`);\n return parsed;\n }\n}\n\n/** Returns a masked speed, e.g. `10 m/s` becomes `*** m/s`. */\nexport function maskSpeed(value: Speed): string {\n return `*** ${value.originalUnit.symbol}`;\n}\n"],
|
|
5
5
|
"mappings": "AAAA,SAAS,eAAe;AAExB,SAAS,UAAU,wBAAwB;AAE3C,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AACpC,SAAS,iBAAiB;AAW1B,MAAM,eAAe,IAAI;AAAA,EACvB,OAAO;AAAA,EACP;AACF;AAMO,MAAM,cAAc,SAAoB;AAAA,EAC7C,OAAgB,WAA8B,OAAO,OAAO;AAAA,IAC1D,aAAa;AAAA,IACb,eAAe;AAAA,IACf,OAAO;AAAA,IACP,SAAS,OAAO,OAAO;AAAA,MACrB;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAAA,EAED,IAAc,QAAyC;AACrD,WAAO,UAAU;AAAA,EACnB;AAAA,EAEU,cAAc,WAAoB,MAAuB;AACjE,WAAO,IAAI,MAAM,WAAW,IAAI;AAAA,EAClC;AAAA,EAEA,IAAI,kBAA0B;AAC5B,WAAO,KAAK,GAAG,UAAU,eAAe;AAAA,EAC1C;AAAA,EACA,IAAI,oBAA4B;AAC9B,WAAO,KAAK,GAAG,UAAU,iBAAiB;AAAA,EAC5C;AAAA,EACA,IAAI,eAAuB;AACzB,WAAO,KAAK,GAAG,UAAU,YAAY;AAAA,EACvC;AAAA,EACA,IAAI,gBAAwB;AAC1B,WAAO,KAAK,GAAG,UAAU,aAAa;AAAA,EACxC;AAAA,EACA,IAAI,QAAgB;AAClB,WAAO,KAAK,GAAG,UAAU,IAAI;AAAA,EAC/B;AAAA;AAAA,EAGA,OAAO,OAAO,OAAyB,M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KAAK;AACvC,QAAI,WAAW,KAAM,OAAM,IAAI,MAAM,2CAA2C,KAAK,EAAE;AACvF,WAAO;AAAA,EACT;AACF;AAGO,SAAS,UAAU,OAAsB;AAC9C,SAAO,OAAO,MAAM,aAAa,MAAM;AACzC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -10,7 +10,7 @@ const CELSIUS_OFFSET = Decimal.parse("273.15");
|
|
|
10
10
|
const NINE = Decimal.parse("9");
|
|
11
11
|
const FIVE = Decimal.parse("5");
|
|
12
12
|
const THIRTY_TWO = Decimal.parse("32");
|
|
13
|
-
const SCAN_PATTERN = /(
|
|
13
|
+
const SCAN_PATTERN = /(?<![\p{L}\p{N}_])(?:[+-]\s*)?\d+[0-9 .,]*\s*(?:°[CF]|[CFK])(?![\p{L}\p{N}_])/giu;
|
|
14
14
|
class Temperature {
|
|
15
15
|
constructor(kelvinValue, originalUnit) {
|
|
16
16
|
this.kelvinValue = kelvinValue;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/temperature.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport { trySplitMeasurement } from \"../internal/measurement-unit-parser.js\";\nimport { formatQuantityDecimal } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { TemperatureDelta } from \"./temperature-delta.js\";\nimport { TemperatureUnit } from \"./temperature-unit.js\";\n\n/** Options for {@link Temperature.format} and {@link Temperature.normalize}. */\nexport interface TemperatureFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this scale instead of preserving the parsed one. */\n unit?: TemperatureUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst CELSIUS_OFFSET = Decimal.parse(\"273.15\")!;\nconst NINE = Decimal.parse(\"9\")!;\nconst FIVE = Decimal.parse(\"5\")!;\nconst THIRTY_TWO = Decimal.parse(\"32\")!;\n\nconst SCAN_PATTERN
|
|
5
|
-
"mappings": "AAAA,SAAS,eAAe;AACxB,SAAS,2BAA2B;AACpC,SAAS,6BAA6B;AAEtC,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AACjC,SAAS,uBAAuB;AAWhC,MAAM,iBAAiB,QAAQ,MAAM,QAAQ;AAC7C,MAAM,OAAO,QAAQ,MAAM,GAAG;AAC9B,MAAM,OAAO,QAAQ,MAAM,GAAG;AAC9B,MAAM,aAAa,QAAQ,MAAM,IAAI;AAErC,MAAM,
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport { trySplitMeasurement } from \"../internal/measurement-unit-parser.js\";\nimport { formatQuantityDecimal } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { TemperatureDelta } from \"./temperature-delta.js\";\nimport { TemperatureUnit } from \"./temperature-unit.js\";\n\n/** Options for {@link Temperature.format} and {@link Temperature.normalize}. */\nexport interface TemperatureFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this scale instead of preserving the parsed one. */\n unit?: TemperatureUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst CELSIUS_OFFSET = Decimal.parse(\"273.15\")!;\nconst NINE = Decimal.parse(\"9\")!;\nconst FIVE = Decimal.parse(\"5\")!;\nconst THIRTY_TWO = Decimal.parse(\"32\")!;\n\nconst SCAN_PATTERN =\n /(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*(?:\u00B0[CF]|[CFK])(?![\\p{L}\\p{N}_])/giu;\n\n/**\n * An absolute temperature stored internally in kelvin, parsed from Celsius,\n * Fahrenheit or kelvin.\n *\n * This is the one dimension that cannot share the machinery the other\n * measurement types use: its scales differ by an offset as well as a factor, so\n * conversion is affine rather than a single multiplication.\n *\n * Sources:\n * - https://www.bipm.org/en/measurement-units/si-base-units \u2014 kelvin definition\n * - https://en.wikipedia.org/wiki/Conversion_of_units\n */\nexport class Temperature {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Temperature\",\n localizedName: \"Temperatur\",\n emoji: \"\uD83C\uDF21\uFE0F\",\n sources: Object.freeze([\n \"https://www.bipm.org/en/measurement-units/si-base-units\",\n \"https://en.wikipedia.org/wiki/Conversion_of_units\",\n ]),\n });\n\n private constructor(\n private readonly kelvinValue: Decimal,\n /** The scale the value was originally parsed or created from. */\n readonly originalUnit: TemperatureUnit,\n ) {}\n\n /** The value in degrees Celsius, so `0` at the ice point. */\n get celsius(): number {\n return this.inDecimal(TemperatureUnit.Celsius).toNumber();\n }\n\n /** The value in degrees Fahrenheit, so `32` at 0 \u00B0C. */\n get fahrenheit(): number {\n return this.inDecimal(TemperatureUnit.Fahrenheit).toNumber();\n }\n\n /** The value in kelvin. */\n get kelvin(): number {\n return this.kelvinValue.toNumber();\n }\n\n /** The exact value converted to `unit`. */\n inDecimal(unit: TemperatureUnit): Decimal {\n TemperatureUnit.assertOwns(unit);\n if (unit === TemperatureUnit.Kelvin) return this.kelvinValue;\n\n const celsius = this.kelvinValue.minus(CELSIUS_OFFSET);\n if (unit === TemperatureUnit.Celsius) return celsius;\n if (unit === TemperatureUnit.Fahrenheit) {\n return celsius.times(NINE).dividedBy(FIVE).plus(THIRTY_TWO);\n }\n\n /* c8 ignore next */\n throw new RangeError(\"Unit belongs to another dimension.\");\n }\n\n /** The value converted to `unit`, as a `number` for ergonomic access. */\n in(unit: TemperatureUnit): number {\n return this.inDecimal(unit).toNumber();\n }\n\n static fromCelsius(value: number | Decimal): Temperature {\n return new Temperature(toDecimal(value).plus(CELSIUS_OFFSET), TemperatureUnit.Celsius);\n }\n\n static fromFahrenheit(value: number | Decimal): Temperature {\n const celsius = toDecimal(value).minus(THIRTY_TWO).times(FIVE).dividedBy(NINE);\n return new Temperature(celsius.plus(CELSIUS_OFFSET), TemperatureUnit.Fahrenheit);\n }\n\n static fromKelvin(value: number | Decimal): Temperature {\n return new Temperature(toDecimal(value), TemperatureUnit.Kelvin);\n }\n\n /** Creates a temperature from a value on the given scale. */\n static create(value: number | Decimal, unit: TemperatureUnit): Temperature {\n TemperatureUnit.assertOwns(unit);\n if (unit === TemperatureUnit.Celsius) return Temperature.fromCelsius(value);\n if (unit === TemperatureUnit.Fahrenheit) return Temperature.fromFahrenheit(value);\n if (unit === TemperatureUnit.Kelvin) return Temperature.fromKelvin(value);\n /* c8 ignore next */\n throw new RangeError(\"Unit belongs to another dimension.\");\n }\n\n static tryParse(input: string | null | undefined): Temperature | null {\n const split = trySplitMeasurement(input);\n if (split === null) return null;\n\n const unit = TemperatureUnit.tryParse(split.unitSuffix);\n if (unit === null) return null;\n\n try {\n return Temperature.create(split.value, unit);\n } catch {\n return null;\n }\n }\n\n static parse(input: string): Temperature {\n const result = Temperature.tryParse(input);\n if (result === null) throw new Error(\"Invalid temperature.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return Temperature.tryParse(input) !== null;\n }\n\n /** Display form on the parsed scale, or `null` when the input is invalid. */\n static format(\n input: string | null | undefined,\n options?: TemperatureFormatOptions,\n ): string | null {\n const parsed = Temperature.tryParse(input);\n if (parsed !== null) {\n if (options?.unit !== undefined || options?.decimals !== undefined) {\n return parsed.toStringInUnit(options.unit ?? parsed.originalUnit, options.decimals);\n }\n return parsed.toString();\n }\n return Temperature.fallback(input, options);\n }\n\n /** The value in kelvin, or `null` when the input is invalid. */\n static normalize(\n input: string | null | undefined,\n options?: TemperatureFormatOptions,\n ): string | null {\n const parsed = Temperature.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return Temperature.fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && Temperature.normalize(input) === input;\n }\n\n /** The value in kelvin, e.g. `293.15 K`. */\n toNormalizedString(): string {\n return `${formatQuantityDecimal(this.kelvinValue)} ${TemperatureUnit.Kelvin.symbol}`;\n }\n\n /** The value on the scale it was parsed from, e.g. `20 \u00B0C`. */\n toString(): string {\n return this.toStringInUnit(this.originalUnit);\n }\n\n /** The value converted to `unit`, e.g. `68 \u00B0F`. */\n toStringInUnit(unit: TemperatureUnit, decimals?: number): string {\n return `${formatQuantityDecimal(this.inDecimal(unit), decimals)} ${unit.symbol}`;\n }\n\n /** Adds a temperature difference, keeping the original scale. */\n plus(delta: TemperatureDelta): Temperature {\n return new Temperature(this.kelvinValue.plus(delta.exactKelvin), this.originalUnit);\n }\n\n /** Subtracts a temperature difference, keeping the original scale. */\n minus(delta: TemperatureDelta): Temperature {\n return new Temperature(this.kelvinValue.minus(delta.exactKelvin), this.originalUnit);\n }\n\n /** The difference between two temperatures. */\n difference(other: Temperature): TemperatureDelta {\n return TemperatureDelta.fromKelvin(this.kelvinValue.minus(other.kelvinValue));\n }\n\n compareTo(other: Temperature | null | undefined): number {\n if (other == null) return 1;\n return this.kelvinValue.compareTo(other.kelvinValue);\n }\n\n equals(other: Temperature | null | undefined): boolean {\n return other != null && this.kelvinValue.equals(other.kelvinValue);\n }\n\n isLessThan(other: Temperature): boolean {\n return this.compareTo(other) < 0;\n }\n\n isGreaterThan(other: Temperature): boolean {\n return this.compareTo(other) > 0;\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<Temperature>[] {\n if (!text) return [];\n\n const out: TextCandidate<Temperature>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = Temperature.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<Temperature>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"Temperature\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskTemperature(value),\n confidence: TextMatchConfidence.Medium,\n value,\n }),\n );\n }\n\n return out;\n }\n\n private static fallback(\n input: string | null | undefined,\n options?: TemperatureFormatOptions,\n ): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n }\n}\n\nfunction toDecimal(value: number | Decimal): Decimal {\n if (value instanceof Decimal) return value;\n const parsed = Decimal.fromNumber(value);\n if (parsed === null) throw new Error(`Value cannot be represented as a temperature: ${value}`);\n return parsed;\n}\n\n/** Returns a masked temperature, e.g. `20 \u00B0C` becomes `*** \u00B0C`. */\nexport function maskTemperature(value: Temperature): string {\n return `*** ${value.originalUnit.symbol}`;\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,eAAe;AACxB,SAAS,2BAA2B;AACpC,SAAS,6BAA6B;AAEtC,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AACjC,SAAS,uBAAuB;AAWhC,MAAM,iBAAiB,QAAQ,MAAM,QAAQ;AAC7C,MAAM,OAAO,QAAQ,MAAM,GAAG;AAC9B,MAAM,OAAO,QAAQ,MAAM,GAAG;AAC9B,MAAM,aAAa,QAAQ,MAAM,IAAI;AAErC,MAAM,eACJ;AAcK,MAAM,YAAY;AAAA,EAWf,YACW,aAER,cACT;AAHiB;AAER;AAAA,EACR;AAAA,EAHgB;AAAA,EAER;AAAA,EAbX,OAAgB,WAA8B,OAAO,OAAO;AAAA,IAC1D,aAAa;AAAA,IACb,eAAe;AAAA,IACf,OAAO;AAAA,IACP,SAAS,OAAO,OAAO;AAA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J,YAAQ,QAAQ,QAAQ,KAAK,IAAI,OAAO,MAAM;AAC5C,YAAM,QAAQ,YAAY,SAAS,MAAM,CAAC,CAAC;AAC3C,UAAI,UAAU,KAAM;AACpB,UAAI;AAAA,QACF,IAAI,cAA2B;AAAA,UAC7B,YAAY,MAAM;AAAA,UAClB,QAAQ,MAAM,CAAC,EAAE;AAAA,UACjB,cAAc,MAAM,CAAC;AAAA,UACrB,UAAU;AAAA,UACV,UAAU,sBAAsB;AAAA,UAChC,gBAAgB,MAAM,mBAAmB;AAAA,UACzC,eAAe,MAAM,SAAS;AAAA,UAC9B,YAAY,gBAAgB,KAAK;AAAA,UACjC,YAAY,oBAAoB;AAAA,UAChC;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,OAAe,SACb,OACA,SACe;AACf,QAAI,SAAS,sCAAsC,KAAM,QAAO;AAChE,QAAI,SAAS,KAAM,QAAO;AAC1B,UAAM,UAAU,MAAM,KAAK;AAC3B,WAAO,QAAQ,SAAS,IAAI,UAAU;AAAA,EACxC;AACF;AAEA,SAAS,UAAU,OAAkC;AACnD,MAAI,iBAAiB,QAAS,QAAO;AACrC,QAAM,SAAS,QAAQ,WAAW,KAAK;AACvC,MAAI,WAAW,KAAM,OAAM,IAAI,MAAM,iDAAiD,KAAK,EAAE;AAC7F,SAAO;AACT;AAGO,SAAS,gBAAgB,OAA4B;AAC1D,SAAO,OAAO,MAAM,aAAa,MAAM;AACzC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,7 +5,7 @@ import { TextCandidateCategory } from "../text-scanning/text-candidate-category.
|
|
|
5
5
|
import { TextMatchConfidence } from "../text-scanning/text-match-confidence.js";
|
|
6
6
|
import { TorqueUnit } from "./torque-unit.js";
|
|
7
7
|
const SCAN_PATTERN = new RegExp(
|
|
8
|
-
String.raw`(
|
|
8
|
+
String.raw`(?<![\p{L}\p{N}_])(?:[+-]\s*)?\d+[0-9 .,]*\s*(?:foot-pounds|foot-pound|inch-pounds|inch-pound|kilogramkraftmeter|kilonewton[\s-]?meters?|millinewton[\s-]?meters?|newtonmeter|fotpund|tumpund|kgf-m|kgf·m|kgfm|ft-lb|ft·lb|in-lb|in·lb|kN·m|kNm|mN·m|mNm|N·m|Nm)(?![\p{L}\p{N}_])`,
|
|
9
9
|
"giu"
|
|
10
10
|
);
|
|
11
11
|
class Torque extends Quantity {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/torque.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { TorqueUnit } from \"./torque-unit.js\";\n\n/** Options for {@link Torque.format} and {@link Torque.normalize}. */\nexport interface TorqueFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: TorqueUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { TorqueUnit } from \"./torque-unit.js\";\n\n/** Options for {@link Torque.format} and {@link Torque.normalize}. */\nexport interface TorqueFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: TorqueUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*(?:foot-pounds|foot-pound|inch-pounds|inch-pound|kilogramkraftmeter|kilonewton[\\s-]?meters?|millinewton[\\s-]?meters?|newtonmeter|fotpund|tumpund|kgf-m|kgf\u00B7m|kgfm|ft-lb|ft\u00B7lb|in-lb|in\u00B7lb|kN\u00B7m|kNm|mN\u00B7m|mNm|N\u00B7m|Nm)(?![\\p{L}\\p{N}_])`,\n \"giu\",\n);\n\n/**\n * A torque stored internally in newton meters,\n * parsed from any supported unit and convertible between all of them.\n */\nexport class Torque extends Quantity<TorqueUnit> {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Torque\",\n localizedName: \"Vridmoment\",\n emoji: \"\uD83D\uDD27\",\n sources: Object.freeze([\n \"https://www.bipm.org/en/measurement-units/si-derived-units\",\n \"https://en.wikipedia.org/wiki/Conversion_of_units\",\n ]),\n });\n\n protected get units(): MeasurementUnitIndex<TorqueUnit> {\n return TorqueUnit.index;\n }\n\n protected withBaseValue(baseValue: Decimal, unit: TorqueUnit): this {\n return new Torque(baseValue, unit) as this;\n }\n\n get millinewtonMeters(): number {\n return this.in(TorqueUnit.MillinewtonMeter);\n }\n get newtonMeters(): number {\n return this.in(TorqueUnit.NewtonMeter);\n }\n get kilonewtonMeters(): number {\n return this.in(TorqueUnit.KilonewtonMeter);\n }\n get footPounds(): number {\n return this.in(TorqueUnit.FootPound);\n }\n get kilogramForceMeters(): number {\n return this.in(TorqueUnit.KilogramForceMeter);\n }\n get inchPounds(): number {\n return this.in(TorqueUnit.InchPound);\n }\n\n /** Creates a torque from a value and unit. */\n static create(value: number | Decimal, unit: TorqueUnit): Torque {\n TorqueUnit.index.assertOwns(unit);\n const amount = value instanceof Decimal ? value : Torque.decimalOf(value);\n return new Torque(unit.toBase(amount), unit);\n }\n\n static fromMillinewtonMeters(value: number | Decimal): Torque {\n return Torque.create(value, TorqueUnit.MillinewtonMeter);\n }\n static fromNewtonMeters(value: number | Decimal): Torque {\n return Torque.create(value, TorqueUnit.NewtonMeter);\n }\n static fromKilonewtonMeters(value: number | Decimal): Torque {\n return Torque.create(value, TorqueUnit.KilonewtonMeter);\n }\n static fromFootPounds(value: number | Decimal): Torque {\n return Torque.create(value, TorqueUnit.FootPound);\n }\n static fromKilogramForceMeters(value: number | Decimal): Torque {\n return Torque.create(value, TorqueUnit.KilogramForceMeter);\n }\n static fromInchPounds(value: number | Decimal): Torque {\n return Torque.create(value, TorqueUnit.InchPound);\n }\n\n static tryParse(input: string | null | undefined): Torque | null {\n const parsed = tryParseQuantity(input, TorqueUnit.index);\n return parsed === null ? null : new Torque(parsed.baseValue, parsed.unit);\n }\n\n static parse(input: string): Torque {\n const result = Torque.tryParse(input);\n if (result === null) throw new Error(\"Invalid torque.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return Torque.tryParse(input) !== null;\n }\n\n /** Display form in the parsed unit, or `null` when the input is invalid. */\n static format(input: string | null | undefined, options?: TorqueFormatOptions): string | null {\n const parsed = Torque.tryParse(input);\n if (parsed !== null) {\n if (options?.unit !== undefined || options?.decimals !== undefined) {\n return parsed.toStringInUnit(options.unit ?? parsed.originalUnit, options.decimals);\n }\n return parsed.toString();\n }\n return Torque.fallback(input, options);\n }\n\n /** The value in newton meters, or `null` when the input is invalid. */\n static normalize(input: string | null | undefined, options?: TorqueFormatOptions): string | null {\n const parsed = Torque.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return Torque.fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && Torque.normalize(input) === input;\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<Torque>[] {\n if (!text) return [];\n\n const out: TextCandidate<Torque>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = Torque.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<Torque>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"Torque\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskTorque(value),\n confidence: TextMatchConfidence.Medium,\n value,\n }),\n );\n }\n\n return out;\n }\n\n private static fallback(\n input: string | null | undefined,\n options?: TorqueFormatOptions,\n ): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n }\n\n private static decimalOf(value: number): Decimal {\n const parsed = Decimal.fromNumber(value);\n if (parsed === null) throw new Error(`Value cannot be represented as a torque: ${value}`);\n return parsed;\n }\n}\n\n/** Returns a masked torque, e.g. `10 Nm` becomes `*** Nm`. */\nexport function maskTorque(value: Torque): string {\n return `*** ${value.originalUnit.symbol}`;\n}\n"],
|
|
5
5
|
"mappings": "AAAA,SAAS,eAAe;AAExB,SAAS,UAAU,wBAAwB;AAE3C,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AACpC,SAAS,kBAAkB;AAW3B,MAAM,eAAe,IAAI;AAAA,EACvB,OAAO;AAAA,EACP;AACF;AAMO,MAAM,eAAe,SAAqB;AAAA,EAC/C,OAAgB,WAA8B,OAAO,OAAO;AAAA,IAC1D,aAAa;AAAA,IACb,eAAe;AAAA,IACf,OAAO;AAAA,IACP,SAAS,OAAO,OAAO;AAAA,MACrB;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAAA,EAED,IAAc,QAA0C;AACtD,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YAAM,QAAQ,OAAO,SAAS,MAAM,CAAC,CAAC;AACtC,UAAI,UAAU,KAAM;AACpB,UAAI;AAAA,QACF,IAAI,cAAsB;AAAA,UACxB,YAAY,MAAM;AAAA,UAClB,QAAQ,MAAM,CAAC,EAAE;AAAA,UACjB,cAAc,MAAM,CAAC;AAAA,UACrB,UAAU;AAAA,UACV,UAAU,sBAAsB;AAAA,UAChC,gBAAgB,MAAM,mBAAmB;AAAA,UACzC,eAAe,MAAM,SAAS;AAAA,UAC9B,YAAY,WAAW,KAAK;AAAA,UAC5B,YAAY,oBAAoB;AAAA,UAChC;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,OAAe,SACb,OACA,SACe;AACf,QAAI,SAAS,sCAAsC,KAAM,QAAO;AAChE,QAAI,SAAS,KAAM,QAAO;AAC1B,UAAM,UAAU,MAAM,KAAK;AAC3B,WAAO,QAAQ,SAAS,IAAI,UAAU;AAAA,EACxC;AAAA,EAEA,OAAe,UAAU,OAAwB;AAC/C,UAAM,SAAS,QAAQ,WAAW,KAAK;AACvC,QAAI,WAAW,KAAM,OAAM,IAAI,MAAM,4CAA4C,KAAK,EAAE;AACxF,WAAO;AAAA,EACT;AACF;AAGO,SAAS,WAAW,OAAuB;AAChD,SAAO,OAAO,MAAM,aAAa,MAAM;AACzC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,7 +5,7 @@ import { TextCandidateCategory } from "../text-scanning/text-candidate-category.
|
|
|
5
5
|
import { TextMatchConfidence } from "../text-scanning/text-match-confidence.js";
|
|
6
6
|
import { VoltageUnit } from "./voltage-unit.js";
|
|
7
7
|
const SCAN_PATTERN = new RegExp(
|
|
8
|
-
String.raw`(
|
|
8
|
+
String.raw`(?<![\p{L}\p{N}_])(?:[+-]\s*)?\d+[0-9 .,]*\s*(?:µV|uV|mV|kV|MV|V)(?![\p{L}\p{N}_])`,
|
|
9
9
|
"giu"
|
|
10
10
|
);
|
|
11
11
|
class Voltage extends Quantity {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/voltage.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { VoltageUnit } from \"./voltage-unit.js\";\n\n/** Options for {@link Voltage.format} and {@link Voltage.normalize}. */\nexport interface VoltageFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: VoltageUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { VoltageUnit } from \"./voltage-unit.js\";\n\n/** Options for {@link Voltage.format} and {@link Voltage.normalize}. */\nexport interface VoltageFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: VoltageUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*(?:\u00B5V|uV|mV|kV|MV|V)(?![\\p{L}\\p{N}_])`,\n \"giu\",\n);\n\n/**\n * A voltage stored internally in volts,\n * parsed from any supported unit and convertible between all of them.\n */\nexport class Voltage extends Quantity<VoltageUnit> {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Voltage\",\n localizedName: \"Sp\u00E4nning\",\n emoji: \"\uD83D\uDD0C\",\n sources: Object.freeze([\n \"https://www.bipm.org/en/measurement-units/si-derived-units\",\n \"https://en.wikipedia.org/wiki/Volt\",\n ]),\n });\n\n protected get units(): MeasurementUnitIndex<VoltageUnit> {\n return VoltageUnit.index;\n }\n\n protected withBaseValue(baseValue: Decimal, unit: VoltageUnit): this {\n return new Voltage(baseValue, unit) as this;\n }\n\n get microvolts(): number {\n return this.in(VoltageUnit.Microvolt);\n }\n get millivolts(): number {\n return this.in(VoltageUnit.Millivolt);\n }\n get volts(): number {\n return this.in(VoltageUnit.Volt);\n }\n get kilovolts(): number {\n return this.in(VoltageUnit.Kilovolt);\n }\n get megavolts(): number {\n return this.in(VoltageUnit.Megavolt);\n }\n\n /** Creates a voltage from a value and unit. */\n static create(value: number | Decimal, unit: VoltageUnit): Voltage {\n VoltageUnit.index.assertOwns(unit);\n const amount = value instanceof Decimal ? value : Voltage.decimalOf(value);\n return new Voltage(unit.toBase(amount), unit);\n }\n\n static fromMicrovolts(value: number | Decimal): Voltage {\n return Voltage.create(value, VoltageUnit.Microvolt);\n }\n static fromMillivolts(value: number | Decimal): Voltage {\n return Voltage.create(value, VoltageUnit.Millivolt);\n }\n static fromVolts(value: number | Decimal): Voltage {\n return Voltage.create(value, VoltageUnit.Volt);\n }\n static fromKilovolts(value: number | Decimal): Voltage {\n return Voltage.create(value, VoltageUnit.Kilovolt);\n }\n static fromMegavolts(value: number | Decimal): Voltage {\n return Voltage.create(value, VoltageUnit.Megavolt);\n }\n\n static tryParse(input: string | null | undefined): Voltage | null {\n const parsed = tryParseQuantity(input, VoltageUnit.index, VoltageUnit.tryParse);\n return parsed === null ? null : new Voltage(parsed.baseValue, parsed.unit);\n }\n\n static parse(input: string): Voltage {\n const result = Voltage.tryParse(input);\n if (result === null) throw new Error(\"Invalid voltage.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return Voltage.tryParse(input) !== null;\n }\n\n /** Display form in the parsed unit, or `null` when the input is invalid. */\n static format(input: string | null | undefined, options?: VoltageFormatOptions): string | null {\n const parsed = Voltage.tryParse(input);\n if (parsed !== null) {\n if (options?.unit !== undefined || options?.decimals !== undefined) {\n return parsed.toStringInUnit(options.unit ?? parsed.originalUnit, options.decimals);\n }\n return parsed.toString();\n }\n return Voltage.fallback(input, options);\n }\n\n /** The value in volts, or `null` when the input is invalid. */\n static normalize(\n input: string | null | undefined,\n options?: VoltageFormatOptions,\n ): string | null {\n const parsed = Voltage.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return Voltage.fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && Voltage.normalize(input) === input;\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<Voltage>[] {\n if (!text) return [];\n\n const out: TextCandidate<Voltage>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = Voltage.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<Voltage>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"Voltage\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskVoltage(value),\n confidence: TextMatchConfidence.Medium,\n value,\n }),\n );\n }\n\n return out;\n }\n\n private static fallback(\n input: string | null | undefined,\n options?: VoltageFormatOptions,\n ): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n }\n\n private static decimalOf(value: number): Decimal {\n const parsed = Decimal.fromNumber(value);\n if (parsed === null) throw new Error(`Value cannot be represented as a voltage: ${value}`);\n return parsed;\n }\n}\n\n/** Returns a masked voltage, e.g. `10 V` becomes `*** V`. */\nexport function maskVoltage(value: Voltage): string {\n return `*** ${value.originalUnit.symbol}`;\n}\n"],
|
|
5
5
|
"mappings": "AAAA,SAAS,eAAe;AAExB,SAAS,UAAU,wBAAwB;AAE3C,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AACpC,SAAS,mBAAmB;AAW5B,MAAM,eAAe,IAAI;AAAA,EACvB,OAAO;AAAA,EACP;AACF;AAMO,MAAM,gBAAgB,SAAsB;AAAA,EACjD,OAAgB,WAA8B,OAAO,OAAO;AAAA,IAC1D,aAAa;AAAA,IACb,eAAe;AAAA,IACf,OAAO;AAAA,IACP,SAAS,OAAO,OAAO;AAAA,MACrB;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAAA,EAED,IAAc,QAA2C;AACvD,WAAO,YAAY;AAAA,EACrB;AAAA,EAEU,cAAc,WAAoB,MAAyB;AACnE,WAAO,IAAI,QAAQ,WAAW,IAAI;AAAA,EACpC;AAAA,EAEA,IAAI,aAAqB;AACvB,WAAO,KAAK,GAAG,YAAY,SAAS;AAAA,EACtC;AAAA,EACA,IAAI,aAAqB;AACvB,WAAO,KAAK,GAAG,YAAY,SAAS;AAAA,EACtC;AAAA,EACA,IAAI,QAAgB;AAClB,WAAO,KAAK,GAAG,YAAY,IAAI;AAAA,EACjC;AAAA,EACA,IAAI,YAAoB;AACtB,WAAO,KAAK,GAAG,YAAY,QAAQ;AAAA,EACrC;AAAA,EACA,IAAI,YAAoB;AACtB,WAAO,KAAK,GAAG,YAAY,QAAQ;AAAA,EACrC;AAAA;AAAA,EAGA,OAAO,OAAO,OAAyB,MAA4B;AACjE,gBAAY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;AACvC,QAAI,WAAW,KAAM,OAAM,IAAI,MAAM,6CAA6C,KAAK,EAAE;AACzF,WAAO;AAAA,EACT;AACF;AAGO,SAAS,YAAY,OAAwB;AAClD,SAAO,OAAO,MAAM,aAAa,MAAM;AACzC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,7 +5,7 @@ import { TextCandidateCategory } from "../text-scanning/text-candidate-category.
|
|
|
5
5
|
import { TextMatchConfidence } from "../text-scanning/text-match-confidence.js";
|
|
6
6
|
import { VolumeUnit } from "./volume-unit.js";
|
|
7
7
|
const SCAN_PATTERN = new RegExp(
|
|
8
|
-
String.raw`(
|
|
8
|
+
String.raw`(?<![\p{L}\p{N}_])(?:[+-]\s*)?\d+[0-9 .,]*\s*(?:kubikmeter|hektoliter|mikroliter|vätskeans|fluid\s+ounces?|fl\s*oz|gallons?|microliters?|milliliters?|centiliters?|deciliters?|hectoliters?|liters?|cubic\s+meters?|koppar|kopp|cups?|pints?|m³|m3|cbm|µL|µl|ul|ml|cl|dl|hL|hl|gal|pt|l)(?![\p{L}\p{N}_])`,
|
|
9
9
|
"giu"
|
|
10
10
|
);
|
|
11
11
|
class Volume extends Quantity {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/volume.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { VolumeUnit } from \"./volume-unit.js\";\n\n/** Options for {@link Volume.format} and {@link Volume.normalize}. */\nexport interface VolumeFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: VolumeUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { VolumeUnit } from \"./volume-unit.js\";\n\n/** Options for {@link Volume.format} and {@link Volume.normalize}. */\nexport interface VolumeFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: VolumeUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*(?:kubikmeter|hektoliter|mikroliter|v\u00E4tskeans|fluid\\s+ounces?|fl\\s*oz|gallons?|microliters?|milliliters?|centiliters?|deciliters?|hectoliters?|liters?|cubic\\s+meters?|koppar|kopp|cups?|pints?|m\u00B3|m3|cbm|\u00B5L|\u00B5l|ul|ml|cl|dl|hL|hl|gal|pt|l)(?![\\p{L}\\p{N}_])`,\n \"giu\",\n);\n\n/**\n * A volume stored internally in liters,\n * parsed from any supported unit and convertible between all of them.\n */\nexport class Volume extends Quantity<VolumeUnit> {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Volume\",\n localizedName: \"Volym\",\n emoji: \"\uD83E\uDDEA\",\n sources: Object.freeze([\n \"https://www.bipm.org/en/measurement-units/si-derived-units\",\n \"https://en.wikipedia.org/wiki/Conversion_of_units\",\n ]),\n });\n\n protected get units(): MeasurementUnitIndex<VolumeUnit> {\n return VolumeUnit.index;\n }\n\n protected withBaseValue(baseValue: Decimal, unit: VolumeUnit): this {\n return new Volume(baseValue, unit) as this;\n }\n\n get microliters(): number {\n return this.in(VolumeUnit.Microliter);\n }\n get milliliters(): number {\n return this.in(VolumeUnit.Milliliter);\n }\n get centiliters(): number {\n return this.in(VolumeUnit.Centiliter);\n }\n get deciliters(): number {\n return this.in(VolumeUnit.Deciliter);\n }\n get liters(): number {\n return this.in(VolumeUnit.Liter);\n }\n get hectoliters(): number {\n return this.in(VolumeUnit.Hectoliter);\n }\n get cubicMeters(): number {\n return this.in(VolumeUnit.CubicMeter);\n }\n get gallons(): number {\n return this.in(VolumeUnit.Gallon);\n }\n get pints(): number {\n return this.in(VolumeUnit.Pint);\n }\n get fluidOunces(): number {\n return this.in(VolumeUnit.FluidOunce);\n }\n get cups(): number {\n return this.in(VolumeUnit.Cup);\n }\n\n /** Creates a volume from a value and unit. */\n static create(value: number | Decimal, unit: VolumeUnit): Volume {\n VolumeUnit.index.assertOwns(unit);\n const amount = value instanceof Decimal ? value : Volume.decimalOf(value);\n return new Volume(unit.toBase(amount), unit);\n }\n\n static fromMicroliters(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Microliter);\n }\n static fromMilliliters(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Milliliter);\n }\n static fromCentiliters(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Centiliter);\n }\n static fromDeciliters(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Deciliter);\n }\n static fromLiters(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Liter);\n }\n static fromHectoliters(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Hectoliter);\n }\n static fromCubicMeters(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.CubicMeter);\n }\n static fromGallons(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Gallon);\n }\n static fromPints(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Pint);\n }\n static fromFluidOunces(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.FluidOunce);\n }\n static fromCups(value: number | Decimal): Volume {\n return Volume.create(value, VolumeUnit.Cup);\n }\n\n static tryParse(input: string | null | undefined): Volume | null {\n const parsed = tryParseQuantity(input, VolumeUnit.index);\n return parsed === null ? null : new Volume(parsed.baseValue, parsed.unit);\n }\n\n static parse(input: string): Volume {\n const result = Volume.tryParse(input);\n if (result === null) throw new Error(\"Invalid volume.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return Volume.tryParse(input) !== null;\n }\n\n /** Display form in the parsed unit, or `null` when the input is invalid. */\n static format(input: string | null | undefined, options?: VolumeFormatOptions): string | null {\n const parsed = Volume.tryParse(input);\n if (parsed !== null) {\n if (options?.unit !== undefined || options?.decimals !== undefined) {\n return parsed.toStringInUnit(options.unit ?? parsed.originalUnit, options.decimals);\n }\n return parsed.toString();\n }\n return Volume.fallback(input, options);\n }\n\n /** The value in liters, or `null` when the input is invalid. */\n static normalize(input: string | null | undefined, options?: VolumeFormatOptions): string | null {\n const parsed = Volume.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return Volume.fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && Volume.normalize(input) === input;\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<Volume>[] {\n if (!text) return [];\n\n const out: TextCandidate<Volume>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = Volume.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<Volume>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"Volume\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskVolume(value),\n confidence: TextMatchConfidence.Medium,\n value,\n }),\n );\n }\n\n return out;\n }\n\n private static fallback(\n input: string | null | undefined,\n options?: VolumeFormatOptions,\n ): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n }\n\n private static decimalOf(value: number): Decimal {\n const parsed = Decimal.fromNumber(value);\n if (parsed === null) throw new Error(`Value cannot be represented as a volume: ${value}`);\n return parsed;\n }\n}\n\n/** Returns a masked volume, e.g. `10 L` becomes `*** L`. */\nexport function maskVolume(value: Volume): string {\n return `*** ${value.originalUnit.symbol}`;\n}\n"],
|
|
5
5
|
"mappings": "AAAA,SAAS,eAAe;AAExB,SAAS,UAAU,wBAAwB;AAE3C,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AACpC,SAAS,kBAAkB;AAW3B,MAAM,eAAe,IAAI;AAAA,EACvB,OAAO;AAAA,EACP;AACF;AAMO,MAAM,eAAe,SAAqB;AAAA,EAC/C,OAAgB,WAA8B,OAAO,OAAO;AAAA,IAC1D,aAAa;AAAA,IACb,eAAe;AAAA,IACf,OAAO;AAAA,IACP,SAAS,OAAO,OAAO;AAAA,MACrB;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAAA,EAED,IAAc,QAA0C;AACtD,WAAO,WAAW;AAAA,EACpB;AAAA,EAEU,cAAc,WAAoB,MAAwB;AAClE,WAAO,IAAI,OAAO,WAAW,IAAI;AAAA,EACnC;AAAA,EAEA,IAAI,cAAsB;AACxB,WAAO,KAAK,GAAG,WAAW,UAAU;AAAA,EACtC;AAAA,EACA,IAAI,cAAsB;AACxB,WAAO,KAAK,GAAG,WAAW,UAAU;AAAA,EACtC;AAAA,EACA,IAAI,cAAsB;AACxB,WAAO,KAAK,GAAG,WAAW,UAAU;AAAA,EACtC;AAAA,EACA,IAAI,aAAqB;AACv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,OAAO,MAAM,aAAa,MAAM;AACzC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,7 +5,7 @@ import { TextCandidateCategory } from "../text-scanning/text-candidate-category.
|
|
|
5
5
|
import { TextMatchConfidence } from "../text-scanning/text-match-confidence.js";
|
|
6
6
|
import { WeightUnit } from "./weight-unit.js";
|
|
7
7
|
const SCAN_PATTERN = new RegExp(
|
|
8
|
-
String.raw`(
|
|
8
|
+
String.raw`(?<![\p{L}\p{N}_])(?:[+-]\s*)?\d+[0-9 .,]*\s*(?:metric\s+ton|microgram|milligram|hectogram|hektogram|kilogram|gram|tonne|ton|ounces|ounce|pounds|pound|stones|stone|hekto|lbs|lb|µg|ug|mg|hg|kg|g|oz|st|t|pund|uns)(?![\p{L}\p{N}_])`,
|
|
9
9
|
"giu"
|
|
10
10
|
);
|
|
11
11
|
class Weight extends Quantity {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/measurement/weight.ts"],
|
|
4
|
-
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { WeightUnit } from \"./weight-unit.js\";\n\n/** Options for {@link Weight.format} and {@link Weight.normalize}. */\nexport interface WeightFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: WeightUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(
|
|
4
|
+
"sourcesContent": ["import { Decimal } from \"../internal/decimal.js\";\nimport type { MeasurementUnitIndex } from \"../internal/measurement-unit.js\";\nimport { Quantity, tryParseQuantity } from \"../internal/quantity.js\";\nimport type { PrimitiveTypeInfo } from \"../primitive-type-info.js\";\nimport { TextCandidate } from \"../text-scanning/text-candidate.js\";\nimport { TextCandidateCategory } from \"../text-scanning/text-candidate-category.js\";\nimport { TextMatchConfidence } from \"../text-scanning/text-match-confidence.js\";\nimport { WeightUnit } from \"./weight-unit.js\";\n\n/** Options for {@link Weight.format} and {@link Weight.normalize}. */\nexport interface WeightFormatOptions {\n fallbackToTrimmedInputWhenInvalid?: boolean;\n /** Convert to this unit instead of preserving the parsed one. */\n unit?: WeightUnit;\n /** Round to this many fractional digits, away from zero. */\n decimals?: number;\n}\n\nconst SCAN_PATTERN = new RegExp(\n String.raw`(?<![\\p{L}\\p{N}_])(?:[+-]\\s*)?\\d+[0-9 .,]*\\s*(?:metric\\s+ton|microgram|milligram|hectogram|hektogram|kilogram|gram|tonne|ton|ounces|ounce|pounds|pound|stones|stone|hekto|lbs|lb|\u00B5g|ug|mg|hg|kg|g|oz|st|t|pund|uns)(?![\\p{L}\\p{N}_])`,\n \"giu\",\n);\n\n/**\n * A weight stored internally in kilograms,\n * parsed from any supported unit and convertible between all of them.\n */\nexport class Weight extends Quantity<WeightUnit> {\n static readonly typeInfo: PrimitiveTypeInfo = Object.freeze({\n englishName: \"Weight\",\n localizedName: \"Vikt\",\n emoji: \"\u2696\uFE0F\",\n sources: Object.freeze([\n \"https://www.bipm.org/en/measurement-units/si-base-units\",\n \"https://en.wikipedia.org/wiki/Conversion_of_units\",\n ]),\n });\n\n protected get units(): MeasurementUnitIndex<WeightUnit> {\n return WeightUnit.index;\n }\n\n protected withBaseValue(baseValue: Decimal, unit: WeightUnit): this {\n return new Weight(baseValue, unit) as this;\n }\n\n get micrograms(): number {\n return this.in(WeightUnit.Microgram);\n }\n get milligrams(): number {\n return this.in(WeightUnit.Milligram);\n }\n get grams(): number {\n return this.in(WeightUnit.Gram);\n }\n get hectograms(): number {\n return this.in(WeightUnit.Hectogram);\n }\n get kilograms(): number {\n return this.in(WeightUnit.Kilogram);\n }\n get metricTons(): number {\n return this.in(WeightUnit.MetricTon);\n }\n get pounds(): number {\n return this.in(WeightUnit.Pound);\n }\n get ounces(): number {\n return this.in(WeightUnit.Ounce);\n }\n get stones(): number {\n return this.in(WeightUnit.Stone);\n }\n\n /** Creates a weight from a value and unit. */\n static create(value: number | Decimal, unit: WeightUnit): Weight {\n WeightUnit.index.assertOwns(unit);\n const amount = value instanceof Decimal ? value : Weight.decimalOf(value);\n return new Weight(unit.toBase(amount), unit);\n }\n\n static fromMicrograms(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.Microgram);\n }\n static fromMilligrams(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.Milligram);\n }\n static fromGrams(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.Gram);\n }\n static fromHectograms(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.Hectogram);\n }\n static fromKilograms(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.Kilogram);\n }\n static fromMetricTons(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.MetricTon);\n }\n static fromPounds(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.Pound);\n }\n static fromOunces(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.Ounce);\n }\n static fromStones(value: number | Decimal): Weight {\n return Weight.create(value, WeightUnit.Stone);\n }\n\n static tryParse(input: string | null | undefined): Weight | null {\n const parsed = tryParseQuantity(input, WeightUnit.index);\n return parsed === null ? null : new Weight(parsed.baseValue, parsed.unit);\n }\n\n static parse(input: string): Weight {\n const result = Weight.tryParse(input);\n if (result === null) throw new Error(\"Invalid weight.\");\n return result;\n }\n\n static isValid(input: string | null | undefined): boolean {\n return Weight.tryParse(input) !== null;\n }\n\n /** Display form in the parsed unit, or `null` when the input is invalid. */\n static format(input: string | null | undefined, options?: WeightFormatOptions): string | null {\n const parsed = Weight.tryParse(input);\n if (parsed !== null) {\n if (options?.unit !== undefined || options?.decimals !== undefined) {\n return parsed.toStringInUnit(options.unit ?? parsed.originalUnit, options.decimals);\n }\n return parsed.toString();\n }\n return Weight.fallback(input, options);\n }\n\n /** The value in kilograms, or `null` when the input is invalid. */\n static normalize(input: string | null | undefined, options?: WeightFormatOptions): string | null {\n const parsed = Weight.tryParse(input);\n if (parsed !== null) return parsed.toNormalizedString();\n return Weight.fallback(input, options);\n }\n\n static isNormalized(input: string | null | undefined): boolean {\n return input != null && Weight.normalize(input) === input;\n }\n\n static findCandidatesInText(text: string): readonly TextCandidate<Weight>[] {\n if (!text) return [];\n\n const out: TextCandidate<Weight>[] = [];\n const pattern = new RegExp(SCAN_PATTERN.source, SCAN_PATTERN.flags);\n let match: RegExpExecArray | null;\n\n while ((match = pattern.exec(text)) !== null) {\n const value = Weight.tryParse(match[0]);\n if (value === null) continue;\n out.push(\n new TextCandidate<Weight>({\n startIndex: match.index,\n length: match[0].length,\n originalText: match[0],\n typeName: \"Weight\",\n category: TextCandidateCategory.Measurement,\n normalizedForm: value.toNormalizedString(),\n formattedForm: value.toString(),\n maskedForm: maskWeight(value),\n confidence: TextMatchConfidence.Medium,\n value,\n }),\n );\n }\n\n return out;\n }\n\n private static fallback(\n input: string | null | undefined,\n options?: WeightFormatOptions,\n ): string | null {\n if (options?.fallbackToTrimmedInputWhenInvalid !== true) return null;\n if (input == null) return null;\n const trimmed = input.trim();\n return trimmed.length > 0 ? trimmed : null;\n }\n\n private static decimalOf(value: number): Decimal {\n const parsed = Decimal.fromNumber(value);\n if (parsed === null) throw new Error(`Value cannot be represented as a weight: ${value}`);\n return parsed;\n }\n}\n\n/** Returns a masked weight, e.g. `10 kg` becomes `*** kg`. */\nexport function maskWeight(value: Weight): string {\n return `*** ${value.originalUnit.symbol}`;\n}\n"],
|
|
5
5
|
"mappings": "AAAA,SAAS,eAAe;AAExB,SAAS,UAAU,wBAAwB;AAE3C,SAAS,qBAA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