This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+29
View File
@@ -0,0 +1,29 @@
declare namespace stringifyAttributes {
interface Attributes {
[attributeName: string]: string | number | boolean | readonly string[];
}
}
/**
Turn an object into a string of HTML attributes.
Note that the string is prepended with a space when there are attributes to simplify using it in a HTML tag.
@example
```
import stringifyAttributes = require('stringify-attributes');
stringifyAttributes({
unicorn: '🦄',
rainbow: true,
number: 1,
multiple: ['a', 'b']
});
//=> ' unicorn="🦄" rainbow number="1" multiple="a b"'
```
*/
declare function stringifyAttributes(
attributes: stringifyAttributes.Attributes
): string;
export = stringifyAttributes;
+26
View File
@@ -0,0 +1,26 @@
'use strict';
const {htmlEscape} = require('escape-goat');
module.exports = attributes => {
const handledAttributes = [];
for (let [key, value] of Object.entries(attributes)) {
if (value === false) {
continue;
}
if (Array.isArray(value)) {
value = value.join(' ');
}
let attribute = htmlEscape(key);
if (value !== true) {
attribute += `="${htmlEscape(String(value))}"`;
}
handledAttributes.push(attribute);
}
return handledAttributes.length > 0 ? ' ' + handledAttributes.join(' ') : '';
};
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+41
View File
@@ -0,0 +1,41 @@
{
"name": "stringify-attributes",
"version": "2.0.0",
"description": "Turn an object into a string of HTML attributes",
"license": "MIT",
"repository": "sindresorhus/stringify-attributes",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"html",
"attributes",
"stringify",
"element",
"tag",
"props",
"object",
"convert",
"transform"
],
"dependencies": {
"escape-goat": "^2.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
+40
View File
@@ -0,0 +1,40 @@
# stringify-attributes [![Build Status](https://travis-ci.org/sindresorhus/stringify-attributes.svg?branch=master)](https://travis-ci.org/sindresorhus/stringify-attributes)
> Turn an object into a string of HTML attributes
## Install
```
$ npm install stringify-attributes
```
## Usage
```js
const stringifyAttributes = require('stringify-attributes');
stringifyAttributes({
unicorn: '🦄',
rainbow: true,
number: 1,
multiple: [
'a',
'b'
]
});
//=> ' unicorn="🦄" rainbow number="1" multiple="a b"'
```
Note that the string is prepended with a space when there are attributes to simplify using it in a HTML tag.
## Related
- [create-html-element](https://github.com/sindresorhus/create-html-element) - Create a HTML element string
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)