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

Remove jQuery from answer types #2278

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/funny-cobras-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus-score": patch
---

Remove jQuery as a dependency of perseus-score
2 changes: 0 additions & 2 deletions packages/perseus-score/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
"@khanacademy/perseus-core": "workspace:*"
},
"devDependencies": {
"jquery": "catalog:",
"underscore": "catalog:"
},
"peerDependencies": {
"jquery": "catalog:",
"underscore": "catalog:"
},
"keywords": []
Expand Down
36 changes: 23 additions & 13 deletions packages/perseus-score/src/util/answer-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import * as KAS from "@khanacademy/kas";
import {KhanMath} from "@khanacademy/kmath";
import {Errors, PerseusError} from "@khanacademy/perseus-core";
import $ from "jquery";
import _ from "underscore";

import ErrorCodes from "../error-codes";
Expand All @@ -11,6 +10,10 @@ const MAXERROR_EPSILON = Math.pow(2, -42);

type Guess = any;
type Predicate = (guess: number, maxError: number) => boolean;
type TransformedFraction = {
value: number;
exact: boolean;
};

// TOOD(kevinb): Figure out how this relates to KEScore in
// perseus-all-package/types.js and see if there's a way to
Expand Down Expand Up @@ -139,7 +142,9 @@ const KhanAnswerTypes = {
}

// Take text looking like a fraction, and turn it into a number
const fractionTransformer = function (text) {
const fractionTransformer = function (
text,
): ReadonlyArray<TransformedFraction> {
text = text
// Replace unicode minus sign with hyphen
.replace(/\u2212/, "-")
Expand Down Expand Up @@ -230,7 +235,8 @@ const KhanAnswerTypes = {

// A proper fraction
proper: function (text) {
return $.map(fractionTransformer(text), function (o) {
const transformed = fractionTransformer(text);
return transformed.flatMap((o: TransformedFraction) => {
// All fractions that are less than 1
if (Math.abs(o.value) < 1) {
return [o];
Expand All @@ -252,7 +258,8 @@ const KhanAnswerTypes = {
return [];
}

return $.map(fractionTransformer(text), function (o) {
const transformed = fractionTransformer(text);
return transformed.flatMap((o: TransformedFraction) => {
// All fractions that are greater than 1
if (Math.abs(o.value) >= 1) {
return [o];
Expand All @@ -264,7 +271,7 @@ const KhanAnswerTypes = {
// pi-like numbers
pi: function (text) {
let match;
let possibilities: any[] = [];
let possibilities: ReadonlyArray<any> = [];

// Replace unicode minus sign with hyphen
text = text.replace(/\u2212/, "-");
Expand Down Expand Up @@ -394,7 +401,7 @@ const KhanAnswerTypes = {
multiplier = Math.PI * 1.5;
}

$.each(possibilities, function (ix, possibility) {
possibilities.forEach((possibility) => {
possibility.value *= multiplier;
});
return possibilities;
Expand Down Expand Up @@ -442,17 +449,17 @@ const KhanAnswerTypes = {

// Numbers with percent signs
percent: function (text) {
text = $.trim(text);
text = String(text).trim();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jQuery's trim coerces numbers to strings and apparently our code was banking on that.

// store whether or not there is a percent sign
let hasPercentSign = false;

if (text.indexOf("%") === text.length - 1) {
text = $.trim(text.substring(0, text.length - 1));
text = text.substring(0, text.length - 1).trim();
hasPercentSign = true;
}

const transformed = forms.decimal(text);
$.each(transformed, function (ix, t) {
transformed.forEach((t) => {
t.exact = hasPercentSign;
// @ts-expect-error - TS2532 - Object is possibly 'undefined'.
t.value = t.value / 100;
Expand Down Expand Up @@ -497,7 +504,7 @@ const KhanAnswerTypes = {
// precision == 1.)
decimal: function (text: string, precision = 1e10) {
const normal = function (text) {
text = $.trim(text);
text = String(text).trim();

const match = text
// Replace unicode minus sign with hyphen
Expand Down Expand Up @@ -547,7 +554,7 @@ const KhanAnswerTypes = {
const fallback =
options.fallback != null ? "" + options.fallback : "";

guess = $.trim(guess) || fallback;
guess = String(guess).trim() || fallback;

const score: Score = {
empty: guess === "",
Expand All @@ -558,7 +565,7 @@ const KhanAnswerTypes = {

// iterate over all the acceptable forms, and if one of the
// answers is correct, return true
$.each(acceptableForms, function (i, form) {
acceptableForms.forEach((form) => {
const transformed = forms[form](guess);
for (let j = 0, l = transformed.length; j < l; j++) {
const val = transformed[j].value;
Expand Down Expand Up @@ -641,7 +648,10 @@ const KhanAnswerTypes = {
function (guess, maxError) {
return Math.abs(guess - correctFloat) < maxError;
},
$.extend({}, options, {type: "predicate"}),
{
...options,
type: "predicate",
},
];
},
createValidatorFunctional: function (
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading