Last active
November 9, 2017 00:16
-
-
Save valiafetisov/15d81e2d0480e7eb94326f5a82f5afe4 to your computer and use it in GitHub Desktop.
raan-filter-example
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
const fetch = require('isomorphic-fetch') | |
const build = require('redux-object').default | |
const normalize = require('json-api-normalizer').default | |
const apiHost = 'https://dev.api.garagemca.org' | |
const apiEndpoint = '/document/' | |
const bookId = 'fff0840c-21da-4764-a91f-401548acebcc' | |
// the function | |
const filterPeople = (relations, type) => { | |
return relations.filter(relation => { | |
return relation.role.libraryWebId === type | |
}).map(relation => { | |
return Object.assign({}, relation.person, {role: relation.role}) | |
}) | |
} | |
// usage example | |
fetch(apiHost + apiEndpoint + bookId) | |
.then(res => { | |
// process json | |
return res.json() | |
}).then(json => { | |
// normalize data | |
const normalised = normalize(json) | |
// construct an object | |
return build(normalised, 'book', bookId, { eager: true }) | |
}).then(data => { | |
// filter only 'mentioned' | |
const mentioned = filterPeople(data.relPersonDocument, 'mentioned') | |
console.log('mentioned', mentioned.length) | |
// filter only 'authors' | |
const authors = filterPeople(data.relPersonDocument, 'authors') | |
console.log('authors', authors.length) | |
// filter only 'editors' | |
const editors = filterPeople(data.relPersonDocument, 'editors') | |
console.log('editors', editors.length) | |
// filter only 'etc' | |
const etc = filterPeople(data.relPersonDocument, 'translators_designers_etc') | |
console.log('etc', etc.length) | |
}) |
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
{ | |
"id": "fb8f94f7-ec1f-4c7c-a61b-f1b7bb9908da", | |
"name": "Fischli Peter", | |
"nameEn": "Fischli Peter ", | |
"defaultNameLang": "en", | |
"lastname": "Fischli", | |
"firstname": "Peter", | |
"midname": null, | |
"lastnameRu": null, | |
"firstnameRu": null, | |
"midnameRu": null, | |
"lastnameEn": "Fischli", | |
"firstnameEn": "Peter", | |
"midnameEn": null, | |
"role": { | |
"id": "role.personalia", | |
"name": "Персоналия", | |
"nameEn": "Mentioned", | |
"libraryWebId": "mentioned" | |
} | |
} |
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
{ | |
"name": "raan-filter-example", | |
"version": "0.0.0", | |
"main": "app.js", | |
"scripts": { | |
"test": "node app.js" | |
}, | |
"dependencies": { | |
"isomorphic-fetch": "^2.2.1", | |
"json-api-normalizer": "^0.4.5", | |
"redux-object": "^0.5.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment