|
1 | 1 | # any-ts |
2 | 2 |
|
| 3 | +## 0.46.0 |
| 4 | + |
| 5 | +### Minor Changes |
| 6 | + |
| 7 | +- bf5df0f: ### new features |
| 8 | + |
| 9 | + - added `match`, a namespace for advanced pattern matching |
| 10 | + - added `any.functions` to describe any array of functions |
| 11 | + |
| 12 | + ### breaking changes |
| 13 | + |
| 14 | + a few members of the `some` namespace behave differently than before: |
| 15 | + |
| 16 | + - `some.keyOf`: this change was made to support a homomorphic `object.map` function |
| 17 | + that operates on both arrays and objects, preserves structure in either case. |
| 18 | + |
| 19 | + An example implementation: |
| 20 | + |
| 21 | + ```typescript |
| 22 | + /** |
| 23 | + * {@link map `map [overload 1/2]`} ("data-last") |
| 24 | + * |
| 25 | + * [TypeScript playground](https://tsplay.dev/weA2Yw) |
| 26 | + * |
| 27 | + * {@link map `map`} takes two arguments: |
| 28 | + * 1. a function |
| 29 | + * 2. a composite data structure that contains one or more targets to apply the function to |
| 30 | + * |
| 31 | + * A unique feature of this implementation is its polymorphism: it doesn't care whether the |
| 32 | + * composite data structure is an array, or whether it's an object. It will apply the argument |
| 33 | + * to each of the children, and will preserve the structure of the original shape. |
| 34 | + * |
| 35 | + * **Trade-off:** the data-last overload of {@link map `map`} is optimized for function composition. |
| 36 | + * It works best when used inside a call to {@link fn.pipe `fn.pipe`} or {@link fn.flow `fn.flow`}. |
| 37 | + * It comes with greater potential for code re-use, at the cost of slightly slower performance. |
| 38 | + * |
| 39 | + * **Ergonomics:** if you'd prefer to provide both arguments at the same time, see overload #2. |
| 40 | + */ |
| 41 | + export function map<const xs, target>( |
| 42 | + fn: (x: xs[some.keyof<xs>], ix: some.keyof<xs>, xs: xs) => target |
| 43 | + ): (xs: xs) => { [ix in keyof xs]: target }; |
| 44 | + /** |
| 45 | + * {@link map `map [overload 2/2]`} ("data-first") |
| 46 | + * |
| 47 | + * [TypeScript playground](https://tsplay.dev/weA2Yw) |
| 48 | + * |
| 49 | + * {@link map `map`} is a polymorphic function that accepts a function and a data structure (such |
| 50 | + * as an array or object) to apply the function to. |
| 51 | + * |
| 52 | + * A unique feature of this implementation is its ability to abstract away the type of the data |
| 53 | + * structure it maps the function over; whether you pass it an object or an array, it will handle |
| 54 | + * applying the function to the data strucuture's values and returning a data structure whose type |
| 55 | + * corresponds 1-1 with the type of input. |
| 56 | + * |
| 57 | + * **Trade-off:** the data-first overload of {@link map `map`} evaluates eagerly. It comes with |
| 58 | + * slightly better performance than the data-last overload, at the cost of reusability. |
| 59 | + * |
| 60 | + * **Ergonomics:** if you'd prefer to use {@link map `map`} in a pipeline, see overload #1. |
| 61 | + */ |
| 62 | + export function map<const xs, target>( |
| 63 | + xs: xs, |
| 64 | + fn: (x: xs[some.keyof<xs>], xs: xs) => target |
| 65 | + ): { [ix in keyof xs]: target }; |
| 66 | + // impl. |
| 67 | + export function map<const xs, target>( |
| 68 | + ...args: |
| 69 | + | [fn: (x: xs[some.keyof<xs>], ix: some.keyof<xs>, xs: xs) => target] |
| 70 | + | [ |
| 71 | + xs: xs, |
| 72 | + fn: (x: xs[some.keyof<xs>], ix: some.keyof<xs>, xs: xs) => target |
| 73 | + ] |
| 74 | + ) { |
| 75 | + if (args.length === 1) return (xs: xs) => map(xs, args[0]); |
| 76 | + else { |
| 77 | + const [xs, fn] = args; |
| 78 | + if (globalThis.Array.isArray(xs)) return xs.map(fn as never); |
| 79 | + else { |
| 80 | + let out: any.struct = {}; |
| 81 | + for (const k in xs) out[k] = fn(xs[k] as never, k as never, xs); |
| 82 | + return out; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + ``` |
| 87 | + |
| 88 | + - `some.entryOf` |
| 89 | + slightly different semantics to support a polymorphic `object.entries` function |
| 90 | + |
| 91 | + - `some.valueOf` |
| 92 | + slightly different semantics to support a polymorphic `object.values` function |
| 93 | + |
3 | 94 | ## 0.45.2 |
4 | 95 |
|
5 | 96 | ### Patch Changes |
|
0 commit comments