Iterable functions - misonou/waterpipe GitHub Wiki
join
Concatenates each element in an array by the given separator.
Input:
[1, 2, 3, 4]
{{join ,}} -> "1,2,3,4"
keys
Gets all keys on an array or object.
Input:
{ values: { foo: 1, bar: 2 }, array: [1, 2, 3, 4] }
{{values keys}} -> "["foo","bar"]"
{{array keys}} -> "[0,1,2,3]"
map
Maps each element of an array or an object to another value. The original array or object is untouched.
If the piped value is an object, the key of each mapped element is preserved.
Input:
{ array: [
{ key: 1, value: 5 },
{ key: 2, value: 6 },
{ key: 3, value: 7 },
{ key: 4, value: 8 }
] }
{{array map value}} -> "[5,6,7,8]"
{{array map [ $value + 1 ]}} -> "[6,7,8,9]"
{{array map [ $key | $value ]}} -> "[1,5],[2,6],[3,7],[4,8](/misonou/waterpipe/wiki/1,5],[2,6],[3,7],[4,8)"
sort, sortby
Sorts the array. The original array is untouched.
Input:
{ simple: [4, 2, 3, 1], complex: [
{ key: 3, value: 2, id: "(3,2)" },
{ key: 3, value: 1, id: "(3,1)" },
{ key: 1, value: 4, id: "(1,4)" },
{ key: 2, value: 3, id: "(2,3)" }
] }
{{simple sort}} -> "[1,2,3,4]"
{{complex sortby key map id}} -> "["(1,4)","(2,3)","(3,2)","(3,1)"]"
{{complex sortby [ $key | $value ] map id}} -> "["(1,4)","(2,3)","(3,1)","(3,2)"]"
reverse
Reverses the array. The original array is untouched.
Input:
[1, 2, 3, 4]
{{reverse}} -> "[4,3,2,1]"
where, first, any, all
Performs operation over an array or object. The original array or object is untouched.
For the where pipe function, if the piped value is an object, the key of each mapped element is preserved.
Input:
[1, 2, 3, 4]
{{where [ odd ]}} -> "[1,3]"
{{first [ even ]}} -> "2"
{{any [ even ]}} -> "true"
{{all [ even ]}} -> "false"
sum
Reduces the array or object into a scalar value, optionally with a seed value.
Input:
{ simple: [4, 2, 3, 1], complex: [
{ key: 3, value: 2, id: "(3,2)" },
{ key: 3, value: 1, id: "(3,1)" },
{ key: 1, value: 4, id: "(1,4)" },
{ key: 2, value: 3, id: "(2,3)" }
] }
{{simple sum}} -> "10"
{{simple sum 5}} -> "15"
{{complex sum [ $id ]}} -> "(3,2)(3,1)(1,4)(2,3)"
{{complex sum [ $value ]}} -> "10"
{{complex sum foobar [ $value ]}} -> "foobar2143"
length
Gets the length of the array or string.
Input:
{ array: [1, 2, 3], string: "foobar" }
{{array length}} -> "3"
{{string length}} -> "6"