@yuhere/js-strings
v1.0.5
Published
utility library aimed at providing common helper functions for strings in JavaScript/TypeScript projects.
Readme
String Utilities
A utility library aimed at providing common language-level helper functions (strings, dates, vars, etc.) in JavaScript/TypeScript projects.
Installation
Install the package via npm:
npm install @yuhere/js-stringsUsage
Import the functions you need from the library:
import {
s_pad,
s_left,
s_right,
s_ellipsis,
s_ellipsis_o,
s_xprint_extract_fmt,
s_xprint_fmts,
s_xprint,
s_glob
} from '@yuhere/js-strings';String Utilities
s_pad
Pad a string to a fixed width.
which: padding direction, supports'l','r','c'padchar: padding character, default is spaceis_trunc: whentrue, truncates the string if it exceedswidth
import { s_pad } from '@yuhere/js-strings';
console.log(s_pad('hello', 10, 'l', '-')); // '-----hello'
console.log(s_pad('hello', 10, 'r', '-')); // 'hello-----'
console.log(s_pad('hello', 10, 'c')); // ' hello '
console.log(s_pad('hello world', 5, 'r', ' ', true)); // 'hello's_left
Get the leftmost characters of a string.
import { s_left } from '@yuhere/js-strings';
console.log(s_left('hello world', 5)); // 'hello'
console.log(s_left('hello', 0)); // ''s_right
Get the rightmost characters of a string.
import { s_right } from '@yuhere/js-strings';
console.log(s_right('hello world', 5)); // 'world'
console.log(s_right('hello', 0)); // ''s_ellipsis
Pad or truncate a string to a fixed width. When truncation happens, ... is inserted according to the alignment.
'l': keep the left part and put...at the end'r': keep the right part and put...at the beginning'c': keep both ends and put...in the middle
import { s_ellipsis } from '@yuhere/js-strings';
console.log(s_ellipsis('hello', 8)); // 'hello '
console.log(s_ellipsis('abcdefghijklmnopqrstuvwxyz', 10, 'l')); // 'abcdefg...'
console.log(s_ellipsis('abcdefghijklmnopqrstuvwxyz', 10, 'r')); // '...tuvwxyz'
console.log(s_ellipsis('abcdefghijklmnopqrstuvwxyz', 10, 'c')); // 'abcd...xyz's_ellipsis_o
Options-object form of s_ellipsis, suitable when parameters come from configuration.
import { s_ellipsis_o } from '@yuhere/js-strings';
console.log(s_ellipsis_o('hello', { width: 8 })); // 'hello '
console.log(s_ellipsis_o('abcdefghijklmnopqrstuvwxyz', { width: 10, align: 'r' })); // '...tuvwxyz's_xprint_extract_fmt
Parse a column format definition used by s_xprint.
- string form:
"<width>[<align><padchar>]" - object form:
{ width, align, padchar }
import { s_xprint_extract_fmt } from '@yuhere/js-strings';
console.log(s_xprint_extract_fmt('8')); // { width: 8, align: 'l', padchar: undefined }
console.log(s_xprint_extract_fmt('8r0')); // { width: 8, align: 'r', padchar: '0' }
console.log(s_xprint_extract_fmt({ width: 12, align: 'l', padchar: '_' }));
// { width: 12, align: 'l', padchar: '_' }s_xprint_fmts
Batch-convert multiple format definitions into normalized format objects.
import { s_xprint_fmts } from '@yuhere/js-strings';
console.log(s_xprint_fmts(['8', '6r0', { width: 10, align: 'l', padchar: '_' }]));
// [
// { width: 8, align: 'l', padchar: undefined },
// { width: 6, align: 'r', padchar: '0' },
// { width: 10, align: 'l', padchar: '_' }
// ]s_xprint
Format multiple columns into one line of fixed-width text. This is useful for CLI table rows, aligned logs, and report output.
columns: values to outputformats: per-column format definitionssep: separator inserted between columnsellipsisEnd: whether the last column should also be padded/truncated
import { s_xprint } from '@yuhere/js-strings';
console.log(
s_xprint({
columns: ['id', 7, 'abcdefghijklmnopqrstuvwxyz'],
formats: ['6l ', '4r0', '10l '],
sep: ' | '
})
);
// 'id | 0007 | abcdefg...'
console.log(
s_xprint({
columns: ['INFO', 'startup ok', 'this part is not truncated'],
formats: ['6l ', '12l ', '8l '],
sep: ' ',
ellipsisEnd: false
})
);
// 'INFO startup ok this part is not truncated's_glob
Match a string against a simple glob pattern.
Current public wrapper behavior uses the default glob conversion rules, which are suitable for common * pattern matching.
import { s_glob } from '@yuhere/js-strings';
console.log(s_glob('*.ts', 'index.ts')); // true
console.log(s_glob('*.ts', 'index.js')); // false
console.log(s_glob('src/*', 'src/strings.ts')); // trueLicense
MIT
