Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a1fa313

Browse files
committed
[WIP] refactor(data): camelCase keys in jqLite#data
This change aligns jqLite with jQuery 3.
1 parent 4f44e01 commit a1fa313

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/jqLite.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ function jqLiteExpandoStore(element, createIfNecessary) {
383383

384384
function jqLiteData(element, key, value) {
385385
if (jqLiteAcceptsData(element)) {
386+
var prop;
386387

387388
var isSimpleSetter = isDefined(value);
388389
var isSimpleGetter = !isSimpleSetter && key && !isObject(key);
@@ -391,16 +392,18 @@ function jqLiteData(element, key, value) {
391392
var data = expandoStore && expandoStore.data;
392393

393394
if (isSimpleSetter) { // data('key', value)
394-
data[key] = value;
395+
data[camelCase(key)] = value;
395396
} else {
396397
if (massGetter) { // data()
397398
return data;
398399
} else {
399400
if (isSimpleGetter) { // data('key')
400401
// don't force creation of expandoStore if it doesn't exist yet
401-
return data && data[key];
402+
return data && data[camelCase(key)];
402403
} else { // mass-setter: data({key1: val1, key2: val2})
403-
extend(data, key);
404+
for (prop in key) {
405+
data[camelCase(prop)] = key[prop];
406+
}
404407
}
405408
}
406409
}

test/jqLiteSpec.js

+33
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,39 @@ describe('jqLite', function() {
594594
}).not.toThrow();
595595
});
596596
});
597+
598+
fdescribe('camelCasing keys', function() {
599+
// jQuery 2.x has different behavior; skip the tests.
600+
// if (isJQuery2x()) return;
601+
602+
it('should camelCase the key in a setter', function() {
603+
var element = jqLite(a);
604+
605+
element.data('a-B-c-d-42--e', 'z-x');
606+
expect(element.data()).toEqual({'a-BCD-42-E': 'z-x'});
607+
});
608+
609+
it('should camelCase the key in a getter', function() {
610+
var element = jqLite(a);
611+
612+
element.data()['a-BCD-42-E'] = 'x-c';
613+
expect(element.data('a-B-c-d-42--e')).toBe('x-c');
614+
});
615+
616+
it('should camelCase the key in a mass setter', function() {
617+
var element = jqLite(a);
618+
619+
element.data({'a-B-c-d-42--e': 'c-v', 'r-t-v': 42});
620+
expect(element.data()).toEqual({'a-BCD-42-E': 'c-v', 'rTV': 42});
621+
});
622+
623+
it('should ignore non-camelCase keys in the data in a getter', function() {
624+
var element = jqLite(a);
625+
626+
element.data()['a-b'] = 'b-n';
627+
expect(element.data('a-b')).toBe(undefined);
628+
});
629+
});
597630
});
598631

599632

0 commit comments

Comments
 (0)