Skip to content

Commit

Permalink
Minor Cleanup (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Jan 2, 2018
1 parent 9bd8bad commit 0e43adf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -66,7 +66,7 @@
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.31",
"duplexify": "^3.5.0",
"eslint": "^4.10.0",
"eslint": "^4.14.0",
"eslint-config-prettier": "^2.6.0",
"eslint-plugin-node": "^5.2.0",
"eslint-plugin-prettier": "^2.3.1",
Expand All @@ -77,7 +77,7 @@
"mocha": "^4.0.0",
"nyc": "^11.2.1",
"power-assert": "^1.4.4",
"prettier": "^1.8.2",
"prettier": "^1.9.2",
"protobufjs": "^6.8.0",
"proxyquire": "^1.7.11",
"typescript": "^2.5.2",
Expand Down
37 changes: 9 additions & 28 deletions src/document.js
Expand Up @@ -576,8 +576,9 @@ class DocumentSnapshot {
* @return {boolean}
*/
get isEmpty() {
return !this._fieldsProto || isEmptyObject(this._fieldsProto);
return is.undefined(this._fieldsProto) || is.empty(this._fieldsProto);
}

/**
* Convert a document snapshot to the Firestore 'Document' Protobuf.
*
Expand All @@ -588,7 +589,7 @@ class DocumentSnapshot {
return {
update: {
name: this._ref.formattedName,
fields: this._fieldsProto || {},
fields: this._fieldsProto,
},
};
}
Expand Down Expand Up @@ -631,17 +632,14 @@ class DocumentSnapshot {
* @param {Object} obj The object to encode.
* @returns {Object} The Firestore 'Fields' representation
*/
static encodeFields(obj, options) {
let fields = null;
static encodeFields(obj) {
let fields = {};

for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
let val = DocumentSnapshot.encodeValue(obj[prop], options);
let val = DocumentSnapshot.encodeValue(obj[prop]);

if (val) {
if (!fields) {
fields = {};
}
fields[prop] = val;
}
}
Expand All @@ -651,8 +649,7 @@ class DocumentSnapshot {
}

/**
* Encodes a JavaScrip value into the Firestore 'Value' representation.
* Encodes a JavaScrip value into the Firestore 'Value' represntation.
* Encodes a JavaScript value into the Firestore 'Value' representation.
*
* @private
* @param {Object} val The object to encode
Expand Down Expand Up @@ -759,9 +756,9 @@ class DocumentSnapshot {

// If we encounter an empty object, we always need to send it to make sure
// the server creates a map entry.
if (!isEmptyObject(val)) {
if (!is.empty(val)) {
map.mapValue.fields = DocumentSnapshot.encodeFields(val);
if (!map.mapValue.fields) {
if (is.empty(map.mapValue.fields)) {
return null;
}
}
Expand Down Expand Up @@ -1329,22 +1326,6 @@ function isPlainObject(input) {
);
}

/*!
* Checks whether 'obj' has any properties
*
* @param {Object} input - The object to verify.
* @returns {boolean} 'true' if the object contains no keys.
*/
function isEmptyObject(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}

return true;
}

module.exports = DocumentRefType => {
DocumentReference = DocumentRefType;
validate = require('./validate')({
Expand Down

0 comments on commit 0e43adf

Please sign in to comment.