-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11412 from Uzlopak/isSimpleValidator
perf: add isSimpleValidator
- Loading branch information
Showing
3 changed files
with
84 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
/*! | ||
* Determines if `arg` is a flat object. | ||
* | ||
* @param {Object|Array|String|Function|RegExp|any} arg | ||
* @api private | ||
* @return {Boolean} | ||
*/ | ||
const isSimpleValidatorCache = new WeakMap(); | ||
|
||
module.exports = function isSimpleValidator(obj) { | ||
if (!isSimpleValidatorCache.has(obj)) { | ||
isSimpleValidatorCache.set(obj, _isSimpleValidator(obj)); | ||
} | ||
return isSimpleValidatorCache.get(obj); | ||
}; | ||
|
||
function _isSimpleValidator(obj) { | ||
const keys = Object.keys(obj); | ||
let result = true; | ||
for (let i = 0, len = keys.length; i < len; ++i) { | ||
if (typeof obj[keys[i]] === 'object' && obj[keys[i]] !== null) { | ||
result = false; | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const isSimpleValidator = require('../../lib/helpers/isSimpleValidator'); | ||
const MongooseDocumentArray = require('../../lib/types/DocumentArray'); | ||
|
||
describe('isSimpleValidator', function() { | ||
it('empty object', function() { | ||
assert.ok(isSimpleValidator({})); | ||
}); | ||
it('object with one primitive attribute', function() { | ||
assert.ok(isSimpleValidator({ a: 1 })); | ||
assert.ok(isSimpleValidator({ a: 1n })); | ||
assert.ok(isSimpleValidator({ a: 'primitive' })); | ||
assert.ok(isSimpleValidator({ a: true })); | ||
}); | ||
it('object with one simple array attribute', function() { | ||
assert.ok(!isSimpleValidator({ a: [1] })); | ||
assert.ok(!isSimpleValidator({ a: [1n] })); | ||
assert.ok(!isSimpleValidator({ a: ['primitive'] })); | ||
assert.ok(!isSimpleValidator({ a: ['primitive'] })); | ||
assert.ok(!isSimpleValidator({ a: ['primitive', 'primitive'] })); | ||
assert.ok(!isSimpleValidator({ a: [new Date()] })); | ||
assert.ok(!isSimpleValidator({ a: [true] })); | ||
}); | ||
it('object with some native objects', function() { | ||
assert.ok(!isSimpleValidator({ a: /a/g })); | ||
assert.ok(!isSimpleValidator({ a: new Date() })); | ||
}); | ||
it('object with one sub object', function() { | ||
assert.ok(!isSimpleValidator({ a: {} })); | ||
assert.ok(!isSimpleValidator({ a: [{}] })); | ||
assert.ok(!isSimpleValidator({ a: { b: 1 } })); | ||
}); | ||
it('object with one attribute of type MongooseDocumentArray', function() { | ||
assert.ok(!isSimpleValidator({ a: new MongooseDocumentArray(['a']) })); | ||
assert.ok(!isSimpleValidator({ a: new MongooseDocumentArray([{}]) })); | ||
}); | ||
}); |