UNPKG

@visulima/string

Version:

Functions for manipulating strings.

17 lines (13 loc) 1.49 kB
import{createRequire as a}from"node:module";import{getStringWidth as c}from"../get-string-width.js";const l=a(import.meta.url),n=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,d=e=>{if(typeof n<"u"&&n.versions&&n.versions.node){const[t,s]=n.versions.node.split(".").map(Number);if(t>22||t===22&&s>=3||t===20&&s>=16)return n.getBuiltinModule(e)}return l(e)},{format:u,stripVTControlCharacters:g}=d("node:util"),o=e=>{const t=g(e);return{ansi:e,json:JSON.stringify(e),lengthDifference:e.length-t.length,stripped:t,visible:e.replaceAll("\x1B",String.raw`\u001B`)}},m=(e,t)=>{const s=o(e),i=o(t);return{message:()=>{if(e===t)return"ANSI strings are identical";const r=s.stripped===i.stripped;return u(`ANSI string comparison failed: Actual: - Visible content: %s - With escape codes: %s - JSON: %s - Length: %d Expected: - Visible content: %s - With escape codes: %s - JSON: %s - Length: %d %s `,s.stripped,s.visible,s.json,c(s.ansi),i.stripped,i.visible,i.json,c(i.ansi),r?"✓ Visible content is identical, but escape codes differ":"✗ Visible content differs")},pass:e===t}},b=(e,t)=>{const s=o(e),i=o(t),r=s.stripped===i.stripped,p=e===t;return{actual:s,ansiEqual:p,expected:i,strippedEqual:r,summary:{actualLength:e.length,actualStrippedLength:s.stripped.length,ansiEqual:p,expectedLength:t.length,expectedStrippedLength:i.stripped.length,strippedEqual:r}}};export{b as compareAnsiStrings,m as expectAnsiStrings,o as formatAnsiString};