Join strategies for Iterables, Maps, and Arrays.
- Zero dependencies: small footprint
- Pure ES6: Node.js and browser support
- TypeScript support
import {
fromIterable,
getDiscardedValues,
innerJoin
} from '@evologi/join'
const left = fromIterable(
[
{ id: 'a', value: 1 },
{ id: 'b', value: 2 },
{ id: 'c', value: 4 }
],
item => item.id
)
const right = fromIterable(
[
{ key: 'c', value: 8 },
{ key: 'a', value: 17 },
{ key: 'd', value: 42 }
],
item => item.key
)
function resolve (leftItem, rightItem, key) {
return { _id: key, value: leftItem.value + rightItem.value }
}
const joined = innerJoin(left, right, resolve)
const discarded = getDiscardedValues(joined)
// [ { _id: 'a', value: 18 }, { _id: 'c', value: 12 } ]
console.log(Array.from(joined))
// [ { id: 'b', value: 2 }, { key: 'd', value: 42 } ]
console.log(Array.from(discarded))
Creates a Map
instance from an iterable object. The getKey(value, index)
function is used to generate each item's key.
iterable
<Iterable>
The iterable object to cast.getKey
<Function>
A function that accepts the current iterated value and its index and returns the key that represents the item.mode
<String>
Used to configure key's collision behaviour. By default a duplicated key will throw an error. Use"override"
to override collisions with the last found value. Use"ignore"
to ignore collisions and keep the first value.- Returns:
<Map>
const array = [
{ name: 'harry', surname: 'potter' },
{ name: 'ron', surname: 'weasley' },
{ name: 'hermione', surname: 'granger' }
]
// {
// harry: { name: 'harry', surname: 'potter' },
// ron: { name: 'ron', surname: 'weasley' }
// }
const map = fromIterable(array, item => item.name[0], 'ignore')
Configurable join function.
leftMap
<Map>
rightMap
<Map>
selectOrType
<String> | <Function>
Can be"left"
,"right"
,"inner"
,"outer"
,"leftOuter"
,"rightOuter"
,"full"
, or a function that accepts three agruments.leftValue
<*>
The key's value from the leftMap
object. Can beundefined
.rightValue
<*>
The key's value from the rightMap
object. Can beundefined
.key
<*>
The currently iterated key.- Returns:
<Boolean>
Returnstrue
to include this key inside the reusulting iterable.
resolve
<Function>
A function that acceptsMap
s values and key and returns the resulting value that will be included inside the iterable.leftValue
<*>
The key's value from the leftMap
object. Can beundefined
.rightValue
<*>
The key's value from the rightMap
object. Can beundefined
.key
<*>
The currently iterated key.- Returns:
<*>
- Returns:
<Iterable>
import { fromIterable, join } from '@evologi/join'
const iterable = join(
fromIterable(
[
{ id: 42, message: 'hello' },
{ id: 80, message: 'oh' }
],
item => item.id
),
fromIterable(
[
{ id: 80, message: 'no' },
{ id: 42, message: 'world' }
],
item => item.id
),
(leftValue, rightValue, key) => key < 50, // custom selection
(leftValue, rightValue, key) => ({
id: key,
message: `${leftValue.message} ${rightValue.message}`
})
)
// [ { id: 42, message: 'hello world' } ]
console.log(Array.from(iterable))
Selects values that have matching values in both Map
s.
leftMap
<Map>
rightMap
<Map>
resolve
<Function>
Seejoin
function.- Returns:
<Iterable>
Selects values that are present in one Map
, but not both.
leftMap
<Map>
rightMap
<Map>
resolve
<Function>
Seejoin
function.- Returns:
<Iterable>
Selects all values from both Map
objects.
leftMap
<Map>
rightMap
<Map>
resolve
<Function>
Seejoin
function.- Returns:
<Iterable>
Selects values that are present in the left Map
object.
leftMap
<Map>
rightMap
<Map>
resolve
<Function>
Seejoin
function.- Returns:
<Iterable>
Selects values that are present in the right Map
object.
leftMap
<Map>
rightMap
<Map>
resolve
<Function>
Seejoin
function.- Returns:
<Iterable>
Selects values that are present only in the left Map
object.
leftMap
<Map>
rightMap
<Map>
resolve
<Function>
Seejoin
function.- Returns:
<Iterable>
Selects values that are present only in the right Map
object.
leftMap
<Map>
rightMap
<Map>
resolve
<Function>
Seejoin
function.- Returns:
<Iterable>
Returns the opposite join type string or a negated select function.
selectOrType
<String> | <Function>
- Returns:
<String> | <Function>
Returns an iterable that yields all discarded values from the passed joined iterable.
iterable
<Iterable>
- Returns:
<Iterable>