JSFiddle - React, Tailwind, and code Playground

by gene

JavaScript

function Document(data) {
    this.docid = data.docid;
    this.title = data.title;
    this.url = data.url;
};

function Query(data) {
    this.id = data.id;
    this.text = data.text;
    this.postings = [];
};

function PostingP(data, query) {
    this.query = query;
    this.id = data.id;
    this.rank = data.rank;
    this.snippet = data.snippet;
    this.score = data.score;
};

var docs = [
    new Document({docid: 1, title: 'first doc', url: 'http://www.foo.com/doc1'}),
    new Document({docid: 2, title: 'second doc', url: 'http://www.foo.com/doc2'}),
    new Document({docid: 3, title: 'third doc', url: 'http://www.foo.com/doc3'}),
    new Document({docid: 4, title: 'fourth doc', url: 'http://www.foo.com/doc4'})
    ];

function findDocument(id) {
    for (var i=0; i<docs.length; i++)
        if (docs[i].docid == id)
            return docs[i];
    return null;
}

function PostingFactory(data, query) {
    
    var document = findDocument(data.docid);
    PostingP.prototype = document;
    return new PostingP(data, query);
}

var query1 = new Query({id: 1, text: 'first query'});
var query2 = new Query({id: 2, text: 'second query'});

var postingsDataForQuery1 = [
    {docid: 1, id: 1, rank: 1, snippet: 'key passage of doc #1'},
    {docid: 2, id: 2, rank: 2, snippet: 'key passage of doc #2'},
    {docid: 3, id: 3, rank: 3, snippet: 'key passage of doc #3'}
];

var postingsDataForQuery2 = [
    {docid: 4, id: 4, rank: 1, snippet: 'key passage of doc #4'},
    {docid: 3, id: 5, rank: 2, snippet: 'key passage of doc #3'},
    {docid: 2, id: 6, rank: 3, snippet: 'key passage of doc #2'}
];

function processQuery(query, results) {
    for (var i=0; i<results.length; i++) {
        query.postings.push(PostingFactory(results[i], query));
    }
    console.log(query);
}

processQuery(query1, postingsDataForQuery1);
processQuery(query2, postingsDataForQuery2);