I published a npm package:
https://www.npmjs.com/package/inputid
A Javascript value object representing an HTML form control ID, making sure the ID generation is deterministic and unique in a document.
[...]
Features
Deterministic ID generation based on HTML element attributes
Unique ID generation in a document
ID sanitization according to the document type
Removal of characters that might be problematic in CSS selectors
ID generation to find an element by its ID attribute or its related labels
[...]
AnInputId
instantiation is a value object representing a form control ID. It should be immutable and can be used as a String.It doesn't require any arguments or configuration:
const InputId = require('inputid'); labelElement.htmlFor = inputElement.id = new InputId();
But you'd get better results if an HTML element or options are provided as an argument.
[...]