Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/stringy.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
// stringify is for passing objects as html atttributes.
// JSON.stringify doesn't work if your object has double and single quotes as it will end the string early.
export function stringifyAttribute(ob) {
if(!ob) return ''
let s = ''
if (!ob) {
return ''
}

let s
if (typeof ob === 'string') {
s = ob
} else {
s = JSON.stringify(ob)
}
s = s.replaceAll(
'"',
'"'
)

// Escape double quotes for HTML attributes
s = s.replaceAll('"', '"')

// Escape single quotes for HTML attributes
s = s.replaceAll("'", ''')

return s
}