-
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.
Move scorers: Plotter and Sorter (#2113)
* plotter * sorter * Move scorer: Orderer (#2114) * orderer * Move scorerer: LabelImage (#2115) * move label-image * rename labelImageScoreMarker * Move scoring logic: Matrix (#2116) * move matrix * Move scoring logic: Expression (#2118) * move expression scorer * respond to Ben's feedback * merge Grapher move * fix conflict again * Move scorer: Grapher (#2119) * move matrix * move expression scorer * move grapher scorer * respond to Ben's feedback * Move scorer: Interactive Graph (#2120) * STOPSHIP some type errors still * add back duplicate declarations * add back Line duplicate * all tests passing * Revert changes to underscore's isEqual (#2125) ## Summary: [Original comment](#2113 (comment)) I made a separate PR because I made this mistake in a couple of PRs so I thought I'd knock them out all at once. Issue: LEMS-2737 ## Test plan: Author: handeyeco Reviewers: jeremywiebe, benchristel Required Reviewers: Approved By: jeremywiebe Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ❌ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ⏹️ [cancelled] Publish npm snapshot (ubuntu-latest, 20.x), ⏹️ [cancelled] Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ⏹️ [cancelled] Check builds for changes in size (ubuntu-latest, 20.x), ⏹️ [cancelled] Cypress (ubuntu-latest, 20.x), ⏹️ [cancelled] Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ⏹️ [cancelled] Publish npm snapshot (ubuntu-latest, 20.x), ⏹️ [cancelled] Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ⏹️ [cancelled] Check builds for changes in size (ubuntu-latest, 20.x), ⏹️ [cancelled] Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ⏹️ [cancelled] Cypress (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x) Pull Request URL: #2125 * respond to Jeremy's feedback
- Loading branch information
Showing
83 changed files
with
1,287 additions
and
1,208 deletions.
There are no files selected for viewing
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type {SineCoefficient} from "./geometry"; | ||
import type {Coord} from "@khanacademy/perseus-core"; | ||
|
||
export type NamedSineCoefficient = { | ||
amplitude: number; | ||
angularFrequency: number; | ||
phase: number; | ||
verticalOffset: number; | ||
}; | ||
|
||
// TODO: there's another, very similar getSinusoidCoefficients function | ||
// they should probably be merged | ||
export function getSinusoidCoefficients( | ||
coords: ReadonlyArray<Coord>, | ||
): SineCoefficient { | ||
// It's assumed that p1 is the root and p2 is the first peak | ||
const p1 = coords[0]; | ||
const p2 = coords[1]; | ||
|
||
// Resulting coefficients are canonical for this sine curve | ||
const amplitude = p2[1] - p1[1]; | ||
const angularFrequency = Math.PI / (2 * (p2[0] - p1[0])); | ||
const phase = p1[0] * angularFrequency; | ||
const verticalOffset = p1[1]; | ||
|
||
return [amplitude, angularFrequency, phase, verticalOffset]; | ||
} | ||
|
||
export type QuadraticCoefficient = [number, number, number]; | ||
|
||
// TODO: there's another, very similar getQuadraticCoefficients function | ||
// they should probably be merged | ||
export function getQuadraticCoefficients( | ||
coords: ReadonlyArray<Coord>, | ||
): QuadraticCoefficient { | ||
const p1 = coords[0]; | ||
const p2 = coords[1]; | ||
const p3 = coords[2]; | ||
|
||
const denom = (p1[0] - p2[0]) * (p1[0] - p3[0]) * (p2[0] - p3[0]); | ||
if (denom === 0) { | ||
// Many of the callers assume that the return value is always defined. | ||
// @ts-expect-error - TS2322 - Type 'undefined' is not assignable to type 'QuadraticCoefficient'. | ||
return; | ||
} | ||
const a = | ||
(p3[0] * (p2[1] - p1[1]) + | ||
p2[0] * (p1[1] - p3[1]) + | ||
p1[0] * (p3[1] - p2[1])) / | ||
denom; | ||
const b = | ||
(p3[0] * p3[0] * (p1[1] - p2[1]) + | ||
p2[0] * p2[0] * (p3[1] - p1[1]) + | ||
p1[0] * p1[0] * (p2[1] - p3[1])) / | ||
denom; | ||
const c = | ||
(p2[0] * p3[0] * (p2[0] - p3[0]) * p1[1] + | ||
p3[0] * p1[0] * (p3[0] - p1[0]) * p2[1] + | ||
p1[0] * p2[0] * (p1[0] - p2[0]) * p3[1]) / | ||
denom; | ||
return [a, b, c]; | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type {Coord} from "@khanacademy/perseus-core"; | ||
|
||
export type QuadraticCoords = [Coord, Coord, Coord]; |
3 changes: 2 additions & 1 deletion
3
packages/math-input/src/components/key-handlers/key-translator.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
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
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,25 @@ | ||
import deepClone from "./deep-clone"; | ||
|
||
describe("deepClone", () => { | ||
it("does nothing to a primitive", () => { | ||
expect(deepClone(3)).toBe(3); | ||
}); | ||
|
||
it("copies an array", () => { | ||
const input = [1, 2, 3]; | ||
|
||
const result = deepClone(input); | ||
|
||
expect(result).toEqual(input); | ||
expect(result).not.toBe(input); | ||
}); | ||
|
||
it("recursively clones array elements", () => { | ||
const input = [[1]]; | ||
|
||
const result = deepClone(input); | ||
|
||
expect(result).toEqual(input); | ||
expect(result[0]).not.toBe(input[0]); | ||
}); | ||
}); |
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,18 @@ | ||
// TODO(benchristel): in the future, we may want to make deepClone work for | ||
// Record<string, Cloneable> as well. Currently, it only does arrays. | ||
type Cloneable = | ||
| null | ||
| undefined | ||
| boolean | ||
| string | ||
| number | ||
| Cloneable[] | ||
| readonly Cloneable[]; | ||
function deepClone<T extends Cloneable>(obj: T): T { | ||
if (Array.isArray(obj)) { | ||
return obj.map(deepClone) as T; | ||
} | ||
return obj; | ||
} | ||
|
||
export default deepClone; |
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,55 @@ | ||
import _ from "underscore"; | ||
|
||
/** | ||
* APPROXIMATE equality on numbers and primitives. | ||
*/ | ||
export function approximateEqual<T>(x: T, y: T): boolean { | ||
if (typeof x === "number" && typeof y === "number") { | ||
return Math.abs(x - y) < 1e-9; | ||
} | ||
return x === y; | ||
} | ||
|
||
/** | ||
* Deep APPROXIMATE equality on primitives, numbers, arrays, and objects. | ||
* Recursive. | ||
*/ | ||
export function approximateDeepEqual<T>(x: T, y: T): boolean { | ||
if (Array.isArray(x) && Array.isArray(y)) { | ||
if (x.length !== y.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < x.length; i++) { | ||
if (!approximateDeepEqual(x[i], y[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (Array.isArray(x) || Array.isArray(y)) { | ||
return false; | ||
} | ||
if (typeof x === "function" && typeof y === "function") { | ||
return approximateEqual(x, y); | ||
} | ||
if (typeof x === "function" || typeof y === "function") { | ||
return false; | ||
} | ||
if (typeof x === "object" && typeof y === "object" && !!x && !!y) { | ||
return ( | ||
x === y || | ||
(_.all(x, function (v, k) { | ||
// @ts-expect-error - TS2536 - Type 'CollectionKey<T>' cannot be used to index type 'T'. | ||
return approximateDeepEqual(y[k], v); | ||
}) && | ||
_.all(y, function (v, k) { | ||
// @ts-expect-error - TS2536 - Type 'CollectionKey<T>' cannot be used to index type 'T'. | ||
return approximateDeepEqual(x[k], v); | ||
})) | ||
); | ||
} | ||
if ((typeof x === "object" && !!x) || (typeof y === "object" && !!y)) { | ||
return false; | ||
} | ||
return approximateEqual(x, y); | ||
} |
File renamed without changes.
Oops, something went wrong.