Skip to content

Commit

Permalink
Avoid unnecessary clones
Browse files Browse the repository at this point in the history
Fixes #1128
  • Loading branch information
Marsup committed Mar 18, 2017
1 parent bad211a commit 33d45d1
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 10 deletions.
24 changes: 23 additions & 1 deletion lib/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,14 @@ module.exports = internals.Any = class {

raw(isRaw) {

const value = isRaw === undefined ? true : isRaw;

if (this._flags.raw === value) {
return this;
}

const obj = this.clone();
obj._flags.raw = isRaw === undefined ? true : isRaw;
obj._flags.raw = value;
return obj;
}

Expand Down Expand Up @@ -270,13 +276,21 @@ module.exports = internals.Any = class {

required() {

if (this._flags.presence === 'required') {
return this;
}

const obj = this.clone();
obj._flags.presence = 'required';
return obj;
}

optional() {

if (this._flags.presence === 'optional') {
return this;
}

const obj = this.clone();
obj._flags.presence = 'optional';
return obj;
Expand All @@ -285,6 +299,10 @@ module.exports = internals.Any = class {

forbidden() {

if (this._flags.presence === 'forbidden') {
return this;
}

const obj = this.clone();
obj._flags.presence = 'forbidden';
return obj;
Expand All @@ -293,6 +311,10 @@ module.exports = internals.Any = class {

strip() {

if (this._flags.strip) {
return this;
}

const obj = this.clone();
obj._flags.strip = true;
return obj;
Expand Down
16 changes: 14 additions & 2 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,15 +529,27 @@ internals.Array = class extends Any {

sparse(enabled) {

const value = enabled === undefined ? true : !!enabled;

if (this._flags.sparse === value) {
return this;
}

const obj = this.clone();
obj._flags.sparse = enabled === undefined ? true : !!enabled;
obj._flags.sparse = value;
return obj;
}

single(enabled) {

const value = enabled === undefined ? true : !!enabled;

if (this._flags.single === value) {
return this;
}

const obj = this.clone();
obj._flags.single = enabled === undefined ? true : !!enabled;
obj._flags.single = value;
return obj;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ internals.Binary = class extends Any {

Hoek.assert(Buffer.isEncoding(encoding), 'Invalid encoding:', encoding);

if (this._flags.encoding === encoding) {
return this;
}

const obj = this.clone();
obj._flags.encoding = encoding;
return obj;
Expand Down
10 changes: 5 additions & 5 deletions lib/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ internals.Boolean = class extends Any {

const insensitive = enabled === undefined ? true : !!enabled;

if (insensitive !== this._flags.insensitive) {
const obj = this.clone();
obj._flags.insensitive = insensitive;
return obj;
if (this._flags.insensitive === insensitive) {
return this;
}

return this;
const obj = this.clone();
obj._flags.insensitive = insensitive;
return obj;
}

describe() {
Expand Down
8 changes: 8 additions & 0 deletions lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ internals.Date = class extends Any {

iso() {

if (this._flags.format === internals.isoDate) {
return this;
}

const obj = this.clone();
obj._flags.format = internals.isoDate;
return obj;
Expand All @@ -109,6 +113,10 @@ internals.Date = class extends Any {
const allowed = ['javascript', 'unix'];
Hoek.assert(allowed.indexOf(type) !== -1, '"type" must be one of "' + allowed.join('", "') + '"');

if (this._flags.timestamp === type) {
return this;
}

const obj = this.clone();
obj._flags.timestamp = type;
obj._flags.multiplier = type === 'unix' ? 1000 : 1;
Expand Down
8 changes: 7 additions & 1 deletion lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,14 @@ internals.Object = class extends Any {

unknown(allow) {

const value = allow !== false;

if (this._flags.allowUnknown === value) {
return this;
}

const obj = this.clone();
obj._flags.allowUnknown = (allow !== false);
obj._flags.allowUnknown = value;
return obj;
}

Expand Down
12 changes: 11 additions & 1 deletion lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ internals.String = class extends Any {

insensitive() {

if (this._flags.insensitive) {
return this;
}

const obj = this.clone();
obj._flags.insensitive = true;
return obj;
Expand Down Expand Up @@ -510,8 +514,14 @@ internals.String = class extends Any {

truncate(enabled) {

const value = enabled === undefined ? true : !!enabled;

if (this._flags.truncate === value) {
return this;
}

const obj = this.clone();
obj._flags.truncate = enabled === undefined ? true : !!enabled;
obj._flags.truncate = value;
return obj;
}

Expand Down
38 changes: 38 additions & 0 deletions test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ describe('any', () => {

done();
});

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.any().raw();
expect(schema.raw()).to.shallow.equal(schema);
done();
});
});

describe('default()', () => {
Expand Down Expand Up @@ -773,6 +780,16 @@ describe('any', () => {
});
});

describe('required', () => {

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.any().required();
expect(schema.required()).to.shallow.equal(schema);
done();
});
});

describe('optional()', () => {

it('validates optional with default required', (done) => {
Expand All @@ -794,6 +811,13 @@ describe('any', () => {
[{ b: 5 }, false, null, 'child "a" fails because ["a" is required]']
], done);
});

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.any().optional();
expect(schema.optional()).to.shallow.equal(schema);
done();
});
});

describe('forbidden()', () => {
Expand All @@ -814,6 +838,13 @@ describe('any', () => {
[{ b: null }, false, null, 'child "b" fails because ["b" is not allowed]']
], done);
});

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.any().forbidden();
expect(schema.forbidden()).to.shallow.equal(schema);
done();
});
});

describe('strip()', () => {
Expand Down Expand Up @@ -841,6 +872,13 @@ describe('any', () => {
done();
});
});

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.any().strip();
expect(schema.strip()).to.shallow.equal(schema);
done();
});
});

describe('description()', () => {
Expand Down
14 changes: 14 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,13 @@ describe('array', () => {
});
done();
});

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.array().sparse();
expect(schema.sparse()).to.shallow.equal(schema);
done();
});
});

