@putkoff/abstract-utilities 0.1.224 → 0.1.225
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 +101 -56
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +86 -57
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.ts +29 -9
- package/dist/types/functions/index.d.ts +1 -0
- package/dist/types/functions/math_utils/index.d.ts +1 -0
- package/dist/types/functions/math_utils/safe_math.d.ts +4 -0
- package/dist/types/functions/type_utils/src/clean_utils.d.ts +3 -0
- package/dist/types/functions/type_utils/src/ensure_utils.d.ts +16 -0
- package/dist/types/functions/type_utils/src/index.d.ts +3 -0
- package/dist/types/functions/type_utils/src/json_utils.d.ts +2 -0
- package/dist/types/functions/type_utils/src/type_utils.d.ts +2 -11
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -1066,22 +1066,55 @@ function ensure_list(obj) {
|
|
|
1066
1066
|
const objArray = Array.isArray(obj) ? obj : [obj];
|
|
1067
1067
|
return objArray;
|
|
1068
1068
|
}
|
|
1069
|
-
|
|
1070
|
-
|
|
1069
|
+
// coerce any single value into a number (0 if it can't)
|
|
1070
|
+
function ensure_number(x) {
|
|
1071
|
+
const n = Number(x);
|
|
1072
|
+
return isNaN(n) ? 0 : n;
|
|
1071
1073
|
}
|
|
1072
|
-
function
|
|
1073
|
-
obj = assureArray(obj);
|
|
1074
|
-
return Array.from(new Set(obj));
|
|
1075
|
-
}
|
|
1076
|
-
function assureArray(input) {
|
|
1074
|
+
function ensure_array(input) {
|
|
1077
1075
|
if (typeof input === 'string') {
|
|
1078
1076
|
return [input];
|
|
1079
1077
|
}
|
|
1080
1078
|
if (Array.isArray(input)) {
|
|
1081
|
-
return input.map(item =>
|
|
1079
|
+
return input.map(item => ensure_string(item));
|
|
1082
1080
|
}
|
|
1083
1081
|
return [];
|
|
1084
1082
|
}
|
|
1083
|
+
function ensure_string(obj) {
|
|
1084
|
+
return String(obj);
|
|
1085
|
+
}
|
|
1086
|
+
const assureList = ensure_list;
|
|
1087
|
+
const assureString = ensure_string;
|
|
1088
|
+
const assureNumber = ensure_number;
|
|
1089
|
+
const assureArray = ensure_array;
|
|
1090
|
+
const assure_list = ensure_list;
|
|
1091
|
+
const assure_string = ensure_string;
|
|
1092
|
+
const assure_number = ensure_number;
|
|
1093
|
+
const assure_array = ensure_array;
|
|
1094
|
+
const ensureList = ensure_list;
|
|
1095
|
+
const ensureString = ensure_string;
|
|
1096
|
+
const ensureNumber = ensure_number;
|
|
1097
|
+
const ensureArray = ensure_array;
|
|
1098
|
+
|
|
1099
|
+
function cleanText(input) {
|
|
1100
|
+
// Replace delimiters with spaces and split
|
|
1101
|
+
const str = ensure_string(input);
|
|
1102
|
+
const words = str.replace(/[_.-]/g, ' '); // Replace _, -, . with space
|
|
1103
|
+
return words;
|
|
1104
|
+
}
|
|
1105
|
+
function cleanArray(obj) {
|
|
1106
|
+
obj = ensure_array(obj);
|
|
1107
|
+
return Array.from(new Set(obj));
|
|
1108
|
+
}
|
|
1109
|
+
function getCleanArray(obj) {
|
|
1110
|
+
obj = obj.split(/\s+/) // Split on any whitespace
|
|
1111
|
+
.filter((item) => typeof item === 'string' && item !== '');
|
|
1112
|
+
// Get basename
|
|
1113
|
+
// Remove duplicates using Set
|
|
1114
|
+
const uniqueWords = cleanArray(obj);
|
|
1115
|
+
return uniqueWords;
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1085
1118
|
// Constrain T so 'in obj' is allowed
|
|
1086
1119
|
function get_key_value(obj, key) {
|
|
1087
1120
|
// we cast to any for the indexing, since TS can’t infer arbitrary string keys
|
|
@@ -1091,7 +1124,7 @@ function get_key_value(obj, key) {
|
|
|
1091
1124
|
return null;
|
|
1092
1125
|
}
|
|
1093
1126
|
function get(obj, keys, defaultValue = null) {
|
|
1094
|
-
const keyArray =
|
|
1127
|
+
const keyArray = ensure_array(keys);
|
|
1095
1128
|
if (!obj || keyArray.length === 0) {
|
|
1096
1129
|
return defaultValue;
|
|
1097
1130
|
}
|
|
@@ -1103,23 +1136,16 @@ function get(obj, keys, defaultValue = null) {
|
|
|
1103
1136
|
}
|
|
1104
1137
|
return defaultValue;
|
|
1105
1138
|
}
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
function getCleanArray(obj) {
|
|
1113
|
-
obj = obj.split(/\s+/) // Split on any whitespace
|
|
1114
|
-
.filter((item) => typeof item === 'string' && item !== '');
|
|
1115
|
-
// Get basename
|
|
1116
|
-
// Remove duplicates using Set
|
|
1117
|
-
const uniqueWords = cleanArray(obj);
|
|
1118
|
-
return uniqueWords;
|
|
1139
|
+
|
|
1140
|
+
function isType(obj, type) {
|
|
1141
|
+
if (typeof obj === type) {
|
|
1142
|
+
return true;
|
|
1143
|
+
}
|
|
1144
|
+
return false;
|
|
1119
1145
|
}
|
|
1120
1146
|
function isStrInString(obj, string) {
|
|
1121
|
-
const obj_str =
|
|
1122
|
-
string =
|
|
1147
|
+
const obj_str = ensure_string(obj).toLowerCase;
|
|
1148
|
+
string = ensure_string(string).toLowerCase;
|
|
1123
1149
|
if (string.includes(obj_str)) {
|
|
1124
1150
|
return true;
|
|
1125
1151
|
}
|
|
@@ -1127,14 +1153,18 @@ function isStrInString(obj, string) {
|
|
|
1127
1153
|
}
|
|
1128
1154
|
function getChar(i, string) {
|
|
1129
1155
|
if (string.length >= i) {
|
|
1130
|
-
return
|
|
1156
|
+
return ensure_string(string)[i];
|
|
1131
1157
|
}
|
|
1132
1158
|
}
|
|
1133
|
-
function
|
|
1134
|
-
|
|
1135
|
-
|
|
1159
|
+
function getAlphaNum(obj) {
|
|
1160
|
+
const is_num = isNum(obj);
|
|
1161
|
+
const alphas = getAlphas();
|
|
1162
|
+
if (is_num) {
|
|
1163
|
+
return getChar(obj, alphas);
|
|
1164
|
+
}
|
|
1165
|
+
if (isStrInString(obj, alphas)) {
|
|
1166
|
+
return getChar(obj, alphas);
|
|
1136
1167
|
}
|
|
1137
|
-
return false;
|
|
1138
1168
|
}
|
|
1139
1169
|
function getNums() {
|
|
1140
1170
|
return '0123456789';
|
|
@@ -1146,36 +1176,9 @@ function isNum(obj) {
|
|
|
1146
1176
|
}
|
|
1147
1177
|
return isStrInString(obj, getNums());
|
|
1148
1178
|
}
|
|
1149
|
-
function ensure_number(object) {
|
|
1150
|
-
if (isNum(object)) {
|
|
1151
|
-
return Number(object);
|
|
1152
|
-
}
|
|
1153
|
-
let nuString = '';
|
|
1154
|
-
const object_str = assureString(object);
|
|
1155
|
-
const str_spl = object_str.split('');
|
|
1156
|
-
for (const obj in str_spl) {
|
|
1157
|
-
if (isNum(obj)) {
|
|
1158
|
-
nuString += obj;
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
if (nuString != '') {
|
|
1162
|
-
return Number(nuString);
|
|
1163
|
-
}
|
|
1164
|
-
return 0;
|
|
1165
|
-
}
|
|
1166
1179
|
function getAlphas() {
|
|
1167
1180
|
return 'abcdefghijklmnopqrstuvwxyz';
|
|
1168
1181
|
}
|
|
1169
|
-
function getAlphaNum(obj) {
|
|
1170
|
-
const is_num = isNum(obj);
|
|
1171
|
-
const alphas = getAlphas();
|
|
1172
|
-
if (is_num) {
|
|
1173
|
-
return getChar(obj, alphas);
|
|
1174
|
-
}
|
|
1175
|
-
if (isStrInString(obj, alphas)) {
|
|
1176
|
-
return getChar(obj, alphas);
|
|
1177
|
-
}
|
|
1178
|
-
}
|
|
1179
1182
|
|
|
1180
1183
|
function getSubstring(obj, maxLength = null, minLength = null) {
|
|
1181
1184
|
const objLength = obj.length;
|
|
@@ -1625,6 +1628,32 @@ function get_keyword_string(keywords) {
|
|
|
1625
1628
|
return allString;
|
|
1626
1629
|
}
|
|
1627
1630
|
|
|
1631
|
+
// take N args and coerce them all to numbers
|
|
1632
|
+
function safeNums(...args) {
|
|
1633
|
+
return args.map(ensure_number);
|
|
1634
|
+
}
|
|
1635
|
+
// divide the first value by each of the following
|
|
1636
|
+
function safeDivide(...args) {
|
|
1637
|
+
const [head, ...rest] = safeNums(...args);
|
|
1638
|
+
// if we don’t have a head or any divisor is zero, bail
|
|
1639
|
+
if (head === 0 || rest.some((d) => d === 0))
|
|
1640
|
+
return 0;
|
|
1641
|
+
return rest.reduce((acc, d) => acc / d, head);
|
|
1642
|
+
}
|
|
1643
|
+
// multiply all the values together
|
|
1644
|
+
function safeMultiply(...args) {
|
|
1645
|
+
const nums = safeNums(...args);
|
|
1646
|
+
// if any number is zero, result is zero
|
|
1647
|
+
if (nums.includes(0))
|
|
1648
|
+
return 0;
|
|
1649
|
+
return nums.reduce((acc, n) => acc * n, 1);
|
|
1650
|
+
}
|
|
1651
|
+
// round a value to two decimals by percent
|
|
1652
|
+
function roundPercentage(x) {
|
|
1653
|
+
const pct = safeMultiply(ensure_number(x), 100);
|
|
1654
|
+
return safeDivide(Math.round(pct), 100);
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1628
1657
|
Object.defineProperty(exports, "useCallback", {
|
|
1629
1658
|
enumerable: true,
|
|
1630
1659
|
get: function () { return react.useCallback; }
|
|
@@ -1654,7 +1683,13 @@ exports.SUB_DIR = SUB_DIR;
|
|
|
1654
1683
|
exports.Spinner = Spinner;
|
|
1655
1684
|
exports.alertit = alertit;
|
|
1656
1685
|
exports.assureArray = assureArray;
|
|
1686
|
+
exports.assureList = assureList;
|
|
1687
|
+
exports.assureNumber = assureNumber;
|
|
1657
1688
|
exports.assureString = assureString;
|
|
1689
|
+
exports.assure_array = assure_array;
|
|
1690
|
+
exports.assure_list = assure_list;
|
|
1691
|
+
exports.assure_number = assure_number;
|
|
1692
|
+
exports.assure_string = assure_string;
|
|
1658
1693
|
exports.callStorage = callStorage;
|
|
1659
1694
|
exports.callWindowMethod = callWindowMethod;
|
|
1660
1695
|
exports.capitalize = capitalize;
|
|
@@ -1670,8 +1705,14 @@ exports.eatAll = eatAll;
|
|
|
1670
1705
|
exports.eatEnd = eatEnd;
|
|
1671
1706
|
exports.eatInner = eatInner;
|
|
1672
1707
|
exports.eatOuter = eatOuter;
|
|
1708
|
+
exports.ensureArray = ensureArray;
|
|
1709
|
+
exports.ensureList = ensureList;
|
|
1710
|
+
exports.ensureNumber = ensureNumber;
|
|
1711
|
+
exports.ensureString = ensureString;
|
|
1712
|
+
exports.ensure_array = ensure_array;
|
|
1673
1713
|
exports.ensure_list = ensure_list;
|
|
1674
1714
|
exports.ensure_number = ensure_number;
|
|
1715
|
+
exports.ensure_string = ensure_string;
|
|
1675
1716
|
exports.fetchIndexHtml = fetchIndexHtml;
|
|
1676
1717
|
exports.fetchIndexHtmlContainer = fetchIndexHtmlContainer;
|
|
1677
1718
|
exports.fetchIt = fetchIt;
|
|
@@ -1749,7 +1790,11 @@ exports.processKeywords = processKeywords;
|
|
|
1749
1790
|
exports.readJsonFile = readJsonFile;
|
|
1750
1791
|
exports.removeToken = removeToken;
|
|
1751
1792
|
exports.requireToken = requireToken;
|
|
1793
|
+
exports.roundPercentage = roundPercentage;
|
|
1794
|
+
exports.safeDivide = safeDivide;
|
|
1752
1795
|
exports.safeGlobalProp = safeGlobalProp;
|
|
1796
|
+
exports.safeMultiply = safeMultiply;
|
|
1797
|
+
exports.safeNums = safeNums;
|
|
1753
1798
|
exports.safeStorage = safeStorage;
|
|
1754
1799
|
exports.sanitizeFilename = sanitizeFilename;
|
|
1755
1800
|
exports.stripPrefixes = stripPrefixes;
|