Skip to content

Commit dca2db7

Browse files
Bumped version to 4.0.0
1 parent 0726f62 commit dca2db7

8 files changed

Lines changed: 106 additions & 221 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### Version 4.0.0
2+
- Removed `$.fn.removeData`
3+
- Removed `$.hasData`
4+
- `$.fn.data`: removed caching mechanism
5+
- `$.fn.data`: added a missing TypeScript method overload
6+
17
### Version 3.2.0
28
- Readme: updated sizes
39
- $.fn.off: added support for removing delegated handlers

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ If you're migrating from jQuery be sure to read our [migration guide](https://gi
3030

3131
## Usage
3232

33-
Get Cash from [CloudFlare](https://cdnjs.cloudflare.com/ajax/libs/cash/3.2.0/cash.min.js) or [jsDelivr](https://cdn.jsdelivr.net/npm/cash-dom@3.2.0/dist/cash.min.js) and use it like this:
33+
Get Cash from [CloudFlare](https://cdnjs.cloudflare.com/ajax/libs/cash/4.0.0/cash.min.js) or [jsDelivr](https://cdn.jsdelivr.net/npm/cash-dom@4.0.0/dist/cash.min.js) and use it like this:
3434

3535
```html
36-
<script src="https://cdnjs.cloudflare.com/ajax/libs/cash/3.2.0/cash.min.js"></script>
36+
<script src="https://cdnjs.cloudflare.com/ajax/libs/cash/4.0.0/cash.min.js"></script>
3737
<script>
3838
$(function () {
3939
$('html').addClass ( 'dom-loaded' );

dist/cash.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,12 @@ interface Cash {
111111
css(prop: string, value: any): this;
112112
css(props: plainObject): this;
113113
}
114-
interface CashStatic {
115-
hasData(ele: HTMLElement): boolean;
116-
}
117114
interface Cash {
115+
data(): plainObject | undefined;
118116
data(name: string): any;
119117
data(name: string, value: any): this;
120118
data(datas: plainObject): this;
121119
}
122-
interface Cash {
123-
removeData(key: string): this;
124-
}
125120
interface Cash {
126121
innerWidth(): number;
127122
innerHeight(): number;

dist/cash.esm.js

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -344,61 +344,41 @@ function css(prop, value) {
344344
;
345345
Cash.prototype.css = css;
346346
// @optional ./css.ts
347-
const dataNamespace = '__cashData', dataAttributeRe = /^data-(.*)/;
348-
// @require core/cash.ts
349-
// @require ./helpers/variables.ts
350-
function hasData(ele) {
351-
return dataNamespace in ele;
352-
}
353-
cash.hasData = hasData;
354-
// @require ./variables.ts
355-
function getDataCache(ele) {
356-
return ele[dataNamespace] = (ele[dataNamespace] || {});
357-
}
358-
// @require attributes/attr.ts
359-
// @require ./get_data_cache.ts
347+
// @require core/camel_case.ts
360348
function getData(ele, key) {
361-
const cache = getDataCache(ele);
362-
if (key) {
363-
if (!(key in cache)) {
364-
let value = ele.dataset ? ele.dataset[key] || ele.dataset[camelCase(key)] : cash(ele).attr(`data-${key}`);
365-
if (value !== undefined) {
366-
try {
367-
value = JSON.parse(value);
368-
}
369-
catch (e) { }
370-
cache[key] = value;
371-
}
372-
}
373-
return cache[key];
349+
const value = ele.dataset ? ele.dataset[key] || ele.dataset[camelCase(key)] : ele.getAttribute(`data-${key}`);
350+
try {
351+
return JSON.parse(value);
374352
}
375-
return cache;
353+
catch (_a) { }
354+
return value;
376355
}
377-
// @require ./variables.ts
378-
// @require ./get_data_cache.ts
379-
function removeData(ele, key) {
380-
if (key === undefined) {
381-
delete ele[dataNamespace];
356+
// @require core/camel_case.ts
357+
function setData(ele, key, value) {
358+
try {
359+
value = JSON.stringify(value);
360+
}
361+
catch (_a) { }
362+
if (ele.dataset) {
363+
ele.dataset[camelCase(key)] = value;
382364
}
383365
else {
384-
delete getDataCache(ele)[key];
366+
ele.setAttribute(`data-${key}`, value);
385367
}
386368
}
387-
// @require ./get_data_cache.ts
388-
function setData(ele, key, value) {
389-
getDataCache(ele)[key] = value;
390-
}
369+
const dataAttributeRe = /^data-(.+)/;
391370
function data(name, value) {
392371
if (!name) {
393372
if (!this[0])
394373
return;
374+
const datas = {};
395375
each(this[0].attributes, (i, attr) => {
396376
const match = attr.name.match(dataAttributeRe);
397377
if (!match)
398378
return;
399-
this.data(match[1]);
379+
datas[match[1]] = this.data(match[1]);
400380
});
401-
return getData(this[0]);
381+
return datas;
402382
}
403383
if (isString(name)) {
404384
if (value === undefined)
@@ -411,11 +391,7 @@ function data(name, value) {
411391
return this;
412392
}
413393
Cash.prototype.data = data;
414-
Cash.prototype.removeData = function (key) {
415-
return this.each((i, ele) => removeData(ele, key));
416-
};
417394
// @optional ./data.ts
418-
// @optional ./remove_data.ts
419395
// @require css/helpers/compute_style_int.ts
420396
function getExtraSpace(ele, xAxis) {
421397
return computeStyleInt(ele, `border${xAxis ? 'Left' : 'Top'}Width`) + computeStyleInt(ele, `padding${xAxis ? 'Left' : 'Top'}`) + computeStyleInt(ele, `padding${xAxis ? 'Right' : 'Bottom'}`) + computeStyleInt(ele, `border${xAxis ? 'Right' : 'Bottom'}Width`);

dist/cash.js

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -445,72 +445,45 @@ function css(prop, value) {
445445

446446
;
447447
Cash.prototype.css = css; // @optional ./css.ts
448-
449-
var dataNamespace = '__cashData',
450-
dataAttributeRe = /^data-(.*)/; // @require core/cash.ts
451-
// @require ./helpers/variables.ts
452-
453-
function hasData(ele) {
454-
return dataNamespace in ele;
455-
}
456-
457-
cash.hasData = hasData; // @require ./variables.ts
458-
459-
function getDataCache(ele) {
460-
return ele[dataNamespace] = ele[dataNamespace] || {};
461-
} // @require attributes/attr.ts
462-
// @require ./get_data_cache.ts
463-
448+
// @require core/camel_case.ts
464449

465450
function getData(ele, key) {
466-
var cache = getDataCache(ele);
451+
var value = ele.dataset ? ele.dataset[key] || ele.dataset[camelCase(key)] : ele.getAttribute("data-" + key);
467452

468-
if (key) {
469-
if (!(key in cache)) {
470-
var value = ele.dataset ? ele.dataset[key] || ele.dataset[camelCase(key)] : cash(ele).attr("data-" + key);
453+
try {
454+
return JSON.parse(value);
455+
} catch (_a) {}
471456

472-
if (value !== undefined) {
473-
try {
474-
value = JSON.parse(value);
475-
} catch (e) {}
476-
477-
cache[key] = value;
478-
}
479-
}
457+
return value;
458+
} // @require core/camel_case.ts
480459

481-
return cache[key];
482-
}
483-
484-
return cache;
485-
} // @require ./variables.ts
486-
// @require ./get_data_cache.ts
487460

461+
function setData(ele, key, value) {
462+
try {
463+
value = JSON.stringify(value);
464+
} catch (_a) {}
488465

489-
function removeData(ele, key) {
490-
if (key === undefined) {
491-
delete ele[dataNamespace];
466+
if (ele.dataset) {
467+
ele.dataset[camelCase(key)] = value;
492468
} else {
493-
delete getDataCache(ele)[key];
469+
ele.setAttribute("data-" + key, value);
494470
}
495-
} // @require ./get_data_cache.ts
496-
497-
498-
function setData(ele, key, value) {
499-
getDataCache(ele)[key] = value;
500471
}
501472

473+
var dataAttributeRe = /^data-(.+)/;
474+
502475
function data(name, value) {
503476
var _this = this;
504477

505478
if (!name) {
506479
if (!this[0]) return;
480+
var datas_1 = {};
507481
each(this[0].attributes, function (i, attr) {
508482
var match = attr.name.match(dataAttributeRe);
509483
if (!match) return;
510-
511-
_this.data(match[1]);
484+
datas_1[match[1]] = _this.data(match[1]);
512485
});
513-
return getData(this[0]);
486+
return datas_1;
514487
}
515488

516489
if (isString(name)) {
@@ -527,17 +500,9 @@ function data(name, value) {
527500
return this;
528501
}
529502

530-
Cash.prototype.data = data;
531-
532-
Cash.prototype.removeData = function (key) {
533-
return this.each(function (i, ele) {
534-
return removeData(ele, key);
535-
});
536-
}; // @optional ./data.ts
537-
// @optional ./remove_data.ts
503+
Cash.prototype.data = data; // @optional ./data.ts
538504
// @require css/helpers/compute_style_int.ts
539505

540-
541506
function getExtraSpace(ele, xAxis) {
542507
return computeStyleInt(ele, "border" + (xAxis ? 'Left' : 'Top') + "Width") + computeStyleInt(ele, "padding" + (xAxis ? 'Left' : 'Top')) + computeStyleInt(ele, "padding" + (xAxis ? 'Right' : 'Bottom')) + computeStyleInt(ele, "border" + (xAxis ? 'Right' : 'Bottom') + "Width");
543508
}

0 commit comments

Comments
 (0)