UNPKG

qa-shadow-report

Version:

npm package that prints formatted test reports into a google sheet or csv file

26 lines (23 loc) 857 B
/** * Truncates a string or number to the specified maximum length. * @param {string|number|null|undefined} str - The string, number, or nullish value to be truncated. * @param {number} maxLength - The maximum length to enforce. * @returns {string} - The truncated string or an empty string if the input is nullish or invalid. * @throws {Error} - If maxLength is not a positive integer. */ export const enforceMaxLength = (str, maxLength) => { if (!Number.isInteger(maxLength) || maxLength < 0) { throw new Error('maxLength must be a positive integer'); } if (str == null) { return ''; } try { const result = typeof str === 'number' || typeof str === 'string' ? String(str) : ''; return result.substring(0, maxLength); } catch (error) { console.error('Error in enforceMaxLength:', error); return ''; } };