Skip to content

Commit

Permalink
Cleanup root
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jun 27, 2019
1 parent be09255 commit 9e385f3
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,46 @@ const internals = {
number: require('./types/number'),
object: require('./types/object'),
string: require('./types/string'),
symbol: require('./types/symbol')
symbol: require('./types/symbol'),

binds: [
'any',
'alt',
'alternatives',
'array',
'bool',
'boolean',
'binary',
'date',
'func',
'lazy',
'number',
'object',
'string',
'symbol',

'assert',
'attempt',
'bind',
'compile',
'defaults',
'extend',
'validate'
]
};


internals.anyMethods = Object.keys(Any.prototype)
.filter((key) => key[0] !== '_' && key !== 'isImmutable')
.concat(internals.binds);


internals.root = function () {

const any = new Any();

const root = any.clone();
Any.prototype._root = root;
root._root = root;
root._binds = new Set(['any', 'alternatives', 'alt', 'array', 'bool', 'boolean', 'binary', 'date', 'func', 'number', 'object', 'string', 'symbol', 'validate', 'describe', 'compile', 'assert', 'attempt', 'lazy', 'defaults', 'extend', 'allow', 'valid', 'only', 'equal', 'invalid', 'disallow', 'not', 'required', 'exist', 'optional', 'forbidden', 'strip', 'when', 'empty', 'default', 'failover']);
root._binds = new Set(internals.anyMethods);

root.any = function (...args) {

Expand Down Expand Up @@ -86,11 +114,9 @@ internals.root = function () {
root.bind = function () {

const joi = Object.create(this);

joi._binds.forEach((bind) => {

for (const bind of joi._binds) {
joi[bind] = joi[bind].bind(joi);
});
}

return joi;
};
Expand Down

0 comments on commit 9e385f3

Please sign in to comment.