@rzl-zone/utils-js
Version:
A modern, lightweight set of JavaScript utility functions with TypeScript support for everyday development, crafted to enhance code readability and maintainability.
238 lines (231 loc) • 9.52 kB
JavaScript
/*!
* ====================================================
* Rzl Utils-JS.
* ----------------------------------------------------
* Version: 3.11.0.
* Author: Rizalvin Dwiky.
* Repository: https://github.com/rzl-zone/utils-js.
* ====================================================
*/
;
var chunkKHO2SBNA_cjs = require('./chunk-KHO2SBNA.cjs');
var chunkNVRZPF5M_cjs = require('./chunk-NVRZPF5M.cjs');
var chunkU23I7JPB_cjs = require('./chunk-U23I7JPB.cjs');
var chunkRMP7VMPB_cjs = require('./chunk-RMP7VMPB.cjs');
var chunkDVMHRLKP_cjs = require('./chunk-DVMHRLKP.cjs');
var chunkHYN6FC5A_cjs = require('./chunk-HYN6FC5A.cjs');
var chunkWLOQQFDS_cjs = require('./chunk-WLOQQFDS.cjs');
var chunkDLS3G6WQ_cjs = require('./chunk-DLS3G6WQ.cjs');
var chunkDAPAK2W3_cjs = require('./chunk-DAPAK2W3.cjs');
var chunkSYHPSOUU_cjs = require('./chunk-SYHPSOUU.cjs');
var constructURL = (baseUrl, queryParams, removeParams) => {
if (chunkSYHPSOUU_cjs.isString(baseUrl)) {
if (chunkWLOQQFDS_cjs.isEmptyString(baseUrl)) {
throw new TypeError(`First parameter (\`baseUrl\`) cannot be an empty-string.`);
}
baseUrl = chunkNVRZPF5M_cjs.normalizeString(baseUrl);
} else if (!chunkKHO2SBNA_cjs.isURL(baseUrl)) {
throw new TypeError(
`First parameter (\`baseUrl\`) must be of type an URL instance or a \`string\` and a non empty-string, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
baseUrl
)}\`, with current value: \`${chunkDAPAK2W3_cjs.safeStableStringify(baseUrl, {
keepUndefined: true
})}\`.`
);
}
if (!chunkSYHPSOUU_cjs.isUndefined(removeParams)) {
chunkDLS3G6WQ_cjs.assertIsArray(removeParams, {
message: ({ currentType, validType }) => `Third parameter (\`removeParams\`) must be of type \`${validType} of strings\`, but received: \`${currentType}\`.`
});
if (!removeParams.every((param) => chunkSYHPSOUU_cjs.isNonEmptyString(param))) {
throw new TypeError(
`Third parameter (\`removeParams\`) must be of type \`array\` and contains \`string\` only and non empty-string.`
);
}
}
try {
if (!chunkSYHPSOUU_cjs.isUndefined(queryParams) && !chunkSYHPSOUU_cjs.isFunction(queryParams[Symbol.iterator])) {
throw new TypeError(
`Second parameter (\`queryParams\`) must be iterable (like URLSearchParams.entries() or an array of [[string, string | number]...]), but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
queryParams
)}\`, with value: \`${chunkDAPAK2W3_cjs.safeStableStringify(queryParams, {
keepUndefined: true
})}\`.`
);
}
const urlInstance = new URL(baseUrl);
if (!chunkSYHPSOUU_cjs.isUndefined(queryParams)) {
const paramObject = Object.fromEntries(queryParams);
if (!chunkRMP7VMPB_cjs.isEmptyValue(paramObject)) {
const mergedParams = new URLSearchParams(urlInstance.search);
for (const [key, value] of Object.entries(paramObject)) {
if (!chunkSYHPSOUU_cjs.isNonEmptyString(value) && !chunkSYHPSOUU_cjs.isNumber(value, { includeNaN: true })) {
throw new TypeError(
`Second parameter (\`queryParams\`) must be iterable (like URLSearchParams.entries() or an array of [[string, string | number]...]), but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
queryParams
)}\`, with value: \`${chunkDAPAK2W3_cjs.safeStableStringify(queryParams, {
keepUndefined: true
})}\`.`
);
}
mergedParams.set(key, String(value));
}
if (removeParams?.length) {
chunkU23I7JPB_cjs.toStringArrayUnRecursive(removeParams).map((paramKey) => {
mergedParams.delete(paramKey);
});
}
urlInstance.search = mergedParams.toString();
}
}
removeParams?.forEach((param) => urlInstance.searchParams.delete(param));
return urlInstance;
} catch (error) {
if (chunkSYHPSOUU_cjs.isError(error)) throw error;
throw new Error(
"Failed to construct a valid URL in `constructURL()`, Error:" + error
);
}
};
var extractURLs = (url) => {
if (!chunkSYHPSOUU_cjs.isNonEmptyString(url)) return null;
let decoded;
try {
decoded = decodeURIComponent(url);
} catch {
return null;
}
const urlPattern = /https?:\/\/.*?(?=https?:\/\/|\s|$)/g;
const matches = decoded.match(urlPattern);
if (!matches) return null;
const cleaned = matches.map((url2) => url2.replace(/[.,;:!?)]*$/, "")).filter((url2) => {
try {
const u = new URL(url2);
return u.protocol === "http:" || u.protocol === "https:";
} catch {
return false;
}
});
return cleaned.length ? cleaned : null;
};
var getFirstPrefixPathname = (result, defaultValue = "/") => {
if (!chunkSYHPSOUU_cjs.isNonEmptyString(defaultValue)) {
throw new TypeError(
`Second parameter (\`defaultValue\`) must be of type \`string\` and not an \`empty-string\`, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
defaultValue
)}\`, with value: \`${chunkDAPAK2W3_cjs.safeStableStringify(defaultValue, {
keepUndefined: true
})}\`.`
);
}
if (chunkSYHPSOUU_cjs.isArray(result)) {
if (!result.every((item) => chunkSYHPSOUU_cjs.isString(item))) {
throw new TypeError(
`First parameter (\`result\`) must be of type \`string\` or \`array of string\`, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
result
)}\`, with value: \`${chunkDAPAK2W3_cjs.safeStableStringify(result, {
keepUndefined: true
})}\`.`
);
}
for (const item of result) {
const normalized = chunkHYN6FC5A_cjs.normalizePathname(item);
if (normalized !== "/") {
return normalized;
}
}
return chunkHYN6FC5A_cjs.normalizePathname(defaultValue);
}
if (chunkSYHPSOUU_cjs.isString(result)) {
const normalized = chunkHYN6FC5A_cjs.normalizePathname(result);
return normalized !== "/" ? normalized : chunkHYN6FC5A_cjs.normalizePathname(defaultValue);
}
if (!chunkSYHPSOUU_cjs.isNil(result)) {
throw new TypeError(
`First parameter (\`result\`) must be of type \`string\`, \`array-string\`, \`null\` or \`undefined\`, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
result
)}\`.`
);
}
return chunkHYN6FC5A_cjs.normalizePathname(defaultValue);
};
var getPrefixPathname = (url, base = null, options = {}) => {
const errors = [];
if (!chunkSYHPSOUU_cjs.isString(url) && !chunkSYHPSOUU_cjs.isArray(url)) {
errors.push(
`First parameter (\`url\`) must be of type \`string\` or \`array-string\`, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
url
)}\`.`
);
}
if (!chunkSYHPSOUU_cjs.isString(base) && !chunkSYHPSOUU_cjs.isArray(base) && !chunkSYHPSOUU_cjs.isNull(base)) {
errors.push(
`Second parameter (\`base\`) must be of type \`string\`, \`array-string\` or \`null\`, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
base
)}\`.`
);
}
if (!chunkSYHPSOUU_cjs.isPlainObject(options)) {
errors.push(
`Second parameter (\`options\`) must be of type \`plain-object\`, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
options
)}\`.`
);
}
const { levels = 1, removeDuplicates = true } = options;
if (!chunkDVMHRLKP_cjs.isInteger(levels) || chunkDVMHRLKP_cjs.isInteger(levels) && levels < 0) {
errors.push(
`Parameter \`levels\` property of the \`options\` (second parameter) must be of type \`integer-number\` and minimum is \`0\`, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
levels
)}\`, with value: \`${chunkDAPAK2W3_cjs.safeStableStringify(levels, {
keepUndefined: true
})}\`.`
);
}
if (!chunkSYHPSOUU_cjs.isBoolean(removeDuplicates)) {
errors.push(
`Parameter \`removeDuplicates\` property of the \`options\` (second parameter) must be of type \`boolean\`, but received: \`${chunkSYHPSOUU_cjs.getPreciseType(
removeDuplicates
)}\`.`
);
}
if (chunkSYHPSOUU_cjs.isNonEmptyArray(errors)) {
throw new TypeError(
`Invalid parameter(s) in \`getPrefixPathname\` function:
- ${errors.join("\n- ")}`
);
}
function getLevel(singleUrl) {
const parts = chunkHYN6FC5A_cjs.normalizePathname(singleUrl).split("/").filter(Boolean);
return `/${parts.slice(0, levels).join("/")}`;
}
function processUrl(singleUrl) {
if (base) {
singleUrl = chunkHYN6FC5A_cjs.normalizePathname(singleUrl);
if (chunkSYHPSOUU_cjs.isArray(base)) {
for (const b of base) {
if (singleUrl.startsWith(chunkHYN6FC5A_cjs.normalizePathname(b))) {
return getLevel(singleUrl);
}
}
} else if (chunkSYHPSOUU_cjs.isNonEmptyString(base) && singleUrl.startsWith(chunkHYN6FC5A_cjs.normalizePathname(base))) {
return getLevel(singleUrl);
}
return null;
}
return getLevel(singleUrl);
}
if (chunkSYHPSOUU_cjs.isArray(url)) {
const result = url.map(processUrl).filter((r) => !chunkSYHPSOUU_cjs.isNull(r));
const uniqueResult = removeDuplicates ? [...new Set(result)] : result;
if (uniqueResult.length === 1) {
return uniqueResult[0];
}
return uniqueResult;
}
return processUrl(url);
};
exports.constructURL = constructURL;
exports.extractURLs = extractURLs;
exports.getFirstPrefixPathname = getFirstPrefixPathname;
exports.getPrefixPathname = getPrefixPathname;