@zthun/helpful-fn 6.0.0 → 6.2.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.
@@ -0,0 +1,152 @@
1
+ /**
2
+ * An object that describes 4 corner values of a quadrilateral.
3
+ *
4
+ * This object is almost identical in functionality to a quadrilateral,
5
+ * but is mostly useful for semantics.
6
+ *
7
+ * @param T -
8
+ * The type of data to associate to each corner.
9
+ */
10
+ export interface IZQuadrilateralCorners<T = number> {
11
+ bottomLeft: T;
12
+ bottomRight: T;
13
+ topLeft: T;
14
+ topRight: T;
15
+ }
16
+ /**
17
+ * A type that describes quadrilateral corners on the vertical axis.
18
+ */
19
+ type ZCornersVertical<T> = {
20
+ bottom?: T;
21
+ top?: T;
22
+ };
23
+ /**
24
+ * A type that describes quadrilateral corners on the horizontal axis.
25
+ */
26
+ type ZCornersHorizontal<T> = {
27
+ left?: T;
28
+ right?: T;
29
+ };
30
+ /**
31
+ * An value that a corners object can be built upon.
32
+ */
33
+ export type ZQuadrilateralCornersLike<T = number> = T | ZCornersVertical<T> | ZCornersHorizontal<T> | Partial<IZQuadrilateralCorners<T>> | null | undefined;
34
+ /**
35
+ * An object that can be used to build an {@link IZQuadrilateralCorners} object.
36
+ *
37
+ * @param T -
38
+ * The type of data to associate to each corner.
39
+ */
40
+ export declare class ZQuadrilateralCornersBuilder<T = number> {
41
+ private _corners;
42
+ constructor(start: T);
43
+ /**
44
+ * Sets the bottom left corner value.
45
+ *
46
+ * @param value -
47
+ * The value to set.
48
+ *
49
+ * @returns
50
+ * This object.
51
+ */
52
+ bottomLeft(value: T): this;
53
+ /**
54
+ * Sets the bottom right corner value.
55
+ *
56
+ * @param value -
57
+ * The value to set.
58
+ *
59
+ * @returns
60
+ * This object.
61
+ */
62
+ bottomRight(value: T): this;
63
+ /**
64
+ * Sets the top left corner value.
65
+ *
66
+ * @param value -
67
+ * The value to set.
68
+ *
69
+ * @returns
70
+ * This object.
71
+ */
72
+ topLeft(value: T): this;
73
+ /**
74
+ * Sets the top right corner value.
75
+ *
76
+ * @param value -
77
+ * The value to set.
78
+ *
79
+ * @returns
80
+ * This object.
81
+ */
82
+ topRight(value: T): this;
83
+ /**
84
+ * Sets the bottom left and bottom right values.
85
+ *
86
+ * @param value -
87
+ * The value for bottom left and bottom right.
88
+ *
89
+ * @returns
90
+ * This object.
91
+ */
92
+ bottom(value: T): this;
93
+ /**
94
+ * Sets the bottom left and top left values.
95
+ *
96
+ * @param value -
97
+ * The value for bottom left and top left.
98
+ *
99
+ * @returns
100
+ * This object.
101
+ */
102
+ left(value: T): this;
103
+ /**
104
+ * Sets the bottom right and top right values.
105
+ *
106
+ * @param value -
107
+ * The value for bottom right and top right.
108
+ *
109
+ * @returns
110
+ * This object.
111
+ */
112
+ right(value: T): this;
113
+ /**
114
+ * Sets the top left and top right values.
115
+ *
116
+ * @param value -
117
+ * The value for top left and top right.
118
+ *
119
+ * @returns
120
+ * This object.
121
+ */
122
+ top(value: T): this;
123
+ /**
124
+ * Sets the corner values based on an object that
125
+ * describes a set of quadrilateral corners.
126
+ *
127
+ * @param other -
128
+ * The object that describes the corners.
129
+ *
130
+ * @returns
131
+ * This object.
132
+ */
133
+ from(other: ZQuadrilateralCornersLike<T>): this;
134
+ /**
135
+ * Copies another corners object into this builder.
136
+ *
137
+ * @param other -
138
+ * The corners object to copy.
139
+ *
140
+ * @returns
141
+ * This object.
142
+ */
143
+ copy(other: IZQuadrilateralCorners<T>): this;
144
+ /**
145
+ * Builds the corners.
146
+ *
147
+ * @returns
148
+ * The built corners.
149
+ */
150
+ build(): IZQuadrilateralCorners<T>;
151
+ }
152
+ export {};
@@ -1,3 +1,4 @@
1
+ import { IZPoint2d } from './point.mjs';
1
2
  /**
2
3
  * Represents a object of 4 side values.
3
4
  *
@@ -22,6 +23,14 @@ export interface IZQuadrilateral<T = number> {
22
23
  */
