{"id":23705707,"url":"https://github.com/pouchdb/collate","last_synced_at":"2025-09-03T11:30:52.481Z","repository":{"id":12076139,"uuid":"14663518","full_name":"pouchdb/collate","owner":"pouchdb","description":"Collation functions for PouchDB map/ reduce and search plugins. ( ⚠️ moved to pouchdb core repo ⚠️ )","archived":false,"fork":false,"pushed_at":"2017-03-07T07:12:32.000Z","size":43,"stargazers_count":38,"open_issues_count":0,"forks_count":5,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-12-12T23:42:20.192Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://pouchdb.com","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pouchdb.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-24T15:23:36.000Z","updated_at":"2024-08-13T09:21:30.000Z","dependencies_parsed_at":"2022-08-23T14:21:14.182Z","dependency_job_id":null,"html_url":"https://github.com/pouchdb/collate","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pouchdb%2Fcollate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pouchdb%2Fcollate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pouchdb%2Fcollate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pouchdb%2Fcollate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pouchdb","download_url":"https://codeload.github.com/pouchdb/collate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231874121,"owners_count":18439218,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-30T14:56:50.865Z","updated_at":"2024-12-30T14:56:51.376Z","avatar_url":"https://github.com/pouchdb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"PouchDB Collate\n===\n\n**Deprecation notice:** this repo is deprecated because the codebase has been [moved into the PouchDB repo](https://github.com/pouchdb/pouchdb/pull/5476). You can still `npm install pouchdb-collate` and use it normally, though.\n\nCollation functions for PouchDB map/reduce. Used by PouchDB map/reduce to maintain consistent [CouchDB collation ordering](https://wiki.apache.org/couchdb/View_collation).\n\nThe 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.\n\nUsage\n-----\n\nIn Node:\n\n```\n$ npm install pouchdb-collate\n```\n\n```\nvar pouchCollate = require('pouchdb-collate');\n```\n\nIn the browser you can install with Bower:\n\n```\n$ bower install pouchdb-collate\n```\n\nOr just download from the [releases page](https://github.com/pouchdb/collate/releases).\n\nThen it will be available as `window.pouchCollate`.\n\nAPI\n----\n\n### toIndexableString(obj)\n\nThis 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).\n\nSo for example, if you want to sort your documents by many properties in an array, you can do e.g.:\n\n```js\nvar pouchCollate = require('pouchdb-collate');\nvar myDoc = {\n  firstName: 'Scrooge',\n  lastName: 'McDuck',\n  age: 67,\n  male: true\n};\n// sort by age, then gender, then last name, then first name\nmyDoc._id = pouchCollate.toIndexableString(\n  [myDoc.age, myDoc.male, mydoc.lastName, mydoc.firstName]);\n```\n\nThe doc ID will be:\n\n```js\n'5323256.70000000000000017764\\u000021\\u00004McDuck\\u00004Scrooge\\u0000\\u0000'\n```\n\nWhich is of course totally not human-readable, but it'll sort everything correctly (floats, booleans, ints \u0026ndash; you name it).  If you need a human-readable doc ID, check out the [DocURI](https://github.com/jo/docuri) project.\n\n**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](https://code.google.com/p/chromium/issues/detail?id=356924), which causes problems in the replicator when it tries to `GET` docs at those URLs.\n\nIn 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:\n\n```js\npouchCollate.toIndexableString([/* ... */])\n    .replace(/\\u0000/g, '\\u0001');\n```\n\n### parseIndexableString(str)\n\nSame as the above, but in reverse. Given an indexable string, it'll give you back a structured object.\n\nFor instance:\n\n```js\nvar pouchCollate = require('pouchdb-collate');\n\n// [ 67, true, 'McDuck', 'Scrooge' ]\npouchCollate.parseIndexableString(\n  '5323256.70000000000000017764\\u000021\\u00004McDuck\\u00004Scrooge\\u0000\\u0000')\n```\n\n### collate(obj1, obj2)\n\nGive it two objects, and it'll return a number comparing them.  For example:\n\n```js\npouchCollate.collate('foo', 'bar'); // 1\npouchCollate.collate('bar', 'foo'); // -1\npouchCollate.collate('foo', 'foo'); // 0\n```\n\nOf course it sorts more than just strings - any valid JavaScript object is sortable.\n\n### normalizeKey(obj)\n\nYou 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 `Date`s become `date.toJSON()`.  It's basically what you would get if you called:\n\n```js\nJSON.parse(JSON.stringify(obj));\n```\n\nbut a bit faster.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpouchdb%2Fcollate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpouchdb%2Fcollate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpouchdb%2Fcollate/lists"}