-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
742 lines (737 loc) · 19.6 KB
/
index.js
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/**
* @file Contains converters from string to number and vice versa
*/
/**
* Converts column letter to number
* @author AdamL
* @see https://stackoverflow.com/questions/21229180/convert-column-index-into-corresponding-column-letter
* @param {string} col
*
* @returns {number}
*/
const colStringToNumber1 = (col) => {
const length = col.length;
let column = 0;
for (let i = 0; i < length; i++)
column += (col.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
return column;
};
/**
* Converts column letter to number
* @author Flambino
* @see https://codereview.stackexchange.com/questions/90112/a1notation-conversion-to-row-column-index
* @param {string} col
*
* @returns {number}
*/
const colStringToNumber2 = (col) => {
let i, l, chr, sum = 0, A = 'A'.charCodeAt(0), radix = 'Z'.charCodeAt(0) - A + 1;
for (i = 0, l = col.length; i < l; i++) {
chr = col.charCodeAt(i);
sum = sum * radix + chr - A + 1;
}
return sum;
};
/**
* Converts column number to letter
* @author AdamL
* @see https://stackoverflow.com/questions/21229180/convert-column-index-into-corresponding-column-letter
* @param {number} col
*
* @returns {string}
*/
const colNumberToString = (col) => {
let letter = '', temp;
while (col > 0) {
temp = (col - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
col = (col - temp - 1) / 26;
}
return letter;
};
/**
* Converts row string to number
* @param {string} row
*
* @returns {number}
*/
const rowStringToNumber = (row) => parseInt(row, 10);
/**
* Converts row number to string
* @param {number} row
*
* @returns {string}
*/
const rowNumberToString = (row) => String(row);
/**
* @file Contains secondary functions
*/
/**
* Returns the type of a value
* @param {unknown} some
*
* @returns {string}
*/
const type = (some) => typeof some;
/**
* Checks if a value is a string
* @param {unknown} some
*
* @returns {boolean}
*/
const isString = (some) => type(some) === 'string';
/**
* Checks if a value is a number
* @param {unknown} some
*
* @returns {boolean}
*/
const isNumber = (some) => type(some) === 'number' && Number.isInteger(some);
/**
* Checks if a value is a positive number
* @param {unknown} some
*
* @returns {boolean}
*/
const isPositiveNumber = (some) => isNumber(some) && some > 0;
/**
* Checks if a value is a stringified number > 0 like "1", "2", ...
* @param {unknown} some
*
* @returns {boolean}
*/
const isStringifiedNumber = (some) => isString(some) && /^[0-9]+$/.test(some) && isPositiveNumber(+some);
/**
* Checks if a value is a letter between a-zA-Z
* @param {unknown} some
*
* @returns {boolean}
*/
const isLetter = (some) => isString(some) && /^[a-z]+$/i.test(some);
/**
* Checks validation of A1 notation
* @param {unknown} some
*
* @returns {boolean}
*/
const isValidA1 = (some) => isString(some) && /^[A-Z]+\d+(:[A-Z]+\d+)?$/i.test(some);
/**
* @fileOverview A1 notation errors
*/
class A1Error extends Error {
constructor(something) {
const str = JSON.stringify(something);
super(str);
this.name = 'A1Error';
this.message = str;
}
/**
* Was string
*/
s() {
this.message = `Invalid A1 notation: ${this.message}`;
return this;
}
/**
* Was number
*/
n() {
this.message = `Invalid A1 number(s): ${this.message}`;
return this;
}
/**
* Was unknown
*/
u() {
this.message = `Invalid A1 argument(s): ${this.message}`;
return this;
}
}
/**
* @file Contains enums
*/
var Axis;
(function (Axis) {
Axis["X"] = "col";
Axis["Y"] = "row";
})(Axis || (Axis = {}));
/**
* @file Math operations and converting in A1 notation
* Supports A1 notation like "A1" and "A1:B2"
* @author FLighter
*/
class A1 {
// Regular expression for parsing
static _reg = /^([A-Z]+)(\d+)(?::([A-Z]+)(\d+))?$/;
/**
* Example: A1:B2
*/
_colStart = 0; // A -> 1
_rowStart = 0; // 1 -> 1
_colEnd = 0; // B -> 2
_rowEnd = 0; // 2 -> 2
_converter = 1; // converter 1 | 2
/**
* Parses A1 notation
* @param {string} a1
* @param {1 | 2} converter
*
* @return {object} {cs: number, rs: number, ce: number, re: number}
*/
static _parse(a1, converter) {
let [, cs, // col start // A
rs, // row start // 1
ce, // col end // B
re, // row end // 2
] = a1.toUpperCase().match(this._reg) ?? [];
ce = ce || cs;
re = re || rs;
const colStart = this._A1Col(cs, converter), colEnd = this._A1Col(ce, converter), rowStart = rowStringToNumber(rs), rowEnd = rowStringToNumber(re);
// For non-standard A1
return {
cs: colEnd > colStart ? colStart : colEnd,
rs: rowEnd > rowStart ? rowStart : rowEnd,
ce: colEnd > colStart ? colEnd : colStart,
re: rowEnd > rowStart ? rowEnd : rowStart,
};
}
/**
* Converts column letter to number using converter 1 or 2
* @param {string} a1
* @param {1 | 2} converter
*
* @return {number}
*/
static _A1Col(a1, converter) {
return converter === 1 ? colStringToNumber1(a1) : colStringToNumber2(a1);
}
/******************
* STATIC METHODS
******************/
/**
* Checks A1 notation
* @param {string} a1
*
* @return {boolean}
*/
static isValid(a1) {
return isValidA1(a1);
}
/**
* Converts the first column letter from A1 to number
* @param {string} a1
* @param {1 | 2} [converter = 1]
*
* @return {number}
*/
static getCol(a1, converter = 1) {
if (!isValidA1(a1))
throw new A1Error(a1).s();
return this._parse(a1, converter).cs;
}
/**
* Converts the last column letter from A1 to number
* @param {string} a1
* @param {1 | 2} [converter = 1]
*
* @return {number}
*/
static getLastCol(a1, converter = 1) {
if (!isValidA1(a1))
throw new A1Error(a1).s();
return this._parse(a1, converter).ce;
}
/**
* Converts number to column letter in A1
* @param {number} col
*
* @return {string}
*/
static toCol(col) {
if (!isPositiveNumber(col))
throw new A1Error(col).n();
return colNumberToString(col);
}
/**
* Converts the first row string to number
* @param {string} a1
*
* @return {number}
*/
static getRow(a1) {
if (!isValidA1(a1))
throw new A1Error(a1).s();
return this._parse(a1, 1).rs;
}
/**
* Converts the last row string to number
* @param {string} a1
*
* @return {number}
*/
static getLastRow(a1) {
if (!isValidA1(a1))
throw new A1Error(a1).s();
return this._parse(a1, 1).re;
}
/**
* Converts number to row string in A1
* @param {number} row
*
* @return {string}
*/
static toRow(row) {
if (!isPositiveNumber(row))
throw new A1Error(row).n();
return rowNumberToString(row);
}
/**
* @param {string} a1
* @param {1 | 2} [converter = 1]
*
* @return {number} columns count
*/
static getWidth(a1, converter = 1) {
if (!isValidA1(a1))
throw new A1Error(a1).s();
let { ce, cs } = this._parse(a1, converter);
return ce - cs + 1;
}
/**
* @param {string} a1
*
* @return {number} rows count
*/
static getHeight(a1) {
if (!isValidA1(a1))
throw new A1Error(a1).s();
let { re, rs } = this._parse(a1, 1);
return re - rs + 1;
}
/***************
* CONSTRUCTOR
***************/
/**
* It handles case:
* constructor(object: options)
* @param {options} options
*/
_initObject(options) {
const { a1Start, a1End, colStart, colEnd, rowStart, rowEnd, nCols, nRows, converter, } = options;
// Set converter
this._converter = converter === 2 ? 2 : 1;
let cs = 0;
let ce = 0;
let rs = 0;
let re = 0;
const getValue = (some, canBeLetter = true) => {
if (isPositiveNumber(some) || isStringifiedNumber(some))
return +some;
if (canBeLetter && isLetter(some))
return A1._A1Col(some, this._converter);
return 0;
};
/**
* Define start range
*/
// From a1Start
if (isValidA1(a1Start)) {
const a1StartParsed = A1._parse(a1Start, this._converter);
cs = a1StartParsed.cs;
rs = a1StartParsed.rs;
const equalCol = a1StartParsed.cs === a1StartParsed.ce, equalRow = a1StartParsed.rs === a1StartParsed.re, equal = equalCol && equalRow;
if (!equal || (equal && a1Start.includes(':'))) {
ce = a1StartParsed.ce;
re = a1StartParsed.re;
}
}
// From colStart & rowStart
if (!cs && colStart) {
cs = getValue(colStart);
}
if (!rs && rowStart) {
rs = getValue(rowStart, false);
}
/**
* Define end range
*/
// From a1End
if (!ce && !re && isValidA1(a1End)) {
const a1EndParsed = A1._parse(a1End, this._converter);
ce = a1EndParsed.ce;
re = a1EndParsed.re;
}
// From colEnd & rowEnd
if (!ce && colEnd)
ce = getValue(colEnd);
if (!re && rowEnd)
re = getValue(rowEnd, false);
// From nCols & nRows
if (!ce && cs && isPositiveNumber(nCols))
ce = cs + nCols - 1;
if (!re && rs && isPositiveNumber(nRows))
re = rs + nRows - 1;
/**
* If only start/end range was defined
*/
(cs && !ce) && (ce = cs);
(!cs && ce) && (cs = ce);
(rs && !re) && (re = rs);
(!rs && re) && (rs = re);
/**
* Check results
*/
if (!cs || !rs || !ce || !re)
throw new A1Error(options).u();
/**
* Set ranges
*/
this._colStart = cs;
this._rowStart = rs;
this._colEnd = ce;
this._rowEnd = re;
}
/**
* It handles cases:
* constructor(col: number, row: number)
* constructor(col: number, row: number, nRows: number)
* constructor(col: number, row: number, nRows: number, nCols: number)
* @param {number[]} args
*/
_initNumber(...args) {
let [col, row, nRows, nCols] = args;
nRows = nRows || 1;
nCols = nCols || 1;
let all = [col, row, nRows, nCols];
if (!all.every(n => isPositiveNumber(n)))
throw new A1Error(all.join(', ')).n();
this._colStart = col; // the first col
this._rowStart = row; // the first row
this._colEnd = col + nCols - 1; // how many cols in total (cols length)
this._rowEnd = row + nRows - 1; // how many rows in total (rows length)
}
/**
* It handles cases:
* constructor(range: string)
* constructor(rangeStart: string, rangeEnd: string)
* @param {string[]} args
*/
_initString(...args) {
const [rangeStart, rangeEnd] = args;
const range = rangeEnd
? `${rangeStart}:${rangeEnd}` // rangeStart: string, rangeEnd: string
: rangeStart; // range: string
if (!isValidA1(range))
throw new A1Error(range).s();
const { cs, rs, ce, re } = A1._parse(range, this._converter);
this._colStart = cs;
this._rowStart = rs;
this._colEnd = ce;
this._rowEnd = re;
}
constructor(something, something2, nRows, nCols) {
// No arguments
if (!arguments.length)
throw new A1Error().u();
// Object
if (something && type(something) === 'object')
this._initObject(something);
// Number
else if (isNumber(something))
this._initNumber.apply(this, arguments);
// String
else if (isString(something))
this._initString.apply(this, arguments);
// Unknown argument
else
throw new A1Error(something).u();
}
/***********
* METHODS
***********/
/**
* @return {string} in A1 notation
*/
get() {
const start = colNumberToString(this._colStart) + rowNumberToString(this._rowStart), end = colNumberToString(this._colEnd) + rowNumberToString(this._rowEnd);
return start === end ? start : `${start}:${end}`;
}
/**
* @return {string} in A1 notation
*/
toString() {
return this.get();
}
/**
* @typedef {Object} Result
* @property {number} colStart
* @property {number} rowStart
* @property {number} colEnd
* @property {number} rowEnd
* @property {string} a1
* @property {number} rowsCount
* @property {number} colsCount
*
* @return {Result} full information about the range
*/
toJSON() {
return {
colStart: this._colStart,
rowStart: this._rowStart,
colEnd: this._colEnd,
rowEnd: this._rowEnd,
a1: this.get(),
rowsCount: this._rowEnd - this._rowStart + 1,
colsCount: this._colEnd - this._colStart + 1,
};
}
/**
* @return {number} start column
*/
getCol() {
return this._colStart;
}
/**
* @return {number} end column
*/
getLastCol() {
return this._colEnd;
}
/**
* @return {number} start row
*/
getRow() {
return this._rowStart;
}
/**
* @return {number} end row
*/
getLastRow() {
return this._rowEnd;
}
/**
* @return {number} columns count
*/
getWidth() {
return this._colEnd - this._colStart + 1;
}
/**
* @return {number} rows count
*/
getHeight() {
return this._rowEnd - this._rowStart + 1;
}
/**
* @return {A1} copy of this object
*/
copy() {
return new A1(this.get());
}
/**
* Sets a value to the start column
* @param {string | number} val
*
* @returns {this}
*/
setCol(val) {
return this._setFields(val, '_colStart', Axis.X);
}
/**
* Sets a value to the end column
* @param {string | number} val
*
* @returns {this}
*/
setLastCol(val) {
return this._setFields(val, '_colEnd', Axis.X);
}
/**
* Sets a value to the start row
* @param {string | number} val
*
* @returns {this}
*/
setRow(val) {
return this._setFields(val, '_rowStart', Axis.Y, false);
}
/**
* Sets a value to the end row
* @param {string | number} val
*
* @returns {this}
*/
setLastRow(val) {
return this._setFields(val, '_rowEnd', Axis.Y, false);
}
/**
* Adds N cells to range along the x-axis
* if count >= 0 - adds to right
* if count < 0 - adds to left
* @param {number} count
*
* @return {this}
*/
addX(count) {
return this._addFields(count, Axis.X);
}
/**
* Adds N cells to range along the y-axis
* if count >= 0 - adds to bottom
* if count < 0 - adds to top
* @param {number} count
*
* @return {this}
*/
addY(count) {
return this._addFields(count, Axis.Y);
}
/**
* Adds N cells to range along the x/y-axis
* @param {number} countX
* @param {number} countY
*
* @return {this}
*/
add(countX, countY) {
return this.addX(countX).addY(countY);
}
/**
* Removes N cells from range along the x-axis
* if count >= 0 - removes from right
* if count < 0 - removes from left
* @param {number} count
*
* @return {this}
*/
removeX(count) {
return this._removeFields(count, Axis.X);
}
/**
* Removes N cells from range along the y-axis
* if count >= 0 - removes from bottom
* if count < 0 - removes from top
* @param {number} count
*
* @return {this}
*/
removeY(count) {
return this._removeFields(count, Axis.Y);
}
/**
* Removes N cells from range along the x/y-axis
* @param {number} countX
* @param {number} countY
*
* @return {this}
*/
remove(countX, countY) {
return this.removeX(countX).removeY(countY);
}
/**
* Shifts the range along the x-axis
* If offset >= 0 - shifts to right
* If offset < 0 - shifts to left
* @param {number} offset
*
* @return {this}
*/
shiftX(offset) {
return this._shiftFields(offset, Axis.X);
}
/**
* Shifts the range along the y-axis
* If offset >= 0 - shifts to bottom
* If offset < 0 - shifts to top
* @param {number} offset
*
* @return {this}
*/
shiftY(offset) {
return this._shiftFields(offset, Axis.Y);
}
/**
* Shifts the range along the x/y-axis
* @param {number} offsetX
* @param {number} offsetY
*
* @return {this}
*/
shift(offsetX, offsetY) {
return this.shiftX(offsetX).shiftY(offsetY);
}
/**
* Sets a value to the specified field
* @param {string | number} val
* @param {string} field
* @param {Axis} axis
* @param {boolean} [canBeLetter = true]
*
* @returns {this}
*/
_setFields(val, field, axis, canBeLetter = true) {
if (isPositiveNumber(val) || isStringifiedNumber(val))
this[field] = +val;
else if (canBeLetter && isLetter(val))
this[field] = A1._A1Col(val, this._converter);
else
throw new A1Error(val).u();
if (this[`_${axis}Start`] > this[`_${axis}End`])
throw new A1Error(`The first column or row can't be bigger than the last, got: ${val}`);
return this;
}
/**
* Adds N cells to the range along the x/y-axis
* @param {number} count
* @param {Axis} axis
*
* @returns {this}
*/
_addFields(count, axis) {
if (!isNumber(count))
throw new A1Error(count).u();
const fieldStart = `_${axis}Start`, fieldEnd = `_${axis}End`;
count >= 0
? this[fieldEnd] += count
: this[fieldStart] += count;
(this[fieldStart] <= 0) && (this[fieldStart] = 1);
return this;
}
/**
* Removes N cells from the range along the x/y-axis
* @param {number} count
* @param {Axis} axis
*
* @returns {this}
*/
_removeFields(count, axis) {
if (!isNumber(count))
throw new A1Error(count).u();
const fieldStart = `_${axis}Start`, fieldEnd = `_${axis}End`;
if (count >= 0) {
this[fieldEnd] -= count;
(this[fieldEnd] < this[fieldStart]) && (this[fieldEnd] = this[fieldStart]);
}
else {
this[fieldStart] -= count;
(this[fieldStart] > this[fieldEnd]) && (this[fieldStart] = this[fieldEnd]);
}
return this;
}
/**
* Shifts the specified fields along x/y-axis
* @param {number} offset
* @param {Axis} axis
*
* @returns {this}
*/
_shiftFields(offset, axis) {
if (!isNumber(offset))
throw new A1Error(offset).u();
const fieldStart = `_${axis}Start`, fieldEnd = `_${axis}End`;
const diff = this[fieldEnd] - this[fieldStart], start = this[fieldStart] + offset, end = this[fieldEnd] + offset;
this[fieldStart] = start > 0 ? start : 1;
this[fieldEnd] = start > 0 ? end : diff + 1;
return this;
}
}
export { A1 as default };