Skip to content

Commit

Permalink
Add import utils
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed Jan 24, 2020
1 parent 50d0ad5 commit dd66b19
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/common/export.js
@@ -1,9 +1,10 @@
'use strict'



const props = {
item: [
'list',
'photo',
'tag',
'template'
],

Expand All @@ -25,6 +26,7 @@ const props = {
'mimetype',
'orientation',
'path',
'selection',
'size',
'template'
],
Expand Down
69 changes: 69 additions & 0 deletions src/common/import.js
@@ -0,0 +1,69 @@
'use strict'

const ex = require('./export')
const { expand, open, toList, toValue } = require('./json')
const { tropy } = require('./ns')
const { array, map, omit, get } = require('./util')

// Expand JSON-LD and ungroup item graph for backwards compatibility!
const normalize = async (json) =>
array(await expand(json))
.flatMap(g =>
g['@graph'].map(item =>
(tropy.template in item) ? item : {
[tropy.template]: g[tropy.template],
...item
}))


const toFirstValue = (_, values) =>
toValue(values[0])

const getMetadata = (data, skip = ['@id', '@type']) =>
map(omit(data, skip), toFirstValue)

const getPhoto = (data) => ({
data: getMetadata(data, props.photo),
id: get(data, ['@id', 0]),
template: get(data, [tropy.template, 0, '@id']),
type: get(data, ['@type', 0])
})

function *eachItem(graph) {
for (let data of graph) {
yield ({
data: getMetadata(data, props.item),
id: get(data, ['@id', 0]),
lists: (data[tropy.list] || []).map(toValue),
photos: toList(data[tropy.photo]).map(getPhoto),
tags: (data[tropy.tag] || []).map(toValue),
template: get(data, [tropy.template, 0, '@id']),
type: get(data, ['@type', 0])
})

}
}

const props = {
item: [
'@id',
'@type',
...ex.props.item.map(prop => tropy[prop])
],

photo: [
'@id',
'@type',
...ex.props.photo.map(prop => tropy[prop])
]
}


module.exports = {
eachItem,
normalize,

async open(...args) {
return normalize(await open(...args))
}
}
6 changes: 6 additions & 0 deletions src/common/json.js
Expand Up @@ -2,6 +2,7 @@

const { readFile, writeFile } = require('fs').promises
const { rdfs } = require('./ns')
const { array } = require('./util')

// NB: load jsonld module on demand, because it's huge.
const jsonld = {
Expand Down Expand Up @@ -48,6 +49,10 @@ const write = async (file, data, {
return writeFile(file, JSON.stringify(data, null, indent), opts)
}

const toList = (node) =>
array(node)
.flatMap(container => container['@list'] || container)

const toValue = (node) =>
node['@id'] ?
{ type: rdfs.Class, text: node['@id'] } :
Expand All @@ -65,5 +70,6 @@ module.exports = {
parse,
write,

toList,
toValue
}

0 comments on commit dd66b19

Please sign in to comment.