MCPcopy Create free account
hub / github.com/ethanniser/the-beth-stack / attributesToString

Function attributesToString

packages/beth-stack/src/jsx/utils.ts:131–231  ·  view source on GitHub ↗
(attributes: object)

Source from the content-addressed store, hash-verified

129}
130
131export function attributesToString(attributes: object): string {
132 if (!attributes) {
133 return "";
134 }
135
136 const keys = Object.keys(attributes);
137 const length = keys.length;
138
139 let key, value, type;
140 let result = "";
141 let index = 0;
142
143 for (; index < length; index++) {
144 key = keys[index];
145
146 // Skips all @kitajs/html specific attributes.
147 if (key === "children" || key === "safe") {
148 continue;
149 }
150
151 // @ts-expect-error - this indexing is safe.
152 value = attributes[key];
153
154 // React className compatibility.
155 if (key === "className") {
156 // @ts-expect-error - both were provided, so use the class attribute.
157 if (attributes.class !== undefined) {
158 continue;
159 }
160
161 key = "class";
162 }
163
164 if (key === "style") {
165 result += ' style="' + styleToString(value) + '"';
166 continue;
167 }
168
169 type = typeof value;
170
171 if (type === "boolean") {
172 // Only add the attribute if the value is true.
173 if (value) {
174 result += " " + key;
175 }
176
177 continue;
178 }
179
180 if (value === null || value === undefined) {
181 continue;
182 }
183
184 result += " " + key;
185
186 if (type !== "string") {
187 // Non objects are
188 if (type !== "object") {

Callers 1

createElementFunction · 0.90

Calls 1

styleToStringFunction · 0.85

Tested by

no test coverage detected