describe('single()', () => {
Expand Down Expand Up @@ -1223,6 +1230,13 @@ describe('array', () => {
});
done();
});

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.array().single();
expect(schema.single()).to.shallow.equal(schema);
done();
});
});

describe('options()', () => {
Expand Down
7 changes: 7 additions & 0 deletions test/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ describe('binary', () => {
}).to.throw('Invalid encoding: base6');
done();
});

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.binary().encoding('base64');
expect(schema.encoding('base64')).to.shallow.equal(schema);
done();
});
});

describe('min()', () => {
Expand Down
14 changes: 14 additions & 0 deletions test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ describe('date', () => {

describe('iso()', () => {

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.date().iso();
expect(schema.iso()).to.shallow.equal(schema);
done();
});

it('validates isoDate', (done) => {

Helper.validate(Joi.date().iso(), [
Expand Down Expand Up @@ -363,6 +370,13 @@ describe('date', () => {

describe('timestamp()', () => {

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.date().timestamp('unix');
expect(schema.timestamp('unix')).to.shallow.equal(schema);
done();
});

it('validates javascript timestamp', (done) => {

const now = new Date();
Expand Down
7 changes: 7 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,13 @@ describe('object', () => {

describe('unknown()', () => {

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.object().unknown();
expect(schema.unknown()).to.shallow.equal(schema);
done();
});

it('allows local unknown without applying to children', (done) => {

const schema = Joi.object({
Expand Down
17 changes: 17 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ describe('string', () => {
], done);
});

describe('insensitive', () => {

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.string().insensitive();
expect(schema.insensitive()).to.shallow.equal(schema);
done();
});
});

describe('valid()', () => {

it('validates case sensitive values', (done) => {
Expand Down Expand Up @@ -1995,6 +2005,13 @@ describe('string', () => {

describe('truncate()', () => {

it('avoids unnecessary cloning when called twice', (done) => {

const schema = Joi.string().truncate();
expect(schema.truncate()).to.shallow.equal(schema);
done();
});

it('switches the truncate flag', (done) => {

const schema = Joi.string().truncate();
Expand Down

0 comments on commit 33d45d1

Please sign in to comment.