Permalink
Browse files

(#6012) - replace js-extend with Object.assign

  • Loading branch information...
1 parent 309f41c commit b325846d23d264121b80ead3f5e7b7567b56b538 @nolanlawson nolanlawson committed with daleharvey Dec 18, 2016
@@ -1,10 +1,10 @@
import CoreLevelPouch from 'pouchdb-adapter-leveldb-core';
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import fruitdown from 'fruitdown';
function FruitDownPouch(opts, callback) {
- var _opts = extend({
+ var _opts = assign({
db: fruitdown
}, opts);
@@ -3,7 +3,7 @@ var MAX_SIMULTANEOUS_REVS = 50;
var supportsBulkGetMap = {};
-import { jsExtend as extend, nextTick } from 'pouchdb-utils';
+import { assign, nextTick } from 'pouchdb-utils';
import Promise from 'pouchdb-promise';
import ajaxCore from 'pouchdb-ajax';
import getArguments from 'argsarray';
@@ -168,7 +168,7 @@ function HttpPouch(opts, callback) {
function ajax(userOpts, options, callback) {
var reqAjax = userOpts.ajax || {};
- var reqOpts = extend(clone(ajaxOpts), reqAjax, options);
+ var reqOpts = assign(clone(ajaxOpts), reqAjax, options);
log(reqOpts.method + ' ' + reqOpts.url);
return api._ajax(reqOpts, callback);
}
@@ -1,4 +1,4 @@
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import Promise from 'pouchdb-promise';
import { createError, IDB_ERROR } from 'pouchdb-errors';
import {
@@ -142,7 +142,7 @@ function postProcessAttachments(results, asBlob) {
var type = attObj.content_type;
return new Promise(function (resolve) {
readBlobData(body, type, asBlob, function (data) {
- row.doc._attachments[att] = extend(
+ row.doc._attachments[att] = assign(
pick(attObj, ['digest', 'content_type']),
{data: data}
);
@@ -1,5 +1,5 @@
import CoreLevelPouch from 'pouchdb-adapter-leveldb-core';
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import requireLeveldown from './requireLeveldown';
import migrate from './migrate';
@@ -19,7 +19,7 @@ function LevelDownPouch(opts, callback) {
}
}
- var _opts = extend({
+ var _opts = assign({
db: leveldown,
migrate: migrate
}, opts);
@@ -1,10 +1,10 @@
import CoreLevelPouch from 'pouchdb-adapter-leveldb-core';
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import localstoragedown from 'localstorage-down';
function LocalStoragePouch(opts, callback) {
- var _opts = extend({
+ var _opts = assign({
db: localstoragedown
}, opts);
@@ -1,10 +1,10 @@
import CoreLevelPouch from 'pouchdb-adapter-leveldb-core';
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import memdown from 'memdown';
function MemDownPouch(opts, callback) {
- var _opts = extend({
+ var _opts = assign({
db: memdown
}, opts);
@@ -1,9 +1,9 @@
import WebSqlPouchCore from 'pouchdb-adapter-websql-core';
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import websql from 'websql';
function NodeWebSqlPouch(opts, callback) {
- var _opts = extend({
+ var _opts = assign({
websql: websql // pass node-websql in as our "openDatabase" function
}, opts);
@@ -1,4 +1,4 @@
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import {
clone,
pick,
@@ -77,7 +77,7 @@ function fetchAttachmentsIfNecessary(doc, opts, api, txn, cb) {
var attObj = doc._attachments[att];
var attOpts = {binary: opts.binary, ctx: txn};
api._getAttachment(doc._id, att, attObj, attOpts, function (_, data) {
- doc._attachments[att] = extend(
+ doc._attachments[att] = assign(
pick(attObj, ['digest', 'content_type']),
{ data: data }
);
@@ -135,7 +135,7 @@ function WebSqlPouch(opts, callback) {
// extend the options here, because sqlite plugin has a ton of options
// and they are constantly changing, so it's more prudent to allow anything
- var websqlOpts = extend({}, opts, {
+ var websqlOpts = assign({}, opts, {
version: POUCH_VERSION,
description: opts.name,
size: size
@@ -585,7 +585,7 @@ function WebSqlPouch(opts, callback) {
var tx = opts.ctx;
if (!tx) {
return db.readTransaction(function (txn) {
- api._get(id, extend({ctx: txn}, opts), callback);
+ api._get(id, assign({ctx: txn}, opts), callback);
});
}
@@ -1,5 +1,5 @@
import WebSqlPouchCore from 'pouchdb-adapter-websql-core';
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import valid from './valid';
function openDB(name, version, description, size) {
@@ -8,7 +8,7 @@ function openDB(name, version, description, size) {
}
function WebSQLPouch(opts, callback) {
- var _opts = extend({
+ var _opts = assign({
websql: openDB
}, opts);
@@ -1,5 +1,5 @@
import request from './request';
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import { generateErrorFromResponse } from 'pouchdb-errors';
import { clone } from 'pouchdb-utils';
import applyTypeToBuffer from './applyTypeToBuffer';
@@ -18,7 +18,7 @@ function ajaxCore(options, callback) {
cache: false
};
- options = extend(defaultOptions, options);
+ options = assign(defaultOptions, options);
function onSuccess(obj, resp, cb) {
if (!options.binary && options.json && typeof obj === 'string') {
@@ -1,5 +1,5 @@
import {
- jsExtend as extend,
+ assign,
guardedConsole
} from 'pouchdb-utils';
import Promise from 'pouchdb-promise';
@@ -126,7 +126,7 @@ function allDocsKeysQuery(api, opts, callback) {
offset: opts.skip
};
return Promise.all(keys.map(function (key) {
- var subOpts = extend({key: key, deleted: 'ok'}, opts);
+ var subOpts = assign({key: key, deleted: 'ok'}, opts);
['limit', 'skip', 'keys'].forEach(function (optKey) {
delete subOpts[optKey];
});
@@ -1,4 +1,4 @@
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import PouchDB from './constructor';
import inherits from 'inherits';
@@ -69,7 +69,7 @@ PouchDB.defaults = function (defaultOpts) {
delete opts.name;
}
- opts = extend({}, PouchAlt.__defaults, opts);
+ opts = assign({}, PouchAlt.__defaults, opts);
PouchDB.call(this, name, opts);
}
@@ -84,7 +84,7 @@ PouchDB.defaults = function (defaultOpts) {
// make default options transitive
// https://github.com/pouchdb/pouchdb/issues/5922
- PouchAlt.__defaults = extend({}, this.__defaults, defaultOpts);
+ PouchAlt.__defaults = assign({}, this.__defaults, defaultOpts);
return PouchAlt;
};
@@ -17,7 +17,7 @@ import {
upsert,
toPromise,
defaultBackOff,
- jsExtend
+ assign
} from 'pouchdb-utils';
import {
@@ -47,8 +47,6 @@ import {
generateErrorFromResponse
} from 'pouchdb-errors';
-import { jsExtend as extend } from 'pouchdb-utils';
-
import generateReplicationId from 'pouchdb-generate-replication-id';
import checkpointer from 'pouchdb-checkpointer';
@@ -62,7 +60,6 @@ export default {
btoa: btoa,
binaryStringToBlobOrBuffer: binaryStringToBlobOrBuffer,
clone: clone,
- extend: extend,
createError: createError,
generateErrorFromResponse: generateErrorFromResponse,
generateReplicationId: generateReplicationId,
@@ -75,7 +72,7 @@ export default {
toPromise: toPromise,
checkpointer: checkpointer,
defaultBackOff: defaultBackOff,
- jsExtend: jsExtend,
+ assign: assign,
mapReduceUtils: {
uniq: uniq,
sequentialize: sequentialize,
@@ -1,4 +1,4 @@
-import { jsExtend as extend } from 'pouchdb-utils';
+import { assign } from 'pouchdb-utils';
import Promise from 'pouchdb-promise';
import {
replicate,
@@ -30,8 +30,8 @@ function Sync(src, target, opts, callback) {
var self = this;
this.canceled = false;
- var optsPush = opts.push ? extend({}, opts, opts.push) : opts;
- var optsPull = opts.pull ? extend({}, opts, opts.pull) : opts;
+ var optsPush = opts.push ? assign({}, opts, opts.push) : opts;
+ var optsPull = opts.pull ? assign({}, opts, opts.pull) : opts;
this.push = replicate(src, target, optsPush);
this.pull = replicate(target, src, optsPull);
@@ -0,0 +1,30 @@
+var assign;
+if (process.env.COVERAGE) { // don't penalize us on code coverage for this polyfill
+ assign = Object.assign;
+} else {
+ if (typeof Object.assign === 'function') {
+ assign = Object.assign;
+ } else {
+ // lite Object.assign polyfill based on
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
+ assign = function (target) {
+ var to = Object(target);
+
+ for (var index = 1; index < arguments.length; index++) {
+ var nextSource = arguments[index];
+
+ if (nextSource != null) { // Skip over if undefined or null
+ for (var nextKey in nextSource) {
+ // Avoid bugs when hasOwnProperty is shadowed
+ if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
+ to[nextKey] = nextSource[nextKey];
+ }
+ }
+ }
+ }
+ return to;
+ };
+ }
+}
+
+export default assign;
@@ -6,7 +6,7 @@ import guardedConsole from './guardedConsole';
import defaultBackOff from './defaultBackOff';
import explainError from './explainError';
import extend from './extend';
-import jsExtend from './jsExtend';
+import assign from './assign';
import filterChange from './filterChange';
import flatten from './flatten';
import functionName from './functionName';
@@ -33,7 +33,7 @@ export {
defaultBackOff,
explainError,
extend,
- jsExtend,
+ assign,
filterChange,
flatten,
functionName,
@@ -1,30 +0,0 @@
-// forked from
-// https://github.com/vmattos/js-extend/blob/7023fd69a9e9552688086b8b8006b1fcf916a306/extend.js
-// TODO: I don't know why we have two different extend() functions in PouchDB
-
-var slice = Array.prototype.slice;
-var each = Array.prototype.forEach;
-
-function extend(obj) {
- if (typeof obj !== 'object') {
- throw obj + ' is not an object' ;
- }
-
- var sources = slice.call(arguments, 1);
-
- each.call(sources, function (source) {
- if (source) {
- for (var prop in source) {
- if (typeof source[prop] === 'object' && obj[prop]) {
- extend.call(obj, obj[prop], source[prop]);
- } else {
- obj[prop] = source[prop];
- }
- }
- }
- });
-
- return obj;
-}
-
-export default extend;
@@ -136,7 +136,7 @@ testUtils.cleanup = function (dbs, done) {
// in rev_tree). Doc must have _rev. If prevRev is not specified
// just insert doc with correct _rev (new_edits=false!)
testUtils.putAfter = function (db, doc, prevRev, callback) {
- var newDoc = testUtils.extend({}, doc);
+ var newDoc = testUtils.assign({}, doc);
if (!prevRev) {
db.put(newDoc, { new_edits: false }, callback);
return;
@@ -277,7 +277,7 @@ testUtils.ajax = PouchForCoverage.ajax;
testUtils.uuid = pouchUtils.uuid;
testUtils.parseUri = pouchUtils.parseUri;
testUtils.errors = PouchForCoverage.Errors;
-testUtils.extend = pouchUtils.jsExtend;
+testUtils.assign = pouchUtils.assign;
testUtils.makeBlob = function (data, type) {
if (typeof process !== 'undefined' && !process.browser) {
Oops, something went wrong.

0 comments on commit b325846

Please sign in to comment.