Skip to content

Commit 973ba11

Browse files
committed
Update dashedToCamelCase
1 parent 94c4dd9 commit 973ba11

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

lib/parsers.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,15 @@ exports.parseImage = function parseImage(val) {
410410
};
411411

412412
// utility to translate from border-width to borderWidth
413-
var dashedToCamelCase = function (dashed) {
414-
var i;
413+
exports.dashedToCamelCase = function (dashed) {
414+
if (dashed.startsWith('--')) {
415+
return dashed;
416+
}
417+
// skip leading hyphen in vendor prefixed value, e.g. -webkit-foo
418+
var i = /^\-[a-z]/.test(dashed) ? 1 : 0;
415419
var camel = '';
416420
var nextCap = false;
417-
for (i = 0; i < dashed.length; i++) {
421+
for (; i < dashed.length; i++) {
418422
if (dashed[i] !== '-') {
419423
camel += nextCap ? dashed[i].toUpperCase() : dashed[i];
420424
nextCap = false;
@@ -424,7 +428,6 @@ var dashedToCamelCase = function (dashed) {
424428
}
425429
return camel;
426430
};
427-
exports.dashedToCamelCase = dashedToCamelCase;
428431

429432
var isSpace = /\s/;
430433
var openingDeliminators = ['"', "'", '('];
@@ -525,7 +528,7 @@ exports.shorthandSetter = function (property, shorthandFor) {
525528
Object.keys(obj).forEach(function (subprop) {
526529
// in case subprop is an implicit property, this will clear
527530
// *its* subpropertiesX
528-
var camel = dashedToCamelCase(subprop);
531+
var camel = exports.dashedToCamelCase(subprop);
529532
this[camel] = obj[subprop];
530533
// in case it gets translated into something else (0 -> 0px)
531534
obj[subprop] = this[camel];

test/parsers.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,37 @@ describe('parseImage', () => {
900900

901901
it.todo('test');
902902
});
903+
903904
describe('dashedToCamelCase', () => {
904-
it.todo('test');
905+
it('should not camelize custom property', () => {
906+
let input = '--foo-bar-baz';
907+
let output = parsers.dashedToCamelCase(input);
908+
909+
assert.strictEqual(output, '--foo-bar-baz');
910+
});
911+
912+
it('should camelize value', () => {
913+
let input = 'foo-bar-baz';
914+
let output = parsers.dashedToCamelCase(input);
915+
916+
assert.strictEqual(output, 'fooBarBaz');
917+
});
918+
919+
it('should camelize vendor prefixed value', () => {
920+
let input = '-webkit-foo';
921+
let output = parsers.dashedToCamelCase(input);
922+
923+
assert.strictEqual(output, 'webkitFoo');
924+
});
925+
926+
it('should not camelize snake cased value', () => {
927+
let input = 'foo_bar_baz';
928+
let output = parsers.dashedToCamelCase(input);
929+
930+
assert.strictEqual(output, 'foo_bar_baz');
931+
});
905932
});
933+
906934
describe('shorthandParser', () => {
907935
it.todo('test');
908936
});

0 commit comments

Comments
 (0)