From f5270c39c5f3ba17ca5f15040582786f1a3541f7 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 17 Aug 2015 12:49:38 -0700 Subject: [PATCH] Refactor idEquals to utils --- lib/dao.js | 22 +--------------------- lib/utils.js | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 262b462e..0743b059 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -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. diff --git a/lib/utils.js b/lib/utils.js index a346a66c..2a3521b4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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; +}