also looking at this
`round`, `ceil`, `floor`, `abs`, `clamp`, `mean` additions
#10
3 files
+312
-0
| @@ -706,6 +706,137 @@ function createCollectionOperations<T>(collection: Collection<T>): CollectionOpe | ||
| 706 | 706 | }, 0) |
| 707 | 707 | }, |
| 708 | 708 | |
| 709 | round(this: CollectionOperations<any>, keyOrPrecision?: keyof T | number, precisionOrUndefined?: number) { | |
| 710 | function roundNumberCollection(this: CollectionOperations<number>, precision: number = 0): CollectionOperations<number> { | |
| 711 | const multiplier = 10 ** precision | |
| 712 | return collect( | |
| 713 | collection.items.map(item => | |
| 714 | Math.round(Number(item) * multiplier) / multiplier | |
| 715 | ) | |
| 716 | ) | |
| 717 | } | |
| 718 | ||
| 719 | function roundObjectCollection<K extends keyof T>(key: K, precision: number = 0): CollectionOperations<T> { | |
| 720 | const multiplier = 10 ** precision | |
| 721 | return collect( | |
| 722 | collection.items.map(item => ({ | |
| 723 | ...item, | |
| 724 | [key]: Math.round(Number(item[key]) * multiplier) / multiplier | |
| 725 | })) | |
| 726 | ) | |
| 727 | } | |
| 728 | ||
| 729 | // If first argument is number or undefined, treat as number collection | |
| 730 | if (typeof keyOrPrecision === 'number' || keyOrPrecision === undefined) { | |
| 731 | return roundNumberCollection.call(this, keyOrPrecision ?? 0) | |
| 732 | } | |
| 733 | ||
| 734 | // Otherwise treat as object collection | |
| 735 | return roundObjectCollection(keyOrPrecision, precisionOrUndefined ?? 0) | |
| 736 | }, | |
| 737 | ||
| 738 | ceil(this: CollectionOperations<any>, keyOrPrecision?: keyof T | number, precisionOrUndefined?: number) { | |
| 739 | function ceilNumberCollection(this: CollectionOperations<number>, precision: number = 0): CollectionOperations<number> { | |
| 740 | const multiplier = 10 ** precision | |
| 741 | return collect( | |
| 742 | collection.items.map(item => | |
| 743 | Math.ceil(Number(item) * multiplier) / multiplier | |
| 744 | ) | |
| 745 | ) | |
| 746 | } | |
| 747 | ||
| 748 | function ceilObjectCollection<K extends keyof T>(key: K, precision: number = 0): CollectionOperations<T> { | |
| 749 | const multiplier = 10 ** precision | |
| 750 | return collect( | |
| 751 | collection.items.map(item => ({ | |
| 752 | ...item, | |
| 753 | [key]: Math.ceil(Number(item[key]) * multiplier) / multiplier | |
| 754 | })) | |
| 755 | ) | |
| 756 | } | |
| 757 | ||
| 758 | if (typeof keyOrPrecision === 'number' || keyOrPrecision === undefined) { | |
| 759 | return ceilNumberCollection.call(this, keyOrPrecision ?? 0) | |
| 760 | } | |
| 761 | return ceilObjectCollection(keyOrPrecision, precisionOrUndefined ?? 0) | |
| 762 | }, | |
| 763 | ||
| 764 | floor(this: CollectionOperations<any>, keyOrPrecision?: keyof T | number, precisionOrUndefined?: number) { | |
| 765 | function floorNumberCollection(this: CollectionOperations<number>, precision: number = 0): CollectionOperations<number> { | |
| 766 | const multiplier = 10 ** precision | |
| 767 | return collect( | |
| 768 | collection.items.map(item => | |
| 769 | Math.floor(Number(item) * multiplier) / multiplier | |
| 770 | ) | |
| 771 | ) | |
| 772 | } | |
| 773 | ||
| 774 | function floorObjectCollection<K extends keyof T>(key: K, precision: number = 0): CollectionOperations<T> { | |
| 775 | const multiplier = 10 ** precision | |
| 776 | return collect( | |
| 777 | collection.items.map(item => ({ | |
| 778 | ...item, | |
| 779 | [key]: Math.floor(Number(item[key]) * multiplier) / multiplier | |
| 780 | })) | |
| 781 | ) | |
| 782 | } | |
| 783 | ||
| 784 | if (typeof keyOrPrecision === 'number' || keyOrPrecision === undefined) { | |
| 785 | return floorNumberCollection.call(this, keyOrPrecision ?? 0) | |
| 786 | } | |
| 787 | return floorObjectCollection(keyOrPrecision, precisionOrUndefined ?? 0) | |
| 788 | }, | |
| 789 | ||
| 790 | abs(this: CollectionOperations<any>, key?: keyof T) { | |
| 791 | function absNumberCollection(this: CollectionOperations<number>): CollectionOperations<number> { | |
| 792 | return collect( | |
| 793 | collection.items.map(item => Math.abs(Number(item))) | |
| 794 | ) | |
| 795 | } | |
| 796 | ||
| 797 | function absObjectCollection<K extends keyof T>(key: K): CollectionOperations<T> { | |
| 798 | return collect( | |
| 799 | collection.items.map(item => ({ | |
| 800 | ...item, | |
| 801 | [key]: Math.abs(Number(item[key])) | |
| 802 | })) | |
| 803 | ) | |
| 804 | } | |
| 805 | ||
| 806 | if (key === undefined) { | |
| 807 | return absNumberCollection.call(this) | |
| 808 | } | |
| 809 | return absObjectCollection(key) | |
| 810 | }, | |
| 811 | ||
| 812 | clamp(this: CollectionOperations<any>, keyOrMin: keyof T | number, minOrMax: number, maxOrUndefined?: number) { | |
| 813 | function clampNumberCollection(this: CollectionOperations<number>, min: number, max: number): CollectionOperations<number> { | |
| 814 | return collect( | |
| 815 | collection.items.map(item => | |
| 816 | Math.min(Math.max(Number(item), min), max) | |
| 817 | ) | |
| 818 | ) | |
| 819 | } | |
| 820 | ||
| 821 | function clampObjectCollection<K extends keyof T>(key: K, min: number, max: number): CollectionOperations<T> { | |
| 822 | return collect( | |
| 823 | collection.items.map(item => ({ | |
| 824 | ...item, | |
| 825 | [key]: Math.min(Math.max(Number(item[key]), min), max) | |
| 826 | })) | |
| 827 | ) | |
| 828 | } | |
| 829 | ||
| 830 | if (typeof keyOrMin === 'number') { | |
| 831 | return clampNumberCollection.call(this, keyOrMin, minOrMax) | |
| 832 | } | |
| 833 | return clampObjectCollection(keyOrMin, minOrMax, maxOrUndefined!) | |
| 834 | }, | |
| 835 | ||
| 836 | mean(this: CollectionOperations<any>, key?: keyof T) { | |
| 837 | return this.avg(key) | |
| 838 | }, | |
| 839 | ||
| 709 | 840 | avg(key?: keyof T): number { |
| 710 | 841 | return collection.length ? this.sum(key) / collection.length : 0 |
| 711 | 842 | }, |