utils/node_modules/@rushstack/node-core-library/lib/Text.js.map
2024-02-07 01:33:07 -05:00

1 line
13 KiB
Plaintext

{"version":3,"file":"Text.js","sourceRoot":"","sources":["../src/Text.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,uCAAyB;AAEzB;;;GAGG;AACH,IAAY,QAEX;AAFD,WAAY,QAAQ;IAClB,yBAAa,CAAA;AACf,CAAC,EAFW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAEnB;AAED;;;GAGG;AACH,IAAY,WAkBX;AAlBD,WAAY,WAAW;IACrB;;OAEG;IACH,4BAAa,CAAA;IAEb;;;;;OAKG;IACH,wBAAS,CAAA;IAET;;OAEG;IACH,+BAAgB,CAAA;AAClB,CAAC,EAlBW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAkBtB;AAwBD,MAAM,aAAa,GAAW,kBAAkB,CAAC;AACjD,MAAM,oBAAoB,GAAW,oBAAoB,CAAC;AAE1D,QAAQ,CAAC,CAAC,kBAAkB;AAC1B,kDAAkD;AAClD,KAA6B,EAC7B,QAAkB,EAClB,gBAAyB,EACzB,KAAkC;IAElC,IAAI,CAAC,KAAK,EAAE;QACV,OAAO;KACR;IACD,MAAM,SAAS,GAAW,KAAK,CAAC,SAAS,GAAG,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC3G,IAAI,UAAU,GAAW,CAAC,CAAC;IAC3B,MAAM,OAAO,GAAuC,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;IACtF,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;QAC3B,MAAM,QAAQ,GAAW,KAAK,CAAC,KAAM,CAAC;QACtC,IAAI,UAAU,KAAK,QAAQ,IAAI,CAAC,gBAAg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B;QACH,CAAC;KAAA;IAED;;;;;;OAMG;IACI,MAAM,CAAC,CAAC,qBAAqB;IAClC,kDAAkD;IAClD,QAA0C,EAC1C,UAAyC,EAAE;QAE3C,MAAM,EAAE,QAAQ,GAAG,QAAQ,CAAC,IAAI,EAAE,gBAAgB,GAAG,KAAK,EAAE,GAAG,OAAO,CAAC;QACvE,MAAM,KAAK,GAAgC,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC;QAC7D,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE;YAC5B,KAAK,CAAC,CAAC,kBAAkB,CAAC,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,KAAK,CAAC,CAAC;SACrE;QACD,MAAM,SAAS,GAAW,KAAK,CAAC,SAAS,CAAC;QAC1C,IAAI,SAAS,CAAC,MAAM,EAAE;YACpB,MAAM,SAAS,CAAC;SACjB;IACH,CAAC;;AAjLuB,kBAAa,GAAW,aAAa,CAAC;AACtC,uBAAkB,GAAW,oBAAoB,CAAC;AAF/D,oBAAI","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport * as os from 'os';\n\n/**\n * The allowed types of encodings, as supported by Node.js\n * @public\n */\nexport enum Encoding {\n Utf8 = 'utf8'\n}\n\n/**\n * Enumeration controlling conversion of newline characters.\n * @public\n */\nexport enum NewlineKind {\n /**\n * Windows-style newlines\n */\n CrLf = '\\r\\n',\n\n /**\n * POSIX-style newlines\n *\n * @remarks\n * POSIX is a registered trademark of the Institute of Electrical and Electronic Engineers, Inc.\n */\n Lf = '\\n',\n\n /**\n * Default newline type for this operating system (`os.EOL`).\n */\n OsDefault = 'os'\n}\n\n/**\n * Options used when calling the {@link Text.readLinesFromIterable} or\n * {@link Text.readLinesFromIterableAsync} methods.\n *\n * @public\n */\nexport interface IReadLinesFromIterableOptions {\n /**\n * The encoding of the input iterable. The default is utf8.\n */\n encoding?: Encoding;\n\n /**\n * If true, empty lines will not be returned. The default is false.\n */\n ignoreEmptyLines?: boolean;\n}\n\ninterface IReadLinesFromIterableState {\n remaining: string;\n}\n\nconst NEWLINE_REGEX: RegExp = /\\r\\n|\\n\\r|\\r|\\n/g;\nconst NEWLINE_AT_END_REGEX: RegExp = /(\\r\\n|\\n\\r|\\r|\\n)$/;\n\nfunction* readLinesFromChunk(\n // eslint-disable-next-line @rushstack/no-new-null\n chunk: string | Buffer | null,\n encoding: Encoding,\n ignoreEmptyLines: boolean,\n state: IReadLinesFromIterableState\n): Generator<string> {\n if (!chunk) {\n return;\n }\n const remaining: string = state.remaining + (typeof chunk === 'string' ? chunk : chunk.toString(encoding));\n let startIndex: number = 0;\n const matches: IterableIterator<RegExpMatchArray> = remaining.matchAll(NEWLINE_REGEX);\n for (const match of matches) {\n const endIndex: number = match.index!;\n if (startIndex !== endIndex || !ignoreEmptyLines) {\n yield remaining.substring(startIndex, endIndex);\n }\n startIndex = endIndex + match[0].length;\n }\n state.remaining = remaining.substring(startIndex);\n}\n\n/**\n * Operations for working with strings that contain text.\n *\n * @remarks\n * The utilities provided by this class are intended to be simple, small, and very\n * broadly applicable.\n *\n * @public\n */\nexport class Text {\n private static readonly _newLineRegEx: RegExp = NEWLINE_REGEX;\n private static readonly _newLineAtEndRegEx: RegExp = NEWLINE_AT_END_REGEX;\n\n /**\n * Returns the same thing as targetString.replace(searchValue, replaceValue), except that\n * all matches are replaced, rather than just the first match.\n * @param input - The string to be modified\n * @param searchValue - The value to search for\n * @param replaceValue - The replacement text\n */\n public static replaceAll(input: string, searchValue: string, replaceValue: string): string {\n return input.split(searchValue).join(replaceValue);\n }\n\n /**\n * Converts all newlines in the provided string to use Windows-style CRLF end of line characters.\n */\n public static convertToCrLf(input: string): string {\n return input.replace(Text._newLineRegEx, '\\r\\n');\n }\n\n /**\n * Converts all newlines in the provided string to use POSIX-style LF end of line characters.\n *\n * POSIX is a registered trademark of the Institute of Electrical and Electronic Engineers, Inc.\n */\n public static convertToLf(input: string): string {\n return input.replace(Text._newLineRegEx, '\\n');\n }\n\n /**\n * Converts all newlines in the provided string to use the specified newline type.\n */\n public static convertTo(input: string, newlineKind: NewlineKind): string {\n return input.replace(Text._newLineRegEx, Text.getNewline(newlineKind));\n }\n\n /**\n * Returns the newline character sequence for the specified `NewlineKind`.\n */\n public static getNewline(newlineKind: NewlineKind): string {\n switch (newlineKind) {\n case NewlineKind.CrLf:\n return '\\r\\n';\n case NewlineKind.Lf:\n return '\\n';\n case NewlineKind.OsDefault:\n return os.EOL;\n default:\n throw new Error('Unsupported newline kind');\n }\n }\n\n /**\n * Append characters to the end of a string to ensure the result has a minimum length.\n * @remarks\n * If the string length already exceeds the minimum length, then the string is unchanged.\n * The string is not truncated.\n */\n public static padEnd(s: string, minimumLength: number, paddingCharacter: string = ' '): string {\n if (paddingCharacter.length !== 1) {\n throw new Error('The paddingCharacter parameter must be a single character.');\n }\n\n if (s.length < minimumLength) {\n const paddingArray: string[] = new Array(minimumLength - s.length);\n paddingArray.unshift(s);\n return paddingArray.join(paddingCharacter);\n } else {\n return s;\n }\n }\n\n /**\n * Append characters to the start of a string to ensure the result has a minimum length.\n * @remarks\n * If the string length already exceeds the minimum length, then the string is unchanged.\n * The string is not truncated.\n */\n public static padStart(s: string, minimumLength: number, paddingCharacter: string = ' '): string {\n if (paddingCharacter.length !== 1) {\n throw new Error('The paddingCharacter parameter must be a single character.');\n }\n\n if (s.length < minimumLength) {\n const paddingArray: string[] = new Array(minimumLength - s.length);\n paddingArray.push(s);\n return paddingArray.join(paddingCharacter);\n } else {\n return s;\n }\n }\n\n /**\n * If the string is longer than maximumLength characters, truncate it to that length\n * using \"...\" to indicate the truncation.\n *\n * @remarks\n * For example truncateWithEllipsis('1234578', 5) would produce '12...'.\n */\n public static truncateWithEllipsis(s: string, maximumLength: number): string {\n if (maximumLength < 0) {\n throw new Error('The maximumLength cannot be a negative number');\n }\n\n if (s.length <= maximumLength) {\n return s;\n }\n\n if (s.length <= 3) {\n return s.substring(0, maximumLength);\n }\n\n return s.substring(0, maximumLength - 3) + '...';\n }\n\n /**\n * Returns the input string with a trailing `\\n` character appended, if not already present.\n */\n public static ensureTrailingNewline(s: string, newlineKind: NewlineKind = NewlineKind.Lf): string {\n // Is there already a newline?\n if (Text._newLineAtEndRegEx.test(s)) {\n return s; // yes, no change\n }\n return s + newlineKind; // no, add it\n }\n\n /**\n * Escapes a string so that it can be treated as a literal string when used in a regular expression.\n */\n public static escapeRegExp(literal: string): string {\n return literal.replace(/[^A-Za-z0-9_]/g, '\\\\$&');\n }\n\n /**\n * Read lines from an iterable object that returns strings or buffers, and return a generator that\n * produces the lines as strings. The lines will not include the newline characters.\n *\n * @param iterable - An iterable object that returns strings or buffers\n * @param options - Options used when reading the lines from the provided iterable\n */\n public static async *readLinesFromIterableAsync(\n iterable: AsyncIterable<string | Buffer>,\n options: IReadLinesFromIterableOptions = {}\n ): AsyncGenerator<string> {\n const { encoding = Encoding.Utf8, ignoreEmptyLines = false } = options;\n const state: IReadLinesFromIterableState = { remaining: '' };\n for await (const chunk of iterable) {\n yield* readLinesFromChunk(chunk, encoding, ignoreEmptyLines, state);\n }\n const remaining: string = state.remaining;\n if (remaining.length) {\n yield remaining;\n }\n }\n\n /**\n * Read lines from an iterable object that returns strings or buffers, and return a generator that\n * produces the lines as strings. The lines will not include the newline characters.\n *\n * @param iterable - An iterable object that returns strings or buffers\n * @param options - Options used when reading the lines from the provided iterable\n */\n public static *readLinesFromIterable(\n // eslint-disable-next-line @rushstack/no-new-null\n iterable: Iterable<string | Buffer | null>,\n options: IReadLinesFromIterableOptions = {}\n ): Generator<string> {\n const { encoding = Encoding.Utf8, ignoreEmptyLines = false } = options;\n const state: IReadLinesFromIterableState = { remaining: '' };\n for (const chunk of iterable) {\n yield* readLinesFromChunk(chunk, encoding, ignoreEmptyLines, state);\n }\n const remaining: string = state.remaining;\n if (remaining.length) {\n yield remaining;\n }\n }\n}\n"]}