Skip to content

Instantly share code, notes, and snippets.

@valiafetisov
Last active November 9, 2017 00:16
Show Gist options
  • Save valiafetisov/15d81e2d0480e7eb94326f5a82f5afe4 to your computer and use it in GitHub Desktop.
Save valiafetisov/15d81e2d0480e7eb94326f5a82f5afe4 to your computer and use it in GitHub Desktop.
raan-filter-example
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)
})
{
"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"
}
}
{
"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