23
24
  top: T;
24
25
  }
26
+ /**
27
+ * Represents an object that can describe a quadrilateral.
28
+ *
29
+ * Note that there will be limitations on what you can describe
30
+ * when building Quadrilaterals of Quadrilaterals, or Quadrilaterals
31
+ * of Point2d's which is not supported and has undefined behavior.
32
+ */
33
+ export type ZQuadrilateralLike<T = number> = T | Partial<IZPoint2d<T>> | Partial<IZQuadrilateral<T>> | null | undefined;
25
34
  /**
26
35
  * Represents a builder for a quadrilateral object.
27
36
  */
@@ -94,6 +103,20 @@ export declare class ZQuadrilateralBuilder<T = number> {
94
103
  * This object.
95
104
  */
96
105
  y(y: T): this;
106
+ /**
107
+ * Constructs a full quadrilateral from an object that describes a quadrilateral.
108
+ *
109
+ * Note the limitations of this method. If T is of type Quadrilateral or Point2d,
110
+ * then this method's behavior is undefined and it will most likely build a
111
+ * corrupt object.
112
+ *
113
+ * @param other -
114
+ * The object to build from.
115
+ *
116
+ * @returns
117
+ * This object.
118
+ */
119
+ from(other: ZQuadrilateralLike<T>): this;
97
120
  /**
98
121
  * Copies another quadrilateral object into the current instance.
99
122
  *
package/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const uuid = require("uuid");
4
+ const lodashEs = require("lodash-es");
4
5
  var ZVerticalAnchor = /* @__PURE__ */ ((ZVerticalAnchor2) => {
5
6
  ZVerticalAnchor2["Top"] = "top";
6
7
  ZVerticalAnchor2["Middle"] = "middle";
@@ -121,6 +122,189 @@ function firstTruthy(fallback, first, ...remaining) {
121
122
  ...remaining
122
123
  );
123
124
  }
125
+ class ZQuadrilateralCornersBuilder {
126
+ constructor(start) {
127
+ this._corners = {
128
+ bottomLeft: start,
129
+ bottomRight: start,
130
+ topLeft: start,
131
+ topRight: start
132
+ };
133
+ }
134
+ /**
135
+ * Sets the bottom left corner value.
136
+ *
137
+ * @param value -
138
+ * The value to set.
139
+ *
140
+ * @returns
141
+ * This object.
142
+ */
143
+ bottomLeft(value) {
144
+ this._corners.bottomLeft = value;
145
+ return this;
146
+ }
147
+ /**
148
+ * Sets the bottom right corner value.
149
+ *
150
+ * @param value -
151
+ * The value to set.
152
+ *
153
+ * @returns
154
+ * This object.
155
+ */
156
+ bottomRight(value) {
157
+ this._corners.bottomRight = value;
158
+ return this;
159
+ }
160
+ /**
161
+ * Sets the top left corner value.
162
+ *
163
+ * @param value -
164
+ * The value to set.
165
+ *
166
+ * @returns
167
+ * This object.
168
+ */
169
+ topLeft(value) {
170
+ this._corners.topLeft = value;
171
+ return this;
172
+ }
173
+ /**
174
+ * Sets the top right corner value.
175
+ *
176
+ * @param value -
177
+ * The value to set.
178
+ *
179
+ * @returns
180
+ * This object.
181
+ */
182
+ topRight(value) {
183
+ this._corners.topRight = value;
184
+ return this;
185
+ }
186
+ /**
187
+ * Sets the bottom left and bottom right values.
188
+ *
189
+ * @param value -
190
+ * The value for bottom left and bottom right.
191
+ *
192
+ * @returns
193
+ * This object.
194
+ */
195
+ bottom(value) {
196
+ return this.bottomLeft(value).bottomRight(value);
197
+ }
198
+ /**
199
+ * Sets the bottom left and top left values.
200
+ *
201
+ * @param value -
202
+ * The value for bottom left and top left.
203
+ *
204
+ * @returns
205
+ * This object.
206
+ */
207
+ left(value) {
208
+ return this.topLeft(value).bottomLeft(value);
209
+ }
210
+ /**
211
+ * Sets the bottom right and top right values.
212
+ *
213
+ * @param value -
214
+ * The value for bottom right and top right.
215
+ *
216
+ * @returns
217
+ * This object.
218
+ */
219
+ right(value) {
220
+ return this.bottomRight(value).topRight(value);
221
+ }
222
+ /**
223
+ * Sets the top left and top right values.
224
+ *
225
+ * @param value -
226
+ * The value for top left and top right.
227
+ *
228
+ * @returns
229
+ * This object.
230
+ */
231
+ top(value) {
232
+ return this.topLeft(value).topRight(value);
233
+ }
234
+ /**
235
+ * Sets the corner values based on an object that
236
+ * describes a set of quadrilateral corners.
237
+ *
238
+ * @param other -
239
+ * The object that describes the corners.
240
+ *
241
+ * @returns
242
+ * This object.
243
+ */
244
+ from(other) {
245
+ function isVerticals(candidate) {
246
+ return candidate != null && (Object.prototype.hasOwnProperty.call(candidate, "bottom") || Object.prototype.hasOwnProperty.call(candidate, "top"));
247
+ }
248
+ function isHorizontals(candidate) {
249
+ return candidate != null && (Object.prototype.hasOwnProperty.call(candidate, "left") || Object.prototype.hasOwnProperty.call(candidate, "right"));
250
+ }
251
+ function isCorners(candidate) {
252
+ return candidate != null && (Object.prototype.hasOwnProperty.call(candidate, "bottomLeft") || Object.prototype.hasOwnProperty.call(candidate, "bottomRight") || Object.prototype.hasOwnProperty.call(candidate, "topLeft") || Object.prototype.hasOwnProperty.call(candidate, "topRight"));
253
+ }
254
+ const { bottomLeft, bottomRight, topLeft, topRight } = this._corners;
255
+ if (isCorners(other)) {
256
+ this._corners.bottomLeft = firstDefined(bottomLeft, other.bottomLeft);
257
+ this._corners.bottomRight = firstDefined(bottomRight, other.bottomRight);
258
+ this._corners.topLeft = firstDefined(topLeft, other.topLeft);
259
+ this._corners.topRight = firstDefined(topRight, other.topRight);
260
+ return this;
261
+ }
262
+ if (isVerticals(other)) {
263
+ this._corners.bottomLeft = firstDefined(bottomLeft, other.bottom);
264
+ this._corners.bottomRight = firstDefined(bottomRight, other.bottom);
265
+ this._corners.topLeft = firstDefined(topLeft, other.top);
266
+ this._corners.topRight = firstDefined(topRight, other.top);
267
+ return this;
268
+ }
269
+ if (isHorizontals(other)) {
270
+ this._corners.bottomLeft = firstDefined(bottomLeft, other.left);
271
+ this._corners.bottomRight = firstDefined(bottomRight, other.right);
272
+ this._corners.topLeft = firstDefined(topLeft, other.left);
273
+ this._corners.topRight = firstDefined(topRight, other.right);
274
+ return this;
275
+ }
276
+ if (typeof other === "object" && lodashEs.isEmpty(other)) {
277
+ return this;
278
+ }
279
+ this._corners.bottomLeft = firstDefined(bottomLeft, other);
280
+ this._corners.bottomRight = firstDefined(bottomRight, other);
281
+ this._corners.topLeft = firstDefined(topLeft, other);
282
+ this._corners.topRight = firstDefined(topRight, other);
283
+ return this;
284
+ }
285
+ /**
286
+ * Copies another corners object into this builder.
287
+ *
288
+ * @param other -
289
+ * The corners object to copy.
290
+ *
291
+ * @returns
292
+ * This object.
293
+ */
294
+ copy(other) {
295
+ this._corners = structuredClone(other);
296
+ return this;
297
+ }
298
+ /**
299
+ * Builds the corners.
300
+ *
301
+ * @returns
302
+ * The built corners.
303
+ */
304
+ build() {
305
+ return structuredClone(this._corners);
306
+ }
307
+ }
124
308
  class ZQuadrilateralBuilder {
125
309
  /**
126
310
  * Initializes a new instance of this object.
@@ -212,6 +396,49 @@ class ZQuadrilateralBuilder {
212
396
  y(y) {
213
397
  return this.bottom(y).top(y);
214
398
  }
399
+ /**
400
+ * Constructs a full quadrilateral from an object that describes a quadrilateral.
401
+ *
402
+ * Note the limitations of this method. If T is of type Quadrilateral or Point2d,
403
+ * then this method's behavior is undefined and it will most likely build a
404
+ * corrupt object.
405
+ *
406
+ * @param other -
407
+ * The object to build from.
408
+ *
409
+ * @returns
410
+ * This object.
411
+ */
412
+ from(other) {
413
+ function isPoint2d(candidate) {
414
+ return candidate != null && (Object.prototype.hasOwnProperty.call(candidate, "x") || Object.prototype.hasOwnProperty.call(candidate, "y"));
415
+ }
416
+ function isQuadrilateral(candidate) {
417
+ return candidate != null && (Object.prototype.hasOwnProperty.call(candidate, "bottom") || Object.prototype.hasOwnProperty.call(candidate, "left") || Object.prototype.hasOwnProperty.call(candidate, "right") || Object.prototype.hasOwnProperty.call(candidate, "top"));
418
+ }
419
+ if (isQuadrilateral(other)) {
420
+ this._quad.bottom = firstDefined(this._quad.bottom, other.bottom);
421
+ this._quad.left = firstDefined(this._quad.left, other.left);
422
+ this._quad.right = firstDefined(this._quad.right, other.right);
423
+ this._quad.top = firstDefined(this._quad.top, other.top);
424
+ return this;
425
+ }
426
+ if (isPoint2d(other)) {
427
+ this._quad.bottom = firstDefined(this._quad.bottom, other.y);
428
+ this._quad.left = firstDefined(this._quad.left, other.x);
429
+ this._quad.right = firstDefined(this._quad.right, other.x);
430
+ this._quad.top = firstDefined(this._quad.top, other.y);
431
+ return this;
432
+ }
433
+ if (typeof other === "object" && lodashEs.isEmpty(other)) {
434
+ return this;
435
+ }
436
+ this._quad.bottom = firstDefined(this._quad.bottom, other);
437
+ this._quad.left = firstDefined(this._quad.left, other);
438
+ this._quad.right = firstDefined(this._quad.right, other);
439
+ this._quad.top = firstDefined(this._quad.top, other);
440
+ return this;
441
+ }
215
442
  /**
216
443
  * Copies another quadrilateral object into the current instance.
217
444
  *
@@ -497,6 +724,7 @@ exports.ZDeserializeTry = ZDeserializeTry;
497
724
  exports.ZHorizontalAnchor = ZHorizontalAnchor;
498
725
  exports.ZOrientation = ZOrientation;
499
726
  exports.ZQuadrilateralBuilder = ZQuadrilateralBuilder;
727
+ exports.ZQuadrilateralCornersBuilder = ZQuadrilateralCornersBuilder;
500
728
  exports.ZRectangle = ZRectangle;
501
729
  exports.ZSerializeJson = ZSerializeJson;
502
730
  exports.ZVerticalAnchor = ZVerticalAnchor;