This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
point.coffee
268 lines (230 loc) · 6.89 KB
/
point.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Public: Represents a point in a buffer in row/column coordinates.
#
# Every public method that takes a point also accepts a *point-compatible*
# {Array}. This means a 2-element array containing {Number}s representing the
# row and column. So the following are equivalent:
#
# ```coffee
# new Point(1, 2)
# [1, 2] # Point compatible Array
# ```
module.exports =
class Point
###
Section: Properties
###
# Public: A zero-indexed {Number} representing the row of the {Point}.
row: null
# Public: A zero-indexed {Number} representing the column of the {Point}.
column: null
###
Section: Construction
###
# Public: Convert any point-compatible object to a {Point}.
#
# * `object` This can be an object that's already a {Point}, in which case it's
# simply returned, or an array containing two {Number}s representing the
# row and column.
# * `copy` An optional boolean indicating whether to force the copying of objects
# that are already points.
#
# Returns: A {Point} based on the given object.
@fromObject: (object, copy) ->
if object instanceof Point
if copy then object.copy() else object
else
if Array.isArray(object)
[row, column] = object
else
{row, column} = object
new Point(row, column)
###
Section: Comparison
###
# Public: Returns the given {Point} that is earlier in the buffer.
#
# * `point1` {Point}
# * `point2` {Point}
@min: (point1, point2) ->
point1 = @fromObject(point1)
point2 = @fromObject(point2)
if point1.isLessThanOrEqual(point2)
point1
else
point2
@max: (point1, point2) ->
point1 = Point.fromObject(point1)
point2 = Point.fromObject(point2)
if point1.compare(point2) >= 0
point1
else
point2
@assertValid: (point) ->
unless isNumber(point.row) and isNumber(point.column)
throw new TypeError("Invalid Point: #{point}")
@ZERO: Object.freeze(new Point(0, 0))
@INFINITY: Object.freeze(new Point(Infinity, Infinity))
###
Section: Construction
###
# Public: Construct a {Point} object
#
# * `row` {Number} row
# * `column` {Number} column
constructor: (row=0, column=0) ->
unless this instanceof Point
return new Point(row, column)
@row = row
@column = column
# Public: Returns a new {Point} with the same row and column.
copy: ->
new Point(@row, @column)
# Public: Returns a new {Point} with the row and column negated.
negate: ->
new Point(-@row, -@column)
###
Section: Operations
###
# Public: Makes this point immutable and returns itself.
#
# Returns an immutable version of this {Point}
freeze: ->
Object.freeze(this)
# Public: Build and return a new point by adding the rows and columns of
# the given point.
#
# * `other` A {Point} whose row and column will be added to this point's row
# and column to build the returned point.
#
# Returns a {Point}.
translate: (other) ->
{row, column} = Point.fromObject(other)
new Point(@row + row, @column + column)
# Public: Build and return a new {Point} by traversing the rows and columns
# specified by the given point.
#
# * `other` A {Point} providing the rows and columns to traverse by.
#
# This method differs from the direct, vector-style addition offered by
# {::translate}. Rather than adding the rows and columns directly, it derives
# the new point from traversing in "typewriter space". At the end of every row
# traversed, a carriage return occurs that returns the columns to 0 before
# continuing the traversal.
#
# ## Examples
#
# Traversing 0 rows, 2 columns:
# `new Point(10, 5).traverse(new Point(0, 2)) # => [10, 7]`
#
# Traversing 2 rows, 2 columns. Note the columns reset from 0 before adding:
# `new Point(10, 5).traverse(new Point(2, 2)) # => [12, 2]`
#
# Returns a {Point}.
traverse: (other) ->
other = Point.fromObject(other)
row = @row + other.row
if other.row is 0
column = @column + other.column
else
column = other.column
new Point(row, column)
traversalFrom: (other) ->
other = Point.fromObject(other)
if @row is other.row
if @column is Infinity and other.column is Infinity
new Point(0, 0)
else
new Point(0, @column - other.column)
else
new Point(@row - other.row, @column)
splitAt: (column) ->
if @row is 0
rightColumn = @column - column
else
rightColumn = @column
[new Point(0, column), new Point(@row, rightColumn)]
###
Section: Comparison
###
# Public:
#
# * `other` A {Point} or point-compatible {Array}.
#
# Returns `-1` if this point precedes the argument.
# Returns `0` if this point is equivalent to the argument.
# Returns `1` if this point follows the argument.
compare: (other) ->
other = Point.fromObject(other)
if @row > other.row
1
else if @row < other.row
-1
else
if @column > other.column
1
else if @column < other.column
-1
else
0
# Public: Returns a {Boolean} indicating whether this point has the same row
# and column as the given {Point} or point-compatible {Array}.
#
# * `other` A {Point} or point-compatible {Array}.
isEqual: (other) ->
return false unless other
other = Point.fromObject(other)
@row is other.row and @column is other.column
# Public: Returns a {Boolean} indicating whether this point precedes the given
# {Point} or point-compatible {Array}.
#
# * `other` A {Point} or point-compatible {Array}.
isLessThan: (other) ->
@compare(other) < 0
# Public: Returns a {Boolean} indicating whether this point precedes or is
# equal to the given {Point} or point-compatible {Array}.
#
# * `other` A {Point} or point-compatible {Array}.
isLessThanOrEqual: (other) ->
@compare(other) <= 0
# Public: Returns a {Boolean} indicating whether this point follows the given
# {Point} or point-compatible {Array}.
#
# * `other` A {Point} or point-compatible {Array}.
isGreaterThan: (other) ->
@compare(other) > 0
# Public: Returns a {Boolean} indicating whether this point follows or is
# equal to the given {Point} or point-compatible {Array}.
#
# * `other` A {Point} or point-compatible {Array}.
isGreaterThanOrEqual: (other) ->
@compare(other) >= 0
isZero: ->
@row is 0 and @column is 0
isPositive: ->
if @row > 0
true
else if @row < 0
false
else
@column > 0
isNegative: ->
if @row < 0
true
else if @row > 0
false
else
@column < 0
###
Section: Conversion
###
# Public: Returns an array of this point's row and column.
toArray: ->
[@row, @column]
# Public: Returns an array of this point's row and column.
serialize: ->
@toArray()
# Public: Returns a string representation of the point.
toString: ->
"(#{@row}, #{@column})"
isNumber = (value) ->
(typeof value is 'number') and (not Number.isNaN(value))