-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TypeScript] Improve List exporter type #9968
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f54479
[TypeScript] Improve List exporter type
fzaninotto f254f21
Improve documentation
fzaninotto 7231ad8
Add link to example in demo
fzaninotto bd1bbea
Convert default to named exports
fzaninotto 6ade3b9
Make Exporter accept a generic parameter
djhi f4c4bee
Simplify type
fzaninotto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import { createContext } from 'react'; | ||
|
||
import { Exporter } from '../types'; | ||
import defaultExporter from './defaultExporter'; | ||
import { defaultExporter } from './defaultExporter'; | ||
|
||
const ExporterContext = createContext<Exporter | false>(defaultExporter); | ||
export const ExporterContext = createContext<Exporter | false>(defaultExporter); | ||
|
||
ExporterContext.displayName = 'ExporterContext'; | ||
|
||
export default ExporterContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import jsonExport from 'jsonexport/dist'; | ||
|
||
import downloadCSV from './downloadCSV'; | ||
import { downloadCSV } from './downloadCSV'; | ||
import { Exporter } from '../types'; | ||
|
||
const defaultExporter: Exporter = (data, _, __, resource) => | ||
export const defaultExporter: Exporter = (data, _, __, resource) => | ||
jsonExport(data, (err, csv) => downloadCSV(csv, resource)); | ||
|
||
export default defaultExporter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import expect from 'expect'; | ||
|
||
import { getRelatedIds } from './getRelatedIds'; | ||
|
||
describe('getRelatedIds', () => { | ||
it('should ignore null or undefined values', () => { | ||
const books = [ | ||
{ id: 1, author_id: 123, title: 'Pride and Prejudice' }, | ||
{ id: 2, author_id: null }, | ||
{ id: 3 }, | ||
]; | ||
expect(getRelatedIds(books, 'author_id')).toEqual([123]); | ||
}); | ||
it('should aggregate scalar related ids', () => { | ||
const books = [ | ||
{ id: 1, author_id: 123, title: 'Pride and Prejudice' }, | ||
{ id: 2, author_id: 123, title: 'Sense and Sensibility' }, | ||
{ id: 3, author_id: 456, title: 'War and Peace' }, | ||
]; | ||
expect(getRelatedIds(books, 'author_id')).toEqual([123, 456]); | ||
}); | ||
it('should aggregate arrays of related ids', () => { | ||
const books = [ | ||
{ id: 1, tag_ids: [1, 2], title: 'Pride and Prejudice' }, | ||
{ id: 2, tag_ids: [2, 3], title: 'Sense and Sensibility' }, | ||
{ id: 3, tag_ids: [4], title: 'War and Peace' }, | ||
]; | ||
expect(getRelatedIds(books, 'tag_ids')).toEqual([1, 2, 3, 4]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { RaRecord, Identifier } from '../types'; | ||
|
||
/** | ||
* Extracts, aggregates and deduplicates the ids of related records | ||
* | ||
* @example | ||
* const books = [ | ||
* { id: 1, author_id: 123, title: 'Pride and Prejudice' }, | ||
* { id: 2, author_id: 123, title: 'Sense and Sensibility' }, | ||
* { id: 3, author_id: 456, title: 'War and Peace' }, | ||
* ]; | ||
* getRelatedIds(books, 'author_id'); => [123, 456] | ||
* | ||
* @example | ||
* const books = [ | ||
* { id: 1, tag_ids: [1, 2], title: 'Pride and Prejudice' }, | ||
* { id: 2, tag_ids: [2, 3], title: 'Sense and Sensibility' }, | ||
* { id: 3, tag_ids: [4], title: 'War and Peace' }, | ||
* ]; | ||
* getRelatedIds(records, 'tag_ids'); => [1, 2, 3, 4] | ||
* | ||
* @param {Object[]} records An array of records | ||
* @param {string} field the identifier of the record field to use | ||
*/ | ||
export const getRelatedIds = ( | ||
records: RaRecord[], | ||
field: string | ||
): Identifier[] => | ||
Array.from( | ||
new Set( | ||
records | ||
.filter(record => record[field] != null) | ||
.map(record => record[field]) | ||
.reduce((ids, value) => ids.concat(value), []) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
import defaultExporter from './defaultExporter'; | ||
import downloadCSV from './downloadCSV'; | ||
import ExporterContext from './ExporterContext'; | ||
import fetchRelatedRecords from './fetchRelatedRecords'; | ||
|
||
export { defaultExporter, downloadCSV, ExporterContext, fetchRelatedRecords }; | ||
export * from './defaultExporter'; | ||
export * from './downloadCSV'; | ||
export * from './ExporterContext'; | ||
export * from './fetchRelatedRecords'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably go one step further and make
exporter
typed when passing a type toList
: