Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 1x 2x 1x 1x 2x 3x 1x 2x 1x 1x 2x 3x 1x 2x 1x 1x 2x 2x 2x 1x 2x 2x 28x 8x 2x 4x 1x 3x 4x 8x 8x 1x 8x 8x 70x 38x 8x 7x 7x 2x 5x 5x 5x 5x 1x 3x 2x 4x 4x 4x 21x 4x 44x 22x 5x 9x 9x 9x 9x 9x 20x 20x 9x 9x 29x 29x 66x 66x 61x 29x 29x 9x 9x 4x 6x 2x 4x 2x 2x 5x 9x 11x 11x 10x 25x 12x 10x 1x 2x 2x 1x 2x 3x 3x 1x 3x 18x 18x 3x 11x 10x 10x 10x 24x 1x 10x 9x 9x 25x 25x 62x 25x 9x 1x 1x 8x 8x 8x 8x 8x 8x 66x 66x 22x 22x 66x 1x 8x 8x 3x 4x 4x 4x 4x 4x 12x 12x 74x 11x 12x 12x 8x 4x 4x 4x 1x 6x 6x 1x 6x 4x 6x 7x 7x 1x 7x 17x 8x 7x 15x 15x 7x 5x 1x 4x 8x 4x 4x 1x 3x 3x 1x 3x 3x 6x 6x 3x 5x 5x 1x 5x 4x 5x 3x 3x 1x 3x 2x 3x 3x 3x 1x 3x 2x 3x 6x 6x 1x 6x 4x 4x 6x 2x 2x 1x 2x 2x 2x 4x 4x 2x 1x 1x 9x 9x 1x 9x 9x 75x 36x 9x 2x 2x 1x 2x 2x 28x 14x 2x 2x 2x 1x 26x 2x 2x 1x 222x 3x 3x 1x 21x 2x 2x 1x 2x 2x 2x 2x 1x 2x 2x 3x 3x 1x 3x 2x 1x 3x 4x 4x 1x 3x 3x 7x 4x 1x 3x 3x 3x 3x 2x 2x 1x 2x 2x 2x 2x 14x 14x 14x 4x 14x 2x 3x 3x 1x 3x 60x 36x 3x 3x 1x 3x 2x 1x 3x 3x 1x 3x 8x 4x 9x 9x 1x 8x 16x 8x 20x 39x 36x 8x | import * as Inputs from "../inputs";
/**
* Contains various list methods.
* <div>
* <img src="../assets/images/blockly-images/math/math.svg" alt="Blockly Image"/>
* </div>
*/
export class Lists {
/**
* Gets an item from the list at a specific position using zero-based indexing.
* Example: From [10, 20, 30, 40], getting index 2 returns 30
* @param inputs a list and an index
* @returns item
* @group get
* @shortname item by index
* @drawable false
*/
getItem<T>(inputs: Inputs.Lists.ListItemDto<T>): T {
if (inputs.index < 0 || inputs.index >= inputs.list.length) {
throw new Error("Index out of bounds");
}
let result;
if (inputs.clone) {
result = structuredClone(inputs.list[inputs.index]);
} else {
result = inputs.list[inputs.index];
}
return result;
}
/**
* Gets the first item from the list.
* Example: From [10, 20, 30, 40], returns 10
* @param inputs a list
* @returns first item
* @group get
* @shortname first item
* @drawable false
*/
getFirstItem<T>(inputs: Inputs.Lists.ListCloneDto<T>): T {
if (inputs.list.length === 0) {
throw new Error("List is empty");
}
let result;
if (inputs.clone) {
result = structuredClone(inputs.list[0]);
} else {
result = inputs.list[0];
}
return result;
}
/**
* Gets the last item from the list.
* Example: From [10, 20, 30, 40], returns 40
* @param inputs a list
* @returns last item
* @group get
* @shortname last item
* @drawable false
*/
getLastItem<T>(inputs: Inputs.Lists.ListCloneDto<T>): T {
if (inputs.list.length === 0) {
throw new Error("List is empty");
}
let result;
if (inputs.clone) {
result = structuredClone(inputs.list[inputs.list.length - 1]);
} else {
result = inputs.list[inputs.list.length - 1];
}
return result;
}
/**
* Randomly keeps items from the list based on a probability threshold (0 to 1).
* Example: From [1, 2, 3, 4, 5] with threshold 0.5, might return [1, 3, 5] (50% chance for each item)
* @param inputs a list and a threshold for randomization of items to remove
* @returns list with remaining items
* @group get
* @shortname random get threshold
* @drawable false
*/
randomGetThreshold<T>(inputs: Inputs.Lists.RandomThresholdDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
const newList = [];
for (let i = 0; i < inputs.list.length; i++) {
if (Math.random() < inputs.threshold) {
newList.push(res[i]);
}
}
return newList;
}
/**
* Extracts a portion of the list between start and end positions (end is exclusive).
* Example: From [10, 20, 30, 40, 50] with start=1 and end=4, returns [20, 30, 40]
* @param inputs a list and start and end indexes
* @returns sub list
* @group get
* @shortname sublist
* @drawable false
*/
getSubList<T>(inputs: Inputs.Lists.SubListDto<T>): T[] {
let result;
if (inputs.clone) {
result = structuredClone(inputs.list.slice(inputs.indexStart, inputs.indexEnd));
} else {
result = inputs.list.slice(inputs.indexStart, inputs.indexEnd);
}
return result;
}
/**
* Gets every nth item from the list, starting from an optional offset position.
* Example: From [0, 1, 2, 3, 4, 5, 6, 7, 8] with nth=3 and offset=0, returns [0, 3, 6]
* Example: From [0, 1, 2, 3, 4, 5, 6, 7, 8] with nth=2 and offset=1, returns [1, 3, 5, 7]
* @param inputs a list and index
* @returns list with filtered items
* @group get
* @shortname every n-th
* @drawable false
*/
getNthItem<T>(inputs: Inputs.Lists.GetNthItemDto<T>): T[] {
let cloned = inputs.list;
if (inputs.clone) {
cloned = structuredClone(inputs.list);
}
const result = [];
for (let i = 0; i < cloned.length; i++) {
if ((i + inputs.offset) % inputs.nth === 0) {
result.push(cloned[i]);
}
}
return result;
}
/**
* Filters items from the list using a repeating true/false pattern.
* Example: From [0, 1, 2, 3, 4, 5] with pattern [true, true, false], returns [0, 1, 3, 4] (keeps items where pattern is true)
* @param inputs a list and index
* @returns list with filtered items
* @group get
* @shortname by pattern
* @drawable false
*/
getByPattern<T>(inputs: Inputs.Lists.GetByPatternDto<T>): T[] {
const { list, pattern } = inputs;
if (!pattern || pattern.length === 0) {
throw new Error("Pattern is empty or does not exist");
}
const patternLength = pattern.length;
const listLength = list.length;
const result = [];
if (patternLength >= listLength) {
list.forEach((item, index) => {
if (pattern[index] === true) {
result.push(item);
}
});
}
else {
const repeatedPattern = [];
const repeatPatternTimes = Math.ceil(listLength / patternLength);
for (let i = 0; i < repeatPatternTimes; i++) {
repeatedPattern.push(...pattern);
}
list.forEach((item, index) => {
if (repeatedPattern[index] === true) {
result.push(item);
}
});
}
return result;
}
/**
* Merges elements from multiple lists at a specific nesting level, grouping elements by position.
* Example: From [[0, 1, 2], [3, 4, 5]] at level 0, returns [[0, 3], [1, 4], [2, 5]]
* @param inputs lists, level and flatten data
* @returns list with merged lists and flattened lists
* @group get
* @shortname merge levels
* @drawable false
*/
mergeElementsOfLists<T>(inputs: Inputs.Lists.MergeElementsOfLists<T[]>): T[] {
const lists = inputs.lists;
const level = inputs.level;
const elToMerge = [];
const result = [];
lists.forEach(list => {
// flatten to certain level;
const elementsToMerge = list.flat(level);
elToMerge.push(elementsToMerge);
});
const lengthMerge = this.getLongestListLength({ lists: elToMerge });
for (let i = 0; i < lengthMerge; i++) {
const temp = [];
for (let j = 0; j < elToMerge.length; j++) {
const element = elToMerge[j][i];
if (element !== undefined) {
temp.push(element);
}
}
Eif (temp.length > 0) {
result.push(temp);
}
}
let final = [];
if (level > 0) {
for (let i = 0; i < level; i++) {
if (i === level - 1 && i !== 0) {
final[i - 1].push(result);
} else if (i === level - 1) {
final.push(result);
} else {
final.push([]);
}
}
} else {
final = result;
}
return final;
}
/**
* Finds the length of the longest list among multiple lists.
* Example: From [[1, 2], [3, 4, 5, 6], [7]], returns 4 (length of [3, 4, 5, 6])
* @param inputs a list of lists
* @returns number of max length
* @group get
* @shortname longest list length
* @drawable false
*/
getLongestListLength<T>(inputs: Inputs.Lists.GetLongestListLength<T[]>): number {
let longestSoFar = 0;
if (inputs.lists) {
inputs.lists.forEach(l => {
if (l.length > longestSoFar) {
longestSoFar = l.length;
}
});
return longestSoFar;
} else {
return undefined;
}
}
/**
* Reverses the order of items in the list.
* Example: From [1, 2, 3, 4, 5], returns [5, 4, 3, 2, 1]
* @param inputs a list and an index
* @returns item
* @group edit
* @shortname reverse
* @drawable false
*/
reverse<T>(inputs: Inputs.Lists.ListCloneDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
return res.reverse();
}
/**
* Randomly rearranges all items in the list (using Fisher-Yates algorithm).
* Example: From [1, 2, 3, 4, 5], might return [3, 1, 5, 2, 4] (order varies each time)
* @param inputs a list
* @returns shuffled list
* @group edit
* @shortname shuffle
* @drawable false
*/
shuffle<T>(inputs: Inputs.Lists.ListCloneDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
for (let i = res.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[res[i], res[j]] = [res[j], res[i]];
}
return res;
}
/**
* Transposes a 2D list by swapping rows and columns (all sublists must be equal length).
* Example: From [[0, 1, 2], [3, 4, 5]], returns [[0, 3], [1, 4], [2, 5]]
* @param inputs a list of lists to flip
* @returns item
* @group edit
* @shortname flip lists
* @drawable false
*/
flipLists<T>(inputs: Inputs.Lists.ListCloneDto<T[]>): T[][] {
if (inputs.list.length > 0) {
const lengthOfFirstList = inputs.list[0].length;
let allListsSameLength = true;
inputs.list.forEach(l => {
if (l.length !== lengthOfFirstList) {
allListsSameLength = false;
}
});
if (allListsSameLength) {
const result = [];
for (let i = 0; i < lengthOfFirstList; i++) {
const newList = [];
inputs.list.forEach(l => {
newList.push(l[i]);
});
result.push(newList);
}
return result;
} else {
throw new Error("Lists are not of the same length");
}
} else {
throw new Error("List is empty");
}
}
/**
* Splits the list into smaller lists of n elements each.
* Example: From [0, 1, 2, 3, 4, 5, 6, 7, 8] with n=3, returns [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
* Example: From [0, 1, 2, 3, 4] with n=2 and keepRemainder=true, returns [[0, 1], [2, 3], [4]]
* @param inputs a list
* @returns items grouped in lists of n elements
* @group edit
* @shortname group elements
* @drawable false
*/
groupNth<T>(inputs: Inputs.Lists.GroupListDto<T>): T[][] {
const groupElements = (inputs: Inputs.Lists.GroupListDto<T>) => {
const { nrElements, list, keepRemainder } = inputs;
const nrElementsInGroup = nrElements;
const result: T[][] = [];
let currentGroup: T[] = [];
list.forEach((item, index) => {
currentGroup.push(item);
if ((index + 1) % nrElementsInGroup === 0) {
result.push(currentGroup);
currentGroup = [];
}
if (currentGroup.length > 0 && keepRemainder && index === list.length - 1) {
result.push(currentGroup);
}
});
return result;
};
return groupElements(inputs);
}
/**
* Checks whether the list contains a specific item.
* Example: List [10, 20, 30, 40] with item 30 returns true, with item 50 returns false
* @param inputs a list and an item
* @returns true if item is in list
* @group get
* @shortname contains item
* @drawable false
*/
includes<T>(inputs: Inputs.Lists.IncludesDto<T>): boolean {
return inputs.list.includes(inputs.item);
}
/**
* Finds the position (index) of the first occurrence of an item in the list.
* Example: In [10, 20, 30, 20, 40], finding 20 returns 1 (first occurrence), finding 50 returns -1 (not found)
* @param inputs a list and an item
* @returns index of the item or -1 if not found
* @group get
* @shortname find index
* @drawable false
*/
findIndex<T>(inputs: Inputs.Lists.IncludesDto<T>): number {
return inputs.list.indexOf(inputs.item);
}
/**
* Determines the maximum nesting level (depth) of a list structure.
* Example: [1, 2, 3] has depth 1, [[1, 2], [3, 4]] has depth 2, [[[1]]] has depth 3
* @param inputs a list
* @returns number of depth
* @group get
* @shortname max list depth
* @drawable false
*/
getListDepth(inputs: Inputs.Lists.ListCloneDto<[]>): number {
let levels = 0;
let deeperLevelsExist = true;
let flatRes = inputs.list;
while (deeperLevelsExist) {
let foundArray = false;
for (let i = 0; i < flatRes.length; i++) {
if (Array.isArray(flatRes[i])) {
foundArray = true;
}
}
flatRes = flatRes.flat();
if (foundArray) {
levels++;
} else {
levels++;
deeperLevelsExist = false;
}
}
return levels;
}
/**
* Returns the number of items in the list.
* Example: [10, 20, 30, 40, 50] returns 5, [] returns 0
* @param inputs a length list
* @returns a number
* @group get
* @shortname list length
* @drawable false
*/
listLength<T>(inputs: Inputs.Lists.ListCloneDto<T>): number {
return inputs.list.length;
}
/**
* Inserts an item at a specific position in the list.
* Example: In [10, 20, 30, 40], adding 99 at index 2 gives [10, 20, 99, 30, 40]
* @param inputs a list, item and an index
* @returns list with added item
* @group add
* @shortname add item
* @drawable false
*/
addItemAtIndex<T>(inputs: Inputs.Lists.AddItemAtIndexDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (inputs.index >= 0 && inputs.index <= res.length) {
res.splice(inputs.index, 0, inputs.item);
}
return res;
}
/**
* Inserts the same item at multiple specified positions in the list.
* Example: In [10, 20, 30], adding 99 at indexes [0, 2] gives [99, 10, 20, 99, 30]
* @param inputs a list, item and an indexes
* @returns list with added item
* @group add
* @shortname add item at indexes
* @drawable false
*/
addItemAtIndexes<T>(inputs: Inputs.Lists.AddItemAtIndexesDto<T>): T[] {
let cloned = inputs.list;
if (inputs.clone) {
cloned = structuredClone(inputs.list);
}
let cloneIndexes = [...inputs.indexes];
cloneIndexes = cloneIndexes.filter(index => index >= 0 && index <= cloned.length);
cloneIndexes.sort((a, b) => a - b);
cloneIndexes.forEach((index, i) => {
Eif (index >= 0 && index + i <= cloned.length) {
cloned.splice(index + i, 0, inputs.item);
}
}
);
return cloned;
}
/**
* Inserts multiple items at corresponding positions (first item at first index, second item at second index, etc.).
* Example: In [10, 20, 30], adding items [88, 99] at indexes [1, 2] gives [10, 88, 20, 99, 30]
* @param inputs a list, items and an indexes
* @returns list with added items
* @group add
* @shortname add items
* @drawable false
*/
addItemsAtIndexes<T>(inputs: Inputs.Lists.AddItemsAtIndexesDto<T>): T[] {
if (inputs.items.length !== inputs.indexes.length) {
throw new Error("Items and indexes must have the same length");
}
for (let i = 0; i < inputs.indexes.length; i++) {
if (i > 0) {
const prev = inputs.indexes[i - 1];
if (prev > inputs.indexes[i]) {
throw new Error("Indexes must be in ascending order");
}
}
}
let cloned = inputs.list;
if (inputs.clone) {
cloned = structuredClone(inputs.list);
}
const cloneIndexes = [...inputs.indexes];
cloneIndexes.forEach((index, i) => {
Eif (index >= 0 && index + i <= cloned.length) {
cloned.splice(index + i, 0, inputs.items[i]);
}
});
return cloned;
}
/**
* Removes the item at a specific position in the list.
* Example: From [10, 20, 30, 40, 50], removing index 2 gives [10, 20, 40, 50]
* @param inputs a list and index
* @returns list with removed item
* @group remove
* @shortname remove item
* @drawable false
*/
removeItemAtIndex<T>(inputs: Inputs.Lists.RemoveItemAtIndexDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (inputs.index >= 0 && inputs.index <= res.length) {
res.splice(inputs.index, 1);
}
return res;
}
/**
* Removes the first item from the list.
* Example: From [10, 20, 30, 40], returns [20, 30, 40]
* @param inputs a list
* @returns list with first item removed
* @group remove
* @shortname remove first item
* @drawable false
*/
removeFirstItem<T>(inputs: Inputs.Lists.ListCloneDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (res.length > 0) {
res.shift();
}
return res;
}
/**
* Removes the last item from the list.
* Example: From [10, 20, 30, 40], returns [10, 20, 30]
* @param inputs a list
* @returns list with last item removed
* @group remove
* @shortname remove last item
* @drawable false
*/
removeLastItem<T>(inputs: Inputs.Lists.ListCloneDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (res.length > 0) {
res.pop();
}
return res;
}
/**
* Removes an item counting from the end of the list (index 0 = last item, 1 = second-to-last, etc.).
* Example: From [10, 20, 30, 40, 50], removing index 1 from end gives [10, 20, 30, 50] (removes 40)
* @param inputs a list and index from end
* @returns list with removed item
* @group remove
* @shortname remove item from end
* @drawable false
*/
removeItemAtIndexFromEnd<T>(inputs: Inputs.Lists.RemoveItemAtIndexDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (inputs.index >= 0 && inputs.index < res.length) {
const actualIndex = res.length - 1 - inputs.index;
res.splice(actualIndex, 1);
}
return res;
}
/**
* Removes items at multiple specified positions from the list.
* Example: From [10, 20, 30, 40, 50], removing indexes [1, 3] gives [10, 30, 50]
* @param inputs a list and indexes
* @returns list with removed items
* @group remove
* @shortname remove items
* @drawable false
*/
removeItemsAtIndexes<T>(inputs: Inputs.Lists.RemoveItemsAtIndexesDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
const cloneIndexes = [...inputs.indexes];
cloneIndexes.sort((a, b) => b - a);
cloneIndexes.forEach(index => {
Eif (index >= 0 && index < res.length) {
res.splice(index, 1);
}
});
return res;
}
/**
* Clears all items from the list, resulting in an empty list.
* Example: From [10, 20, 30, 40], returns []
* @param inputs a list
* @returns The length is set to 0 and same array memory object is returned
* @group remove
* @shortname remove all items
* @drawable false
*/
removeAllItems<T>(inputs: Inputs.Lists.ListDto<T>): T[] {
inputs.list.length = 0;
return inputs.list;
}
/**
* Removes every nth item from the list, starting from an optional offset position.
* Example: From [0, 1, 2, 3, 4, 5, 6, 7, 8] with nth=3 and offset=0, returns [1, 2, 4, 5, 7, 8] (removes 0, 3, 6)
* @param inputs a list and index
* @returns list with removed item
* @group remove
* @shortname every n-th
* @drawable false
*/
removeNthItem<T>(inputs: Inputs.Lists.RemoveNthItemDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
const result = [];
for (let i = 0; i < res.length; i++) {
if ((i + inputs.offset) % inputs.nth !== 0) {
result.push(res[i]);
}
}
return result;
}
/**
* Randomly removes items from the list based on a probability threshold (0 to 1).
* Example: From [1, 2, 3, 4, 5] with threshold 0.5, might return [2, 4] (50% chance to remove each item)
* @param inputs a list and a threshold for randomization of items to remove
* @returns list with removed items
* @group remove
* @shortname random remove threshold
* @drawable false
*/
randomRemoveThreshold<T>(inputs: Inputs.Lists.RandomThresholdDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
const newList = [];
for (let i = 0; i < inputs.list.length; i++) {
if (Math.random() > inputs.threshold) {
newList.push(res[i]);
}
}
return newList;
}
/**
* Removes duplicate numbers from the list, keeping only the first occurrence of each value.
* Example: From [1, 2, 3, 2, 4, 3, 5], returns [1, 2, 3, 4, 5]
* @param inputs a list of numbers
* @returns list with unique numbers
* @group remove
* @shortname remove duplicate numbers
* @drawable false
*/
removeDuplicateNumbers(inputs: Inputs.Lists.RemoveDuplicatesDto<number>): number[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
return res.filter((value, index, self) => self.indexOf(value) === index);
}
/**
* Removes duplicate numbers that are within a specified tolerance range of each other.
* Example: From [1.0, 1.001, 2.0, 2.002, 3.0] with tolerance 0.01, returns [1.0, 2.0, 3.0]
* @param inputs a list of numbers and the tolerance
* @returns list with unique numbers
* @group remove
* @shortname remove duplicates tol
* @drawable false
*/
removeDuplicateNumbersTolerance(inputs: Inputs.Lists.RemoveDuplicatesToleranceDto<number>): number[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
return res.filter((value, index, self) => self.findIndex(s => Math.abs(s - value) < inputs.tolerance) === index);
}
/**
* Removes duplicate items from the list using strict equality comparison (works with any type).
* Example: From ['a', 'b', 'c', 'a', 'd', 'b'], returns ['a', 'b', 'c', 'd']
* @param inputs a list
* @returns list with unique items
* @group remove
* @shortname remove duplicates
* @drawable false
*/
removeDuplicates<T>(inputs: Inputs.Lists.RemoveDuplicatesDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
return res.filter((value, index, self) => self.indexOf(value) === index);
}
/**
* Appends an item to the end of the list.
* Example: To [10, 20, 30], adding 40 gives [10, 20, 30, 40]
* @param inputs a list and an item
* @returns list with added item
* @group add
* @shortname add item to list
* @drawable false
*/
addItem<T>(inputs: Inputs.Lists.AddItemDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
res.push(inputs.item);
return res;
}
/**
* Adds an item to the beginning of the list.
* Example: To [10, 20, 30], prepending 5 gives [5, 10, 20, 30]
* @param inputs a list and an item
* @returns list with added item
* @group add
* @shortname prepend item to list
* @drawable false
*/
prependItem<T>(inputs: Inputs.Lists.AddItemDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
res.unshift(inputs.item);
return res;
}
/**
* Adds an item either at the beginning or end of the list based on the position parameter.
* Example: To [10, 20, 30], adding 5 at 'first' gives [5, 10, 20, 30], at 'last' gives [10, 20, 30, 5]
* @param inputs a list, item and an option for first or last position
* @returns list with added item
* @group add
* @shortname item at first or last
* @drawable false
*/
addItemFirstLast<T>(inputs: Inputs.Lists.AddItemFirstLastDto<T>): T[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (inputs.position === Inputs.Lists.firstLastEnum.first) {
res.unshift(inputs.item);
} else {
res.push(inputs.item);
}
return res;
}
/**
* Combines multiple lists into a single list by joining them end-to-end.
* Example: From [[1, 2], [3, 4], [5, 6]], returns [1, 2, 3, 4, 5, 6]
* @param inputs lists to concatenate
* @returns concatenated list
* @group add
* @shortname concatenate lists
* @drawable false
*/
concatenate<T>(inputs: Inputs.Lists.ConcatenateDto<T>): T[] {
let result = [];
if (inputs.clone) {
inputs.lists.forEach(list => {
result = result.concat(structuredClone(list));
});
} else {
inputs.lists.forEach(list => {
result = result.concat(list);
});
}
return result;
}
/**
* Creates a new empty list with no items.
* Example: Returns []
* @returns an empty array list
* @group create
* @shortname empty list
* @drawable false
*/
createEmptyList(): [] {
return [];
}
/**
* Creates a new list by repeating an item a specified number of times.
* Example: Repeating 5 three times returns [5, 5, 5]
* @param inputs an item to multiply
* @returns list
* @group create
* @shortname repeat
* @drawable false
*/
repeat<T>(inputs: Inputs.Lists.MultiplyItemDto<T>): T[] {
const result = [];
for (let i = 0; i < inputs.times; i++) {
result.push(inputs.item);
}
return result;
}
/**
* Repeats a pattern of items cyclically until reaching a target list length.
* Example: Pattern [1, 2, 3] with length 7 returns [1, 2, 3, 1, 2, 3, 1]
* @param inputs a list to multiply and a length limit
* @returns list
* @group create
* @shortname repeat in pattern
* @drawable false
*/
repeatInPattern<T>(inputs: Inputs.Lists.RepeatInPatternDto<T>): T[] {
// will repeat the items provided in the patten till the certain length of the list is reached
let inpList = inputs.list;
if (inputs.clone) {
inpList = structuredClone(inputs.list);
}
const res = [];
let counter = 0;
let index = 0;
while (counter < inputs.lengthLimit) {
res.push(inpList[index]);
index++;
if (index === inpList.length) {
index = 0;
}
counter++;
}
return res;
}
/**
* Sorts numbers in ascending (lowest to highest) or descending (highest to lowest) order.
* Example: [5, 2, 8, 1, 9] ascending returns [1, 2, 5, 8, 9], descending returns [9, 8, 5, 2, 1]
* @param inputs a list of numbers to sort and an option for ascending or descending order
* @returns list
* @group sorting
* @shortname sort numbers
* @drawable false
*/
sortNumber(inputs: Inputs.Lists.SortDto<number>): number[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (inputs.orderAsc) {
return res.sort((a, b) => a - b);
} else {
return res.sort((a, b) => b - a);
}
}
/**
* Sorts text strings alphabetically in ascending (A to Z) or descending (Z to A) order.
* Example: ['dog', 'apple', 'cat', 'banana'] ascending returns ['apple', 'banana', 'cat', 'dog']
* @param inputs a list of texts to sort and an option for ascending or descending order
* @returns list
* @group sorting
* @shortname sort texts
* @drawable false
*/
sortTexts(inputs: Inputs.Lists.SortDto<string>): string[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (inputs.orderAsc) {
return res.sort();
} else {
return res.sort().reverse();
}
}
/**
* Sorts objects by comparing numeric values of a specified property.
* Example: [{age: 30}, {age: 20}, {age: 25}] sorted by 'age' ascending returns [{age: 20}, {age: 25}, {age: 30}]
* @param inputs a list to sort, a property to sort by and an option for ascending or descending order
* @returns list
* @group sorting
* @shortname sort json objects
* @drawable false
*/
sortByPropValue(inputs: Inputs.Lists.SortJsonDto<any>): any[] {
let res = inputs.list;
if (inputs.clone) {
res = structuredClone(inputs.list);
}
if (inputs.orderAsc) {
return res.sort((a, b) => a[inputs.property] - b[inputs.property]);
} else {
return res.sort((a, b) => b[inputs.property] - a[inputs.property]);
}
}
/**
* Combines multiple lists by alternating elements from each list (first from list1, first from list2, second from list1, etc.).
* Example: From [[0, 1, 2], [3, 4, 5]], returns [0, 3, 1, 4, 2, 5]
* @param inputs Lists to interleave
* @returns Flattened interleaved list
* @group transform
* @shortname interleave lists
* @drawable false
*/
interleave<T>(inputs: Inputs.Lists.InterleaveDto<T>): T[] {
const lists = inputs.clone ? structuredClone(inputs.lists) : inputs.lists;
if (!lists || lists.length === 0) {
throw new Error("Lists array is empty or does not exist");
}
const result: T[] = [];
const maxLength = Math.max(...lists.map(list => list.length));
for (let i = 0; i < maxLength; i++) {
for (const list of lists) {
if (i < list.length) {
result.push(list[i]);
}
}
}
return result;
}
}
|