Skip to content

Commit

Permalink
fixed; utils.clone retains RegExp opts
Browse files Browse the repository at this point in the history
closes #1355
  • Loading branch information
aheckmann committed Feb 20, 2013
1 parent d5197dc commit 862c452
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,13 @@ exports.clone = function clone (obj, options) {
if ('Date' === obj.constructor.name)
return new obj.constructor(+obj);

if ('RegExp' === obj.constructor.name)
return new RegExp(obj.source);
if ('RegExp' === obj.constructor.name) {
var flags = [];
if (obj.global) flags.push('g');
if (obj.multiline) flags.push('m');
if (obj.ignoreCase) flags.push('i');
return new RegExp(obj.source, flags.join(''));
}

if (obj instanceof ObjectId)
return new ObjectId(obj.id);
Expand Down
16 changes: 16 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,21 @@ describe('utils', function(){
assert.deepEqual([0,1,2,3,4,5,6,7,8,9], utils.array.flatten(orig));
done();
})

describe('clone', function(){
it('retains RegExp options gh-1355', function(done){
var a = new RegExp('hello', 'igm');
assert.ok(a.global);
assert.ok(a.ignoreCase);
assert.ok(a.multiline);

var b = utils.clone(a);
assert.equal(b.source, a.source);
assert.equal(a.global, b.global);
assert.equal(a.ignoreCase, b.ignoreCase);
assert.equal(a.multiline, b.multiline);
done();
})
})
})

0 comments on commit 862c452

Please sign in to comment.