also looking at this
`round`, `ceil`, `floor`, `abs`, `clamp`, `mean` additions
#10
3 files
+312
-0
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -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 | }, |
| @@ -323,6 +323,54 @@ export interface CollectionOperations<T> extends Collection<T> { | ||
| 323 | 323 | median: (key?: keyof T) => number | undefined |
| 324 | 324 | mode: (key?: keyof T) => T | undefined |
| 325 | 325 | |
| 326 | /** | |
| 327 | * Round numeric values to the specified precision | |
| 328 | */ | |
| 329 | round: { | |
| 330 | (this: CollectionOperations<number>, precision?: number): CollectionOperations<number> | |
| 331 | <K extends keyof T>(key: K, precision?: number): CollectionOperations<T> | |
| 332 | } | |
| 333 | ||
| 334 | /** | |
| 335 | * Ceil numeric values | |
| 336 | */ | |
| 337 | ceil: { | |
| 338 | (this: CollectionOperations<number>, precision?: number): CollectionOperations<number> | |
| 339 | <K extends keyof T>(key: K, precision?: number): CollectionOperations<T> | |
| 340 | } | |
| 341 | ||
| 342 | /** | |
| 343 | * Floor numeric values | |
| 344 | */ | |
| 345 | floor: { | |
| 346 | (this: CollectionOperations<number>, precision?: number): CollectionOperations<number> | |
| 347 | <K extends keyof T>(key: K, precision?: number): CollectionOperations<T> | |
| 348 | } | |
| 349 | ||
| 350 | /** | |
| 351 | * Get absolute values | |
| 352 | */ | |
| 353 | abs: { | |
| 354 | (this: CollectionOperations<number>): CollectionOperations<number> | |
| 355 | <K extends keyof T>(key: K): CollectionOperations<T> | |
| 356 | } | |
| 357 | ||
| 358 | /** | |
| 359 | * Clamp values between min and max | |
| 360 | */ | |
| 361 | clamp: { | |
| 362 | (this: CollectionOperations<number>, min: number, max: number): CollectionOperations<number> | |
| 363 | <K extends keyof T>(key: K, min: number, max: number): CollectionOperations<T> | |
| 364 | } | |
| 365 | ||
| 366 | /** | |
| 367 | * Calculate mean (alias for avg) | |
| 368 | */ | |
| 369 | mean: { | |
| 370 | (this: CollectionOperations<number>): number | |
| 371 | <K extends keyof T>(key: K): number | |
| 372 | } | |
| 373 | ||
| 326 | 374 | // Advanced Mathematical Operations |
| 327 | 375 | product: (key?: keyof T) => number |
| 328 | 376 | standardDeviation: (key?: keyof T) => StandardDeviationResult |
| @@ -1956,6 +1956,139 @@ describe('Collection Aggregation Methods', () => { | ||
| 1956 | 1956 | }) |
| 1957 | 1957 | }) |
| 1958 | 1958 | |
| 1959 | describe('round()', () => { | |
| 1960 | it('should round numbers', () => { | |
| 1961 | const numbers = collect([1.234, 5.678, 9.123]) | |
| 1962 | expect(numbers.round().toArray()).toEqual([1, 6, 9]) | |
| 1963 | expect(numbers.round(2).toArray()).toEqual([1.23, 5.68, 9.12]) | |
| 1964 | }) | |
| 1965 | ||
| 1966 | it('should round by key', () => { | |
| 1967 | const items = collect([ | |
| 1968 | { value: 1.234 }, | |
| 1969 | { value: 5.678 }, | |
| 1970 | { value: 9.123 }, | |
| 1971 | ]) | |
| 1972 | expect(items.round('value', 2).toArray()).toEqual([ | |
| 1973 | { value: 1.23 }, | |
| 1974 | { value: 5.68 }, | |
| 1975 | { value: 9.12 }, | |
| 1976 | ]) | |
| 1977 | }) | |
| 1978 | }) | |
| 1979 | ||
| 1980 | describe('ceil()', () => { | |
| 1981 | it('should ceil numbers', () => { | |
| 1982 | const numbers = collect([1.234, 5.678, 9.123]) | |
| 1983 | expect(numbers.ceil().toArray()).toEqual([2, 6, 10]) | |
| 1984 | expect(numbers.ceil(2).toArray()).toEqual([1.24, 5.68, 9.13]) | |
| 1985 | }) | |
| 1986 | ||
| 1987 | it('should ceil by key', () => { | |
| 1988 | const items = collect([ | |
| 1989 | { value: 1.234 }, | |
| 1990 | { value: 5.678 }, | |
| 1991 | { value: 9.123 }, | |
| 1992 | ]) | |
| 1993 | expect(items.ceil('value', 2).toArray()).toEqual([ | |
| 1994 | { value: 1.24 }, | |
| 1995 | { value: 5.68 }, | |
| 1996 | { value: 9.13 }, | |
| 1997 | ]) | |
| 1998 | }) | |
| 1999 | }) | |
| 2000 | ||
| 2001 | describe('floor()', () => { | |
| 2002 | it('should floor numbers', () => { | |
| 2003 | const numbers = collect([1.234, 5.678, 9.123]) | |
| 2004 | expect(numbers.floor().toArray()).toEqual([1, 5, 9]) | |
| 2005 | expect(numbers.floor(2).toArray()).toEqual([1.23, 5.67, 9.12]) | |
| 2006 | }) | |
| 2007 | ||
| 2008 | it('should floor by key', () => { | |
| 2009 | const items = collect([ | |
| 2010 | { value: 1.234 }, | |
| 2011 | { value: 5.678 }, | |
| 2012 | { value: 9.123 }, | |
| 2013 | ]) | |
| 2014 | expect(items.floor('value', 2).toArray()).toEqual([ | |
| 2015 | { value: 1.23 }, | |
| 2016 | { value: 5.67 }, | |
| 2017 | { value: 9.12 }, | |
| 2018 | ]) | |
| 2019 | }) | |
| 2020 | }) | |
| 2021 | ||
| 2022 | describe('abs()', () => { | |
| 2023 | it('should get absolute values', () => { | |
| 2024 | const numbers = collect([-1.5, 2.5, -3.5, 4.5]) | |
| 2025 | expect(numbers.abs().toArray()).toEqual([1.5, 2.5, 3.5, 4.5]) | |
| 2026 | }) | |
| 2027 | ||
| 2028 | it('should get absolute values by key', () => { | |
| 2029 | const items = collect([ | |
| 2030 | { value: -1.5 }, | |
| 2031 | { value: 2.5 }, | |
| 2032 | { value: -3.5 }, | |
| 2033 | ]) | |
| 2034 | expect(items.abs('value').toArray()).toEqual([ | |
| 2035 | { value: 1.5 }, | |
| 2036 | { value: 2.5 }, | |
| 2037 | { value: 3.5 }, | |
| 2038 | ]) | |
| 2039 | }) | |
| 2040 | }) | |
| 2041 | ||
| 2042 | describe('clamp()', () => { | |
| 2043 | it('should clamp numbers', () => { | |
| 2044 | const numbers = collect([1, 5, 10, 15, 20]) | |
| 2045 | expect(numbers.clamp(5, 15).toArray()).toEqual([5, 5, 10, 15, 15]) | |
| 2046 | }) | |
| 2047 | ||
| 2048 | it('should clamp by key', () => { | |
| 2049 | const items = collect([ | |
| 2050 | { value: 1 }, | |
| 2051 | { value: 5 }, | |
| 2052 | { value: 10 }, | |
| 2053 | { value: 15 }, | |
| 2054 | { value: 20 }, | |
| 2055 | ]) | |
| 2056 | expect(items.clamp('value', 5, 15).toArray()).toEqual([ | |
| 2057 | { value: 5 }, | |
| 2058 | { value: 5 }, | |
| 2059 | { value: 10 }, | |
| 2060 | { value: 15 }, | |
| 2061 | { value: 15 }, | |
| 2062 | ]) | |
| 2063 | }) | |
| 2064 | }) | |
| 2065 | ||
| 2066 | describe('mean()', () => { | |
| 2067 | it('should calculate mean of numbers', () => { | |
| 2068 | const numbers = collect([1, 2, 3, 4, 5]) | |
| 2069 | expect(numbers.mean()).toBe(3) | |
| 2070 | }) | |
| 2071 | ||
| 2072 | it('should calculate mean by key', () => { | |
| 2073 | const items = collect([ | |
| 2074 | { value: 10 }, | |
| 2075 | { value: 20 }, | |
| 2076 | { value: 30 }, | |
| 2077 | ]) | |
| 2078 | expect(items.mean('value')).toBe(20) | |
| 2079 | }) | |
| 2080 | ||
| 2081 | it('should handle empty collections', () => { | |
| 2082 | const empty = collect([]) | |
| 2083 | expect(empty.mean()).toBe(0) | |
| 2084 | }) | |
| 2085 | ||
| 2086 | it('should handle NaN values', () => { | |
| 2087 | const withNaN = collect([1, Number.NaN, 3, 4, Number.NaN]) | |
| 2088 | expect(withNaN.mean()).toBe(1.6) // (1 + 0 + 3 + 4 + 0) / 5 | |
| 2089 | }) | |
| 2090 | }) | |
| 2091 | ||
| 1959 | 2092 | describe('median()', () => { |
| 1960 | 2093 | it('should find median of odd length collection', () => { |
| 1961 | 2094 | const numbers = collect([1, 2, 3, 4, 5]) |