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

Remove jQuery from answer types #2278

wants to merge 4 commits into from

Conversation

handeyeco
Copy link
Contributor

@handeyeco handeyeco commented Mar 4, 2025

Summary:

I'm kind of just trying this out, but I think it would be good to remove jQuery where possible anyway.

See: https://khanacademy.slack.com/archives/CDHPTMSTB/p1741115861977089

@handeyeco handeyeco self-assigned this Mar 4, 2025
Copy link
Contributor

github-actions bot commented Mar 4, 2025

Size Change: -22 B (0%)

Total Size: 735 kB

Filename Size Change
packages/perseus-score/dist/es/index.js 20.6 kB -22 B (-0.11%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 39.7 kB
packages/keypad-context/dist/es/index.js 760 B
packages/kmath/dist/es/index.js 11.1 kB
packages/math-input/dist/es/index.js 77.5 kB
packages/math-input/dist/es/strings.js 1.79 kB
packages/perseus-core/dist/es/index.js 31.9 kB
packages/perseus-editor/dist/es/index.js 136 kB
packages/perseus-linter/dist/es/index.js 22.8 kB
packages/perseus/dist/es/index.js 369 kB
packages/perseus/dist/es/strings.js 6.73 kB
packages/pure-markdown/dist/es/index.js 4.14 kB
packages/simple-markdown/dist/es/index.js 13.1 kB

compressed-size-action

return [];
});
const transformed = fractionTransformer(text);
return transformed.reduce(
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 map seemingly does some special flattening/filtering that's not as straight-forward as JS' map: HubSpot/youmightnotneedjquery#355

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed. jQuery's map says for the mapping callback:

The function can return:

* the translated value, which will be mapped to the resulting array
* null or undefined, to remove the item
* an array of values, which will be flattened into the full array

That last point comes into play. I think you could just use flatMap() here instead of reduce()

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call out, thank you

@@ -442,17 +461,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.

Copy link
Contributor

github-actions bot commented Mar 4, 2025

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (ae6ed1f) and published it to npm. You
can install it using the tag PR2278.

Example:

pnpm add @khanacademy/perseus@PR2278

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR2278

@handeyeco handeyeco requested review from a team March 4, 2025 20:34
@handeyeco handeyeco changed the title [jquery-answer-types] remove jQuery from answer types Remove jQuery from answer types Mar 4, 2025
@handeyeco handeyeco added 🐍 project: sss Server-Side Scoring ('24-'25) project agnostic PRs reviewable by any Perseus team member and removed 🐍 project: sss Server-Side Scoring ('24-'25) labels Mar 11, 2025
Copy link
Collaborator

@jeremywiebe jeremywiebe left a comment

Choose a reason for hiding this comment

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

I'd love to see us build more unit tests around this file. It's pretty complex and has a pretty light set of tests. :) Not blocking this PR though!

return [];
});
const transformed = fractionTransformer(text);
return transformed.reduce(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed. jQuery's map says for the mapping callback:

The function can return:

* the translated value, which will be mapped to the resulting array
* null or undefined, to remove the item
* an array of values, which will be flattened into the full array

That last point comes into play. I think you could just use flatMap() here instead of reduce()

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project agnostic PRs reviewable by any Perseus team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants