Refactor idEquals to utils
This commit is contained in:
parent
84c4d0e922
commit
f5270c39c5
22
lib/dao.js
22
lib/dao.js
|
@ -19,6 +19,7 @@ var utils = require('./utils');
|
||||||
var fieldsToArray = utils.fieldsToArray;
|
var fieldsToArray = utils.fieldsToArray;
|
||||||
var removeUndefined = utils.removeUndefined;
|
var removeUndefined = utils.removeUndefined;
|
||||||
var setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
|
var setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
|
||||||
|
var idEquals = utils.idEquals;
|
||||||
var mergeQuery = utils.mergeQuery;
|
var mergeQuery = utils.mergeQuery;
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var assert = require('assert');
|
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.
|
* Update set of attributes.
|
||||||
* Performs validation before updating.
|
* Performs validation before updating.
|
||||||
|
|
24
lib/utils.js
24
lib/utils.js
|
@ -14,6 +14,7 @@ exports.createPromiseCallback = createPromiseCallback;
|
||||||
exports.uniq = uniq;
|
exports.uniq = uniq;
|
||||||
exports.toRegExp = toRegExp;
|
exports.toRegExp = toRegExp;
|
||||||
exports.hasRegExpFlags = hasRegExpFlags;
|
exports.hasRegExpFlags = hasRegExpFlags;
|
||||||
|
exports.idEquals = idEquals;
|
||||||
|
|
||||||
var traverse = require('traverse');
|
var traverse = require('traverse');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
@ -389,7 +390,7 @@ function mergeSettings(target, src) {
|
||||||
// The source item value is an object
|
// The source item value is an object
|
||||||
if (target == null || typeof target !== 'object' ||
|
if (target == null || typeof target !== 'object' ||
|
||||||
target[key] == null) {
|
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;
|
dst[key] = srcValue;
|
||||||
} else {
|
} else {
|
||||||
dst[key] = mergeSettings(target[key], src[key]);
|
dst[key] = mergeSettings(target[key], src[key]);
|
||||||
|
@ -551,3 +552,24 @@ function hasRegExpFlags(regex) {
|
||||||
regex.toString().split('/').pop() :
|
regex.toString().split('/').pop() :
|
||||||
!!regex.match(/.*\/.+$/);
|
!!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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue