Skip to content

Commit

Permalink
added support for boolean attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Feb 11, 2021
1 parent 7310a9e commit f1e595d
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 12 deletions.
8 changes: 8 additions & 0 deletions cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const attribute = (node, name) => {
};
exports.attribute = attribute;

const boolean = (node, key) => value => {
if (value)
node.setAttribute(key, '');
else
node.removeAttribute(key);
};
exports.boolean = boolean;

const data = ({dataset}) => values => {
for (const key in values) {
const value = values[key];
Expand Down
2 changes: 1 addition & 1 deletion es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export const attribute = (node, name) => {
};
};

export const boolean = (node, key) => value => {
if (value)
node.setAttribute(key, '');
else
node.removeAttribute(key);
};

export const data = ({dataset}) => values => {
for (const key in values) {
const value = values[key];
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ var uhtmlHandlers = (function (exports) {
}
};
};

var _boolean = function _boolean(node, key) {
return function (value) {
if (value) node.setAttribute(key, '');else node.removeAttribute(key);
};
};
var data = function data(_ref) {
var dataset = _ref.dataset;
return function (values) {
Expand Down Expand Up @@ -80,6 +86,7 @@ var uhtmlHandlers = (function (exports) {

exports.aria = aria;
exports.attribute = attribute;
exports.boolean = _boolean;
exports.data = data;
exports.event = event;
exports.ref = ref;
Expand Down
2 changes: 1 addition & 1 deletion min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
"author": "Andrea Giammarchi",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.11.4",
"@babel/preset-env": "^7.11.0",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"ascjs": "^4.0.1",
"@babel/core": "^7.12.13",
"@babel/preset-env": "^7.12.13",
"@rollup/plugin-babel": "^5.2.3",
"@rollup/plugin-node-resolve": "^11.1.1",
"ascjs": "^5.0.0",
"coveralls": "^3.1.0",
"linkedom": "^0.1.39",
"linkedom": "^0.4.12",
"nyc": "^15.1.0",
"rollup": "^2.26.5",
"rollup-plugin-terser": "^7.0.0",
"terser": "^5.2.1"
"rollup": "^2.38.5",
"rollup-plugin-terser": "^7.0.2",
"terser": "^5.6.0-beta"
},
"module": "./esm/index.js",
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions rollup/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {
})
],
output: {
esModule: false,
exports: 'named',
file: './index.js',
format: 'iife',
Expand Down
1 change: 1 addition & 0 deletions rollup/es.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default {
terser()
],
output: {
esModule: false,
exports: 'named',
file: './es.js',
format: 'iife',
Expand Down
9 changes: 8 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ global.document = document;
if (!('onclick' in HTMLElement.prototype))
HTMLElement.prototype.onclick = function () {};

const {aria, attribute, data, event, ref, setter, text} = require('../cjs');
const {aria, attribute, boolean, data, event, ref, setter, text} = require('../cjs');

const div = document.createElement('div');

Expand Down Expand Up @@ -61,3 +61,10 @@ textfy('value');
console.assert(div.textContent === 'value', 'text(value)');
textfy(void 0);
console.assert(div.textContent === '', 'text(void 0)');

const booleanify = boolean(div, 'boolean');
console.assert(!div.hasAttribute('boolean'), 'no boolean');
booleanify(1);
console.assert(div.hasAttribute('boolean'), 'yes boolean');
booleanify(0);
console.assert(!div.hasAttribute('boolean'), 'no boolean again');

0 comments on commit f1e595d

Please sign in to comment.