Skip to content

Commit

Permalink
Merge pull request #111 from appscot/matchRID
Browse files Browse the repository at this point in the history
Make matchRecordId() quicker
  • Loading branch information
dmarcelino committed Jun 3, 2015
2 parents c41e67c + 09150c4 commit 8fc5794
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
7 changes: 3 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
44 changes: 43 additions & 1 deletion test/unit/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
});
});
});



});

0 comments on commit 8fc5794

Please sign in to comment.