overleaf/services/web/scripts/ukamf/ukamf-db.js
Liangjun Song e3b93f0a22 Merge pull request #21284 from overleaf/ls-scripts-to-esm-6
Migrate scripts/ukamf to esm

GitOrigin-RevId: e7318d9fb112304153912303649fc597ef7a19db
2024-10-25 08:05:37 +00:00

28 lines
668 B
JavaScript

import fs from 'fs-extra'
import xml2js from 'xml2js'
import UKAMFEntity from './ukamf-entity.js'
class UKAMFDB {
constructor(file) {
this.file = file
}
async init() {
const data = await fs.readFile(this.file, 'utf8')
const parser = new xml2js.Parser()
const xml = await parser.parseStringPromise(data)
this.entities = xml.EntitiesDescriptor.EntityDescriptor
}
findByEntityID(matcher) {
const entity = this.entities.find(
matcher instanceof RegExp
? e => e.$.entityID.match(matcher)
: e => e.$.entityID.includes(matcher)
)
return entity ? new UKAMFEntity(entity) : null
}
}
export default UKAMFDB