@slugbugblue/trax 0.21.0 → 0.22.0

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.
package/src/point.js DELETED
@@ -1,173 +0,0 @@
1
- /* Copyright 2022 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- /** Because javascript doesn't have a "0 < 3 < 7" syntax.
17
- * @arg {number} end1 - A number on one end or the other
18
- * @arg {number} mid - The middle number
19
- * @arg {number} end2 - The other end
20
- * @returns {boolean} - If the number is in the middle
21
- */
22
- const between = (end1, mid, end2) => {
23
- const low = Math.min(end1, end2)
24
- const high = Math.max(end1, end2)
25
- return low <= mid && mid <= high
26
- }
27
-
28
- /** Are you tired of doing x,y movements in multiple statements?
29
- *
30
- * I know I am.
31
- *
32
- * Point: an immutable two-dimensional location (x,y) with
33
- * functions for moving around quickly to neighboring points.
34
- */
35
- export class Point {
36
- #x = 0
37
- #y = 0
38
-
39
- static dirs = ['up', 'down', 'left', 'right']
40
-
41
- /** You can call this with new Point(point) or new Point({x, y}) or new Point(x, y)
42
- * @arg {number | PointLike} x - the x value or a point
43
- * @arg {number} [y] - the y value
44
- */
45
- constructor(x, y) {
46
- if (
47
- typeof x === 'object' &&
48
- typeof x.x === 'number' &&
49
- typeof x.y === 'number'
50
- ) {
51
- this.#x = x.x
52
- this.#y = x.y
53
- } else {
54
- if (typeof x !== 'number' || Number.isNaN(x)) {
55
- throw new SyntaxError('x must be a number or a point-like object')
56
- }
57
-
58
- if (typeof y !== 'number' || Number.isNaN(x)) {
59
- throw new SyntaxError('y must be a number')
60
- }
61
-
62
- this.#x = x
63
- this.#y = y
64
- }
65
- }
66
-
67
- /** The horizontal value of the point. */
68
- get x() {
69
- return this.#x
70
- }
71
-
72
- /** Throws an error. Points are immutable. */
73
- set x(_) {
74
- throw new ReferenceError('Point is immutable')
75
- }
76
-
77
- /** The vertical value of the point. */
78
- get y() {
79
- return this.#y
80
- }
81
-
82
- /** Throws an error. Points are immutable. */
83
- set y(_) {
84
- throw new ReferenceError('Point is immutable')
85
- }
86
-
87
- /** The point immediately above this point. */
88
- get up() {
89
- return new Point(this.#x, this.#y - 1)
90
- }
91
-
92
- /** The point immediately below this point. */
93
- get down() {
94
- return new Point(this.#x, this.#y + 1)
95
- }
96
-
97
- /** The point to the immediate left of this point. */
98
- get left() {
99
- return new Point(this.#x - 1, this.#y)
100
- }
101
-
102
- /** The point to the immediate right of this point. */
103
- get right() {
104
- return new Point(this.#x + 1, this.#y)
105
- }
106
-
107
- /** A list of the four points that surround this point: up, down, left, and right. */
108
- get around() {
109
- return [this.up, this.down, this.left, this.right]
110
- }
111
-
112
- /** Move in a specific direction by passing in a direction string.
113
- * @arg {string} direction - any string that begins with u, d, l, or r
114
- * @returns {Point}
115
- */
116
- dir(direction) {
117
- const error = 'direction must be one of: up, down, left, right, u, d, l, r'
118
- if (typeof direction !== 'string') throw new SyntaxError(error)
119
- direction = direction.toLowerCase()
120
- if (direction[0] === 'u') return this.up
121
- if (direction[0] === 'd') return this.down
122
- if (direction[0] === 'l') return this.left
123
- if (direction[0] === 'r') return this.right
124
- throw new SyntaxError(error)
125
- }
126
-
127
- /** @arg {PointLike} from - the point to measure x against. */
128
- distX(from) {
129
- return Math.abs(this.#x - from.x)
130
- }
131
-
132
- /** @arg {PointLike} from - the point to measure y against. */
133
- distY(from) {
134
- return Math.abs(this.#y - from.y)
135
- }
136
-
137
- /** @arg {PointLike} from - the point to measure against. */
138
- distance(from) {
139
- return Math.hypot(this.distX(from), this.distY(from))
140
- }
141
-
142
- /** @arg {PointLike} that - the point to test equality against. */
143
- eq(that) {
144
- return this.#x === that.x && this.#y === that.y
145
- }
146
-
147
- /** Bounding box corners can be passed in any order.
148
- * @arg {PointLike} a - one corner of the bounding box
149
- * @arg {PointLike} b - the opposite corner
150
- * @returns {boolean} - true if the point is on or in the box
151
- */
152
- in(a, b) {
153
- // Allow for the bounding points to be passed in any order
154
- // as long as they are opposite corners:
155
- return between(a.x, this.x, b.x) && between(a.y, this.y, b.y)
156
- }
157
-
158
- // Play nicely with the rest of the world
159
-
160
- toString() {
161
- return 'Point(' + this.#x + ',' + this.#y + ')'
162
- }
163
-
164
- /** @returns {PointLike} */
165
- toJSON() {
166
- return { x: this.#x, y: this.#y }
167
- }
168
-
169
- [Symbol.for('nodejs.util.inspect.custom')](_, options) {
170
- const [y, s, n] = [options.stylize, 'special', 'number']
171
- return `${y('Point', s)}(${y(this.#x, n)},${y(this.#y, n)})`
172
- }
173
- }