PouchDB Collate ===
Collation functions for PouchDB map/reduce. Used by PouchDB map/reduce to maintain consistent CouchDB collation ordering.
The PouchDB Collate API is not exposed by PouchDB itself, but if you'd like to use it in your own projects, it's pretty small, and it has a few functions you may find useful.
$ npm install pouchdb-collate
var pouchCollate = require('pouchdb-collate');
In the browser you can install with Bower:
$ bower install pouchdb-collate
Or just download from the releases page.
Then it will be available as
This is probably the most useful function in PouchDB Collate. It converts any object to a serialized string that maintains proper CouchDB collation ordering in both PouchDB and CouchDB (ignoring some subtleties with ICU string ordering in CouchDB vs. ASCII string ordering in PouchDB).
So for example, if you want to sort your documents by many properties in an array, you can do e.g.:
var pouchCollate = require'pouchdb-collate';var myDoc =firstName: 'Scrooge'lastName: 'McDuck'age: 67male: true;// sort by age, then gender, then last name, then first namemyDoc_id = pouchCollatetoIndexableStringmyDocage myDocmale mydoclastName mydocfirstName;
The doc ID will be:
Which is of course totally not human-readable, but it'll sort everything correctly (floats, booleans, ints – you name it). If you need a human-readable doc ID, check out the DocURI project.
Give it two objects, and it'll return a number comparing them. For example:
pouchCollatecollate'foo' 'bar'; // 1pouchCollatecollate'bar' 'foo'; // -1pouchCollatecollate'foo' 'foo'; // 0
You shouldn't need to use this, but this function will normalize the object and return what CouchDB would expect - e.g.
date.toJSON(). It's basically what you would get if you called:
but a bit faster.