Skip to content

Commit

Permalink
respond to Jeremys feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
handeyeco committed Jan 10, 2025
1 parent e7a5e47 commit 25c4a94
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 57 deletions.
19 changes: 19 additions & 0 deletions packages/perseus-score/src/error-codes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const MISSING_PERCENT_ERROR = "MISSING_PERCENT_ERROR";
const NEEDS_TO_BE_SIMPLIFIED_ERROR = "NEEDS_TO_BE_SIMPLIFIED_ERROR";
const APPROXIMATED_PI_ERROR = "APPROXIMATED_PI_ERROR";
const EXTRA_SYMBOLS_ERROR = "EXTRA_SYMBOLS_ERROR";
const WRONG_CASE_ERROR = "WRONG_CASE_ERROR";
const WRONG_LETTER_ERROR = "WRONG_LETTER_ERROR";
const MULTIPLICATION_SIGN_ERROR = "MULTIPLICATION_SIGN_ERROR";

const ErrorCodes = {
MISSING_PERCENT_ERROR,
NEEDS_TO_BE_SIMPLIFIED_ERROR,
APPROXIMATED_PI_ERROR,
EXTRA_SYMBOLS_ERROR,
WRONG_CASE_ERROR,
WRONG_LETTER_ERROR,
MULTIPLICATION_SIGN_ERROR,
};

export default ErrorCodes;
13 changes: 0 additions & 13 deletions packages/perseus-score/src/error-identifiers.ts

This file was deleted.

10 changes: 1 addition & 9 deletions packages/perseus-score/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
export {default as KhanAnswerTypes} from "./util/answer-types";
export type {Score} from "./util/answer-types";
export {
APPROXIMATED_PI_ERROR,
EXTRA_SYMBOLS_ERROR,
MISSING_PERCENT_ERROR,
NEEDS_TO_BE_SIMPLIFIED_ERROR,
WRONG_CASE_ERROR,
WRONG_LETTER_ERROR,
MULTIPLICATION_SIGN_ERROR,
} from "./error-identifiers";
export {default as ErrorCodes} from "./error-codes";
27 changes: 11 additions & 16 deletions packages/perseus-score/src/util/answer-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ import {Errors, PerseusError} from "@khanacademy/perseus-core";
import $ from "jquery";
import _ from "underscore";

import {
APPROXIMATED_PI_ERROR,
EXTRA_SYMBOLS_ERROR,
MISSING_PERCENT_ERROR,
NEEDS_TO_BE_SIMPLIFIED_ERROR,
WRONG_CASE_ERROR,
WRONG_LETTER_ERROR,
MULTIPLICATION_SIGN_ERROR,
} from "../error-identifiers";
import ErrorCodes from "../error-codes";

const MAXERROR_EPSILON = Math.pow(2, -42);

