From 09150c4dc5a0af3a05f29fd5968b850771da5931 Mon Sep 17 00:00:00 2001 From: Dario Marcelino Date: Wed, 3 Jun 2015 18:40:26 +0100 Subject: [PATCH] Make matchRecordId quicker --- lib/utils.js | 7 +++---- test/unit/utils.test.js | 44 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index e7d49e7..1e48349 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -146,10 +146,9 @@ exports.rewriteIdsRecursive = function rewriteIdsRecursive(models, schema, accum * @api public */ exports.matchRecordId = function matchRecordId(id) { - if (id === null) return false; - var test = _.cloneDeep(id); - if(typeof test.toString !== 'undefined') - test = id.toString(); + if (!id) return false; + if(id instanceof RecordId) { return true; } + var test = id.toString(); return test.match(/^\#\-?\d+\:\d+$/) ? true : false; }; diff --git a/test/unit/utils.test.js b/test/unit/utils.test.js index d0479f6..1d02559 100644 --- a/test/unit/utils.test.js +++ b/test/unit/utils.test.js @@ -2,7 +2,8 @@ * Test dependencies */ var assert = require('assert'), - utils = require('../../lib/utils'); + utils = require('../../lib/utils'), + RecordId = require('oriento').RID; describe('utils helper class', function () { @@ -256,4 +257,45 @@ describe('utils helper class', function () { done(); }); }); + + describe('matchRecordId:', function () { + + var fixturesTrue = [ + new RecordId('#10:1'), + new RecordId('#0:0'), + new RecordId('#-2:1'), + '#5:5', + '#5:0', + '#-2:0', + '#-2:1' + ]; + + var fixturesFalse = [ + null, + undefined, + 4, + '5', + '5:5', + '-2:1', + '#5', + '#5:5:5', + '#2:-1', + '#:1' + ]; + + it('should match genuine Record IDs', function () { + fixturesTrue.forEach(function(rid){ + assert.equal(utils.matchRecordId(rid), true, 'test failed for: ' + rid); + }); + }); + + it('should not match invalid Record IDs', function () { + fixturesFalse.forEach(function(rid){ + assert.equal(utils.matchRecordId(rid), false, 'test failed for: ' + rid); + }); + }); + }); + + + });