_hyperscript
_hyperscript copied to clipboard
Feature Request: Map Command
I think hyperscript would really benefit from a map command that takes an array and maps it to a series of values. We originally discussed this as a property of arrays, but I think it would work easier as a command on its own -- perhaps bundled into a separate library of other similar commands. Here's how it might work
Map takes a variable name (which must be an array) and a result template and returns a new array with values mapped from their original values into the result. It would work very similar to the built-in array.map function, without the lambda calculus. The syntax would look something like map <variable> (to|into) <expression>
When evaluating the result template for each item in the array, these variable names would be available for use:
index: the current index number of the item in the array
value: the value in the array at that particular location
Examples
fetch /my-server
map it into {}
This could also work with template literals
fetch /my-server as json
map it to
`<tr>
<td>Record #${index + 1}</td>
<td>${value.firstName}</td>
<td>${value.lastName}</td>
<td>${value.emailAddress}</td>
</tr>`
put it into #my-table
If this syntax is acceptable, I'm happy to try submitting a PR. If I can figure out how to do it, I'll probably propose match (all|first) from <variable> (on|with) <expression> next :)
This could be part of a LINQ-like feature in hyperscript:
from record in result.data index i select
`<tr>
<td>Record #${i + 1}</td>
<td>${record.firstName}</td>
<td>${record.lastName}</td>
<td>${record.emailAddress}</td>
</tr>`
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/basic-linq-query-operations
Interesting idea. I checked out the LINQ link. It seems like a flavor of SQL. This map command feels like a gateway into a lot of interesting, related features. If working with sets is important, we could easily justify several set-based commands like:
| Command | Example |
|---|---|
| filter | filter it where record.firstName is not null |
| sort | sort it alphabetically by firstName |
| join | join it to <p.queryable /> on record.firstName == @firstName |
| group | as in group it by record.category |
Crazy example:
fetch /my-server as json
filter it where record.firstName is not null
sort it alphabetically by firstName
map it using #myTemplate
put it into #my-table