UNPKG

@alifd/next

Version:

A configurable component library for web built on React.

32 lines (31 loc) 1.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.randomId = exports.escapeForId = void 0; /** * Replace characters not allowed for HTML id attributes */ function escapeForId(text) { if (!text) { return ''; } if (typeof text === 'object') { text = JSON.stringify(text); } else if (typeof text !== 'string') { text = String(text); } return text.replace(/['"]/gm, '').replace(/[\s'"]/gm, '-'); } exports.escapeForId = escapeForId; /** * Generate a string to be used for HTML id attributes * * @param prefix - prefix text for the id. Important because without one, eventually there will be 2 elements with the same id on the same page * @param max - range for the random number generator. Defaults to 1,000,000, but can be set higher if necessary. */ function randomId(prefix, max) { if (max === void 0) { max = 1000000; } var rand = Math.ceil(Math.random() * max); return prefix ? "".concat(escapeForId(prefix), "-").concat(rand) : rand.toString(10); } exports.randomId = randomId;