also looking at this
`round`, `ceil`, `floor`, `abs`, `clamp`, `mean` additions
#10
3 files
+312
-0
| @@ -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]) |