-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9968 from marmelab/typescript-exporter-type
[TypeScript] Improve List exporter type
- Loading branch information
Showing
16 changed files
with
163 additions
and
128 deletions.
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.