Skip to content

Commit 86b6446

Browse files
committed
[fixed] camelcase should maintain leading underscores
1 parent ae5641c commit 86b6446

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/object.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ var MixedSchema = require('./mixed')
1515

1616
let isRecursive = schema => (schema._subType || schema) === '$this'
1717

18+
c.type('altCamel', function(str) {
19+
let result = c.camel(str)
20+
, idx = str.search(/[^_]/)
21+
22+
return idx === 0 ? result : (str.substr(0, idx) + result)
23+
})
24+
1825
let childSchema = (field, parent) => {
1926
return isRecursive(field)
2027
? field.of
@@ -215,7 +222,7 @@ inherits(ObjectSchema, MixedSchema, {
215222

216223
camelcase(){
217224
return this.transform(obj => obj == null ? obj
218-
: transform(obj, (newobj, val, key ) => newobj[c.camel(key)] = val))
225+
: transform(obj, (newobj, val, key ) => newobj[c.altCamel(key)] = val))
219226
},
220227

221228
constantcase(){

test/object.js

+9
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,15 @@ describe('Object types', function(){
446446
.cast(null)).to.equal(null)
447447
})
448448

449+
it('should camelCase with leading underscore', function(){
450+
var inst = object().camelcase()
451+
452+
inst
453+
.cast({ CON_STAT: 5, __isNew: true, __IS_FUN: true })
454+
.should
455+
.eql({ conStat: 5, __isNew: true, __isFun: true })
456+
})
457+
449458
it('should CONSTANT_CASE keys', function(){
450459
var inst = object().shape({
451460
CON_STAT: number(),

0 commit comments

Comments
 (0)