DataLoader-Sort

Sort function for DataLoader to ensure the correct data is returned for the matching keys

DataLoader Sort

Sort function for DataLoader to ensure the correct data is returned for the matching keys

npm version Build Status Coverage Status

Installation

npm i -S dataloader-sort

Notes

  • If no match is found it will return null for this key
  • Includes Flow types

Usage

Basic Usage

import sort from 'dataloader-sort';

const keys = [1, 2, 3];
const data = [
  { id: 3, value: 'three' },
  { id: 1, value: 'one' },
  { id: 4, value: 'four' },
];

const result = sort(keys, data);
Output
[
  { id: 1, value: 'one' },
  null,
  { id: 3, value: 'three' },
]

Custom Prop Usage

const keys = [1, 2, 3];
const data = [
  { other: 3, value: 'three' },
  { other: 1, value: 'one' },
  { other: 2, value: 'two' },
];

const result = sort(keys, data, 'other');
Output
[
  { other: 1, value: 'one' },
  { other: 2, value: 'two' },
  { other: 3, value: 'three' },
]

Subscribe to the Newsletter

Get our latest news,tutorials,guides,tips & deals delivered to your inbox.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.