Refactor idEquals to utils

This commit is contained in:
Raymond Feng 2015-08-17 12:49:38 -07:00
parent 84c4d0e922
commit f5270c39c5
2 changed files with 24 additions and 22 deletions

View File

@ -19,6 +19,7 @@ var utils = require('./utils');
var fieldsToArray = utils.fieldsToArray;
var removeUndefined = utils.removeUndefined;
var setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
var idEquals = utils.idEquals;
var mergeQuery = utils.mergeQuery;
var util = require('util');
var assert = require('assert');
@ -2275,27 +2276,6 @@ DataAccessObject.prototype.unsetAttribute = function unsetAttribute(name, nullif
}
};
// Compare two id values to decide if updateAttributes is trying to change
// the id value for a given instance
function idEquals(id1, id2) {
if (id1 === id2) {
return true;
}
// Allows number/string conversions
if ((typeof id1 === 'number' && typeof id2 === 'string') ||
(typeof id1 === 'string' && typeof id2 === 'number')) {
return id1 == id2;
}
// For complex id types such as MongoDB ObjectID
id1 = JSON.stringify(id1);
id2 = JSON.stringify(id2);
if (id1 === id2) {
return true;
}
return false;
}
/**
* Update set of attributes.
* Performs validation before updating.

View File

@ -14,6 +14,7 @@ exports.createPromiseCallback = createPromiseCallback;
exports.uniq = uniq;
exports.toRegExp = toRegExp;
exports.hasRegExpFlags = hasRegExpFlags;
exports.idEquals = idEquals;
var traverse = require('traverse');
var assert = require('assert');
@ -389,7 +390,7 @@ function mergeSettings(target, src) {
// The source item value is an object
if (target == null || typeof target !== 'object' ||
target[key] == null) {
// If target is not an object or target item value
// If target is not an object or target item value
dst[key] = srcValue;
} else {
dst[key] = mergeSettings(target[key], src[key]);
@ -551,3 +552,24 @@ function hasRegExpFlags(regex) {
regex.toString().split('/').pop() :
!!regex.match(/.*\/.+$/);
}
// Compare two id values to decide if updateAttributes is trying to change
// the id value for a given instance
function idEquals(id1, id2) {
if (id1 === id2) {
return true;
}
// Allows number/string conversions
if ((typeof id1 === 'number' && typeof id2 === 'string') ||
(typeof id1 === 'string' && typeof id2 === 'number')) {
return id1 == id2;
}
// For complex id types such as MongoDB ObjectID
id1 = JSON.stringify(id1);
id2 = JSON.stringify(id2);
if (id1 === id2) {
return true;
}
return false;
}