MCPcopy Create free account
hub / github.com/cure53/DOMPurify / stringifyValue

Function stringifyValue

src/utils.ts:229–279  ·  view source on GitHub ↗

* Convert non-node values into strings without depending on direct property access. * * @param value - The value to stringify. * @returns A string representation of the provided value.

(value: unknown)

Source from the content-addressed store, hash-verified

227 * @returns A string representation of the provided value.
228 */
229function stringifyValue(value: unknown): string {
230 switch (typeof value) {
231 case 'string': {
232 return value;
233 }
234
235 case 'number': {
236 return numberToString(value);
237 }
238
239 case 'boolean': {
240 return booleanToString(value);
241 }
242
243 case 'bigint': {
244 return bigintToString ? bigintToString(value) : '0';
245 }
246
247 case 'symbol': {
248 return symbolToString ? symbolToString(value) : 'Symbol()';
249 }
250
251 case 'undefined': {
252 return objectToString(value);
253 }
254
255 case 'function':
256 case 'object': {
257 if (value === null) {
258 return objectToString(value);
259 }
260
261 const valueAsRecord = value as Record<string, any>;
262 const valueToString = lookupGetter(valueAsRecord, 'toString');
263
264 if (typeof valueToString === 'function') {
265 const stringified = valueToString(valueAsRecord);
266
267 return typeof stringified === 'string'
268 ? stringified
269 : objectToString(stringified);
270 }
271
272 return objectToString(value);
273 }
274
275 default: {
276 return objectToString(value);
277 }
278 }
279}
280
281/**
282 * This method automatically checks if the prop is function or getter and behaves accordingly.

Callers 1

_sanitizeFunction · 0.85

Calls 1

lookupGetterFunction · 0.85

Tested by

no test coverage detected