vcpkg-tool/vcpkg-artifacts/util/percentage-scaler.ts

36 строки
1.3 KiB
TypeScript
Исходник Обычный вид История

Move vcpkg-ce development into the vcpkg-tool repo. (#428) * Move vcpkg-ce development into the vcpkg-tool repo. This change obsoletes https://github.com/microsoft/vcpkg-ce , which will be archived shortly after this merges. ce development did not maintain a linear history but vcpkg-tool does maintain a linear history, so I have not attempted to preserve "blame". The following files were not copied over: * `.git/**/*` * `.github/**/*` (workflows changes merged into pipelines.yaml) * `.scripts/verify-pr.yaml` * `.scripts/signing/**/*` * `LICENSE.md` * `test/vcpkg-ce.test.build.log` * `azure-pipelines.yml` The following files were modified: * `.vscode/**/*` was placed in the root, had paths rewritten to target the "ce" subdirectory, and a few tasks renamed to indicate that they target the "ce" subset. * `CODE_OF_CONDUCT.md`/`SECURITY.md`/`SUPPORT.md`/`PolicheckExclusion.xml` were placed in the root. * `.gitattributes` was merged with the existing one in the root. * `.gitignore` had a few things explicitly copied into the root but most were discarded. * `readme.md` had some content merged. * Add missing gitignores * Remove broken fast-xml-parser reference. * Fix some more vcpkg-ce names. * Fix some repo docs. * Add back overzealous vcpkg- removal for vcpkg-init. * Delete support.md as it's irrelevant in the tool repo. * Replay https://github.com/microsoft/vcpkg-ce/pull/22/ * Remove big test assets. * Update ce/getting-started.md Co-authored-by: Robert Schumacher <roschuma@microsoft.com> * Delete 'docs' folder. Co-authored-by: Robert Schumacher <roschuma@microsoft.com>
2022-03-23 03:07:57 +03:00
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { strict } from 'assert';
export class PercentageScaler {
private readonly scaledDomainMax : number;
private readonly scaledPercentMax : number;
private static clamp(test: number, min: number, max:number) : number {
if (test < min) { return min; }
if (test > max) { return max; }
return test;
}
constructor(public readonly lowestDomain: number, public readonly highestDomain: number,
public readonly lowestPercentage = 0, public readonly highestPercentage = 100) {
strict.ok(lowestDomain <= highestDomain);
strict.ok(lowestPercentage <= highestPercentage);
this.scaledDomainMax = highestDomain - lowestDomain;
this.scaledPercentMax = highestPercentage - lowestPercentage;
}
scalePosition(domain: number) : number {
if (this.scaledDomainMax === 0 || this.scaledPercentMax === 0) {
return this.highestPercentage;
}
const domainClamped = PercentageScaler.clamp(domain, this.lowestDomain, this.highestDomain);
const domainScaled = domainClamped - this.lowestDomain;
const domainProportion = domainScaled / this.scaledDomainMax;
const partialPercent = this.scaledPercentMax * domainProportion;
const percentage = this.lowestPercentage + partialPercent;
return Math.round(percentage * 10) / 10;
}
}