@putkoff/abstract-utilities 1.0.22 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +155 -129
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +154 -130
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/types/functions/string_utils/src/string_utils.d.ts +2 -0
- package/package.json +3 -3
package/dist/cjs/index.js
CHANGED
|
@@ -1193,135 +1193,6 @@ posix.posix = posix;
|
|
|
1193
1193
|
|
|
1194
1194
|
var pathBrowserify = posix;
|
|
1195
1195
|
|
|
1196
|
-
function getSubstring(obj, maxLength = null, minLength = null) {
|
|
1197
|
-
const objLength = obj.length;
|
|
1198
|
-
const effectiveMaxLength = maxLength !== null && maxLength !== void 0 ? maxLength : objLength; // Use nullish coalescing for clarity
|
|
1199
|
-
const effectiveMinLength = minLength !== null && minLength !== void 0 ? minLength : 0;
|
|
1200
|
-
// Ensure bounds are valid
|
|
1201
|
-
const clampedMaxLength = Math.min(Math.max(effectiveMaxLength, 0), objLength);
|
|
1202
|
-
const clampedMinLength = Math.min(Math.max(effectiveMinLength, 0), objLength);
|
|
1203
|
-
// If minLength exceeds maxLength, return empty string or adjust logic as needed
|
|
1204
|
-
if (clampedMinLength >= clampedMaxLength) {
|
|
1205
|
-
return '';
|
|
1206
|
-
}
|
|
1207
|
-
return obj.substring(clampedMinLength, clampedMaxLength);
|
|
1208
|
-
}
|
|
1209
|
-
function truncateString(obj, maxLength = 20) {
|
|
1210
|
-
const objLength = obj.length;
|
|
1211
|
-
if (objLength > maxLength && maxLength) {
|
|
1212
|
-
obj = getSubstring(obj, maxLength) + '...';
|
|
1213
|
-
}
|
|
1214
|
-
return obj;
|
|
1215
|
-
}
|
|
1216
|
-
// string_utils/src/string_utils.ts
|
|
1217
|
-
function stripPrefixes(str, bases = []) {
|
|
1218
|
-
/* NEW: coerce whatever arrives into a string */
|
|
1219
|
-
str = String(str);
|
|
1220
|
-
const prefixes = (Array.isArray(bases) ? bases : [bases])
|
|
1221
|
-
.filter(Boolean)
|
|
1222
|
-
.sort((a, b) => b.length - a.length); // longest first
|
|
1223
|
-
let changed = true;
|
|
1224
|
-
while (changed) {
|
|
1225
|
-
changed = false;
|
|
1226
|
-
for (const prefix of prefixes) {
|
|
1227
|
-
if (str.startsWith(prefix)) {
|
|
1228
|
-
str = str.slice(prefix.length);
|
|
1229
|
-
changed = true;
|
|
1230
|
-
break; // restart from longest prefix
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
return str;
|
|
1235
|
-
}
|
|
1236
|
-
/**
|
|
1237
|
-
* Removes characters from the beginning of the string
|
|
1238
|
-
* if they are found in the list of characters.
|
|
1239
|
-
*
|
|
1240
|
-
* @param str - The input string.
|
|
1241
|
-
* @param listObjects - A string or an array of characters to remove.
|
|
1242
|
-
* @returns The modified string.
|
|
1243
|
-
*/
|
|
1244
|
-
function eatInner(str, listObjects) {
|
|
1245
|
-
if (!Array.isArray(listObjects)) {
|
|
1246
|
-
listObjects = [listObjects];
|
|
1247
|
-
}
|
|
1248
|
-
// Ensure str is a string
|
|
1249
|
-
str = String(str);
|
|
1250
|
-
// Remove characters from the beginning while they are in listObjects
|
|
1251
|
-
while (str.length > 0 && listObjects.includes(str[0])) {
|
|
1252
|
-
str = str.slice(1);
|
|
1253
|
-
}
|
|
1254
|
-
return str;
|
|
1255
|
-
}
|
|
1256
|
-
/**
|
|
1257
|
-
* Removes characters from the end of the string
|
|
1258
|
-
* if they are found in the list of characters.
|
|
1259
|
-
*
|
|
1260
|
-
* @param str - The input string.
|
|
1261
|
-
* @param listObjects - A string or an array of characters to remove.
|
|
1262
|
-
* @returns The modified string.
|
|
1263
|
-
*/
|
|
1264
|
-
function eatOuter(str, listObjects) {
|
|
1265
|
-
if (!Array.isArray(listObjects)) {
|
|
1266
|
-
listObjects = [listObjects];
|
|
1267
|
-
}
|
|
1268
|
-
// Ensure str is a string
|
|
1269
|
-
str = String(str);
|
|
1270
|
-
// Remove characters from the end while they are in listObjects
|
|
1271
|
-
while (str.length > 0 && listObjects.includes(str[str.length - 1])) {
|
|
1272
|
-
str = str.slice(0, -1);
|
|
1273
|
-
}
|
|
1274
|
-
return str;
|
|
1275
|
-
}
|
|
1276
|
-
/**
|
|
1277
|
-
* Removes characters from both the beginning and the end of the string
|
|
1278
|
-
* if they are found in the list of characters.
|
|
1279
|
-
*
|
|
1280
|
-
* @param str - The input string.
|
|
1281
|
-
* @param listObjects - A string or an array of characters to remove.
|
|
1282
|
-
* @returns The modified string.
|
|
1283
|
-
*/
|
|
1284
|
-
function eatAll(str, listObjects) {
|
|
1285
|
-
return eatOuter(eatInner(str, listObjects), listObjects);
|
|
1286
|
-
}
|
|
1287
|
-
function eatEnd(obj, endings = ['/']) {
|
|
1288
|
-
let result = obj;
|
|
1289
|
-
let modified = true;
|
|
1290
|
-
while (modified) {
|
|
1291
|
-
modified = false;
|
|
1292
|
-
for (const ending of endings) {
|
|
1293
|
-
if (result.endsWith(ending)) {
|
|
1294
|
-
result = result.slice(0, -ending.length);
|
|
1295
|
-
modified = true;
|
|
1296
|
-
break;
|
|
1297
|
-
}
|
|
1298
|
-
}
|
|
1299
|
-
}
|
|
1300
|
-
return result;
|
|
1301
|
-
}
|
|
1302
|
-
function tryParse(obj) {
|
|
1303
|
-
try {
|
|
1304
|
-
obj = JSON.stringify(obj);
|
|
1305
|
-
}
|
|
1306
|
-
catch (err) {
|
|
1307
|
-
try {
|
|
1308
|
-
obj = JSON.parse(obj);
|
|
1309
|
-
}
|
|
1310
|
-
catch (err) {
|
|
1311
|
-
}
|
|
1312
|
-
}
|
|
1313
|
-
return obj;
|
|
1314
|
-
}
|
|
1315
|
-
function create_list_string(array_obj) {
|
|
1316
|
-
let string = '';
|
|
1317
|
-
for (const obj in array_obj) {
|
|
1318
|
-
const array_value = array_obj[obj];
|
|
1319
|
-
const parsed_value = tryParse(array_value);
|
|
1320
|
-
string += `${obj} == ${parsed_value}\n`;
|
|
1321
|
-
}
|
|
1322
|
-
return string;
|
|
1323
|
-
}
|
|
1324
|
-
|
|
1325
1196
|
function ensure_list(obj) {
|
|
1326
1197
|
const objArray = Array.isArray(obj) ? obj : [obj];
|
|
1327
1198
|
return objArray;
|
|
@@ -1762,6 +1633,159 @@ const confirm_type = confirmType;
|
|
|
1762
1633
|
const is_media_type = isMediaType;
|
|
1763
1634
|
const get_mime_type = getMimeType;
|
|
1764
1635
|
|
|
1636
|
+
function getSubstring(obj, maxLength = null, minLength = null) {
|
|
1637
|
+
const objLength = obj.length;
|
|
1638
|
+
const effectiveMaxLength = maxLength !== null && maxLength !== void 0 ? maxLength : objLength; // Use nullish coalescing for clarity
|
|
1639
|
+
const effectiveMinLength = minLength !== null && minLength !== void 0 ? minLength : 0;
|
|
1640
|
+
// Ensure bounds are valid
|
|
1641
|
+
const clampedMaxLength = Math.min(Math.max(effectiveMaxLength, 0), objLength);
|
|
1642
|
+
const clampedMinLength = Math.min(Math.max(effectiveMinLength, 0), objLength);
|
|
1643
|
+
// If minLength exceeds maxLength, return empty string or adjust logic as needed
|
|
1644
|
+
if (clampedMinLength >= clampedMaxLength) {
|
|
1645
|
+
return '';
|
|
1646
|
+
}
|
|
1647
|
+
return obj.substring(clampedMinLength, clampedMaxLength);
|
|
1648
|
+
}
|
|
1649
|
+
function truncateString(obj, maxLength = 20) {
|
|
1650
|
+
const objLength = obj.length;
|
|
1651
|
+
if (objLength > maxLength && maxLength) {
|
|
1652
|
+
obj = getSubstring(obj, maxLength) + '...';
|
|
1653
|
+
}
|
|
1654
|
+
return obj;
|
|
1655
|
+
}
|
|
1656
|
+
function capitalize_str(string) {
|
|
1657
|
+
string = assureString(string);
|
|
1658
|
+
const string_len = string.length;
|
|
1659
|
+
let init_char = string.toUpperCase();
|
|
1660
|
+
if (string_len > 0) {
|
|
1661
|
+
init_char = string[0].toUpperCase();
|
|
1662
|
+
}
|
|
1663
|
+
let rest_chars = '';
|
|
1664
|
+
if (string_len > 1) {
|
|
1665
|
+
rest_chars = string.slice(1).toLowerCase();
|
|
1666
|
+
}
|
|
1667
|
+
const fin_chars = `${init_char}${rest_chars}`;
|
|
1668
|
+
return fin_chars;
|
|
1669
|
+
}
|
|
1670
|
+
function capitalize(string) {
|
|
1671
|
+
let nu_string = '';
|
|
1672
|
+
string = assureString(string);
|
|
1673
|
+
let objs = string.replace('-', '_').split('_');
|
|
1674
|
+
for (const obj of objs) {
|
|
1675
|
+
let str_obj = capitalize_str(obj);
|
|
1676
|
+
nu_string = `${nu_string} ${str_obj}`;
|
|
1677
|
+
}
|
|
1678
|
+
return eatAll(nu_string, [' ']);
|
|
1679
|
+
}
|
|
1680
|
+
// string_utils/src/string_utils.ts
|
|
1681
|
+
function stripPrefixes(str, bases = []) {
|
|
1682
|
+
/* NEW: coerce whatever arrives into a string */
|
|
1683
|
+
str = String(str);
|
|
1684
|
+
const prefixes = (Array.isArray(bases) ? bases : [bases])
|
|
1685
|
+
.filter(Boolean)
|
|
1686
|
+
.sort((a, b) => b.length - a.length); // longest first
|
|
1687
|
+
let changed = true;
|
|
1688
|
+
while (changed) {
|
|
1689
|
+
changed = false;
|
|
1690
|
+
for (const prefix of prefixes) {
|
|
1691
|
+
if (str.startsWith(prefix)) {
|
|
1692
|
+
str = str.slice(prefix.length);
|
|
1693
|
+
changed = true;
|
|
1694
|
+
break; // restart from longest prefix
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
return str;
|
|
1699
|
+
}
|
|
1700
|
+
/**
|
|
1701
|
+
* Removes characters from the beginning of the string
|
|
1702
|
+
* if they are found in the list of characters.
|
|
1703
|
+
*
|
|
1704
|
+
* @param str - The input string.
|
|
1705
|
+
* @param listObjects - A string or an array of characters to remove.
|
|
1706
|
+
* @returns The modified string.
|
|
1707
|
+
*/
|
|
1708
|
+
function eatInner(str, listObjects) {
|
|
1709
|
+
if (!Array.isArray(listObjects)) {
|
|
1710
|
+
listObjects = [listObjects];
|
|
1711
|
+
}
|
|
1712
|
+
// Ensure str is a string
|
|
1713
|
+
str = String(str);
|
|
1714
|
+
// Remove characters from the beginning while they are in listObjects
|
|
1715
|
+
while (str.length > 0 && listObjects.includes(str[0])) {
|
|
1716
|
+
str = str.slice(1);
|
|
1717
|
+
}
|
|
1718
|
+
return str;
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Removes characters from the end of the string
|
|
1722
|
+
* if they are found in the list of characters.
|
|
1723
|
+
*
|
|
1724
|
+
* @param str - The input string.
|
|
1725
|
+
* @param listObjects - A string or an array of characters to remove.
|
|
1726
|
+
* @returns The modified string.
|
|
1727
|
+
*/
|
|
1728
|
+
function eatOuter(str, listObjects) {
|
|
1729
|
+
if (!Array.isArray(listObjects)) {
|
|
1730
|
+
listObjects = [listObjects];
|
|
1731
|
+
}
|
|
1732
|
+
// Ensure str is a string
|
|
1733
|
+
str = String(str);
|
|
1734
|
+
// Remove characters from the end while they are in listObjects
|
|
1735
|
+
while (str.length > 0 && listObjects.includes(str[str.length - 1])) {
|
|
1736
|
+
str = str.slice(0, -1);
|
|
1737
|
+
}
|
|
1738
|
+
return str;
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* Removes characters from both the beginning and the end of the string
|
|
1742
|
+
* if they are found in the list of characters.
|
|
1743
|
+
*
|
|
1744
|
+
* @param str - The input string.
|
|
1745
|
+
* @param listObjects - A string or an array of characters to remove.
|
|
1746
|
+
* @returns The modified string.
|
|
1747
|
+
*/
|
|
1748
|
+
function eatAll(str, listObjects) {
|
|
1749
|
+
return eatOuter(eatInner(str, listObjects), listObjects);
|
|
1750
|
+
}
|
|
1751
|
+
function eatEnd(obj, endings = ['/']) {
|
|
1752
|
+
let result = obj;
|
|
1753
|
+
let modified = true;
|
|
1754
|
+
while (modified) {
|
|
1755
|
+
modified = false;
|
|
1756
|
+
for (const ending of endings) {
|
|
1757
|
+
if (result.endsWith(ending)) {
|
|
1758
|
+
result = result.slice(0, -ending.length);
|
|
1759
|
+
modified = true;
|
|
1760
|
+
break;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
return result;
|
|
1765
|
+
}
|
|
1766
|
+
function tryParse(obj) {
|
|
1767
|
+
try {
|
|
1768
|
+
obj = JSON.stringify(obj);
|
|
1769
|
+
}
|
|
1770
|
+
catch (err) {
|
|
1771
|
+
try {
|
|
1772
|
+
obj = JSON.parse(obj);
|
|
1773
|
+
}
|
|
1774
|
+
catch (err) {
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
return obj;
|
|
1778
|
+
}
|
|
1779
|
+
function create_list_string(array_obj) {
|
|
1780
|
+
let string = '';
|
|
1781
|
+
for (const obj in array_obj) {
|
|
1782
|
+
const array_value = array_obj[obj];
|
|
1783
|
+
const parsed_value = tryParse(array_value);
|
|
1784
|
+
string += `${obj} == ${parsed_value}\n`;
|
|
1785
|
+
}
|
|
1786
|
+
return string;
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1765
1789
|
/**
|
|
1766
1790
|
* In the browser we already have a WHATWG URL constructor on window.
|
|
1767
1791
|
* Here we re-export it as “url” so other modules can import it.
|
|
@@ -2179,6 +2203,8 @@ exports.assure_number = assure_number;
|
|
|
2179
2203
|
exports.assure_string = assure_string;
|
|
2180
2204
|
exports.callStorage = callStorage;
|
|
2181
2205
|
exports.callWindowMethod = callWindowMethod;
|
|
2206
|
+
exports.capitalize = capitalize;
|
|
2207
|
+
exports.capitalize_str = capitalize_str;
|
|
2182
2208
|
exports.checkResponse = checkResponse;
|
|
2183
2209
|
exports.cleanArray = cleanArray;
|
|
2184
2210
|
exports.cleanText = cleanText;
|