Expand Down Expand Up @@ -577,12 +569,14 @@ const KhanAnswerTypes = {
} else if (form === "percent") {
// Otherwise, an error was returned
score.empty = true;
score.message = MISSING_PERCENT_ERROR;
score.message =
ErrorCodes.MISSING_PERCENT_ERROR;
} else {
if (options.simplify !== "enforced") {
score.empty = true;
}
score.message = NEEDS_TO_BE_SIMPLIFIED_ERROR;
score.message =
ErrorCodes.NEEDS_TO_BE_SIMPLIFIED_ERROR;
}
// The return false below stops the looping of the
// callback since predicate check succeeded.
Expand All @@ -591,7 +585,7 @@ const KhanAnswerTypes = {
}
if (piApprox && predicate(val, Math.abs(val * 0.001))) {
score.empty = true;
score.message = APPROXIMATED_PI_ERROR;
score.message = ErrorCodes.APPROXIMATED_PI_ERROR;
}
}
});
Expand All @@ -609,7 +603,7 @@ const KhanAnswerTypes = {
});
if (!interpretedGuess) {
score.empty = true;
score.message = EXTRA_SYMBOLS_ERROR;
score.message = ErrorCodes.EXTRA_SYMBOLS_ERROR;
return score;
}
}
Expand Down Expand Up @@ -789,8 +783,8 @@ const KhanAnswerTypes = {
score.ungraded = true;
// @ts-expect-error - TS2540 - Cannot assign to 'message' because it is a read-only property.
score.message = result.wrongVariableCase
? WRONG_CASE_ERROR
: WRONG_LETTER_ERROR;
? ErrorCodes.WRONG_CASE_ERROR
: ErrorCodes.WRONG_LETTER_ERROR;
// Don't tell the use they're "almost there" in this case, that may not be true and isn't helpful.
// @ts-expect-error - TS2339 - Property 'suppressAlmostThere' does not exist on type '{ readonly empty: false; readonly correct: false; readonly message: string | null | undefined; readonly guess: any; readonly ungraded: false; }'.
score.suppressAlmostThere = true;
Expand Down Expand Up @@ -823,7 +817,8 @@ const KhanAnswerTypes = {
// @ts-expect-error - TS2540 - Cannot assign to 'ungraded' because it is a read-only property.
score.ungraded = true;
// @ts-expect-error - TS2540 - Cannot assign to 'message' because it is a read-only property.
score.message = MULTIPLICATION_SIGN_ERROR;
score.message =
ErrorCodes.MULTIPLICATION_SIGN_ERROR;
} else if (resultX.message) {
// TODO(aasmund): I18nize `score.message`
// @ts-expect-error - TS2540 - Cannot assign to 'message' because it is a read-only property.
Expand Down
25 changes: 9 additions & 16 deletions packages/perseus/src/strings.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
APPROXIMATED_PI_ERROR,
EXTRA_SYMBOLS_ERROR,
MISSING_PERCENT_ERROR,
NEEDS_TO_BE_SIMPLIFIED_ERROR,
WRONG_CASE_ERROR,
WRONG_LETTER_ERROR,
MULTIPLICATION_SIGN_ERROR,
} from "@khanacademy/perseus-score";
import {ErrorCodes} from "@khanacademy/perseus-score";

/**
* The translated strings that are used to render Perseus.
Expand Down Expand Up @@ -709,13 +701,14 @@ export const mockStrings: PerseusStrings = {
};

const errorToString = {
[MISSING_PERCENT_ERROR]: strings.MISSING_PERCENT_ERROR,
[NEEDS_TO_BE_SIMPLIFIED_ERROR]: strings.NEEDS_TO_BE_SIMPLFIED_ERROR,
[APPROXIMATED_PI_ERROR]: strings.APPROXIMATED_PI_ERROR,
[EXTRA_SYMBOLS_ERROR]: strings.EXTRA_SYMBOLS_ERROR,
[WRONG_CASE_ERROR]: strings.WRONG_CASE_ERROR,
[WRONG_LETTER_ERROR]: strings.WRONG_LETTER_ERROR,
[MULTIPLICATION_SIGN_ERROR]: strings.MULTIPLICATION_SIGN_ERROR,
[ErrorCodes.MISSING_PERCENT_ERROR]: strings.MISSING_PERCENT_ERROR,
[ErrorCodes.NEEDS_TO_BE_SIMPLIFIED_ERROR]:
strings.NEEDS_TO_BE_SIMPLFIED_ERROR,
[ErrorCodes.APPROXIMATED_PI_ERROR]: strings.APPROXIMATED_PI_ERROR,
[ErrorCodes.EXTRA_SYMBOLS_ERROR]: strings.EXTRA_SYMBOLS_ERROR,
[ErrorCodes.WRONG_CASE_ERROR]: strings.WRONG_CASE_ERROR,
[ErrorCodes.WRONG_LETTER_ERROR]: strings.WRONG_LETTER_ERROR,
[ErrorCodes.MULTIPLICATION_SIGN_ERROR]: strings.MULTIPLICATION_SIGN_ERROR,
};
export function mapErrorToString(err: string | null | undefined) {
if (!err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ describe("invalid", function () {
mockStrings,
);
expect(err).toEqual({
message: "EXTRA_SYMBOLS_ERROR",
message: "EXTRA_SYMBOLS_ERROR",
type: "invalid",
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("scoreInputNumber", () => {

const score = scoreInputNumber(useInput, rubric, mockStrings);

expect(score).toHaveInvalidInput("EXTRA_SYMBOLS_ERROR");
expect(score).toHaveInvalidInput("EXTRA_SYMBOLS_ERROR");
});

// Don't default to validating the answer as a pi answer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("static function validate", () => {

const score = scoreNumericInput(userInput, rubric, mockStrings);

expect(score).toHaveInvalidInput("EXTRA_SYMBOLS_ERROR");
expect(score).toHaveInvalidInput("EXTRA_SYMBOLS_ERROR");
});

// Don't default to validating the answer as a pi answer
Expand Down

0 comments on commit 25c4a94

Please sign in to comment.