Socket
Socket
Sign inDemoInstall

couchdb-tools

Package Overview
Dependencies
14
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.1 to 0.0.2

134

index.js

@@ -5,2 +5,3 @@ var uglifyjs = require('uglify-js')

var shortid = require('shortid');
var _ = require('lodash');

@@ -17,3 +18,3 @@ /**

*/
module.exports.ddoc = function (obj,name) {
exports.ddoc = function (obj,name) {
obj = JSON.parse(JSON.stringify(obj,function (key,val) {

@@ -37,15 +38,128 @@ if ( typeof val == 'function' ) {

/**
* Create a new object that is the result of copying the properties of the
* second parameter into the first.
* Normalise a CouchDB result object into a flat array of document objects
* containing _id and _rev properties.
*
* @param {object} a First parameter
* @param {object} b Second parameter
* @param {object} CouchDB result object
*
* @returns {object} A new object, a combination of a and b
* @returns {array} Normalised flat array of documents
*/
module.exports.combine = function (a,b) {
return extend(a,b);
module.exports.normalise = function (collection) {
if ( !_.isArray(collection) ) {
collection = collection.rows;
}
return collection.map(function (obj) {
if ( typeof obj.doc == 'object' ) {
return obj.doc;
} else {
return {
_id: obj.id,
_rev: obj.rev
}
}
});
}
/**
* Algorithm for comparing two collections of documents. One collection is
* deemed to be new, the other old. The returned result object categorises
* the documents into a set of arrays: onlyOld, onlyNew, bothAndEqual and
* bothAndUnEqual. For example, you may decide that the documents in onlyOld
* need deleting and those in onlyNew need inserting.
*
* @param {array} current Array of up-to-date documents
* @param {array} old Arrray of out-of-date documents
*/
exports.sync = function (newColl,oldColl) {
var onlyOld = [];
var bothAndEqual = [];
var bothAndUnEqual = [];
var onlyNew = [];
oldColl.forEach(function (oldDoc) {
if ( !exports.find(newColl,oldDoc._id) ) {
onlyOld.push(oldDoc);
}
});
newColl.forEach(function (newDoc) {
var oldDoc = exports.find(oldColl,newDoc._id);
if ( oldDoc ) {
if ( !exports.equal(oldDoc,newDoc) ) {
bothAndUnEqual.push({
old: oldDoc,
new: newDoc
});
} else {
bothAndEqual.push({
old: oldDoc,
new: newDoc
});
}
} else {
onlyNew.push(newDoc);
}
});
return {
onlyOld: onlyOld,
bothAndEqual: bothAndEqual,
bothAndUnEqual: bothAndUnEqual,
onlyNew: onlyNew
}
}
/**
* Clone a document. Ensures that any manipulation of complex types (objects and
* arrays) in the clone will not corrupt data in the source document.
*/
exports.clone = function (obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* Find a document by _id in a collection.
*
* @param {array} Normalised collection of documents
* @param {string} The _id to search for
*
* @returns {object} The found object or undefined
*/
exports.find = function (collection,id) {
return _.find(collection,function (item) {
return item._id === id;
});
}
/**
* Compare two documents for equality while ignoring their _rev properties.
*
* @param {object} a The first object to compare
* @param {object} a The second object to compare
*
* @returns {boolean} The objects' equality
*/
exports.equal = function (a,b) {
var aRev = a._rev;
var bRev = b._rev;
delete a._rev;
delete b._rev;
var equal = _.isEqual(a,b);
if ( aRev ) a._rev = aRev;
if ( bRev ) b._rev = bRev;
return equal;
}
/**
* Create a new document that is the result of merging the properties of an
* arbitrary number of documents. Right-most parameters passed to the function
* gain increasing priority during the merge.
*
* @param {object} Any number of documents
*
* @returns {object} A new merged document
*/
exports.combine = function () {
var args = Array.prototype.slice.call(arguments);
return extend.apply(null,args);
}
/**
* Generate URI-safe, short, non-sequential unqiue ids. Optionally pass in a

@@ -59,3 +173,3 @@ * unique-for-your-app random number seed which will make your ids more secure.

*/
module.exports.shortid = function (seed) {
exports.shortid = function (seed) {
if ( typeof seed != 'undefined' ) {

@@ -75,4 +189,4 @@ shortid.seed(seed);

*/
module.exports.slug = function (text) {
exports.slug = function (text) {
return slug(text);
}

5

package.json
{
"name": "couchdb-tools",
"version": "0.0.1",
"version": "0.0.2",
"description": "A library of handy functions for use when working with CouchDB documents.",

@@ -19,3 +19,4 @@ "main": "index.js",

"slugg": "~0.1.2",
"xtend": "~2.2.0"
"xtend": "~2.2.0",
"lodash": "~2.4.1"
},

@@ -22,0 +23,0 @@ "keywords": [

couchdb-tools
=============
`npm install coucbdb-tools --save`
A library of handy functions for use when working with CouchDB documents. All functions are database adapter agnostic.
Some functions are unique to this library, others are just wrappers around some of the best packages for the job from the Node.JS community - and in which case thanks for your hard work :)
Some functions are unique to this library, others are just wrappers around some of the best packages for the job from the Node.JS community - and which case thanks for your hard work :)
#### `ddoc(obj[,name])`

@@ -50,2 +50,22 @@

#### `normalise(collection)`
Documentation to do.
#### `sync(current,old)`
Documentation to do.
#### `clone(obj,obj[,obj...])`
Documentation to do.
#### `find(collection,id)`
Documentation to do.
#### `equal(a,b)`
Documentation to do.
#### `combine(a,b)`

@@ -99,5 +119,5 @@

Converts a string into a URI-safe slug. Symbols are removed and foreign characters are coerced into to their English equivalent.
Converts a string into a URI-safe slug. Symbols are removed and foreign characters are coerced into their English equivalent.
#### Example
##### Example

@@ -104,0 +124,0 @@ ```javascript

var tools = require('./index');
console.log('\n>>> ddoc\n');
var projects = {

@@ -19,7 +21,12 @@ views: {

console.log('\n>>> combine\n');
var a = {foo:'bar',baz:'qux'};
var b = {foo:'quux',qux:'quuux'};
var c = {quuuux:'quuuuux'};
console.log(tools.combine(a,b));
console.log(tools.combine(a,b,c));
console.log('\n>>> shortid\n');
tools.shortid(343983);

@@ -30,3 +37,35 @@ console.log(tools.shortid());

console.log('\n>>> slug\n');
console.log(tools.slug('Hello world'));
console.log(tools.slug('A test with symbols £'));
console.log('\n>>> sync\n');
var current = [
{
_id: '1'
},
{
_id: '2',
foo: 'bar'
},
{
_id: '3'
}
];
var old = [
{
_id: '1'
},
{
_id: '2'
},
{
_id: '4'
}
]
var synced = tools.sync(current,old);
console.log(synced);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc