-
Notifications
You must be signed in to change notification settings - Fork 47
/
helpers.ts
978 lines (892 loc) · 28 KB
/
helpers.ts
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
// HELPERS
import { DateTime, isDateTime, numberToJsDate, parseDateTime } from "../helpers/dates";
import { memoize } from "../helpers/misc";
import { isNumber, parseNumber } from "../helpers/numbers";
import { _t } from "../translation";
import {
Arg,
CellValue,
FunctionResultNumber,
FunctionResultObject,
Locale,
Matrix,
Maybe,
isMatrix,
} from "../types";
import { CellErrorType, EvaluationError, errorTypes } from "../types/errors";
const SORT_TYPES_ORDER = ["number", "string", "boolean", "undefined"];
export function assert(condition: () => boolean, message: string, value?: string): void {
if (!condition()) {
throw new EvaluationError(message, value);
}
}
export function inferFormat(data: Arg | undefined): string | undefined {
if (data === undefined) {
return undefined;
}
if (isMatrix(data)) {
return data[0][0]?.format;
}
return data.format;
}
export function isEvaluationError(error: Maybe<CellValue>): error is string {
return typeof error === "string" && errorTypes.has(error);
}
// -----------------------------------------------------------------------------
// FORMAT FUNCTIONS
// -----------------------------------------------------------------------------
const expectNumberValueError = (value: string) =>
_t(
"The function [[FUNCTION_NAME]] expects a number value, but '%s' is a string, and cannot be coerced to a number.",
value
);
export const expectNumberRangeError = (lowerBound: number, upperBound: number, value: number) =>
_t(
"The function [[FUNCTION_NAME]] expects a number value between %s and %s inclusive, but receives %s.",
lowerBound.toString(),
upperBound.toString(),
value.toString()
);
export const expectStringSetError = (stringSet: string[], value: string) => {
const stringSetString = stringSet.map((str) => `'${str}'`).join(", ");
return _t(
"The function [[FUNCTION_NAME]] has an argument with value '%s'. It should be one of: %s.",
value,
stringSetString
);
};
export function toNumber(
data: FunctionResultObject | CellValue | undefined,
locale: Locale
): number {
const value = toValue(data);
switch (typeof value) {
case "number":
return value;
case "boolean":
return value ? 1 : 0;
case "string":
if (isNumber(value, locale) || value === "") {
return parseNumber(value, locale);
}
const internalDate = parseDateTime(value, locale);
if (internalDate) {
return internalDate.value;
}
throw new EvaluationError(expectNumberValueError(value));
default:
return 0;
}
}
export function tryToNumber(
value: string | number | boolean | null | undefined,
locale: Locale
): number | undefined {
try {
return toNumber(value, locale);
} catch (e) {
return undefined;
}
}
export function toNumberMatrix(data: Arg, argName: string): Matrix<number> {
return toMatrix(data).map((row) => {
return row.map((cell) => {
if (typeof cell.value !== "number") {
throw new EvaluationError(
_t(
"Function [[FUNCTION_NAME]] expects number values for %s, but got a %s.",
argName,
typeof cell.value
)
);
}
return cell.value;
});
});
}
export function strictToNumber(
data: FunctionResultObject | CellValue | undefined,
locale: Locale
): number {
const value = toValue(data);
if (value === "") {
throw new EvaluationError(expectNumberValueError(value));
}
return toNumber(value, locale);
}
export function toInteger(value: FunctionResultObject | CellValue | undefined, locale: Locale) {
return Math.trunc(toNumber(value, locale));
}
export function strictToInteger(
value: FunctionResultObject | CellValue | undefined,
locale: Locale
) {
return Math.trunc(strictToNumber(value, locale));
}
export function assertNumberGreaterThanOrEqualToOne(value: number) {
assert(
() => value >= 1,
_t(
"The function [[FUNCTION_NAME]] expects a number value to be greater than or equal to 1, but receives %s.",
value.toString()
)
);
}
export function assertNotZero(value: number) {
assert(
() => value !== 0,
_t("Evaluation of function [[FUNCTION_NAME]] caused a divide by zero error."),
CellErrorType.DivisionByZero
);
}
export function toString(data: FunctionResultObject | CellValue | undefined): string {
const value = toValue(data);
switch (typeof value) {
case "string":
return value;
case "number":
return value.toString();
case "boolean":
return value ? "TRUE" : "FALSE";
default:
return "";
}
}
/**
* Normalize range by setting all the string in the range to lowercase and replacing
* accent letters with plain letters
*/
export function normalizeRange<T>(range: T[]) {
return range.map((item) => (typeof item === "string" ? normalizeString(item) : item));
}
/** Normalize string by setting it to lowercase and replacing accent letters with plain letters */
const normalizeString = memoize(function normalizeString(str: string) {
return str
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "");
});
const expectBooleanValueError = (value: string) =>
_t(
"The function [[FUNCTION_NAME]] expects a boolean value, but '%s' is a text, and cannot be coerced to a boolean.",
value
);
export function toBoolean(data: FunctionResultObject | CellValue | undefined): boolean {
const value = toValue(data);
switch (typeof value) {
case "boolean":
return value;
case "string":
if (value) {
let uppercaseVal = value.toUpperCase();
if (uppercaseVal === "TRUE") {
return true;
}
if (uppercaseVal === "FALSE") {
return false;
}
throw new EvaluationError(expectBooleanValueError(value));
} else {
return false;
}
case "number":
return value ? true : false;
default:
return false;
}
}
function strictToBoolean(data: FunctionResultObject | CellValue | undefined): boolean {
const value = toValue(data);
if (value === "") {
throw new EvaluationError(expectBooleanValueError(value));
}
return toBoolean(value);
}
export function toJsDate(
data: FunctionResultObject | CellValue | undefined,
locale: Locale
): DateTime {
const value = toValue(data);
return numberToJsDate(toNumber(value, locale));
}
function toValue(data: FunctionResultObject | CellValue | undefined): CellValue | undefined {
if (typeof data === "object" && data !== null && "value" in data) {
if (isEvaluationError(data.value)) {
throw data;
}
return data.value;
}
if (isEvaluationError(data)) {
throw new EvaluationError("", data as string);
}
return data;
}
// -----------------------------------------------------------------------------
// VISIT FUNCTIONS
// -----------------------------------------------------------------------------
function visitArgs<T extends FunctionResultObject | CellValue>(
args: (T | Matrix<T> | undefined)[],
cellCb: (a: T) => void,
dataCb: (a: T | undefined) => void
): void {
for (let arg of args) {
if (isMatrix(arg)) {
// arg is ref to a Cell/Range
const lenRow = arg.length;
const lenCol = arg[0].length;
for (let y = 0; y < lenCol; y++) {
for (let x = 0; x < lenRow; x++) {
cellCb(arg[x][y]);
}
}
} else {
// arg is set directly in the formula function
dataCb(arg);
}
}
}
export function visitAny(args: Arg[], cb: (a: Maybe<FunctionResultObject>) => void): void {
visitArgs(
args,
(cell) => {
if (isEvaluationError(cell.value)) {
throw cell;
}
cb(cell);
},
(arg) => {
if (isEvaluationError(arg?.value)) {
throw arg;
}
cb(arg);
}
);
}
export function visitNumbers(
args: Arg[],
cb: (arg: FunctionResultNumber) => void,
locale: Locale
): void {
visitArgs(
args,
(cell) => {
if (typeof cell?.value === "number") {
cb(cell as FunctionResultNumber);
}
if (isEvaluationError(cell?.value)) {
throw cell;
}
},
(arg) => {
cb({ value: strictToNumber(arg, locale), format: arg?.format });
}
);
}
// -----------------------------------------------------------------------------
// REDUCE FUNCTIONS
// -----------------------------------------------------------------------------
function reduceArgs<T, M>(
args: (T | Matrix<T>)[],
cellCb: (acc: M, a: T) => M,
dataCb: (acc: M, a: T) => M,
initialValue: M,
dir: "rowFirst" | "colFirst" = "rowFirst"
): M {
let val = initialValue;
for (let arg of args) {
if (isMatrix(arg)) {
// arg is ref to a Cell/Range
const numberOfCols = arg.length;
const numberOfRows = arg[0].length;
if (dir === "rowFirst") {
for (let row = 0; row < numberOfRows; row++) {
for (let col = 0; col < numberOfCols; col++) {
val = cellCb(val, arg[col][row]);
}
}
} else {
for (let col = 0; col < numberOfCols; col++) {
for (let row = 0; row < numberOfRows; row++) {
val = cellCb(val, arg[col][row]);
}
}
}
} else {
// arg is set directly in the formula function
val = dataCb(val, arg);
}
}
return val;
}
export function reduceAny<T, M>(
args: (T | Matrix<T>)[],
cb: (acc: M, a: T) => M,
initialValue: M,
dir: "rowFirst" | "colFirst" = "rowFirst"
): M {
return reduceArgs(args, cb, cb, initialValue, dir);
}
export function reduceNumbers(
args: Arg[],
cb: (acc: number, a: number) => number,
initialValue: number,
locale: Locale
): number {
return reduceArgs(
args,
(acc, arg) => {
const argValue = arg?.value;
if (typeof argValue === "number") {
return cb(acc, argValue);
} else if (isEvaluationError(argValue)) {
throw arg;
}
return acc;
},
(acc, arg) => {
return cb(acc, strictToNumber(arg, locale));
},
initialValue
);
}
export function reduceNumbersTextAs0(
args: Arg[],
cb: (acc: number, a: number) => number,
initialValue: number,
locale: Locale
): number {
return reduceArgs(
args,
(acc, arg) => {
const argValue = arg?.value;
if (argValue !== undefined && argValue !== null) {
if (typeof argValue === "number") {
return cb(acc, argValue);
} else if (typeof argValue === "boolean") {
return cb(acc, toNumber(argValue, locale));
} else if (isEvaluationError(argValue)) {
throw arg;
} else {
return cb(acc, 0);
}
}
return acc;
},
(acc, arg) => {
return cb(acc, toNumber(arg, locale));
},
initialValue
);
}
// -----------------------------------------------------------------------------
// MATRIX FUNCTIONS
// -----------------------------------------------------------------------------
/**
* Generate a matrix of size nColumns x nRows and apply a callback on each position
*/
export function generateMatrix<T>(
nColumns: number,
nRows: number,
callback: (col: number, row: number) => T
): Matrix<T> {
const returned = Array(nColumns);
for (let col = 0; col < nColumns; col++) {
returned[col] = Array(nRows);
for (let row = 0; row < nRows; row++) {
returned[col][row] = callback(col, row);
}
}
return returned;
}
export function matrixMap<T, M>(matrix: Matrix<T>, callback: (value: T) => M): Matrix<M> {
if (matrix.length === 0) {
return [];
}
return generateMatrix(matrix.length, matrix[0].length, (col, row) => callback(matrix[col][row]));
}
export function matrixForEach<T>(matrix: Matrix<T>, fn: (value: T) => void): void {
const numberOfCols = matrix.length;
const numberOfRows = matrix[0]?.length ?? 0;
for (let col = 0; col < numberOfCols; col++) {
for (let row = 0; row < numberOfRows; row++) {
fn(matrix[col][row]);
}
}
}
export function transposeMatrix<T>(matrix: Matrix<T>): Matrix<T> {
if (!matrix.length) {
return [];
}
return generateMatrix(matrix[0].length, matrix.length, (i, j) => matrix[j][i]);
}
// -----------------------------------------------------------------------------
// CONDITIONAL EXPLORE FUNCTIONS
// -----------------------------------------------------------------------------
/**
* This function allows to visit arguments and stop the visit if necessary.
* It is mainly used to bypass argument evaluation for functions like OR or AND.
*/
function conditionalVisitArgs(
args: Arg[],
cellCb: (a: FunctionResultObject | undefined) => boolean,
dataCb: (a: Maybe<FunctionResultObject>) => boolean
): void {
for (let arg of args) {
if (isMatrix(arg)) {
// arg is ref to a Cell/Range
const lenRow = arg.length;
const lenCol = arg[0].length;
for (let y = 0; y < lenCol; y++) {
for (let x = 0; x < lenRow; x++) {
if (!cellCb(arg[x][y] ?? undefined)) return;
}
}
} else {
// arg is set directly in the formula function
if (!dataCb(arg)) return;
}
}
}
export function conditionalVisitBoolean(args: Arg[], cb: (a: boolean) => boolean): void {
return conditionalVisitArgs(
args,
(arg) => {
const argValue = arg?.value;
if (typeof argValue === "boolean") {
return cb(argValue);
}
if (typeof argValue === "number") {
return cb(argValue ? true : false);
}
if (isEvaluationError(argValue)) {
throw arg;
}
return true;
},
(arg) => {
if (arg !== undefined && arg.value !== null) {
return cb(strictToBoolean(arg));
}
return true;
}
);
}
// -----------------------------------------------------------------------------
// CRITERION FUNCTIONS
// -----------------------------------------------------------------------------
type Operator = ">" | ">=" | "<" | "<=" | "<>" | "=";
interface Predicate {
operator: Operator;
operand: number | string | boolean;
}
function getPredicate(descr: string, locale: Locale): Predicate {
let operator: Operator;
let operand: Maybe<CellValue>;
let subString = descr.substring(0, 2);
if (subString === "<=" || subString === ">=" || subString === "<>") {
operator = subString;
operand = descr.substring(2);
} else {
subString = descr.substring(0, 1);
if (subString === "<" || subString === ">" || subString === "=") {
operator = subString;
operand = descr.substring(1);
} else {
operator = "=";
operand = descr;
}
}
if (isNumber(operand, locale) || isDateTime(operand, locale)) {
operand = toNumber(operand, locale);
} else if (operand === "TRUE" || operand === "FALSE") {
operand = toBoolean(operand);
}
return { operator, operand };
}
/**
* Converts a search string containing wildcard characters to a regular expression.
*
* The function iterates over each character in the input string. If the character is a wildcard
* character ("?" or "*") and is not preceded by a "~", it is replaced by the corresponding regular
* expression.
* If the character is a special regular expression character, it is escaped with "\\".
*/
const wildcardToRegExp = memoize(function wildcardToRegExp(operand: string): RegExp {
if (operand === "*") {
return /.+/;
}
let exp = "";
let predecessor = "";
for (let char of operand) {
if (char === "?" && predecessor !== "~") {
exp += ".";
} else if (char === "*" && predecessor !== "~") {
exp += ".*";
} else {
if (char === "*" || char === "?") {
//remove "~"
exp = exp.slice(0, -1);
}
if (["^", ".", "[", "]", "$", "(", ")", "*", "+", "?", "|", "{", "}", "\\"].includes(char)) {
exp += "\\";
}
exp += char;
}
predecessor = char;
}
return new RegExp("^" + exp + "$", "i");
});
function evaluatePredicate(
value: CellValue | undefined = "",
criterion: Predicate,
locale: Locale
): boolean {
const { operator, operand } = criterion;
if (operand === undefined || value === null || operand === null) {
return false;
}
if (typeof operand === "number" && operator === "=") {
if (typeof value === "string" && (isNumber(value, locale) || isDateTime(value, locale))) {
return toNumber(value, locale) === operand;
}
return value === operand;
}
if (operator === "<>" || operator === "=") {
let result: boolean;
if (typeof value === typeof operand) {
if (typeof value === "string" && typeof operand === "string") {
result = wildcardToRegExp(operand).test(value);
} else {
result = value === operand;
}
} else {
result = false;
}
return operator === "=" ? result : !result;
}
if (typeof value === typeof operand) {
switch (operator) {
case "<":
return value < operand;
case ">":
return value > operand;
case "<=":
return value <= operand;
case ">=":
return value >= operand;
}
}
return false;
}
/**
* Functions used especially for predicate evaluation on ranges.
*
* Take ranges with same dimensions and take predicates, one for each range.
* For (i, j) coordinates, if all elements with coordinates (i, j) of each
* range correspond to the associated predicate, then the function uses a callback
* function with the parameters "i" and "j".
*
* Syntax:
* visitMatchingRanges([range1, predicate1, range2, predicate2, ...], cb(i,j), likeSelection)
*
* - range1 (range): The range to check against predicate1.
* - predicate1 (string): The pattern or test to apply to range1.
* - range2: (range, repeatable) ranges to check.
* - predicate2 (string, repeatable): Additional pattern or test to apply to range2.
*
* - cb(i: number, j: number) => void: the callback function.
*
* - isQuery (boolean) indicates if the comparison with a string should be done as a SQL-like query.
* (Ex1 isQuery = true, predicate = "abc", element = "abcde": predicate match the element),
* (Ex2 isQuery = false, predicate = "abc", element = "abcde": predicate not match the element).
* (Ex3 isQuery = true, predicate = "abc", element = "abc": predicate match the element),
* (Ex4 isQuery = false, predicate = "abc", element = "abc": predicate match the element).
*/
export function visitMatchingRanges(
args: Arg[],
cb: (i: number, j: number) => void,
locale: Locale,
isQuery: boolean = false
): void {
const countArg = args.length;
if (countArg % 2 === 1) {
throw new EvaluationError(
_t("Function [[FUNCTION_NAME]] expects criteria_range and criterion to be in pairs.")
);
}
const firstArg = toMatrix(args[0]);
const dimRow = firstArg.length;
const dimCol = firstArg[0].length;
let predicates: Predicate[] = [];
for (let i = 0; i < countArg - 1; i += 2) {
const criteriaRange = toMatrix(args[i]);
if (criteriaRange.length !== dimRow || criteriaRange[0].length !== dimCol) {
throw new EvaluationError(
_t("Function [[FUNCTION_NAME]] expects criteria_range to have the same dimension")
);
}
const description = toString(args[i + 1] as Maybe<FunctionResultObject>);
const predicate = getPredicate(description, locale);
if (isQuery && typeof predicate.operand === "string") {
predicate.operand += "*";
}
predicates.push(predicate);
}
for (let i = 0; i < dimRow; i++) {
for (let j = 0; j < dimCol; j++) {
let validatedPredicates = true;
for (let k = 0; k < countArg - 1; k += 2) {
const criteriaValue = toMatrix(args[k])[i][j].value;
const criterion = predicates[k / 2];
validatedPredicates = evaluatePredicate(criteriaValue ?? undefined, criterion, locale);
if (!validatedPredicates) {
break;
}
}
if (validatedPredicates) {
cb(i, j);
}
}
}
}
// -----------------------------------------------------------------------------
// COMMON FUNCTIONS
// -----------------------------------------------------------------------------
/**
* Perform a dichotomic search on an array and return the index of the nearest match.
*
* The array should be sorted, if not an incorrect value might be returned. In the case where multiple
* element of the array match the target, the method will return the first match if the array is sorted
* in descending order, and the last match if the array is in ascending order.
*
*
* @param data the array in which to search.
* @param target the value to search.
* @param mode "nextGreater/nextSmaller" : return next greater/smaller value if no exact match is found.
* @param sortOrder whether the array is sorted in ascending or descending order.
* @param rangeLength the number of elements to consider in the search array.
* @param getValueInData function returning the element at index i in the search array.
*/
export function dichotomicSearch<T>(
data: T,
target: Maybe<FunctionResultObject>,
mode: "nextGreater" | "nextSmaller" | "strict",
sortOrder: "asc" | "desc",
rangeLength: number,
getValueInData: (range: T, index: number) => CellValue | undefined
): number {
if (target === undefined || target.value === null) {
return -1;
}
if (isEvaluationError(target.value)) {
throw target;
}
const _target = normalizeValue(target.value);
const targetType = typeof _target;
let matchVal: CellValue | undefined = undefined;
let matchValIndex: number | undefined = undefined;
let indexLeft = 0;
let indexRight = rangeLength - 1;
let indexMedian: number;
let currentIndex: number;
let currentVal: CellValue | undefined;
let currentType: string;
while (indexRight - indexLeft >= 0) {
indexMedian = Math.floor((indexLeft + indexRight) / 2);
currentIndex = indexMedian;
currentVal = normalizeValue(getValueInData(data, currentIndex));
currentType = typeof currentVal;
// 1 - linear search to find value with the same type
while (indexLeft < currentIndex && targetType !== currentType) {
currentIndex--;
currentVal = normalizeValue(getValueInData(data, currentIndex));
currentType = typeof currentVal;
}
if (currentType !== targetType || currentVal === undefined || currentVal === null) {
indexLeft = indexMedian + 1;
continue;
}
// 2 - check if value match
if (mode === "strict" && currentVal === _target) {
matchVal = currentVal;
matchValIndex = currentIndex;
} else if (mode === "nextSmaller" && currentVal <= _target) {
if (
matchVal === undefined ||
matchVal === null ||
matchVal < currentVal ||
(matchVal === currentVal && sortOrder === "asc" && matchValIndex! < currentIndex) ||
(matchVal === currentVal && sortOrder === "desc" && matchValIndex! > currentIndex)
) {
matchVal = currentVal;
matchValIndex = currentIndex;
}
} else if (mode === "nextGreater" && currentVal >= _target) {
if (
matchVal === undefined ||
matchVal > currentVal ||
(matchVal === currentVal && sortOrder === "asc" && matchValIndex! < currentIndex) ||
(matchVal === currentVal && sortOrder === "desc" && matchValIndex! > currentIndex)
) {
matchVal = currentVal;
matchValIndex = currentIndex;
}
}
// 3 - give new indexes for the Binary search
if (
(sortOrder === "asc" && currentVal > _target) ||
(sortOrder === "desc" && currentVal <= _target)
) {
indexRight = currentIndex - 1;
} else {
indexLeft = indexMedian + 1;
}
}
// note that valMinIndex could be 0
return matchValIndex !== undefined ? matchValIndex : -1;
}
export type LinearSearchMode = "nextSmaller" | "nextGreater" | "strict" | "wildcard";
/**
* Perform a linear search and return the index of the match.
* -1 is returned if no value is found.
*
* Example:
* - [3, 6, 10], 3 => 0
* - [3, 6, 10], 6 => 1
* - [3, 6, 10], 9 => -1
* - [3, 6, 10], 2 => -1
*
* @param data the array to search in.
* @param target the value to search in the array.
* @param mode if "strict" return exact match index. "nextGreater" returns the next greater
* element from the target and "nextSmaller" the next smaller
* @param numberOfValues the number of elements to consider in the search array.
* @param getValueInData function returning the element at index i in the search array.
* @param reverseSearch if true, search in the array starting from the end.
*/
export function linearSearch<T>(
data: T,
target: Maybe<FunctionResultObject> | undefined,
mode: LinearSearchMode,
numberOfValues: number,
getValueInData: (data: T, index: number) => CellValue | undefined,
reverseSearch = false
): number {
if (target === undefined || target.value === null) {
return -1;
}
if (isEvaluationError(target.value)) {
throw target;
}
const _target = normalizeValue(target.value);
const getValue = reverseSearch
? (data: T, i: number) => getValueInData(data, numberOfValues - i - 1)
: getValueInData;
let indexMatchTarget: (index: number) => boolean = (i) => {
return normalizeValue(getValue(data, i)) === _target;
};
if (
mode === "wildcard" &&
typeof _target === "string" &&
(_target.includes("*") || _target.includes("?"))
) {
const regExp = wildcardToRegExp(_target);
indexMatchTarget = (i) => {
const value = normalizeValue(getValue(data, i));
if (typeof value === "string") {
return regExp.test(value);
}
return false;
};
}
let closestMatch: CellValue | undefined = undefined;
let closestMatchIndex = -1;
if (mode === "nextSmaller") {
indexMatchTarget = (i) => {
const value = normalizeValue(getValue(data, i));
if (
(!closestMatch && compareCellValues(_target, value) >= 0) ||
(compareCellValues(_target, value) >= 0 && compareCellValues(value, closestMatch) > 0)
) {
closestMatch = value;
closestMatchIndex = i;
}
return value === _target;
};
}
if (mode === "nextGreater") {
indexMatchTarget = (i) => {
const value = normalizeValue(getValue(data, i));
if (
(!closestMatch && compareCellValues(_target, value) <= 0) ||
(compareCellValues(_target, value) <= 0 && compareCellValues(value, closestMatch) < 0)
) {
closestMatch = value;
closestMatchIndex = i;
}
return value === _target;
};
}
for (let i = 0; i < numberOfValues; i++) {
if (indexMatchTarget(i)) {
return reverseSearch ? numberOfValues - i - 1 : i;
}
}
return reverseSearch && closestMatchIndex !== -1
? numberOfValues - closestMatchIndex - 1
: closestMatchIndex;
}
/**
* Normalize a value.
* If the cell value is a string, this will set it to lowercase and replacing accent letters with plain letters
*/
function normalizeValue<T>(value: T): T | string {
return typeof value === "string" ? normalizeString(value) : value;
}
function compareCellValues(left: CellValue | undefined, right: CellValue | undefined): number {
let typeOrder = SORT_TYPES_ORDER.indexOf(typeof left) - SORT_TYPES_ORDER.indexOf(typeof right);
if (typeOrder === 0) {
if (typeof left === "string" && typeof right === "string") {
typeOrder = left.localeCompare(right);
} else if (typeof left === "number" && typeof right === "number") {
typeOrder = left - right;
} else if (typeof left === "boolean" && typeof right === "boolean") {
typeOrder = Number(left) - Number(right);
}
}
return typeOrder;
}
export function toMatrix<T>(data: T | Matrix<T> | undefined): Matrix<T> {
if (data === undefined) {
return [[]];
}
return isMatrix(data) ? data : [[data]];
}
/**
* Flatten an array of items, where each item can be a single value or a 2D array, and apply the
* callback to each element.
*
* The 2D array are flattened row first.
*/
export function flattenRowFirst<T, K>(items: Array<T | Matrix<T>>, callback: (val: T) => K): K[] {
/**/
return reduceAny(
items,
(array: K[], val: T) => {
array.push(callback(val));
return array;
},
[],
"rowFirst"
);
}
export function isDataNonEmpty(data: FunctionResultObject | undefined): boolean {
if (data === undefined) {
return false;
}
const { value } = data;
if (value === null || value === "") {
return false;
}
return true;
}