GithubHelp home page GithubHelp logo

rlugojr / collate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pouchdb/collate

0.0 2.0 0.0 42 KB

Collation functions for PouchDB map/ reduce and search plugins. (deprecated repo)

Home Page: http://pouchdb.com

License: Apache License 2.0

JavaScript 100.00%

collate's Introduction

PouchDB Collate

Deprecation notice: this repo is deprecated because the codebase has been moved into the PouchDB repo. You can still npm install pouchdb-collate and use it normally, though.

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.

Usage

In Node:

$ 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 window.pouchCollate.

API

toIndexableString(obj)

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: 67,
  male: true
};
// sort by age, then gender, then last name, then first name
myDoc._id = pouchCollate.toIndexableString(
  [myDoc.age, myDoc.male, mydoc.lastName, mydoc.firstName]);

The doc ID will be:

'5323256.70000000000000017764\u000021\u00004McDuck\u00004Scrooge\u0000\u0000'

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.

Warning! If you are syncing or storing docs in CouchDB, then you will need to modify these doc IDs, due to a bug in how Chrome parses URLs, which causes problems in the replicator when it tries to GET docs at those URLs.

In short, you will need to replace all the \u0000 characters with some other separator. Assuming you're storing text data and not binary data, \u0001 should be fine:

pouchCollate.toIndexableString([/* ... */])
    .replace(/\u0000/g, '\u0001');

parseIndexableString(str)

Same as the above, but in reverse. Given an indexable string, it'll give you back a structured object.

For instance:

var pouchCollate = require('pouchdb-collate');

// [ 67, true, 'McDuck', 'Scrooge' ]
pouchCollate.parseIndexableString(
  '5323256.70000000000000017764\u000021\u00004McDuck\u00004Scrooge\u0000\u0000')

collate(obj1, obj2)

Give it two objects, and it'll return a number comparing them. For example:

pouchCollate.collate('foo', 'bar'); // 1
pouchCollate.collate('bar', 'foo'); // -1
pouchCollate.collate('foo', 'foo'); // 0

Of course it sorts more than just strings - any valid JavaScript object is sortable.

normalizeKey(obj)

You shouldn't need to use this, but this function will normalize the object and return what CouchDB would expect - e.g. undefined becomes null, and Dates become date.toJSON(). It's basically what you would get if you called:

JSON.parse(JSON.stringify(obj));

but a bit faster.

collate's People

Contributors

adboomlodestar avatar calvinmetcalf avatar nolanlawson avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.