123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import { isVueViewModel, isString, isRegExp } from './is.js';
- function truncate(str, max = 0) {
- if (typeof str !== 'string' || max === 0) {
- return str;
- }
- return str.length <= max ? str : `${str.slice(0, max)}...`;
- }
- function snipLine(line, colno) {
- let newLine = line;
- const lineLength = newLine.length;
- if (lineLength <= 150) {
- return newLine;
- }
- if (colno > lineLength) {
-
- colno = lineLength;
- }
- let start = Math.max(colno - 60, 0);
- if (start < 5) {
- start = 0;
- }
- let end = Math.min(start + 140, lineLength);
- if (end > lineLength - 5) {
- end = lineLength;
- }
- if (end === lineLength) {
- start = Math.max(end - 140, 0);
- }
- newLine = newLine.slice(start, end);
- if (start > 0) {
- newLine = `'{snip} ${newLine}`;
- }
- if (end < lineLength) {
- newLine += ' {snip}';
- }
- return newLine;
- }
- function safeJoin(input, delimiter) {
- if (!Array.isArray(input)) {
- return '';
- }
- const output = [];
-
- for (let i = 0; i < input.length; i++) {
- const value = input[i];
- try {
-
-
-
-
-
- if (isVueViewModel(value)) {
- output.push('[VueViewModel]');
- } else {
- output.push(String(value));
- }
- } catch (e) {
- output.push('[value cannot be serialized]');
- }
- }
- return output.join(delimiter);
- }
- function isMatchingPattern(
- value,
- pattern,
- requireExactStringMatch = false,
- ) {
- if (!isString(value)) {
- return false;
- }
- if (isRegExp(pattern)) {
- return pattern.test(value);
- }
- if (isString(pattern)) {
- return requireExactStringMatch ? value === pattern : value.includes(pattern);
- }
- return false;
- }
- function stringMatchesSomePattern(
- testString,
- patterns = [],
- requireExactStringMatch = false,
- ) {
- return patterns.some(pattern => isMatchingPattern(testString, pattern, requireExactStringMatch));
- }
- export { isMatchingPattern, safeJoin, snipLine, stringMatchesSomePattern, truncate };
|