-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate PerseusItem.answerArea, removing unused fields (#1895)
`answerArea` sometimes includes `options` and `type` fields. `type` always seems to be set to `multiple` if it's present. I couldn't find evidence in Perseus or webapp that this data is used anywhere, so I am removing it. This is necessary to make parsePerseusItem return a valid PerseusItem while still being able to handle existing data that has the extra fields. Issue: https://khanacademy.atlassian.net/browse/LEMS-2582 ## Test plan: `yarn test` Author: benchristel Reviewers: jeremywiebe, catandthemachines, benchristel, anakaren-rojas, nishasy Required Reviewers: Approved By: jeremywiebe Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1895
- Loading branch information
1 parent
b7cf1c6
commit 841551a
Showing
13 changed files
with
408 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": patch | ||
--- | ||
|
||
Internal: remove unused fields from `answerArea` when parsing `PerseusItem`s. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/perseus/src/util/parse-perseus-json/general-purpose-parsers/pipe-parsers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {success} from "../result"; | ||
|
||
import {pipeParsers} from "./pipe-parsers"; | ||
import {string} from "./string"; | ||
import {anyFailure, ctx} from "./test-helpers"; | ||
|
||
import type {PartialParser} from "../parser-types"; | ||
|
||
describe("pipeParsers given a single parser", () => { | ||
const string2 = pipeParsers(string).parser; | ||
it("accepts a valid value", () => { | ||
expect(string2("abc", ctx())).toEqual(success("abc")); | ||
}); | ||
|
||
it("rejects an invalid value", () => { | ||
expect(string2(99, ctx())).toEqual(anyFailure); | ||
}); | ||
}); | ||
|
||
describe("pipeParsers given a chain of parsers", () => { | ||
const stringToNumber: PartialParser<string, number> = (rawVal, ctx) => { | ||
if (/^\d+$/.test(rawVal)) { | ||
return ctx.success(parseInt(rawVal, 10)); | ||
} | ||
return ctx.failure("a numeric string", rawVal); | ||
}; | ||
|
||
const numericString = pipeParsers(string).then(stringToNumber).parser; | ||
|
||
it("accepts a valid value", () => { | ||
expect(numericString("7", ctx())).toEqual(success(7)); | ||
}); | ||
|
||
it("rejects a value that fails the first parser", () => { | ||
expect(numericString(99, ctx())).toEqual(anyFailure); | ||
}); | ||
|
||
it("rejects a value that fails the second parser", () => { | ||
expect(numericString("abc", ctx())).toEqual(anyFailure); | ||
}); | ||
}); |
18 changes: 15 additions & 3 deletions
18
...eneral-purpose-parsers/compose-parsers.ts → ...n/general-purpose-parsers/pipe-parsers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ages/perseus/src/util/parse-perseus-json/general-purpose-parsers/pipe-parsers.typetest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Test: pipeParsers()...then().parser returns the expected type | ||
|
||
import {pipeParsers} from "./pipe-parsers"; | ||
import {string} from "./string"; | ||
|
||
import type {Parser, PartialParser} from "../parser-types"; | ||
|
||
const stringToNumber = summon<PartialParser<string, number>>(); | ||
const numberToBoolean = summon<PartialParser<number, boolean>>(); | ||
|
||
{ | ||
pipeParsers(string).then(stringToNumber).then(numberToBoolean) | ||
.parser satisfies Parser<boolean>; | ||
} | ||
|
||
{ | ||
// @ts-expect-error - partial parser types don't match | ||
pipeParsers(string).then(stringToNumber).then(stringToNumber).parser; | ||
} | ||
|
||
{ | ||
const p = pipeParsers(string) | ||
.then(stringToNumber) | ||
.then(numberToBoolean).parser; | ||
// @ts-expect-error - return value is not assignable to Parser<string> | ||
p satisfies Parser<string>; | ||
} | ||
|
||
function summon<T>(): T { | ||
return "fake summoned value" as any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/perseus/src/util/parse-perseus-json/general-purpose-parsers/unknown.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import type {Parser} from "../parser-types"; | ||
|
||
export const unknown: Parser<unknown> = (rawValue, ctx) => | ||
ctx.success(rawValue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.