0

I'm trying to index bible verses creating a view in a design doc. The idea was to create keys that would allow lexicographic ordered search, concatenating book, chapter and verse number. For this to work, i'd need to pad zeros on chapter and verse number.

I've tried to use .padStart(2,'0') function, but the view created is empty.

a verse doc may look like this: {"_id":"007e0b09","_rev":"1-58c889c","ch":"05","vn":"16","verse":"bla bla bla"}

function (doc) {
  if (doc.verse) {
    //the function isn't rejected, but the resulting view is empty
    var idx = doc.numBook+doc.ch.padStart(4,'0')+doc.vn.padStart(4,'0');
    // if uncomented, a view is generated, but padded zeros are lost
    //var idx = doc.numBook+doc.ch+doc.vn;m
    emit(idx, doc._id);
  }
  return 
}

Is there an alternate function better to use in couchdb design doc environment? Am i doing an error in the design of my index key? In this case, what should i do else? Is the design ok, but I should rewrite or import padStart some way? How? Thanks for reading.

spacm
  • 160
  • 1
  • 16

1 Answers1

0

This works. found this at Pad a number with leading zeros in JavaScript

function (doc) {
  if (doc.verse) {
    var idx = doc.numBook+("0000" + doc.ch).slice(-4)+("0000" + doc.vn).slice(-4);
    emit(idx, doc._id);
  }
  return 
}

By the way, i don't like very much this heavy line, improvement advice welcome. I'm also interested in answers to the design-related question.

spacm
  • 160
  • 1
  • 16