@gitlab/ui
Version:
GitLab UI Components
45 lines (42 loc) • 1.37 kB
JavaScript
/**
* Normalizes a single option into a standard { value, text, html, disabled } shape.
*/
function normalizeOption(option) {
if (option !== null && typeof option === 'object') {
const value = option.value,
text = option.text,
html = option.html,
disabled = option.disabled;
return {
value: value === undefined ? text : value,
text: String(text),
html,
disabled: Boolean(disabled)
};
}
return {
value: option,
text: String(option),
disabled: false
};
}
/**
* Normalizes an options array into a consistent format for rendering.
*
* Each option can be a primitive (string, number) or an object with
* `{ value, text, html, disabled }` properties. Primitives are converted
* to `{ value, text, disabled: false }`. For objects, `value` defaults to
* `text` when omitted and `disabled` defaults to `false`.
*
* @param {Array<string|number|{value?: *, text: string, html?: string, disabled?: boolean}>} options
* The raw options array to normalize.
* @returns {Array<{value: *, text: string, html?: string, disabled: boolean}>}
* The normalized options, or an empty array if the input is not an array.
*/
function normalizeOptions(options) {
if (Array.isArray(options)) {
return options.map(option => normalizeOption(option));
}
return [];
}
export { normalizeOptions };