Skip to content

Commit

Permalink
Calculator field default can accept numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Oct 18, 2023
1 parent d0ddeda commit 674d3f5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/squiggle-lang/__tests__/reducer/frTypes_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
frDict,
frOptional,
frAny,
frNumberOrString,
} from "../../src/library/registry/frTypes.js";
import { ImmutableMap } from "../../src/utility/immutableMap.js";

Expand Down Expand Up @@ -80,6 +81,22 @@ describe("frDistOrNumber", () => {
});
});

describe("frNumberOrString", () => {
test("number", () => {
const number = 123;
const value = vNumber(number);
expect(frNumberOrString.unpack(value)).toBe(number);
expect(frNumberOrString.pack(number)).toEqual(value);
});

test("string", () => {
const string = "foo";
const value = vString(string);
expect(frNumberOrString.unpack(value)).toBe(string);
expect(frNumberOrString.pack(string)).toEqual(value);
});
});

describe("frDist", () => {
const dResult = Normal.make({ mean: 2, stdev: 5 });
if (!dResult.ok) {
Expand Down
15 changes: 13 additions & 2 deletions packages/squiggle-lang/src/fr/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
frArray,
frString,
frOptional,
frNumberOrString,
} from "../library/registry/frTypes.js";
import { FnFactory } from "../library/registry/helpers.js";
import { vCalculator } from "../value/index.js";
Expand All @@ -14,6 +15,16 @@ const maker = new FnFactory({
requiresNamespace: true,
});

const convertFieldDefault = (value: number | string | null): string => {
if (typeof value === "number") {
return value.toString();
} else if (typeof value === "string") {
return value;
} else {
return "";
}
};

export const library = [
maker.make({
name: "make",
Expand All @@ -30,7 +41,7 @@ export const library = [
frArray(
frDict(
["name", frString],
["default", frOptional(frString)],
["default", frOptional(frNumberOrString)],
["description", frOptional(frString)]
)
),
Expand All @@ -43,7 +54,7 @@ export const library = [
description: description || undefined,
fields: fields.map((vars) => ({
name: vars.name,
default: vars.default || "",
default: convertFieldDefault(vars.default),
description: vars.description || undefined,
})),
});
Expand Down
6 changes: 6 additions & 0 deletions packages/squiggle-lang/src/library/registry/frTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export const frDistOrNumber: FRType<BaseDist | number> = {
pack: (v) => (typeof v === "number" ? vNumber(v) : vDist(v)),
getName: () => "distribution|number",
};
export const frNumberOrString: FRType<string | number> = {
unpack: (v) =>
v.type === "String" ? v.value : v.type === "Number" ? v.value : undefined,
pack: (v) => (typeof v === "number" ? vNumber(v) : vString(v)),
getName: () => "number|string",
};
export const frDist: FRType<BaseDist> = {
unpack: (v) => (v.type === "Dist" ? v.value : undefined),
pack: (v) => vDist(v),
Expand Down
2 changes: 1 addition & 1 deletion packages/website/src/pages/docs/Api/Calculator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Calculator.make: ({
description: string,
fields: list<{
name: string,
default?: string,
default?: string | number,
description?: string
}>
}) => calculator
Expand Down

0 comments on commit 674d3f5

Please sign in to comment.