{"version":3,"file":"utils.cjs","names":["isValueDefined"],"sources":["../../src/truncate/utils.ts"],"sourcesContent":["import { isValueDefined } from '../utils/values';\nimport type { TruncateProps } from './types';\n\nexport const TRUNCATE_ELLIPSIS = '…';\nexport const TRUNCATE_TYPE = {\n\tauto: 'auto',\n\thead: 'head',\n\tmiddle: 'middle',\n\ttail: 'tail',\n\tnone: 'none',\n} as const;\n\nexport const TRUNCATE_DEFAULT_PROPS = {\n\tellipsis: TRUNCATE_ELLIPSIS,\n\tellipsizeMode: TRUNCATE_TYPE.auto,\n\tlimit: 0,\n\tnumberOfLines: 0,\n};\n\n// Source\n// https://github.com/kahwee/truncate-middle\nexport function truncateMiddle(\n\tword: string,\n\theadLength: number,\n\ttailLength: number,\n\tellipsis: string\n) {\n\tif ( typeof word !== 'string' ) {\n\t\treturn '';\n\t}\n\tconst wordLength = word.length;\n\t// Setting default values\n\t// eslint-disable-next-line no-bitwise\n\tconst frontLength = ~~headLength; // Will cast to integer\n\t// eslint-disable-next-line no-bitwise\n\tconst backLength = ~~tailLength;\n\t/* istanbul ignore next */\n\tconst truncateStr = isValueDefined( ellipsis )\n\t\t? ellipsis\n\t\t: TRUNCATE_ELLIPSIS;\n\n\tif (\n\t\t( frontLength === 0 && backLength === 0 ) ||\n\t\tfrontLength >= wordLength ||\n\t\tbackLength >= wordLength ||\n\t\tfrontLength + backLength >= wordLength\n\t) {\n\t\treturn word;\n\t} else if ( backLength === 0 ) {\n\t\treturn word.slice( 0, frontLength ) + truncateStr;\n\t}\n\treturn (\n\t\tword.slice( 0, frontLength ) +\n\t\ttruncateStr +\n\t\tword.slice( wordLength - backLength )\n\t);\n}\n\nexport function truncateContent(\n\twords: string = '',\n\tprops: Omit< TruncateProps, 'children' >\n) {\n\tconst mergedProps = { ...TRUNCATE_DEFAULT_PROPS, ...props };\n\tconst { ellipsis, ellipsizeMode, limit } = mergedProps;\n\n\tif ( ellipsizeMode === TRUNCATE_TYPE.none ) {\n\t\treturn words;\n\t}\n\n\tlet truncateHead: number;\n\tlet truncateTail: number;\n\n\tswitch ( ellipsizeMode ) {\n\t\tcase TRUNCATE_TYPE.head:\n\t\t\ttruncateHead = 0;\n\t\t\ttruncateTail = limit;\n\t\t\tbreak;\n\t\tcase TRUNCATE_TYPE.middle:\n\t\t\ttruncateHead = Math.floor( limit / 2 );\n\t\t\ttruncateTail = Math.floor( limit / 2 );\n\t\t\tbreak;\n\t\tdefault:\n\t\t\ttruncateHead = limit;\n\t\t\ttruncateTail = 0;\n\t}\n\n\tconst truncatedContent =\n\t\tellipsizeMode !== TRUNCATE_TYPE.auto\n\t\t\t? truncateMiddle( words, truncateHead, truncateTail, ellipsis )\n\t\t\t: words;\n\n\treturn truncatedContent;\n}\n"],"mappings":";;;;;;;AAGA,MAAa,oBAAoB;AACjC,MAAa,gBAAgB;CAC5B,MAAM;CACN,MAAM;CACN,QAAQ;CACR,MAAM;CACN,MAAM;AACP;AAEA,MAAa,yBAAyB;CACrC,UAAA;CACA,eAAe,cAAc;CAC7B,OAAO;CACP,eAAe;AAChB;AAIA,SAAgB,eACf,MACA,YACA,YACA,UACC;CACD,IAAK,OAAO,SAAS,UACpB,OAAO;CAER,MAAM,aAAa,KAAK;CAGxB,MAAM,cAAc,CAAC,CAAC;CAEtB,MAAM,aAAa,CAAC,CAAC;;CAErB,MAAM,eAAA,GAAcA,mBAAAA,eAAAA,CAAgB,QAAS,IAC1C,WAAA;CAGH,IACG,gBAAgB,KAAK,eAAe,KACtC,eAAe,cACf,cAAc,cACd,cAAc,cAAc,YAE5B,OAAO;MACD,IAAK,eAAe,GAC1B,OAAO,KAAK,MAAO,GAAG,WAAY,IAAI;CAEvC,OACC,KAAK,MAAO,GAAG,WAAY,IAC3B,cACA,KAAK,MAAO,aAAa,UAAW;AAEtC;AAEA,SAAgB,gBACf,QAAgB,IAChB,OACC;CAED,MAAM,EAAE,UAAU,eAAe,UAAU;EADrB,GAAG;EAAwB,GAAG;CACC;CAErD,IAAK,kBAAkB,cAAc,MACpC,OAAO;CAGR,IAAI;CACJ,IAAI;CAEJ,QAAS,eAAT;EACC,KAAK,cAAc;GAClB,eAAe;GACf,eAAe;GACf;EACD,KAAK,cAAc;GAClB,eAAe,KAAK,MAAO,QAAQ,CAAE;GACrC,eAAe,KAAK,MAAO,QAAQ,CAAE;GACrC;EACD;GACC,eAAe;GACf,eAAe;CACjB;CAOA,OAJC,kBAAkB,cAAc,OAC7B,eAAgB,OAAO,cAAc,cAAc,QAAS,IAC5D;AAGL"}