Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculator field default can accept numbers #2342

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading