UNPKG

compress-tag

Version:

Template literal tag to remove excess whitespace and newlines from strings.

106 lines (105 loc) 3.49 kB
/** * A function that can be used to tag a string template literal. * @param stringOrStrings A single string value, or when used as a template * literal tag, the automatically generated `TemplateStringsArray`. * @param placeholders Values of the placeholder expressions. * @returns The processed template string. * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates|MDN} */ type ChainableTemplateLiteralTag = <T extends TemplateStringsArray | string>(stringOrStrings: T, ...placeholders: T extends TemplateStringsArray ? unknown[] : []) => string; /** * Parses the string and placeholders as normal, then removes any line breaks * and the spaces surrounding each line (ie, indentation), replacing each line * break with a single space. Empty lines are removed completely. * * Can be used either as a template literal tag or as a function that accepts * a string. This section option is used when the template literal already must * be tagged with some other tag. * * If you desire a linebreak to be present in the final result, you must provide * it as a linebreak character. (`\n` or `\r\n`). If you desire indentation in * the result, you must include that as a tab character `\t`. * @example * let sampleText = "This is some sample text." * compress` * <div> * <span>${sampleText}</span> * </div> * ` * // => "<div> <span>This is some sample text.</span> </div>" * @example * compress(uppercase` * This is some * sample text. * `) * // => "THIS IS SOME SAMPLE TEXT." * @returns The processed, minified / compressed string. * @param stringOrStrings A string to process, or (when used as a template * literal tag), an automatically generated `TemplateStringsArray`. * * **Note:** If passing a string to this function directly, manual linebreaks * will not be preserved. * * **Note:** You should typically not need to pass a `TemplateStringsArray` * directly to the function. Instead, use it as a template literal tag per the * example. * @param placeholders Values of the placeholder expressions. * * **Note:** You should typically not need to pass this value directly to the * function. Instead, use it as a template literal tag per the example. */ export declare const compress: ChainableTemplateLiteralTag; /** * Acts exactly the same as `compress` except that it doesn't insert spaces * where line breaks were removed. This makes it useful for code and URLs, for * example. * @see compress For full documentation. * * @example * let sampleText = "This is some sample text." * compressTight` * <div> * <span>${sampleText}</span> * </div> * ` * // => "<div><span>This is some sample text.</span></div>" * * @example * compressTight(uppercase` * This is some * sample text. * `) * // => "THIS IS SOMESAMPLE TEXT." */ export declare const compressTight: ChainableTemplateLiteralTag; /** * Shorthand for `compress`. * @see compress for full documentation. * * @example * c`Example * text.` * // => Example text. * * @example * c(capitalize`Example * text.`) * // => EXAMPLE TEXT. */ export declare const c: ChainableTemplateLiteralTag; /** * Shorthand for `compressTight`. * @see compressTight for full documentation. * * @example * t`Example * text.` * // => Exampletext. * * @example * t(capitalize`Example * text.`) * // => EXAMPLETEXT. */ export declare const t: ChainableTemplateLiteralTag; export {};