time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

scale.js (5099B)


      1 import { REArgumentError, REOther } from "../errors/messages.js";
      2 import { makeDefinition } from "../library/registry/fnDefinition.js";
      3 import { frDict, frNumber, frOptional, frString, } from "../library/registry/frTypes.js";
      4 import { FnFactory } from "../library/registry/helpers.js";
      5 import { vScale } from "../value/index.js";
      6 const maker = new FnFactory({
      7     nameSpace: "Scale",
      8     requiresNamespace: true,
      9 });
     10 const commonDict = frDict(["min", frOptional(frNumber)], ["max", frOptional(frNumber)], ["tickFormat", frOptional(frString)], ["title", frOptional(frString)]);
     11 function checkMinMax(min, max) {
     12     if (min !== null && max !== null && max <= min) {
     13         throw new REArgumentError(`Max must be greater than min, got: min=${min}, max=${max}`);
     14     }
     15 }
     16 const d3TickFormatRegex = /^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;
     17 function checkTickFormat(tickFormat) {
     18     if (tickFormat && !d3TickFormatRegex.test(tickFormat)) {
     19         throw new REArgumentError(`Tick format [${tickFormat}] is invalid.`);
     20     }
     21 }
     22 export const library = [
     23     maker.make({
     24         name: "linear",
     25         output: "Scale",
     26         examples: [`Scale.linear({ min: 3, max: 10 })`],
     27         definitions: [
     28             makeDefinition([commonDict], ([{ min, max, tickFormat, title }]) => {
     29                 checkMinMax(min, max);
     30                 checkTickFormat(tickFormat);
     31                 return vScale({
     32                     type: "linear",
     33                     min: min ?? undefined,
     34                     max: max ?? undefined,
     35                     tickFormat: tickFormat ?? undefined,
     36                     title: title ?? undefined,
     37                 });
     38             }),
     39             makeDefinition([], () => {
     40                 return vScale({ type: "linear" });
     41             }),
     42         ],
     43     }),
     44     maker.make({
     45         name: "log",
     46         output: "Scale",
     47         examples: [`Scale.log({ min: 1, max: 100 })`],
     48         definitions: [
     49             makeDefinition([commonDict], ([{ min, max, tickFormat, title }]) => {
     50                 if (min !== null && min <= 0) {
     51                     throw new REOther(`Min must be over 0 for log scale, got: ${min}`);
     52                 }
     53                 checkMinMax(min, max);
     54                 checkTickFormat(tickFormat);
     55                 return vScale({
     56                     type: "log",
     57                     min: min ?? undefined,
     58                     max: max ?? undefined,
     59                     tickFormat: tickFormat ?? undefined,
     60                     title: title ?? undefined,
     61                 });
     62             }),
     63             makeDefinition([], () => {
     64                 return vScale({ type: "log" });
     65             }),
     66         ],
     67     }),
     68     maker.make({
     69         name: "symlog",
     70         output: "Scale",
     71         examples: [`Scale.symlog({ min: -10, max: 10 })`],
     72         definitions: [
     73             makeDefinition([
     74                 frDict(["min", frOptional(frNumber)], ["max", frOptional(frNumber)], ["tickFormat", frOptional(frString)], ["title", frOptional(frString)], ["constant", frOptional(frNumber)]),
     75             ], ([{ min, max, tickFormat, title, constant }]) => {
     76                 checkMinMax(min, max);
     77                 checkTickFormat(tickFormat);
     78                 if (constant !== null && constant === 0) {
     79                     throw new REOther(`Symlog scale constant cannot be 0.`);
     80                 }
     81                 return vScale({
     82                     type: "symlog",
     83                     min: min ?? undefined,
     84                     max: max ?? undefined,
     85                     tickFormat: tickFormat ?? undefined,
     86                     constant: constant ?? undefined,
     87                     title: title ?? undefined,
     88                 });
     89             }),
     90             makeDefinition([], () => {
     91                 return vScale({
     92                     type: "symlog",
     93                 });
     94             }),
     95         ],
     96     }),
     97     maker.make({
     98         name: "power",
     99         output: "Scale",
    100         examples: [`Scale.power({ min: 1, max: 100, exponent: 0.1 })`],
    101         definitions: [
    102             makeDefinition([
    103                 frDict(["min", frOptional(frNumber)], ["max", frOptional(frNumber)], ["tickFormat", frOptional(frString)], ["title", frOptional(frString)], ["exponent", frOptional(frNumber)]),
    104             ], ([{ min, max, tickFormat, title, exponent }]) => {
    105                 checkMinMax(min, max);
    106                 checkTickFormat(tickFormat);
    107                 if (exponent !== null && exponent <= 0) {
    108                     throw new REOther(`Power Scale exponent must be over 0.`);
    109                 }
    110                 return vScale({
    111                     type: "power",
    112                     min: min ?? undefined,
    113                     max: max ?? undefined,
    114                     tickFormat: tickFormat ?? undefined,
    115                     exponent: exponent ?? undefined,
    116                     title: title ?? undefined,
    117                 });
    118             }),
    119             makeDefinition([], () => {
    120                 return vScale({
    121                     type: "power",
    122                 });
    123             }),
    124         ],
    125     }),
    126 ];
    127 //# sourceMappingURL=scale.js.map