@khanacademy/perseus 19.0.0 → 19.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/es/index.css +209 -104
  3. package/dist/es/index.js +806 -877
  4. package/dist/es/index.js.map +1 -1
  5. package/dist/index.css +209 -104
  6. package/dist/index.d.ts +3 -2
  7. package/dist/index.js +849 -920
  8. package/dist/index.js.map +1 -1
  9. package/dist/mixins/changeable.d.ts +1 -1
  10. package/dist/perseus-api.d.ts +1 -0
  11. package/dist/renderer.d.ts +2 -4
  12. package/dist/server-item-renderer.d.ts +1 -0
  13. package/dist/types.d.ts +4 -0
  14. package/dist/util/geometry.d.ts +2 -0
  15. package/dist/util/graphie.d.ts +107 -1
  16. package/dist/util/math.d.ts +2 -0
  17. package/dist/util/parse-perseus-json.d.ts +3 -3
  18. package/dist/widgets/expression.d.ts +6 -0
  19. package/dist/widgets/label-image.d.ts +2 -0
  20. package/package.json +3 -4
  21. package/src/__testdata__/renderer.testdata.ts +152 -0
  22. package/src/__tests__/renderer.test.tsx +17 -0
  23. package/src/components/math-input.tsx +4 -6
  24. package/src/index.ts +8 -3
  25. package/src/mixins/changeable.ts +1 -1
  26. package/src/renderer.tsx +14 -8
  27. package/src/types.ts +9 -0
  28. package/src/util/geometry.test.ts +75 -0
  29. package/src/util/geometry.ts +19 -0
  30. package/src/util/graphie.test.ts +1291 -0
  31. package/src/util/graphie.ts +832 -784
  32. package/src/util/interactive.ts +23 -51
  33. package/src/util/math.test.ts +11 -0
  34. package/src/util/math.ts +8 -0
  35. package/src/util/parse-perseus-json.test.ts +3 -3
  36. package/src/util/parse-perseus-json.ts +4 -4
  37. package/src/widgets/__tests__/__snapshots__/input-number.test.ts.snap +4 -2
  38. package/src/widgets/__tests__/__snapshots__/numeric-input.test.ts.snap +4 -2
  39. package/src/widgets/__tests__/expression-mobile.test.tsx +8 -5
  40. package/src/widgets/expression.tsx +1 -23
  41. package/src/widgets/label-image.tsx +0 -1
  42. package/tsconfig-build.tsbuildinfo +1 -1
@@ -0,0 +1,75 @@
1
+ import {clockwise, reverseVector} from "./geometry";
2
+
3
+ describe("reverseVector", () => {
4
+ it("flips the sign of zero", () => {
5
+ // This is a characterization test for legacy behavior. It might not
6
+ // actually be desired behavior.
7
+ expect(reverseVector([0, 0])).toEqual([-0, -0]);
8
+ });
9
+
10
+ it("flips the sign of each component", () => {
11
+ expect(reverseVector([1, -5])).toEqual([-1, 5]);
12
+ });
13
+ });
14
+
15
+ describe("clockwise", () => {
16
+ it("returns false given no points", () => {
17
+ expect(clockwise([])).toBe(false);
18
+ });
19
+
20
+ it("returns false given a single point at the origin", () => {
21
+ expect(clockwise([[0, 0]])).toBe(false);
22
+ });
23
+
24
+ it("returns false given a single point not at the origin", () => {
25
+ expect(clockwise([[7, 7]])).toBe(false);
26
+ });
27
+
28
+ it("returns false given two identical points", () => {
29
+ expect(
30
+ clockwise([
31
+ [7, 7],
32
+ [7, 7],
33
+ ]),
34
+ ).toBe(false);
35
+ });
36
+
37
+ it("returns false given two different points", () => {
38
+ expect(
39
+ clockwise([
40
+ [8, 8],
41
+ [7, 7],
42
+ ]),
43
+ ).toBe(false);
44
+ });
45
+
46
+ it("returns true given a clockwise triangle in the first quadrant", () => {
47
+ expect(
48
+ clockwise([
49
+ [0, 1],
50
+ [1, 0],
51
+ [0, 0],
52
+ ]),
53
+ ).toBe(true);
54
+ });
55
+
56
+ it("returns false given a counter-clockwise triangle in the first quadrant", () => {
57
+ expect(
58
+ clockwise([
59
+ [1, 0],
60
+ [0, 1],
61
+ [0, 0],
62
+ ]),
63
+ ).toBe(false);
64
+ });
65
+
66
+ it("returns true given a clockwise triangle in the second quadrant", () => {
67
+ expect(
68
+ clockwise([
69
+ [0, 1],
70
+ [0, 0],
71
+ [-1, 0],
72
+ ]),
73
+ ).toBe(true);
74
+ });
75
+ });
@@ -7,6 +7,8 @@ import _ from "underscore";
7
7
 
8
8
  import Util from "../util";
9
9
 
10
+ import {sum} from "./math";
11
+
10
12
  import type {Coord, Line} from "../interactive2/types";
11
13
 
12
14
  const {eq, deepEq} = Util;
@@ -83,6 +85,23 @@ export function vector(a, b) {
83
85
  });
84
86
  }
85
87
 
88
+ export function reverseVector(vector: Coord): Coord {
89
+ return [-vector[0], -vector[1]];
90
+ }
91
+
92
+ // Returns whether connecting the given sequence of `points` forms a clockwise
93
+ // path (assuming a closed loop, where the last point connects back to the
94
+ // first).
95
+ export function clockwise(points: Coord[]): boolean {
96
+ const segments = _.zip(points, points.slice(1).concat(points.slice(0, 1)));
97
+ const areas = _.map(segments, function (segment) {
98
+ const p1 = segment[0];
99
+ const p2 = segment[1];
100
+ return (p2[0] - p1[0]) * (p2[1] + p1[1]);
101
+ });
102
+ return sum(areas) > 0;
103
+ }
104
+
86
105
  export function magnitude(v: ReadonlyArray<Coord>): number {
87
106
  return Math.sqrt(
88
107
  _.reduce(