UNPKG

@beenotung/tslib

Version:
149 lines 4.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extract_lines = exports.compare_string = exports.split_string_num = exports.str_minus = exports.str_unix2dos = exports.str_dos2unix = exports.is_non_empty_string = exports.str_like = exports.string_nbyte = exports.strReplaceAll = exports.escapeRegExp = exports.string_to_chars = exports.strToCapWords = exports.str_contains_any = exports.str_contains = void 0; const compare_1 = require("./compare"); const set_1 = require("./set"); function str_contains(pattern, target, ignore_case = false) { if (ignore_case) { return str_contains(pattern.toLowerCase(), target.toLowerCase()); } return target.indexOf(pattern) !== -1; } exports.str_contains = str_contains; function str_contains_any(patterns, target, ignore_case = false) { return patterns.some(p => str_contains(p, target, ignore_case)); } exports.str_contains_any = str_contains_any; /** * example : 'change the words' ~> 'Change The Words' * */ function strToCapWords(s) { let res = ''; let lastSpace = true; for (const c of s) { if (c === ' ') { lastSpace = true; res += ' '; } else { if (lastSpace) { res += c.toUpperCase(); lastSpace = false; } else { res += c; } } } return res; } exports.strToCapWords = strToCapWords; function string_to_chars(s) { return s.split(''); } exports.string_to_chars = string_to_chars; /* source: https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript */ function escapeRegExp(str) { return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1'); } exports.escapeRegExp = escapeRegExp; function strReplaceAll(str, find, replace) { return str.replace(new RegExp(escapeRegExp(find), 'g'), replace); } exports.strReplaceAll = strReplaceAll; function string_nbyte(s) { return encodeURI(s).split(/%..|./).length - 1; } exports.string_nbyte = string_nbyte; function str_like(a, b, ignore_case = true) { if (ignore_case) { return str_like(a.toUpperCase(), b.toUpperCase(), false); } else { return a.includes(b) || b.includes(a); } } exports.str_like = str_like; function is_non_empty_string(s) { return typeof s === 'string' && s !== ''; } exports.is_non_empty_string = is_non_empty_string; function str_dos2unix(s) { return strReplaceAll(s, '\r\n', '\n'); } exports.str_dos2unix = str_dos2unix; function str_unix2dos(s) { return strReplaceAll(s, '\n', '\r\n'); } exports.str_unix2dos = str_unix2dos; function str_minus(a, b) { return Array.from(set_1.setMinus(new Set(a), new Set(b))).join(''); } exports.str_minus = str_minus; function toNum(s, i) { const code = s.charCodeAt(i); if (48 <= code && code <= 48 + 10) { return code - 48; } else { return false; } } function parseString(s, i, res) { let acc = ''; for (; i < s.length; i++) { const num = toNum(s, i); if (num === false) { acc += s[i]; } else { if (acc.length > 0) { res.push(acc); } parseNumber(s, i + 1, num, res); return; } } if (acc.length > 0) { res.push(acc); } } function parseNumber(s, i, acc, res) { for (; i < s.length; i++) { const num = toNum(s, i); if (num === false) { res.push(acc); parseString(s, i, res); return; } acc = acc * 10 + num; } res.push(acc); } function split_string_num(s) { const acc = []; parseString(s, 0, acc); return acc; } exports.split_string_num = split_string_num; function compare_string(a, b) { const as = split_string_num(a); const bs = split_string_num(b); const n = Math.min(as.length, bs.length); for (let i = 0; i < n; i++) { const res = compare_1.compare(as[i], bs[i]); if (res !== 0) { return res; } } return compare_1.compare(as.length, bs.length); } exports.compare_string = compare_string; function extract_lines(s) { return s .split('\n') .map(s => s.trim()) .filter(s => s); } exports.extract_lines = extract_lines; //# sourceMappingURL=string.js.map