@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
254 lines (253 loc) • 7.54 kB
JavaScript
;
/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var strings_exports = {};
__export(strings_exports, {
ansiRegex: () => import_ansi.ansiRegex,
applyLinePrefix: () => applyLinePrefix,
camelToKebab: () => camelToKebab,
centerText: () => centerText,
fromCharCode: () => fromCharCode,
indentString: () => indentString,
isBlankString: () => isBlankString,
isNonEmptyString: () => isNonEmptyString,
repeatString: () => repeatString,
search: () => search,
stringWidth: () => stringWidth,
stripAnsi: () => import_ansi.stripAnsi,
stripBom: () => stripBom,
toKebabCase: () => toKebabCase,
trimNewlines: () => trimNewlines
});
module.exports = __toCommonJS(strings_exports);
var import_ansi = require("./ansi");
var import_get_east_asian_width = require("./external/get-east-asian-width");
const fromCharCode = String.fromCharCode;
// @__NO_SIDE_EFFECTS__
function applyLinePrefix(str, options) {
const { prefix = "" } = {
__proto__: null,
...options
};
return prefix.length ? `${prefix}${str.includes("\n") ? str.replace(/\n/g, `
${prefix}`) : str}` : str;
}
// @__NO_SIDE_EFFECTS__
function camelToKebab(str) {
const { length } = str;
if (!length) {
return "";
}
let result = "";
let i = 0;
while (i < length) {
const char = str[i];
if (!char) {
break;
}
const charCode = char.charCodeAt(0);
const isUpperCase = charCode >= 65 && charCode <= 90;
if (isUpperCase) {
if (result.length > 0) {
result += "-";
}
while (i < length) {
const currChar = str[i];
if (!currChar) {
break;
}
const currCharCode = currChar.charCodeAt(0);
const isCurrUpper = currCharCode >= 65 && currCharCode <= 90;
if (isCurrUpper) {
result += fromCharCode(
currCharCode + 32
/*'a'-'A'*/
);
i += 1;
} else {
break;
}
}
} else {
result += char;
i += 1;
}
}
return result;
}
// @__NO_SIDE_EFFECTS__
function indentString(str, options) {
const { count = 1 } = { __proto__: null, ...options };
return str.replace(/^(?!\s*$)/gm, " ".repeat(count));
}
// @__NO_SIDE_EFFECTS__
function isBlankString(value) {
return typeof value === "string" && (!value.length || /^\s+$/.test(value));
}
// @__NO_SIDE_EFFECTS__
function isNonEmptyString(value) {
return typeof value === "string" && value.length > 0;
}
// @__NO_SIDE_EFFECTS__
function search(str, regexp, options) {
const { fromIndex = 0 } = { __proto__: null, ...options };
const { length } = str;
if (fromIndex >= length) {
return -1;
}
if (fromIndex === 0) {
return str.search(regexp);
}
const offset = fromIndex < 0 ? Math.max(length + fromIndex, 0) : fromIndex;
const result = str.slice(offset).search(regexp);
return result === -1 ? -1 : result + offset;
}
// @__NO_SIDE_EFFECTS__
function stripBom(str) {
return str.length > 0 && str.charCodeAt(0) === 65279 ? str.slice(1) : str;
}
const segmenter = new Intl.Segmenter();
let zeroWidthClusterRegex;
let leadingNonPrintingRegex;
let emojiRegex;
try {
zeroWidthClusterRegex = new RegExp("^(?:\\p{Default_Ignorable_Code_Point}|\\p{Control}|\\p{Mark}|\\p{Surrogate})+$", "v");
leadingNonPrintingRegex = new RegExp("^[\\p{Default_Ignorable_Code_Point}\\p{Control}\\p{Format}\\p{Mark}\\p{Surrogate}]+", "v");
emojiRegex = new RegExp("^\\p{RGI_Emoji}$", "v");
} catch {
zeroWidthClusterRegex = new RegExp("^(?:\\p{Default_Ignorable_Code_Point}|\\p{Control}|\\p{Mark})+$", "u");
leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}]+/u;
emojiRegex = new RegExp("^\\p{Extended_Pictographic}$", "u");
}
// @__NO_SIDE_EFFECTS__
function stringWidth(text) {
if (typeof text !== "string" || !text.length) {
return 0;
}
const plainText = (0, import_ansi.stripAnsi)(text);
if (!plainText.length) {
return 0;
}
let width = 0;
const eastAsianWidthOptions = { ambiguousAsWide: false };
for (const { segment } of segmenter.segment(plainText)) {
if (zeroWidthClusterRegex.test(segment)) {
continue;
}
if (emojiRegex.test(segment)) {
width += 2;
continue;
}
const baseSegment = segment.replace(leadingNonPrintingRegex, "");
const codePoint = baseSegment.codePointAt(0);
if (codePoint === void 0) {
continue;
}
width += (0, import_get_east_asian_width.eastAsianWidth)(codePoint, eastAsianWidthOptions);
if (segment.length > 1) {
for (const char of segment.slice(1)) {
const charCode = char.charCodeAt(0);
if (charCode >= 65280 && charCode <= 65519) {
const trailingCodePoint = char.codePointAt(0);
if (trailingCodePoint !== void 0) {
width += (0, import_get_east_asian_width.eastAsianWidth)(trailingCodePoint, eastAsianWidthOptions);
}
}
}
}
}
return width;
}
// @__NO_SIDE_EFFECTS__
function toKebabCase(str) {
if (!str.length) {
return str;
}
return str.replace(/([a-z]+[0-9]*)([A-Z])/g, "$1-$2").replace(/_/g, "-").toLowerCase();
}
// @__NO_SIDE_EFFECTS__
function trimNewlines(str) {
const { length } = str;
if (length === 0) {
return str;
}
const first = str.charCodeAt(0);
const noFirstNewline = first !== 13 && first !== 10;
if (length === 1) {
return noFirstNewline ? str : "";
}
const last = str.charCodeAt(length - 1);
const noLastNewline = last !== 13 && last !== 10;
if (noFirstNewline && noLastNewline) {
return str;
}
let start = 0;
let end = length;
while (start < end) {
const code = str.charCodeAt(start);
if (code !== 13 && code !== 10) {
break;
}
start += 1;
}
while (end > start) {
const code = str.charCodeAt(end - 1);
if (code !== 13 && code !== 10) {
break;
}
end -= 1;
}
return start === 0 && end === length ? str : str.slice(start, end);
}
// @__NO_SIDE_EFFECTS__
function repeatString(str, count) {
if (count <= 0) {
return "";
}
return str.repeat(count);
}
// @__NO_SIDE_EFFECTS__
function centerText(text, width) {
const textLength = (0, import_ansi.stripAnsi)(text).length;
if (textLength >= width) {
return text;
}
const padding = width - textLength;
const leftPad = Math.floor(padding / 2);
const rightPad = padding - leftPad;
return " ".repeat(leftPad) + text + " ".repeat(rightPad);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ansiRegex,
applyLinePrefix,
camelToKebab,
centerText,
fromCharCode,
indentString,
isBlankString,
isNonEmptyString,
repeatString,
search,
stringWidth,
stripAnsi,
stripBom,
toKebabCase,
trimNewlines
});