Skip to content

Commit

Permalink
[fixed] camelcase should maintain leading underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Feb 8, 2016
1 parent ae5641c commit 86b6446
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ var MixedSchema = require('./mixed')

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

c.type('altCamel', function(str) {
let result = c.camel(str)
, idx = str.search(/[^_]/)

return idx === 0 ? result : (str.substr(0, idx) + result)
})

let childSchema = (field, parent) => {
return isRecursive(field)
? field.of
Expand Down Expand Up @@ -215,7 +222,7 @@ inherits(ObjectSchema, MixedSchema, {

camelcase(){
return this.transform(obj => obj == null ? obj
: transform(obj, (newobj, val, key ) => newobj[c.camel(key)] = val))
: transform(obj, (newobj, val, key ) => newobj[c.altCamel(key)] = val))
},

constantcase(){
Expand Down
9 changes: 9 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ describe('Object types', function(){
.cast(null)).to.equal(null)
})

it('should camelCase with leading underscore', function(){
var inst = object().camelcase()

inst
.cast({ CON_STAT: 5, __isNew: true, __IS_FUN: true })
.should
.eql({ conStat: 5, __isNew: true, __isFun: true })
})

it('should CONSTANT_CASE keys', function(){
var inst = object().shape({
CON_STAT: number(),
Expand Down

0 comments on commit 86b6446

Please sign in to comment.