Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async findUnit(unitLabel: string, unitFamily?: string): Promise {
for (const entry of unitData) {
if (unitFamily) {
if (entry.unitFamily !== unitFamily)
continue;
}
if (entry.displayLabel === unitLabel || entry.name === unitLabel) {
const unitProps = new BasicUnit(entry.name, entry.displayLabel, entry.unitFamily, entry.altDisplayLabels);
return Promise.resolve(unitProps);
}
if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {
if (entry.altDisplayLabels.findIndex((ref) => ref === unitLabel) !== -1) {
const unitProps = new BasicUnit(entry.name, entry.displayLabel, entry.unitFamily, entry.altDisplayLabels);
return Promise.resolve(unitProps);
}
}
}
return Promise.resolve(new BadUnit());
}
public async getUnitsByFamily(unitFamily: string): Promise {
const units: UnitProps[] = [];
for (const entry of unitData) {
if (entry.unitFamily !== unitFamily)
continue;
units.push(new BasicUnit(entry.name, entry.displayLabel, entry.unitFamily, entry.altDisplayLabels));
}
return Promise.resolve(units);
}
public async findUnitByName(unitName: string): Promise {
const unitDataEntry = this.findUnitDefinition(unitName);
if (unitDataEntry) {
return Promise.resolve(new BasicUnit(unitDataEntry.name, unitDataEntry.displayLabel, unitDataEntry.unitFamily, unitDataEntry.altDisplayLabels));
}
return Promise.resolve(new BadUnit());
}
continue;
}
if (entry.displayLabel === unitLabel || entry.name === unitLabel) {
const unitProps = new BasicUnit(entry.name, entry.displayLabel, entry.unitFamily, entry.altDisplayLabels);
return Promise.resolve(unitProps);
}
if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {
if (entry.altDisplayLabels.findIndex((ref) => ref === unitLabel) !== -1) {
const unitProps = new BasicUnit(entry.name, entry.displayLabel, entry.unitFamily, entry.altDisplayLabels);
return Promise.resolve(unitProps);
}
}
}
return Promise.resolve(new BadUnit());
}
public async findUnitByName(unitName: string): Promise {
const unitDataEntry = this.findUnitDefinition(unitName);
if (unitDataEntry) {
return Promise.resolve(new BasicUnit(unitDataEntry.name, unitDataEntry.displayLabel, unitDataEntry.unitFamily, unitDataEntry.altDisplayLabels));
}
return Promise.resolve(new BadUnit());
}
protected async loadFormatSpecsForQuantityTypes(useImperial: boolean): Promise {
const typeArray: QuantityType[] = [QuantityType.Length, QuantityType.Angle, QuantityType.Area, QuantityType.Volume, QuantityType.LatLong, QuantityType.Coordinate, QuantityType.Stationing, QuantityType.LengthSurvey, QuantityType.LengthEngineering];
const activeMap = useImperial ? this._imperialFormatSpecsByType : this._metricFormatSpecsByType;
activeMap.clear();
for (const quantityType of typeArray) {
const formatPromise = this.getFormatByQuantityType(quantityType, useImperial);
const unitPromise = this.getUnitByQuantityType(quantityType);
const [format, unit] = await Promise.all([formatPromise, unitPromise]);
const spec = await FormatterSpec.create(format.name, format, this, unit);
activeMap.set(quantityType, spec);
}
return Promise.resolve();
}
protected async loadParsingSpecsForQuantityTypes(useImperial: boolean): Promise {
const typeArray: QuantityType[] = [QuantityType.Length, QuantityType.Angle, QuantityType.Area, QuantityType.Volume, QuantityType.LatLong, QuantityType.Coordinate, QuantityType.Stationing, QuantityType.LengthSurvey, QuantityType.LengthEngineering];
const activeMap = useImperial ? this._imperialParserSpecsByType : this._metricUnitParserSpecsByType;
activeMap.clear();
for (const quantityType of typeArray) {
const formatPromise = this.getFormatByQuantityType(quantityType, useImperial);
const unitPromise = this.getUnitByQuantityType(quantityType);
const [format, outUnit] = await Promise.all([formatPromise, unitPromise]);
const parserSpec = await ParserSpec.create(format, this, outUnit);
activeMap.set(quantityType, parserSpec);
}
return Promise.resolve();
}
protected async loadStdFormat(type: QuantityType, imperial: boolean): Promise {
let formatData: any;
const formatArray = imperial ? defaultsFormats.imperial : defaultsFormats.metric;
for (const entry of formatArray) {
if (entry.type === type as number) {
formatData = entry.format;
const format = new Format("stdFormat");
await format.fromJson(this, formatData);
return Promise.resolve(format);
}
}
throw new BentleyError(BentleyStatus.ERROR, "IModelApp must define a formatsProvider class to provide formats for tools");
}
public formatQuantity(magnitude: number, formatSpec: FormatterSpec): string {
return Formatter.formatQuantity(magnitude, formatSpec);
}
public parseIntoQuantityValue(inString: string, parserSpec: ParserSpec): ParseResult {
return Parser.parseQuantityString(inString, parserSpec);
}