74 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-07-07 17:27:59 +03:00
///
2025-02-25 09:39:16 +02:00
/// Copyright © 2016-2025 The Thingsboard Authors
2023-07-07 17:27:59 +03:00
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { AllMeasures } from '@core/services/unit/definitions/all';
export enum UnitsType {
capacity = 'capacity'
}
export type TbUnitConvertor = (value: number) => number;
2025-04-28 18:27:35 +03:00
export type UnitDescriptionGroupByMeasure<TMeasure extends string> = Partial<Record<TMeasure, UnitDescription[]>>;
export interface UnitDescription {
abbr: string;
measure: AllMeasures;
system: UnitSystem;
name: string;
tags: string[];
}
export enum UnitSystem {
METRIC = 'METRIC',
IMPERIAL = 'IMPERIAL',
HYBRID = 'HYBRID'
}
export const UnitSystems = Object.values(UnitSystem);
2023-07-07 17:27:59 +03:00
export interface Unit {
name: string;
tags: string[];
to_anchor: number;
anchor_shift?: number;
2023-07-07 17:27:59 +03:00
}
export type TbUnit = string | TbUnitMapping;
export interface TbUnitMapping {
from: string;
METRIC: string;
IMPERIAL: string;
HYBRID: string;
2023-09-12 15:07:33 +03:00
}
export type TbMeasure<TUnits extends string> = Partial<Record<UnitSystem, TbMeasureUnits<TUnits>>>;
export interface TbMeasureUnits<TUnits extends string> {
ratio?: number;
transform?: (value: number) => number;
units?: Partial<Record<TUnits, Unit>>;
}
2023-07-07 17:27:59 +03:00
const searchUnitTags = (unit: UnitDescription, searchText: string): boolean =>
2025-04-28 18:27:35 +03:00
!!unit.tags.find(t => t.toUpperCase().includes(searchText));
2023-07-07 17:27:59 +03:00
export const searchUnits = (_units: Array<UnitDescription>, searchText: string): Array<UnitDescription> => _units.filter(
2025-04-28 18:27:35 +03:00
u => u.abbr.toUpperCase().includes(searchText) ||
u.name.toUpperCase().includes(searchText) ||
2023-07-07 17:27:59 +03:00
searchUnitTags(u, searchText)
);