From 86b64461b6968f4d99c9c091232e52edad40ef98 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Mon, 8 Feb 2016 11:16:58 -0500 Subject: [PATCH] [fixed] camelcase should maintain leading underscores --- src/object.js | 9 ++++++++- test/object.js | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/object.js b/src/object.js index 44601f6a9..6a6964a47 100644 --- a/src/object.js +++ b/src/object.js @@ -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 @@ -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(){ diff --git a/test/object.js b/test/object.js index a834762e4..75ad6e795 100644 --- a/test/object.js +++ b/test/object.js @@ -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(),