|
| 1 | +import { |
| 2 | + filter, |
| 3 | + find, |
| 4 | + groupBy, |
| 5 | + indexBy, |
| 6 | + omit, |
| 7 | + pick, |
| 8 | + pipe, |
| 9 | + sortBy, |
| 10 | + sum, |
| 11 | + uniq, |
| 12 | + zip, |
| 13 | +} from 'rambda' |
| 14 | +import { describe, expectTypeOf, it } from 'vitest' |
| 15 | +import { |
| 16 | + chain as radashiChain, |
| 17 | + group as radashiGroup, |
| 18 | + objectify as radashiObjectify, |
| 19 | + omit as radashiOmit, |
| 20 | + pick as radashiPick, |
| 21 | + select as radashiSelect, |
| 22 | + selectFirst as radashiSelectFirst, |
| 23 | + sort as radashiSort, |
| 24 | + sum as radashiSum, |
| 25 | + unique as radashiUnique, |
| 26 | + zip as radashiZip, |
| 27 | +} from 'radashi' |
| 28 | + |
| 29 | +describe('Radashi vs Rambda: API design', () => { |
| 30 | + it('Radashi is data-first, Rambda is curried data-last', () => { |
| 31 | + const items = [3, 1, 2] |
| 32 | + |
| 33 | + // Radashi: data-first, all args at once |
| 34 | + const radashiResult = radashiSort(items, x => x) |
| 35 | + expectTypeOf(radashiResult).toEqualTypeOf<number[]>() |
| 36 | + |
| 37 | + // Rambda: curried data-last, designed for pipe |
| 38 | + const rambdaResult = pipe(items, sortBy(x => x)) |
| 39 | + expectTypeOf(rambdaResult).toEqualTypeOf<number[]>() |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +describe('Radashi vs Rambda: selectFirst vs find (type guard narrowing)', () => { |
| 44 | + it('Rambda find supports type guard narrowing; Radashi selectFirst does not', () => { |
| 45 | + const items = ['hello', 'world', 42] as (string | number)[] |
| 46 | + |
| 47 | + const radashiResult = radashiSelectFirst( |
| 48 | + items, |
| 49 | + x => String(x), |
| 50 | + (x): x is string => typeof x === 'string', |
| 51 | + ) |
| 52 | + // Radashi condition is typed as (item: T, index: number) => boolean |
| 53 | + // No type guard overload — result is still string | undefined |
| 54 | + // because mapper returns string for all T, and condition narrows runtime only |
| 55 | + expectTypeOf(radashiResult).toEqualTypeOf<string | undefined>() |
| 56 | + |
| 57 | + const rambdaResult = pipe( |
| 58 | + items, |
| 59 | + find((x): x is string => typeof x === 'string'), |
| 60 | + ) |
| 61 | + // Rambda find has type guard overload: predicate(value is S) => S | undefined |
| 62 | + expectTypeOf(rambdaResult).toEqualTypeOf<string | undefined>() |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +describe('Radashi vs Rambda: select vs filter (type guard narrowing)', () => { |
| 67 | + it('Rambda filter supports type guard narrowing; Radashi select does not', () => { |
| 68 | + interface Foo { a: number } |
| 69 | + interface Bar extends Foo { b: string } |
| 70 | + const items = [{ a: 1 }, { a: 2, b: 'x' }] as (Foo | Bar)[] |
| 71 | + |
| 72 | + const radashiResult = radashiSelect( |
| 73 | + items, |
| 74 | + x => x, |
| 75 | + (x): x is Bar => 'b' in x, |
| 76 | + ) |
| 77 | + // Radashi condition is (item: T, index: number) => boolean — no narrowing |
| 78 | + // Result type is (Foo | Bar)[] because condition doesn't narrow |
| 79 | + expectTypeOf(radashiResult).toEqualTypeOf<(Foo | Bar)[]>() |
| 80 | + |
| 81 | + const rambdaResult = pipe( |
| 82 | + items, |
| 83 | + filter((x): x is Bar => 'b' in x), |
| 84 | + ) |
| 85 | + // Rambda filter supports type guard → Bar[] |
| 86 | + expectTypeOf(rambdaResult).toEqualTypeOf<Bar[]>() |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +describe('Radashi vs Rambda: group vs groupBy (partial records)', () => { |
| 91 | + it('Both return Partial<Record<K, T[]>>', () => { |
| 92 | + const items = [ |
| 93 | + { category: 'a', val: 1 }, |
| 94 | + { category: 'b', val: 2 }, |
| 95 | + { category: 'a', val: 3 }, |
| 96 | + ] |
| 97 | + |
| 98 | + const radashiResult = radashiGroup(items, x => x.category) |
| 99 | + // Radashi: { [K in Key]?: T[] } — equivalent to Partial<Record<string, T[]>> |
| 100 | + expectTypeOf(radashiResult).toEqualTypeOf< |
| 101 | + Partial<Record<string, { category: string; val: number }[]>> |
| 102 | + >() |
| 103 | + |
| 104 | + const rambdaResult = pipe(items, groupBy(x => x.category)) |
| 105 | + // Rambda: Partial<Record<K, T[]>> where K extends string |
| 106 | + expectTypeOf(rambdaResult).toEqualTypeOf< |
| 107 | + Partial<Record<string, { category: string; val: number }[]>> |
| 108 | + >() |
| 109 | + }) |
| 110 | + |
| 111 | + it('Both allow number/symbol keys via groupBy', () => { |
| 112 | + const items = [1, 2, 3, 4] |
| 113 | + |
| 114 | + const radashiResult = radashiGroup(items, n => (n % 2 === 0 ? 'even' : 'odd')) |
| 115 | + expectTypeOf(radashiResult).toEqualTypeOf< |
| 116 | + Partial<Record<string, number[]>> |
| 117 | + >() |
| 118 | + }) |
| 119 | +}) |
| 120 | + |
| 121 | +describe('Radashi vs Rambda: objectify vs indexBy (key inference)', () => { |
| 122 | + it('Radashi objectify infers literal keys; Rambda indexBy returns wide Record<string, T>', () => { |
| 123 | + const list = [ |
| 124 | + { id: 'a' as const, name: 'Alice' }, |
| 125 | + { id: 'b' as const, name: 'Bob' }, |
| 126 | + ] |
| 127 | + |
| 128 | + // Radashi objectify: Record<Key, Value> with inferred literal keys |
| 129 | + const radashiResult = radashiObjectify(list, x => x.id) |
| 130 | + expectTypeOf(radashiResult).toEqualTypeOf< |
| 131 | + Record<'a' | 'b', { id: 'a' | 'b'; name: string }> |
| 132 | + >() |
| 133 | + |
| 134 | + // Rambda indexBy: takes property key string, returns Record<string, T> |
| 135 | + const rambdaResult = indexBy('id')(list) |
| 136 | + expectTypeOf(rambdaResult).toEqualTypeOf< |
| 137 | + Record<string, { id: 'a' | 'b'; name: string }> |
| 138 | + >() |
| 139 | + }) |
| 140 | + |
| 141 | + it('Radashi objectify supports value mapper', () => { |
| 142 | + const list = [ |
| 143 | + { id: 'a' as const, name: 'Alice' }, |
| 144 | + { id: 'b' as const, name: 'Bob' }, |
| 145 | + ] |
| 146 | + |
| 147 | + // Radashi: objectify(array, getKey, getValue?) — can map values too |
| 148 | + const radashiResult = radashiObjectify(list, x => x.id, x => x.name) |
| 149 | + expectTypeOf(radashiResult).toEqualTypeOf<Record<'a' | 'b', string>>() |
| 150 | + |
| 151 | + // Rambda: indexBy only indexes by property — no value mapping overload |
| 152 | + }) |
| 153 | +}) |
| 154 | + |
| 155 | +describe('Radashi vs Rambda: sort vs sortBy (comparator types)', () => { |
| 156 | + it('Radashi sort only accepts numeric getter; Rambda sortBy accepts any Ord', () => { |
| 157 | + const items = [{ a: 2 }, { a: 1 }, { a: 0 }] |
| 158 | + |
| 159 | + // Radashi sort: getter must return number |
| 160 | + const radashiResult = radashiSort(items, x => x.a) |
| 161 | + expectTypeOf(radashiResult).toEqualTypeOf<{ a: number }[]>() |
| 162 | + |
| 163 | + // Radashi sort supports boolean desc flag |
| 164 | + const radashiDesc = radashiSort(items, x => x.a, true) |
| 165 | + expectTypeOf(radashiDesc).toEqualTypeOf<{ a: number }[]>() |
| 166 | + |
| 167 | + // Rambda sortBy: getter returns any Ord (number | string | boolean | Date) |
| 168 | + const rambdaResult = pipe(items, sortBy(x => x.a)) |
| 169 | + expectTypeOf(rambdaResult).toEqualTypeOf<{ a: number }[]>() |
| 170 | + }) |
| 171 | + |
| 172 | + it('Rambda sortBy supports string and Date comparators; Radashi sort does not', () => { |
| 173 | + const strItems = [{ name: 'Charlie' }, { name: 'Alice' }, { name: 'Bob' }] |
| 174 | + |
| 175 | + const rambdaResult = pipe(strItems, sortBy(x => x.name)) |
| 176 | + expectTypeOf(rambdaResult).toEqualTypeOf<{ name: string }[]>() |
| 177 | + |
| 178 | + // Radashi sort: getter must return number — would error on string |
| 179 | + // @ts-expect-error — Radashi sort getter requires number return |
| 180 | + radashiSort(strItems, x => x.name) |
| 181 | + }) |
| 182 | +}) |
| 183 | + |
| 184 | +describe('Radashi vs Rambda: pick (key inference)', () => { |
| 185 | + it('Radashi pick supports predicate filter; Rambda pick supports string-path', () => { |
| 186 | + const input = { a: 'foo', b: 2, c: 3 } as const |
| 187 | + |
| 188 | + // Radashi pick with key array |
| 189 | + const radashiArray = radashiPick(input, ['a', 'c']) |
| 190 | + expectTypeOf(radashiArray).toEqualTypeOf<{ readonly a: 'foo'; readonly c: 3 }>() |
| 191 | + |
| 192 | + // Radashi pick with predicate filter |
| 193 | + const radashiPredicate = radashiPick(input, (_value, key) => key !== 'b') |
| 194 | + expectTypeOf(radashiPredicate).toEqualTypeOf<{ readonly a: 'foo'; readonly c: 3 }>() |
| 195 | + |
| 196 | + // Rambda pick with array |
| 197 | + const rambdaArray = pipe(input, pick(['a', 'c'])) |
| 198 | + expectTypeOf(rambdaArray.a).toEqualTypeOf<'foo'>() |
| 199 | + |
| 200 | + // Rambda pick with string-path |
| 201 | + const rambdaString = pipe(input, pick('a,c')) |
| 202 | + expectTypeOf(rambdaString.a).toEqualTypeOf<'foo'>() |
| 203 | + }) |
| 204 | +}) |
| 205 | + |
| 206 | +describe('Radashi vs Rambda: omit', () => { |
| 207 | + it('Both infer remaining keys when omitting', () => { |
| 208 | + const input = { a: 'foo', b: 2, c: 3 } as const |
| 209 | + |
| 210 | + const radashiResult = radashiOmit(input, ['b']) |
| 211 | + expectTypeOf(radashiResult).toEqualTypeOf<{ readonly a: 'foo'; readonly c: 3 }>() |
| 212 | + |
| 213 | + const rambdaResult = pipe(input, omit(['b'])) |
| 214 | + expectTypeOf(rambdaResult.a).toEqualTypeOf<'foo'>() |
| 215 | + }) |
| 216 | +}) |
| 217 | + |
| 218 | +describe('Radashi vs Rambda: unique vs uniq', () => { |
| 219 | + it('Radashi unique supports optional toKey; Rambda uniq does not', () => { |
| 220 | + const items = [1, 2, 1, 3] |
| 221 | + |
| 222 | + const rambdaResult = uniq(items) |
| 223 | + expectTypeOf(rambdaResult).toEqualTypeOf<number[]>() |
| 224 | + |
| 225 | + // Radashi unique with optional toKey (similar to uniqBy but combined) |
| 226 | + const radashiResult = radashiUnique(items) |
| 227 | + expectTypeOf(radashiResult).toEqualTypeOf<number[]>() |
| 228 | + |
| 229 | + // Radashi unique with toKey function |
| 230 | + const objItems = [{ id: 1 }, { id: 2 }, { id: 1 }] |
| 231 | + const radashiKeyed = radashiUnique(objItems, x => x.id) |
| 232 | + expectTypeOf(radashiKeyed).toEqualTypeOf<{ id: number }[]>() |
| 233 | + }) |
| 234 | +}) |
| 235 | + |
| 236 | +describe('Radashi vs Rambda: sum', () => { |
| 237 | + it('Radashi sum has mapper overload; Rambda sum only accepts number[]', () => { |
| 238 | + const numbers = [1, 2, 3] |
| 239 | + |
| 240 | + const radashiResult = radashiSum(numbers) |
| 241 | + expectTypeOf(radashiResult).toEqualTypeOf<number>() |
| 242 | + |
| 243 | + const rambdaResult = sum(numbers) |
| 244 | + expectTypeOf(rambdaResult).toEqualTypeOf<number>() |
| 245 | + |
| 246 | + // Radashi sum with mapper function |
| 247 | + const items = [{ value: 1 }, { value: 2 }] |
| 248 | + const radashiMapped = radashiSum(items, x => x.value) |
| 249 | + expectTypeOf(radashiMapped).toEqualTypeOf<number>() |
| 250 | + }) |
| 251 | +}) |
| 252 | + |
| 253 | +describe('Radashi vs Rambda: zip', () => { |
| 254 | + it('Radashi zip is variadic multi-array; Rambda zip is curried pair', () => { |
| 255 | + const keys = ['a', 'b'] |
| 256 | + const vals = [1, 2] |
| 257 | + |
| 258 | + // Radashi zip: variadic, tuple-per-element return |
| 259 | + const radashiResult = radashiZip(keys, vals) |
| 260 | + expectTypeOf(radashiResult).toEqualTypeOf<[string, number][]>() |
| 261 | + |
| 262 | + // Rambda zip: curried, returns KeyValuePair[] |
| 263 | + const rambdaResult = zip(keys)(vals) |
| 264 | + expectTypeOf(rambdaResult).toEqualTypeOf<[string, number][]>() |
| 265 | + }) |
| 266 | +}) |
| 267 | + |
| 268 | +describe('Radashi vs Rambda: chain vs pipe (composition arity)', () => { |
| 269 | + it('Rambda pipe supports up to 20 operations; Radashi chain supports 10', () => { |
| 270 | + const add1 = (x: number) => x + 1 |
| 271 | + const double = (x: number) => x * 2 |
| 272 | + const toString = (x: number) => String(x) |
| 273 | + |
| 274 | + // Radashi: chain data-first, overload-based (10 max) |
| 275 | + const radashiChained = radashiChain(add1, double, toString) |
| 276 | + expectTypeOf(radashiChained(0)).toEqualTypeOf<string>() |
| 277 | + |
| 278 | + // Rambda: pipe data-last, variadic-tuple-based (20 overloads) |
| 279 | + const rambdaResult = pipe(0, add1, double, toString) |
| 280 | + expectTypeOf(rambdaResult).toEqualTypeOf<string>() |
| 281 | + }) |
| 282 | +}) |
| 283 | + |
| 284 | +describe('Radashi vs Rambda: generic type utilities', () => { |
| 285 | + it('Radashi exposes Simplify, IsExactType, Falsy; Rambda does not export these', () => { |
| 286 | + // Radashi exports advanced type utilities: |
| 287 | + // Simplify<T> — flattens intersection types |
| 288 | + // IsExactType<A, B> — exact type equality check |
| 289 | + // Falsy — union of all falsy values |
| 290 | + // These are not exported from Rambda. |
| 291 | + // Test is structural: Radashi's types are ambient, verified at compile time. |
| 292 | + |
| 293 | + // Radashi Falsy type = null | undefined | false | '' | 0 | 0n |
| 294 | + // Rambda has ExcludeFalsy in filter(Boolean) but doesn't export the type |
| 295 | + const radashiFalsy: (typeof import('radashi'))['Falsy'] = false as any |
| 296 | + expectTypeOf(radashiFalsy).not.toEqualTypeOf<true>() |
| 297 | + }) |
| 298 | +}) |
0 commit comments