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

Switch to standard ES exports for objective utils #1948

Merged
merged 3 commits into from
Dec 5, 2024
Merged
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/chilled-cameras-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Refactor internally used object mapping utilities to use ES6 exports
4 changes: 2 additions & 2 deletions packages/perseus/src/interactive2/movable-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import KhanColors from "../util/colors";

import InteractiveUtil from "./interactive-util";
import MovableLineOptions from "./movable-line-options";
import objective_ from "./objective_";
import {pluck} from "./objective_";
import WrappedLine from "./wrapped-line";

const assert = InteractiveUtil.assert;
Expand Down Expand Up @@ -83,7 +83,7 @@ _.extend(MovableLine.prototype, {
// - are objects, not primitives (and need a deeper copy)
// - they don't need getters created for them
// TODO(jack): Consider "default" once we es3ify perseus
objective_.pluck(MovableLineOptions, "standard"),
pluck(MovableLineOptions, "standard"),
),
DEFAULT_PROPS,
);
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/interactive2/movable-point.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import Tex from "../util/tex";

import InteractiveUtil from "./interactive-util";
import MovablePointOptions from "./movable-point-options";
import objective_ from "./objective_";
import {pluck} from "./objective_";
import WrappedEllipse from "./wrapped-ellipse";

import type {Movable} from "./movable";
Expand Down Expand Up @@ -170,7 +170,7 @@ export class MovablePoint {
// - are objects, not primitives (and need a deeper copy)
// - they don't need getters created for them
// TODO(jack): Consider "default" once we es3ify perseus
objective_.pluck(MovablePointOptions, "standard"),
pluck(MovablePointOptions, "standard"),
// We only update props here, because we want things on state to
// be persistent, and updated appropriately in modify()
),
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/interactive2/movable-polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import GraphUtils from "../util/graph-utils";

import InteractiveUtil from "./interactive-util";
import MovablePolygonOptions from "./movable-polygon-options";
import objective_ from "./objective_";
import {pluck} from "./objective_";

const assert = InteractiveUtil.assert;
const normalizeOptions = InteractiveUtil.normalizeOptions;
Expand Down Expand Up @@ -91,7 +91,7 @@ _.extend(MovablePolygon.prototype, {
// - are objects, not primitives (and need a deeper copy)
// - they don't need getters created for them
// TODO(jack): Consider "default" once we es3ify perseus
objective_.pluck(MovablePolygonOptions, "standard"),
pluck(MovablePolygonOptions, "standard"),

// We only update props here, because we want things on state to
// be persistent, and updated appropriately in modify()
Expand Down
29 changes: 2 additions & 27 deletions packages/perseus/src/interactive2/objective_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import _ from "underscore";
* rotation: false
* }
*/
const pluck = function (table: any, subKey: string): any {
export const pluck = function (table: any, subKey: string): any {
return _.object(
_.map(table, function (value, key) {
return [key, value[subKey]];
Expand All @@ -41,7 +41,7 @@ const pluck = function (table: any, subKey: string): any {
* });
* {a: 2, b: 3}
*/
const mapObject = function <K extends string, V, U>(
export const mapObject = function <K extends string, V, U>(
obj: Record<K, V>,
lambda: (arg1: V, arg2: K) => U,
): Record<K, U> {
Expand All @@ -52,28 +52,3 @@ const mapObject = function <K extends string, V, U>(
});
return result;
};

/**
* Maps an array to an object
*
* > mapObjectFromArray(['a', 'b'], function(elem) {
* return elem + elem;
* });
* {a: 'aa', b: 'bb'}
*/
const mapObjectFromArray = function <K extends string, V>(
arr: ReadonlyArray<K>,
lambda: (arg1: K) => V,
): Record<K, V> {
const result: Record<string, any> = {};
arr.forEach((elem) => {
result[elem] = lambda(elem);
});
return result;
};

export default {
pluck: pluck,
mapObject: mapObject,
mapObjectFromArray: mapObjectFromArray,
};
4 changes: 1 addition & 3 deletions packages/perseus/src/renderer-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Objective from "./interactive2/objective_";
import {mapObject} from "./interactive2/objective_";
import Util from "./util";
import {getWidgetIdsFromContent} from "./widget-type-utils";
import {getWidgetScorer, upgradeWidgetInfoToLatestVersion} from "./widgets";
Expand All @@ -8,8 +8,6 @@ import type {PerseusStrings} from "./strings";
import type {PerseusScore} from "./types";
import type {UserInput, UserInputMap} from "./validation.types";

const {mapObject} = Objective;

export function getUpgradedWidgetOptions(
oldWidgetOptions: PerseusWidgetsMap,
): PerseusWidgetsMap {
Expand Down
4 changes: 1 addition & 3 deletions packages/perseus/src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {DefinitionProvider} from "./definition-context";
import {getDependencies} from "./dependencies";
import ErrorBoundary from "./error-boundary";
import InteractionTracker from "./interaction-tracker";
import Objective from "./interactive2/objective_";
import {mapObject} from "./interactive2/objective_";
import JiptParagraphs from "./jipt-paragraphs";
import {Log} from "./logging/log";
import {ClassNames as ApiClassNames, ApiOptions} from "./perseus-api";
Expand Down Expand Up @@ -65,8 +65,6 @@ import type {LinterContextProps} from "@khanacademy/perseus-linter";

import "./styles/perseus-renderer.less";

const {mapObject} = Objective;

const rContainsNonWhitespace = /\S/;
const rImageURL = /(web\+graphie|https):\/\/[^\s]*/;

Expand Down
5 changes: 2 additions & 3 deletions packages/perseus/src/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

import _ from "underscore";

// TODO(aria): Pull this out of interactive2 / replace with new _.mapObject
import objective_ from "./interactive2/objective_";
import {mapObject} from "./interactive2/objective_";
import * as Widgets from "./widgets";

const noop = function () {};
Expand Down Expand Up @@ -93,7 +92,7 @@ const traverseRenderer = function (
}
}

const newWidgets = objective_.mapObject(
const newWidgets = mapObject(
rendererOptions.widgets || {},
function (widgetInfo, widgetId) {
// Widgets without info or a type are empty widgets, and
Expand Down
Loading