backbone-publication
Advanced tools
Comparing version 1.0.0 to 1.0.1
import _ from 'underscore'; | ||
import Backbone from 'backbone'; | ||
/** | ||
* A collection of object-related utilities. | ||
*/ | ||
var ObjectUtils = { | ||
/** | ||
* Performs a deep merge of two objects, source into target. | ||
* | ||
* @param {Object} target | ||
* @param {Object} source | ||
* @return {Object} | ||
*/ | ||
deepExtend: function deepExtend(target, source) { | ||
_.each(source, function (value, key) { | ||
if (_.has(target, key) && ObjectUtils.isPlainObject(target[key]) && ObjectUtils.isPlainObject(source[key])) { | ||
ObjectUtils.deepExtend(target[key], source[key]); | ||
} else { | ||
target[key] = source[key]; | ||
} | ||
}); | ||
return target; | ||
}, | ||
/** | ||
* Performs a deep pick of a given object. | ||
* | ||
* @param {Object} fields The object to pick from | ||
* @param {*List} Accepts an array of acceptable field, a callback function or a list of arguments. | ||
* @return {Object} Filtered out fields. | ||
*/ | ||
deepPick: function deepPick(fields, whitelist) { | ||
// arguments does not have `slice`, so convert to array first. | ||
if (_.isString(whitelist)) whitelist = _.rest(arguments); | ||
var picked = _.pick(fields, whitelist); | ||
var nested = _.pick(fields, function (f) { | ||
return _.isObject(f) && !_.isArray(f); | ||
}); | ||
_.each(nested, function (value, key, object) { | ||
var localObj = ObjectUtils.deepPick(value, whitelist); | ||
if (!_.isEmpty(localObj)) picked[key] = localObj; | ||
}); | ||
return picked; | ||
}, | ||
/** | ||
* Performs a deep omit of a given object. | ||
* | ||
* @param {Object} fields The object to filter fields out. | ||
* @param {*List} blacklist Accepts an array of fields, a callback function, or a list of arguments. | ||
* @return {Object} Objects with fields omitted. | ||
*/ | ||
deepOmit: function deepOmit(fields, blacklist) { | ||
// arguments does not have `slice`, so convert to array first. | ||
if (_.isString(blacklist)) blacklist = _.rest(arguments); | ||
var omitted = _.omit(fields, blacklist); | ||
var nested = _.pick(fields, function (f) { | ||
return _.isObject(f) && !_.isArray(f); | ||
}); | ||
_.each(nested, function (value, key, object) { | ||
var localObj = ObjectUtils.deepOmit(value, blacklist); | ||
if (!_.isEmpty(localObj)) omitted[key] = localObj;else delete omitted[key]; | ||
}); | ||
return omitted; | ||
}, | ||
/** | ||
* isObject is a cheap version of $.isPlainObject because importing jquery for | ||
* a single function is overkill. The only difference in functionality is that | ||
* this `isObject` fails to return false for an ES6 class created object (which | ||
* is fine for our current needs). | ||
* | ||
* @param {*} obj Anything. | ||
* @returns {Boolean} true if `obj` is a plain object or ES6 class instantiated | ||
* object, false otherwise. | ||
*/ | ||
isObject: function isObject(obj) { | ||
return _.isObject(obj) && !_.isArray(obj); | ||
} | ||
}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
@@ -79,3 +162,3 @@ return typeof obj; | ||
var oldAttributes = _.omit(this.attributes, '_id', 'createdAt'); | ||
var fullAttributes = _.deepExtend(oldAttributes, newAttributes); | ||
var fullAttributes = ObjectUtils.deepExtend(oldAttributes, newAttributes); | ||
@@ -139,6 +222,6 @@ // Perform the standard Backbone.js set. Do this first, so when we trigger | ||
}; | ||
var toUnset = _.deepPick(fields, isUndefinedOrNull); | ||
var toUnset = ObjectUtils.deepPick(fields, isUndefinedOrNull); | ||
if (!_.isEmpty(toUnset)) this.unset(toUnset); | ||
var toSet = _.deepOmit(fields, isUndefinedOrNull); | ||
var toSet = ObjectUtils.deepOmit(fields, isUndefinedOrNull); | ||
if (!_.isEmpty(toSet)) this.set(toSet); | ||
@@ -250,6 +333,6 @@ }, | ||
}; | ||
var toUnset = _.deepPick(fields, isUndefinedOrNull); | ||
var toUnset = ObjectUtils.deepPick(fields, isUndefinedOrNull); | ||
model.unset(toUnset); | ||
var toSet = _.deepOmit(fields, isUndefinedOrNull); | ||
var toSet = ObjectUtils.deepOmit(fields, isUndefinedOrNull); | ||
model.set(toSet); | ||
@@ -256,0 +339,0 @@ } |
{ | ||
"name": "backbone-publication", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Supports backbone classes backed by `publication-client` reactive queries.", | ||
@@ -5,0 +5,0 @@ "main": "dist/browser/index.js", |
@@ -36,2 +36,3 @@ # backbone-publication | ||
### Changelog | ||
* 1.0.0 Initial release. | ||
* 1.0.1 - Use our own local utils instead of external underscore extensions. | ||
* 1.0.0 - Initial release. |
import _ from 'underscore'; | ||
import Backbone from 'backbone'; | ||
import PublicationModel from './PublicationModel'; | ||
import ObjectUtils from './ObjectUtils'; | ||
@@ -80,6 +82,6 @@ | ||
}; | ||
var toUnset = _.deepPick(fields, isUndefinedOrNull); | ||
var toUnset = ObjectUtils.deepPick(fields, isUndefinedOrNull); | ||
model.unset(toUnset); | ||
var toSet = _.deepOmit(fields, isUndefinedOrNull); | ||
var toSet = ObjectUtils.deepOmit(fields, isUndefinedOrNull); | ||
model.set(toSet); | ||
@@ -86,0 +88,0 @@ } |
import _ from 'underscore'; | ||
import Backbone from 'backbone'; | ||
import ObjectUtils from './ObjectUtils'; | ||
/** | ||
@@ -70,3 +72,3 @@ * A PublicationModel is a class that provides an integration point between | ||
var oldAttributes = _.omit(this.attributes, '_id', 'createdAt'); | ||
var fullAttributes = _.deepExtend(oldAttributes, newAttributes); | ||
var fullAttributes = ObjectUtils.deepExtend(oldAttributes, newAttributes); | ||
@@ -129,6 +131,6 @@ // Perform the standard Backbone.js set. Do this first, so when we trigger | ||
}; | ||
var toUnset = _.deepPick(fields, isUndefinedOrNull); | ||
var toUnset = ObjectUtils.deepPick(fields, isUndefinedOrNull); | ||
if (!_.isEmpty(toUnset)) this.unset(toUnset); | ||
var toSet = _.deepOmit(fields, isUndefinedOrNull); | ||
var toSet = ObjectUtils.deepOmit(fields, isUndefinedOrNull); | ||
if (!_.isEmpty(toSet)) this.set(toSet); | ||
@@ -135,0 +137,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28928
7
666
38