Skip to content

Commit

Permalink
Moved JSON methods to Danger
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Dec 30, 2023
1 parent cbb90e4 commit 47cbe08
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { testEvalToBe } from "../helpers/reducerHelpers.js";

describe("JSON", () => {
testEvalToBe("JSON(1)", "1");
testEvalToBe("JSON([1,2,3])", "[1,2,3]");
testEvalToBe("JSON({foo: 'bar'})", '{foo: "bar"}');
testEvalToBe("Danger.json(1)", "1");
testEvalToBe("Danger.json([1,2,3])", "[1,2,3]");
testEvalToBe("Danger.json({foo: 'bar'})", '{foo: "bar"}');
});
9 changes: 4 additions & 5 deletions packages/squiggle-lang/__tests__/public/SqValue_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OrderedMap } from "immutable";

import { run, sq } from "../../src/index.js";
import { SqSampleSetDistribution } from "../../src/public/SqValue/SqDistribution/index.js";
import { testRun } from "../helpers/helpers.js";

describe("SqValue.asJS", () => {
Expand All @@ -8,7 +9,7 @@ describe("SqValue.asJS", () => {
await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }')
).asJS();

expect(value).toBeInstanceOf(Map);
expect(value).toBeInstanceOf(OrderedMap);
});

test("Dict fields", async () => {
Expand All @@ -22,9 +23,7 @@ describe("SqValue.asJS", () => {
await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }')
).asJS();

expect((value as any).get("y")[2].get("dist")).toBeInstanceOf(
SqSampleSetDistribution
);
expect((value as any).get("y")[2].get("dist")).toBeInstanceOf(Array);
});
});

Expand Down
9 changes: 0 additions & 9 deletions packages/squiggle-lang/src/fr/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
makeNumericComparisons,
} from "../library/registry/helpers.js";
import { isEqual } from "../value/index.js";
import { fromValue, toValue } from "../value/simpleValue.js";

const maker = new FnFactory({
nameSpace: "", // no namespaced versions
Expand Down Expand Up @@ -100,14 +99,6 @@ export const library = [
}),
],
}),
maker.make({
name: "JSON",
definitions: [
makeDefinition([frAny()], frAny(), ([v]) => {
return toValue(fromValue(v));
}),
],
}),
maker.make({
name: "inspect",
definitions: [
Expand Down
23 changes: 23 additions & 0 deletions packages/squiggle-lang/src/fr/danger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
frDistPointset,
frLambda,
frNumber,
frString,
} from "../library/registry/frTypes.js";
import {
FnFactory,
Expand All @@ -32,6 +33,12 @@ import { ReducerContext } from "../reducer/context.js";
import { Lambda } from "../reducer/lambda.js";
import * as E_A from "../utility/E_A.js";
import { vArray, vNumber } from "../value/index.js";
import {
fromValue,
removeLambdas,
toJson,
toValue,
} from "../value/simpleValue.js";

const { factorial } = jstat;

Expand Down Expand Up @@ -451,6 +458,22 @@ const mapYLibrary: FRFunction[] = [
),
],
}),
maker.make({
name: "json",
definitions: [
makeDefinition([frAny()], frAny(), ([v]) => {
return toValue(fromValue(v));
}),
],
}),
maker.make({
name: "jsonString",
definitions: [
makeDefinition([frAny()], frString, ([v]) => {
return JSON.stringify(toJson(removeLambdas(fromValue(v))));
}),
],
}),
];

export const library = [
Expand Down
2 changes: 1 addition & 1 deletion packages/squiggle-lang/src/value/simpleValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type SimpleValueWithoutLambda =
| string
| null;

function removeLambdas(value: SimpleValue): SimpleValueWithoutLambda {
export function removeLambdas(value: SimpleValue): SimpleValueWithoutLambda {
if (value instanceof BaseLambda) {
return ImmutableMap([
["vType", "Lambda"],
Expand Down

0 comments on commit 47cbe08

Please sign in to comment.