Skip to content

Commit 47c051d

Browse files
committed
A few optimizations
1 parent 76bf21c commit 47c051d

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

lib/array.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ internals.Array = function () {
3838
Hoek.inherits(internals.Array, Any);
3939

4040

41+
internals.safeParse = function (value, result) {
42+
43+
try {
44+
const converted = JSON.parse(value);
45+
if (Array.isArray(converted)) {
46+
result.value = converted;
47+
}
48+
}
49+
catch (e) { }
50+
};
51+
4152
internals.Array.prototype._base = function (value, state, options) {
4253

4354
const result = {
@@ -47,13 +58,7 @@ internals.Array.prototype._base = function (value, state, options) {
4758
if (typeof value === 'string' &&
4859
options.convert) {
4960

50-
try {
51-
const converted = JSON.parse(value);
52-
if (Array.isArray(converted)) {
53-
result.value = converted;
54-
}
55-
}
56-
catch (e) { }
61+
internals.safeParse(value, result);
5762
}
5863

5964
let isArray = Array.isArray(result.value);

lib/object.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ internals.Object = function () {
2525

2626
Hoek.inherits(internals.Object, Any);
2727

28+
internals.safeParse = function (value) {
29+
30+
try {
31+
return JSON.parse(value);
32+
}
33+
catch (parseErr) {}
34+
35+
return value;
36+
};
2837

2938
internals.Object.prototype._base = function (value, state, options) {
3039

@@ -41,10 +50,7 @@ internals.Object.prototype._base = function (value, state, options) {
4150
if (typeof value === 'string' &&
4251
options.convert) {
4352

44-
try {
45-
value = JSON.parse(value);
46-
}
47-
catch (parseErr) { }
53+
value = internals.safeParse(value);
4854
}
4955

5056
const type = this._flags.func ? 'function' : 'object';
@@ -95,42 +101,42 @@ internals.Object.prototype._base = function (value, state, options) {
95101

96102
const renamed = {};
97103
for (let i = 0; i < this._inner.renames.length; ++i) {
98-
const item = this._inner.renames[i];
104+
const rename = this._inner.renames[i];
99105

100-
if (item.options.ignoreUndefined && target[item.from] === undefined) {
106+
if (rename.options.ignoreUndefined && target[rename.from] === undefined) {
101107
continue;
102108
}
103109

104-
if (!item.options.multiple &&
105-
renamed[item.to]) {
110+
if (!rename.options.multiple &&
111+
renamed[rename.to]) {
106112

107-
errors.push(this.createError('object.rename.multiple', { from: item.from, to: item.to }, state, options));
113+
errors.push(this.createError('object.rename.multiple', { from: rename.from, to: rename.to }, state, options));
108114
if (options.abortEarly) {
109115
return finish();
110116
}
111117
}
112118

113-
if (Object.prototype.hasOwnProperty.call(target, item.to) &&
114-
!item.options.override &&
115-
!renamed[item.to]) {
119+
if (Object.prototype.hasOwnProperty.call(target, rename.to) &&
120+
!rename.options.override &&
121+
!renamed[rename.to]) {
116122

117-
errors.push(this.createError('object.rename.override', { from: item.from, to: item.to }, state, options));
123+
errors.push(this.createError('object.rename.override', { from: rename.from, to: rename.to }, state, options));
118124
if (options.abortEarly) {
119125
return finish();
120126
}
121127
}
122128

123-
if (target[item.from] === undefined) {
124-
delete target[item.to];
129+
if (target[rename.from] === undefined) {
130+
delete target[rename.to];
125131
}
126132
else {
127-
target[item.to] = target[item.from];
133+
target[rename.to] = target[rename.from];
128134
}
129135

130-
renamed[item.to] = true;
136+
renamed[rename.to] = true;
131137

132-
if (!item.options.alias) {
133-
delete target[item.from];
138+
if (!rename.options.alias) {
139+
delete target[rename.from];
134140
}
135141
}
136142

@@ -185,15 +191,15 @@ internals.Object.prototype._base = function (value, state, options) {
185191

186192
for (let i = 0; i < unprocessedKeys.length; ++i) {
187193
const key = unprocessedKeys[i];
194+
const localState = { key, path: (state.path ? state.path + '.' : '') + key, parent: target, reference: state.reference };
195+
const item = target[key];
188196

189197
for (let j = 0; j < this._inner.patterns.length; ++j) {
190198
const pattern = this._inner.patterns[j];
191199

192200
if (pattern.regex.test(key)) {
193201
delete unprocessed[key];
194202

195-
const item = target[key];
196-
const localState = { key, path: (state.path ? state.path + '.' : '') + key, parent: target, reference: state.reference };
197203
const result = pattern.rule._validate(item, localState, options);
198204
if (result.errors) {
199205
errors.push(this.createError('object.child', { key, reason: result.errors }, localState, options));

0 commit comments

Comments
 (0)