UNPKG

3.15 kBTypeScriptView Raw
1/** Represents a point in a buffer in row/column coordinates. */
2export class Point {
3 // Properties
4 /** A zero-indexed number representing the row of the Point. */
5 row: number;
6
7 /** A zero-indexed number representing the column of the Point. */
8 column: number;
9
10 // Construction
11 /**
12 * Create a Point from an array containing two numbers representing the
13 * row and column.
14 */
15 static fromObject(object: PointCompatible, copy?: boolean): Point;
16
17 /** Construct a Point object */
18 constructor(row?: number, column?: number);
19
20 /** Returns a new Point with the same row and column. */
21 copy(): Point;
22
23 /** Returns a new Point with the row and column negated. */
24 negate(): Point;
25
26 // Comparison
27 /** Returns the given Point that is earlier in the buffer. */
28 static min(point1: PointCompatible, point2: PointCompatible): Point;
29
30 /**
31 * Compare another Point to this Point instance.
32 * Returns -1 if this point precedes the argument.
33 * Returns 0 if this point is equivalent to the argument.
34 * Returns 1 if this point follows the argument.
35 */
36 compare(other: PointCompatible): number;
37
38 /**
39 * Returns a boolean indicating whether this point has the same row and
40 * column as the given Point.
41 */
42 isEqual(other: PointCompatible): boolean;
43
44 /** Returns a Boolean indicating whether this point precedes the given Point. */
45 isLessThan(other: PointCompatible): boolean;
46
47 /**
48 * Returns a Boolean indicating whether this point precedes or is equal to
49 * the given Point.
50 */
51 isLessThanOrEqual(other: PointCompatible): boolean;
52
53 /** Returns a Boolean indicating whether this point follows the given Point. */
54 isGreaterThan(other: PointCompatible): boolean;
55
56 /**
57 * Returns a Boolean indicating whether this point follows or is equal to
58 * the given Point.
59 */
60 isGreaterThanOrEqual(other: PointCompatible): boolean;
61
62 // Operations
63 /** Makes this point immutable and returns itself. */
64 freeze(): Readonly<Point>;
65
66 /**
67 * Build and return a new point by adding the rows and columns of the
68 * given point.
69 */
70 translate(other: PointCompatible): Point;
71
72 /**
73 * Build and return a new Point by traversing the rows and columns
74 * specified by the given point.
75 */
76 traverse(other: PointCompatible): Point;
77
78 /** Returns an array of this point's row and column. */
79 toArray(): [number, number];
80
81 /** Returns an array of this point's row and column. */
82 serialize(): [number, number];
83
84 /** Returns a string representation of the point. */
85 toString(): string;
86}
87
88/** The interface that should be implemented for all "point-compatible" objects. */
89export interface PointLike {
90 /** A zero-indexed number representing the row of the Point. */
91 row: number;
92
93 /** A zero-indexed number representing the column of the Point. */
94 column: number;
95}
96
97/** The types usable when constructing a point via the Point::fromObject method. */
98export type PointCompatible = PointLike | [number, number];