feat(core): Added the Attributes.setIfUnset() method
This commit is contained in:
parent
567046f52c
commit
2eb7a23750
4 changed files with 58 additions and 5 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
unreleased:
|
||||||
|
|
||||||
|
Added:
|
||||||
|
core:
|
||||||
|
- Added the new attributes.setIfUnset() method
|
||||||
|
|
||||||
2.19.5:
|
2.19.5:
|
||||||
date: 2022-11-13
|
date: 2022-11-13
|
||||||
|
|
||||||
|
|
24
markdown/dev/reference/api/attributes/setifunset/en.md
Normal file
24
markdown/dev/reference/api/attributes/setifunset/en.md
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
---
|
||||||
|
title: setIfUnset()
|
||||||
|
---
|
||||||
|
|
||||||
|
```js
|
||||||
|
Attributes attributes.setIfUnset(string key, string value)
|
||||||
|
```
|
||||||
|
|
||||||
|
Sets the attribute identified by `key` to value `value` but only if it's currently unset (`undefined`).
|
||||||
|
|
||||||
|
<Tip>
|
||||||
|
|
||||||
|
This will never overwrite any value and thus is a safe way to set attributes
|
||||||
|
|
||||||
|
</Tip>
|
||||||
|
|
||||||
|
```js
|
||||||
|
let { Path, paths } = part.shorthand();
|
||||||
|
|
||||||
|
// This will render as: class="classA"
|
||||||
|
paths.demo = new Path();
|
||||||
|
paths.demo.attributes.set('class', 'classA');
|
||||||
|
paths.demo.attributes.setIfUnset('class', 'classB');
|
||||||
|
```
|
|
@ -19,6 +19,13 @@ Attributes.prototype.set = function (name, value) {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Sets an attribute, but only if it's not currently set */
|
||||||
|
Attributes.prototype.setIfUnset = function (name, value) {
|
||||||
|
if (typeof this.list[name] === 'undefined') this.list[name] = [value]
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
/** Removes an attribute */
|
/** Removes an attribute */
|
||||||
Attributes.prototype.remove = function (name) {
|
Attributes.prototype.remove = function (name) {
|
||||||
delete this.list[name]
|
delete this.list[name]
|
||||||
|
|
|
@ -1,9 +1,25 @@
|
||||||
let expect = require("chai").expect;
|
const expect = require("chai").expect;
|
||||||
let Point = require("./dist").Point;
|
const Point = require("./dist").Point;
|
||||||
|
|
||||||
function newAttr() {
|
const newAttr = () => new Point(0, 0).attributes;
|
||||||
return new Point(0, 0).attributes;
|
|
||||||
}
|
const a = newAttr();
|
||||||
|
it("Should set an attribute", () => {
|
||||||
|
a.set('hold', 'me')
|
||||||
|
expect(a.get("hold")).to.equal('me');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should remove an attribute", () => {
|
||||||
|
a.remove('hold')
|
||||||
|
expect(a.get("hold")).to.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should only set an unset attribute", () => {
|
||||||
|
a.setIfUnset('hold', 'me')
|
||||||
|
expect(a.get("hold")).to.equal('me');
|
||||||
|
a.setIfUnset('hold', 'thatDudeOverThere')
|
||||||
|
expect(a.get("hold")).to.equal('me');
|
||||||
|
});
|
||||||
|
|
||||||
it("Should return false when getting an unset attribute", () => {
|
it("Should return false when getting an unset attribute", () => {
|
||||||
let a = newAttr();
|
let a = newAttr();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue