项目作者: iredchuk

项目描述 :
Join arrays of objects by a common key or with a custom match function
高级语言: JavaScript
项目地址: git://github.com/iredchuk/array-join.git
创建时间: 2017-03-20T23:14:55Z
项目社区:https://github.com/iredchuk/array-join

开源协议:MIT License

下载


array-join

Join two iterables (e.g. arrays) of objects by a common (user-defined) key, similarly to how SQL JOIN, LEFT JOIN and FULL JOIN work.

code style: prettier

Installation

  1. npm install array-join

or

  1. yarn add array-join

Usage

join(leftCollection, rightCollection, getLeftKey, getRightKey, getResultItem)

leftJoin(leftCollection, rightCollection, getLeftKey, getRightKey, getResultItem)

fullJoin(leftCollection, rightCollection, getLeftKey, getRightKey, getResultItem)

  • leftCollection, rightCollection - two Iterable collections to join (e.g. arrays)

  • getLeftKey, getRightKey - functions to extract the key to join on from each item of the first and second collection respectively. The key type should be a primitive type as matching is based on SameValueZero equality.

  • getResultItem - a function to create a result item from two matching items of the given collections. In case of leftJoin the second argument for each non-matching item from the right collection will be undefined, in case of fullJoin both arguments will be undefined in case items from the left or the right collection do not match.

All parameters are required. In a non-TypeScript environment a runtime error will occur when some parameters are skipped.

Examples

  1. import { join, leftJoin, fullJoin } from "array-join";
  2. const people = [
  3. { id: 1, name: "Bob" },
  4. { id: 2, name: "Mary" },
  5. { id: 3, name: "Alice" },
  6. ];
  7. const addresses = [
  8. { personId: 1, address: { city: "New York", country: "US" } },
  9. { personId: 2, address: { city: "Toronto", country: "Canada" } },
  10. { personId: 4, address: { city: "London", country: "UK" } },
  11. ];
  12. console.log(
  13. join(
  14. people,
  15. addresses,
  16. (left) => left.id,
  17. (right) => right.personId,
  18. (left, right) => ({ ...left, ...right })
  19. )
  20. );

will output

  1. [
  2. {
  3. id: 1,
  4. name: 'Bob',
  5. personId: 1,
  6. address: { city: 'New York', country: 'US' }
  7. },
  8. {
  9. id: 2,
  10. name: 'Mary',
  11. personId: 2,
  12. address: { city: 'Toronto', country: 'Canada' }
  13. }
  14. ]

leftJoin

  1. console.log(
  2. leftJoin(
  3. people,
  4. addresses,
  5. (left) => left.id,
  6. (right) => right.personId,
  7. (left, right) => ({ left, right })
  8. )
  9. );

will also include non-matching items from the left collection

  1. [
  2. {
  3. left: { id: 1, name: 'Bob' },
  4. right: { personId: 1, address: { city: 'New York', country: 'US' } }
  5. },
  6. {
  7. left: { id: 2, name: 'Mary' },
  8. right: { personId: 2, address: { city: 'Toronto', country: 'Canada' } }
  9. },
  10. {
  11. left: { id: 3, name: 'Alice' },
  12. right: undefined
  13. }
  14. ]

fullJoin

  1. console.log(
  2. fullJoin(
  3. people,
  4. addresses,
  5. (left) => left.id,
  6. (right) => right.personId,
  7. (left, right) => ({ left, right })
  8. )
  9. );

will include non-matching items from both collections

  1. [
  2. {
  3. left: { id: 1, name: 'Bob' },
  4. right: { personId: 1, address: { city: 'New York', country: 'US' } }
  5. },
  6. {
  7. left: { id: 2, name: 'Mary' },
  8. right: { personId: 2, address: { city: 'Toronto', country: 'Canada' } }
  9. },
  10. {
  11. left: { id: 3, name: 'Alice' },
  12. right: undefined
  13. }
  14. {
  15. left: undefined,
  16. right:{ personId: 4, address: { city: 'London', country: 'UK' } }
  17. }
  18. ]

Breaking changes in version 3

  • Rewritten on TypeScript (which ensures typings to be correct)

  • Simplified interface with all required parameters

  • Accept an arbitrary function to create a result item

  • Deprecated matching items by a custom function for the sake of performance improvement (up to 10x)