Skip to content

Commit

Permalink
Merge pull request #73 from Ryanc1256/master
Browse files Browse the repository at this point in the history
Updated the replace method for the sanitize method.
  • Loading branch information
ryanc1256 committed Jul 15, 2015
2 parents 72ced91 + 46080ea commit ecb7795
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Database.prototype.create = function create(collectionName, data, cb) {
primary = self.schema._primary[name];


name = typeof name == "string" ? name.replace(/\s/gi, "_") : name;
name = Utils.replaceSpaces(name);

async.auto({

Expand Down
2 changes: 1 addition & 1 deletion lib/database/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Schema.prototype.indexKey = function(collectionName, index) {

Schema.prototype.recordKey = function(collectionName, index, key) {
var name = collectionName.toLowerCase();
var keyName = typeof key == "string" ? key.replace(/\s/gi, "_") : key;
var keyName = Utils.replaceSpaces( key );
return 'waterline:' + name + ':' + index + ':' + keyName;
};

Expand Down
10 changes: 10 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ utils.sanitize = function sanitize(str) {
return typeof str === 'string' ? str.replace(/\s+/g, '_') : str;
};

/**
* Sanitize a key by removing spaces or reserved charaters
*
* @param {String} str
*/

utils.replaceSpaces = function replaceSpaces(str) {
return typeof str === 'string' ? str.replace(/\s/g, '_') : str;
};

/**
* A simple object key count method
*
Expand Down
22 changes: 22 additions & 0 deletions test/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Test dependencies
*/

var assert = require('assert'),
Adapter = require('../../'),
Utils = require('../../lib/utils'),
Support = require('../support')(Adapter),
Errors = require('waterline-errors').adapter;

/**
* Raw adapter specific tests
*/

describe('Utility `.replaceSpaces()`', function() {
describe('replace spaces', function() {
it('should replace spaces with a _ ', function(done) {
assert.equal( Utils.replaceSpaces("My string with spaces"), "My_string_with_spaces" );
done();
});
});
});

0 comments on commit ecb7795

Please sign in to comment.