← Registry lesson
Lab

Registry

Sign in to run your code

Read the brief and the starter here. With a free account you can edit it, run the sample, step through it and check your work, and your progress is kept.

type Formula = (args: number[]) => number;
// An add-in the user enabled, and the functions it brings: [name, formula].
type AddIn = { id: string; functions: [string, Formula][] };

// The built-in functions every workbook has.
const sum: Formula = (args) => args.reduce((total, n) => total + n, 0);
const maxOf: Formula = (args) => Math.max(...args);

// A workbook: it loads the add-ins the user enabled and evaluates formulas.
class Workbook {
  // What setup refused, as "add-in: reason". Nothing is refused yet.
  refused: string[] = [];

  constructor(addIns: AddIn[]) {
    // The add-ins are loaded, but nothing records their functions.
  }

  // The formula engine: one branch per function it knows.
  evaluate(name: string, args: number[]): string {
    switch (name) {
      case 'SUM':
        return String(sum(args));
      case 'MAX':
        return String(maxOf(args));
      default:
        return '#NAME?';
    }
  }

  // The function picker's list.
  functionNames(): string[] {
    return ['MAX', 'SUM'];
  }
}