Skip to content

Commit 8a8cc5b

Browse files
committed
[changed] remove case aliases and simplify camelCase
1 parent 8efaf09 commit 8a8cc5b

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

src/object.js

+20-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import changeCase from 'case';
21
import has from 'lodash/has';
32
import omit from 'lodash/omit';
3+
import snakeCase from 'lodash/snakeCase';
4+
import camelCase from 'lodash/camelCase';
45
import mapKeys from 'lodash/mapKeys';
56
import transform from 'lodash/transform';
67

@@ -20,20 +21,8 @@ function unknown(ctx, value) {
2021
.filter(key => known.indexOf(key) === -1)
2122
}
2223

23-
/**
24-
* maintain "private" fields
25-
* `"__FOO_BAR"` becomes `"__fooBar"` not `"fooBar"`
26-
*/
27-
function camelize(str) {
28-
let result = changeCase.camel(str)
29-
, idx = str.search(/[^_]/)
3024

31-
return idx === 0 ? result : (str.substr(0, idx) + result)
32-
}
33-
34-
module.exports = ObjectSchema
35-
36-
function ObjectSchema(spec) {
25+
export default function ObjectSchema(spec) {
3726
if (!(this instanceof ObjectSchema))
3827
return new ObjectSchema(spec)
3928

@@ -222,8 +211,10 @@ inherits(ObjectSchema, MixedSchema, {
222211
},
223212

224213
noUnknown(noAllow = true, message = locale.noUnknown) {
225-
if (typeof noAllow === 'string')
226-
message = noAllow, noAllow = true;
214+
if (typeof noAllow === 'string') {
215+
message = noAllow;
216+
noAllow = true;
217+
}
227218

228219
var next = this.test({
229220
name: 'noUnknown',
@@ -244,18 +235,21 @@ inherits(ObjectSchema, MixedSchema, {
244235
return next
245236
},
246237

247-
camelcase() {
248-
return this.transform(obj =>
249-
obj && mapKeys(obj, (_, key) => camelize(key))
238+
transformKeys(fn) {
239+
return this.transform(obj => obj &&
240+
mapKeys(obj, (_, key) => fn(key))
250241
)
251242
},
252243

253-
constantcase() {
254-
return this.transform(obj =>
255-
obj && mapKeys(obj, (_, key) => changeCase.constant(key))
256-
)
244+
camelCase() {
245+
return this.transformKeys(camelCase)
246+
},
247+
248+
snakeCase() {
249+
return this.transformKeys(snakeCase)
257250
},
258-
})
259251

260-
ObjectSchema.prototype.camelCase = ObjectSchema.prototype.camelcase;
261-
ObjectSchema.prototype.constantCase = ObjectSchema.prototype.constantcase;
252+
constantCase() {
253+
return this.transformKeys(key => snakeCase(key).toUpperCase())
254+
},
255+
})

0 commit comments

Comments
 (0)