From 1260aa495815a1705bf0e6eacfdd13178ac03d06 Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Wed, 22 May 2024 11:21:37 +0200 Subject: [PATCH 01/16] Migrate str to Typescript --- lib/str.ts | 1065 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1065 insertions(+) create mode 100644 lib/str.ts diff --git a/lib/str.ts b/lib/str.ts new file mode 100644 index 00000000..e324e34e --- /dev/null +++ b/lib/str.ts @@ -0,0 +1,1065 @@ +/* eslint-disable no-control-regex */ +import _ from 'underscore'; +import * as HtmlEntities from 'html-entities'; +import * as Constants from './CONST'; +import * as UrlPatterns from './Url'; + +const REMOVE_SMS_DOMAIN_PATTERN = /@expensify\.sms/gi; + +/** + * Checks if parameter is a string or function + * if it is a function then we will call it with + * any additional arguments. + */ +function result(parameter: string): string; +function result(parameter: (...args: A) => R, ...args: A): R; +function result(parameter: string | ((...args: A) => R), ...args: A): string | R { + if (typeof parameter === 'function') { + return parameter(...args); + } + + return parameter; +} + +const Str = { + /** + * Return true if the string is ending with the provided suffix + * + * @param str String ot search in + * @param suffix What to look for + */ + endsWith(str: string, suffix: string): boolean { + if (!str || !suffix) { + return false; + } + return str.substr(-suffix.length) === suffix; + }, + + /** + * Converts a USD string into th number of cents it represents. + * + * @param amountStr A string representing a USD value. + * @param allowFraction Flag indicating if fractions of cents should be + * allowed in the output. + * + * @return number The cent value of the @p amountStr. + */ + fromUSDToNumber(amountStr: string, allowFraction: boolean): number { + let amount: string | number = String(amountStr).replace(/[^\d.\-()]+/g, ''); + if (amount.match(/\(.*\)/)) { + const modifiedAmount = amount.replace(/[()]/g, ''); + amount = `-${modifiedAmount}`; + } + amount = Number(amount) * 100; + + amount = Math.round(amount * 1e3) / 1e3; + return allowFraction ? amount : Math.round(amount); + }, + + /** + * Truncates the middle section of a string based on the max allowed length + */ + truncateInMiddle(fullStr: string, maxLength: number): string { + if (fullStr.length <= maxLength) { + return fullStr; + } + + const separator = '...'; + const halfLengthToShow = (maxLength - separator.length) / 2; + const beginning = fullStr.substr(0, Math.ceil(halfLengthToShow)); + const end = fullStr.substr(fullStr.length - Math.floor(halfLengthToShow)); + + return beginning + separator + end; + }, + + /** + * Convert new line to
+ */ + nl2br(str: string): string { + return str.replace(/\n/g, '
'); + }, + + /** + * Decodes the given HTML encoded string. + * + * @param s The string to decode. + * @return string The decoded string. + */ + htmlDecode(s: string): string { + // Use jQuery if it exists or else use html-entities + if (typeof jQuery !== 'undefined') { + return jQuery('