A class to help with specific use-cases where you are using a percentage (ex. 1 / n) but don't know n until runtime.

const autoPercentage = new AutoPercentage();

const steps = [
{ name: 'init', progress: autoPercentage.percentage() },
{ name: 'modify', progress: autoPercentage.percentage() },
{ name: 'verify', progress: autoPercentage.percentage() },
{ name: 'cleanup', progress: 0 }
];

let progress = 0;

for (const step of steps) {
if (progress === 1) {
return;
}

progress += step.progress;

console.log(`${step.name} finished - job is ${Math.round(progress * 100)}% complete`);
}

Constructors

Methods

Constructors

Methods

  • Increment and get the number of times the percentage has been incremented.

    Time complexity: O(1)

    Space complexity: O(1)

    Returns {
        [toPrimitive](): number;
    }

    The number of times this percentage has been incremented

    const autoPercentage = new AutoPercentage();

    const a = autoPercentage.count();

    Number(a); // 1

    const b = autoPercentage.count();

    Number(a); // 2
    Number(b); // 2
  • Increment and get the percentage.

    Time complexity: O(1)

    Space complexity: O(1)

    Returns {
        [toPrimitive](): number;
    }

    The percentage

    const autoPercentage = new AutoPercentage();

    const a = autoPercentage.percentage();

    Number(a); // 1

    const b = autoPercentage.percentage();

    Number(a); // 0.5
    Number(b); // 0.5