core-ts-utils
Version:
Commonly used utilities in JS and TS
630 lines (596 loc) • 17.3 kB
JavaScript
// src/debounce.ts
function debounce(func, delay = 300) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => func.apply(this, args), delay);
};
}
var debounce_default = debounce;
// src/throttle.ts
function throttle(func, limit) {
let lastExecuted = 0;
let timeout = null;
return function(...args) {
const context = this;
const now = Date.now();
if (now - lastExecuted >= limit) {
func.apply(context, args);
lastExecuted = now;
} else if (!timeout) {
timeout = setTimeout(() => {
func.apply(context, args);
lastExecuted = Date.now();
timeout = null;
}, limit - (now - lastExecuted));
}
};
}
var throttle_default = throttle;
// src/curry.ts
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
return (...nextArgs) => curried.apply(this, [...args, ...nextArgs]);
};
}
var curry_default = curry;
// src/memoize.ts
function memoize(fn) {
const cache = /* @__PURE__ */ new Map();
return function(...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = fn.apply(this, args);
cache.set(key, result);
return result;
};
}
var memoize_default = memoize;
// src/once.ts
function once(fn) {
let called = false;
let result;
return function(...args) {
if (!called) {
called = true;
result = fn.apply(this, args);
}
return result;
};
}
var once_default = once;
// src/deepClone.ts
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
var deepClone_default = deepClone;
// src/randomId.ts
function randomId(length = 16) {
const id1 = Date.now().toString();
const id2 = Math.random().toString(36).substr(2);
const shuffled = (id1 + id2).split("").sort(() => Math.random() - 0.5).join("");
return shuffled.slice(0, length);
}
var randomId_default = randomId;
// src/sleep.ts
function sleep(ms = 1e3) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
var sleep_default = sleep;
// src/flatten.ts
function flatten(arr) {
return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val), []);
}
var flatten_default = flatten;
// src/capitalize.ts
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
var capitalize_default = capitalize;
// src/isEmpty.ts
function isEmpty(obj) {
return obj === "" || obj === null || obj === void 0 || typeof obj === "object" && !Array.isArray(obj) && Object.keys(obj).length === 0 || Array.isArray(obj) && obj.length === 0;
}
var isEmpty_default = isEmpty;
// src/chunk.ts
function chunk(array, size = 1) {
if (!Array.isArray(array)) {
throw new TypeError("Expected an array as the first argument");
}
if (size <= 0) {
throw new Error("Chunk size must be greater than 0");
}
const result = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
}
var chunk_default = chunk;
// src/groupBy.ts
function groupBy(array, iteratee) {
if (!Array.isArray(array)) {
throw new TypeError("Expected an array as the first argument");
}
const result = {};
const callback = typeof iteratee === "function" ? iteratee : (item) => item[iteratee];
for (const item of array) {
const key = callback(item).toString();
if (!result[key]) {
result[key] = [];
}
result[key].push(item);
}
return result;
}
var groupBy_default = groupBy;
// src/omit.ts
function omit(object, paths) {
if (typeof object !== "object" || object === null) {
throw new TypeError("Expected an object as the first argument");
}
const result = { ...object };
const keysToOmit = Array.isArray(paths) ? paths : [paths];
for (const key of keysToOmit) {
delete result[key];
}
return result;
}
var omit_default = omit;
// src/pick.ts
function pick(object, paths) {
if (typeof object !== "object" || object === null) {
throw new TypeError("Expected an object as the first argument");
}
const result = {};
const keysToPick = Array.isArray(paths) ? paths : [paths];
for (const key of keysToPick) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
result[key] = object[key];
}
}
return result;
}
var pick_default = pick;
// src/merge.ts
function merge(...objects) {
return objects.reduce((result, current) => {
if (!current) return result;
Object.keys(current).forEach((key) => {
const resultValue = result[key];
const currentValue = current[key];
if (Array.isArray(resultValue) && Array.isArray(currentValue)) {
result[key] = [...resultValue, ...currentValue];
} else if (resultValue && currentValue && typeof resultValue === "object" && typeof currentValue === "object" && !Array.isArray(resultValue) && !Array.isArray(currentValue)) {
result[key] = merge(resultValue, currentValue);
} else {
result[key] = currentValue;
}
});
return result;
}, {});
}
var merge_default = merge;
// src/uniqueBy.ts
function uniqueBy(array, iteratee) {
if (!Array.isArray(array)) {
throw new TypeError("Expected an array as the first argument");
}
const seen = /* @__PURE__ */ new Set();
const callback = typeof iteratee === "function" ? iteratee : (item) => item[iteratee];
return array.filter((item) => {
const key = callback(item);
if (seen.has(key)) {
return false;
}
seen.add(key);
return true;
});
}
var uniqueBy_default = uniqueBy;
// src/camelCase.ts
function camelCase(str) {
if (typeof str !== "string") {
throw new TypeError("Expected a string as argument");
}
return str.replace(/[\s_-]+(.)?/g, (_, char) => char ? char.toUpperCase() : "").replace(/^([A-Z])/, (match) => match.toLowerCase());
}
var camelCase_default = camelCase;
// src/kebabCase.ts
function kebabCase(str) {
if (typeof str !== "string") {
throw new TypeError("Expected a string as argument");
}
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
}
var kebabCase_default = kebabCase;
// src/snakeCase.ts
function snakeCase(str) {
if (typeof str !== "string") {
throw new TypeError("Expected a string as argument");
}
return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[\s-]+/g, "_").toLowerCase();
}
var snakeCase_default = snakeCase;
// src/truncate.ts
function truncate(str, maxLength, suffix = "...") {
if (typeof str !== "string") {
throw new TypeError("Expected a string as first argument");
}
if (str.length <= maxLength) {
return str;
}
const truncatedLength = maxLength - suffix.length;
return truncatedLength <= 0 ? suffix : str.slice(0, truncatedLength) + suffix;
}
var truncate_default = truncate;
// src/template.ts
function template(template2, data) {
if (typeof template2 !== "string") {
throw new TypeError("Expected a string as first argument");
}
return template2.replace(/{([^{}]*)}/g, (match, key) => {
const value = key.split(".").reduce((obj, prop) => {
return obj && obj[prop] !== void 0 ? obj[prop] : void 0;
}, data);
return value !== void 0 ? String(value) : match;
});
}
var template_default = template;
// src/compose.ts
function compose(...fns) {
if (fns.length === 0) {
return (x) => x;
}
if (fns.length === 1) {
return fns[0];
}
return fns.reduce((a, b) => (arg) => a(b(arg)));
}
var compose_default = compose;
// src/pipe.ts
function pipe(...fns) {
if (fns.length === 0) {
return (x) => x;
}
if (fns.length === 1) {
return fns[0];
}
return (x) => fns.reduce((acc, fn) => fn(acc), x);
}
var pipe_default = pipe;
// src/partial.ts
function partial(fn, ...args) {
return (...remainingArgs) => fn(...args, ...remainingArgs);
}
var partial_default = partial;
// src/isEmail.ts
function isEmail(value) {
if (typeof value !== "string") {
return false;
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(value);
}
var isEmail_default = isEmail;
// src/isURL.ts
function isURL(value) {
if (typeof value !== "string") {
return false;
}
try {
const url = new URL(value);
return url.protocol === "http:" || url.protocol === "https:";
} catch {
return false;
}
}
var isURL_default = isURL;
// src/isNumeric.ts
function isNumeric(value) {
if (typeof value === "number") {
return !isNaN(value) && isFinite(value);
}
if (typeof value !== "string") {
return false;
}
return !isNaN(Number(value)) && isFinite(Number(value));
}
var isNumeric_default = isNumeric;
// src/cookies.ts
var cookies = {
/**
* Get cookie value by name
* @param name - Cookie name
* @returns Cookie value or null if not found
*/
get(name) {
if (typeof document === "undefined") {
throw new Error("cookies.get() requires a browser environment");
}
const matches = document.cookie.match(new RegExp(`(?:^|; )${name.replace(/([.$?*|{}()[\]\\/+^])/g, "\\$1")}=([^;]*)`));
return matches ? decodeURIComponent(matches[1]) : null;
},
/**
* Set a cookie
* @param name - Cookie name
* @param value - Cookie value
* @param options - Cookie options
*/
set(name, value, options = {}) {
if (typeof document === "undefined") {
throw new Error("cookies.set() requires a browser environment");
}
const optionsWithDefaults = {
path: "/",
...options
};
if (optionsWithDefaults.expires instanceof Date) {
optionsWithDefaults.expires = optionsWithDefaults.expires.toUTCString();
} else if (typeof optionsWithDefaults.expires === "number") {
const days = optionsWithDefaults.expires;
const t = optionsWithDefaults.expires = /* @__PURE__ */ new Date();
t.setTime(t.getTime() + days * 864e5);
optionsWithDefaults.expires = t.toUTCString();
}
let updatedCookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
for (const [key, val] of Object.entries(optionsWithDefaults)) {
if (val === true) {
updatedCookie += `; ${key}`;
} else if (val !== false) {
updatedCookie += `; ${key}=${val}`;
}
}
document.cookie = updatedCookie;
},
/**
* Delete a cookie
* @param name - Cookie name
* @param options - Cookie options
*/
delete(name, options = {}) {
this.set(name, "", {
...options,
expires: -1
});
}
};
var cookies_default = cookies;
// src/storage.ts
var storage = {
/**
* Set a value in localStorage with optional expiration
* @param key - Storage key
* @param value - Value to store
* @param expiresInMs - Milliseconds until expiration (optional)
*/
set(key, value, expiresInMs) {
if (typeof localStorage === "undefined") {
throw new Error("storage.set() requires a browser environment");
}
const item = {
value,
expires: expiresInMs ? Date.now() + expiresInMs : null
};
localStorage.setItem(key, JSON.stringify(item));
},
/**
* Get a value from localStorage
* @param key - Storage key
* @param defaultValue - Default value if not found or expired
* @returns Stored value or defaultValue
*/
get(key, defaultValue = null) {
if (typeof localStorage === "undefined") {
throw new Error("storage.get() requires a browser environment");
}
const itemStr = localStorage.getItem(key);
if (!itemStr) {
return defaultValue;
}
try {
const item = JSON.parse(itemStr);
if (item.expires && Date.now() > item.expires) {
localStorage.removeItem(key);
return defaultValue;
}
return item.value;
} catch (e) {
return defaultValue;
}
},
/**
* Remove a value from localStorage
* @param key - Storage key
*/
remove(key) {
if (typeof localStorage === "undefined") {
throw new Error("storage.remove() requires a browser environment");
}
localStorage.removeItem(key);
},
/**
* Clear all values in localStorage
*/
clear() {
if (typeof localStorage === "undefined") {
throw new Error("storage.clear() requires a browser environment");
}
localStorage.clear();
}
};
var storage_default = storage;
// src/queryParams.ts
var queryParams = {
/**
* Get all query parameters as an object
* @param url - URL to parse (defaults to current URL)
* @returns Object with all query parameters
*/
getAll(url) {
const searchParams = new URLSearchParams(url ? new URL(url).search : typeof window !== "undefined" ? window.location.search : "");
const params = {};
searchParams.forEach((value, key) => {
params[key] = value;
});
return params;
},
/**
* Get a specific query parameter
* @param name - Parameter name
* @param url - URL to parse (defaults to current URL)
* @returns Parameter value or null if not found
*/
get(name, url) {
const searchParams = new URLSearchParams(url ? new URL(url).search : typeof window !== "undefined" ? window.location.search : "");
return searchParams.get(name);
},
/**
* Set query parameters and return the new URL
* @param params - Parameters to set
* @param url - Base URL (defaults to current URL)
* @returns New URL with updated parameters
*/
set(params, url) {
const baseUrl = url || (typeof window !== "undefined" ? window.location.href : "");
const urlObj = new URL(baseUrl);
Object.entries(params).forEach(([key, value]) => {
urlObj.searchParams.set(key, value);
});
return urlObj.toString();
},
/**
* Remove query parameters and return the new URL
* @param paramNames - Parameter names to remove
* @param url - Base URL (defaults to current URL)
* @returns New URL with parameters removed
*/
remove(paramNames, url) {
const baseUrl = url || (typeof window !== "undefined" ? window.location.href : "");
const urlObj = new URL(baseUrl);
const paramsToRemove = Array.isArray(paramNames) ? paramNames : [paramNames];
paramsToRemove.forEach((name) => {
urlObj.searchParams.delete(name);
});
return urlObj.toString();
}
};
var queryParams_default = queryParams;
// src/retry.ts
async function retry(fn, options = {}) {
const { maxAttempts = 5, initialDelay = 100, maxDelay = 1e4, factor = 2, onRetry = () => {
} } = options;
let attempt = 0;
let delay = initialDelay;
while (true) {
try {
return await fn();
} catch (error) {
attempt++;
if (attempt >= maxAttempts) {
throw error;
}
if (error instanceof Error) {
onRetry(error, attempt);
}
delay = Math.min(delay * factor, maxDelay);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}
var retry_default = retry;
// src/promisisfy.ts
function promisify(fn) {
return (...args) => {
return new Promise((resolve, reject) => {
fn(...args, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
};
}
var promisisfy_default = promisify;
// src/parallel.ts
async function parallel(tasks, concurrency = Infinity) {
if (!Array.isArray(tasks)) {
throw new TypeError("Expected tasks to be an array of functions");
}
if (tasks.length === 0) {
return [];
}
const results = new Array(tasks.length);
const runningTasks = /* @__PURE__ */ new Set();
let nextIndex = 0;
return new Promise((resolve, reject) => {
const startNext = () => {
if (nextIndex >= tasks.length) {
if (runningTasks.size === 0) {
resolve(results);
}
return;
}
const index = nextIndex++;
const task = tasks[index]();
const runningTask = task.then((result) => {
results[index] = result;
runningTasks.delete(runningTask);
startNext();
}).catch((error) => {
reject(error);
});
runningTasks.add(runningTask);
if (runningTasks.size < concurrency) {
startNext();
}
};
for (let i = 0; i < Math.min(concurrency, tasks.length); i++) {
startNext();
}
});
}
var parallel_default = parallel;
export {
camelCase_default as camelCase,
capitalize_default as capitalize,
chunk_default as chunk,
compose_default as compose,
cookies_default as cookies,
curry_default as curry,
debounce_default as debounce,
deepClone_default as deepClone,
flatten_default as flatten,
groupBy_default as groupBy,
isEmail_default as isEmail,
isEmpty_default as isEmpty,
isNumeric_default as isNumeric,
isURL_default as isURL,
kebabCase_default as kebabCase,
memoize_default as memoize,
merge_default as merge,
omit_default as omit,
once_default as once,
parallel_default as parallel,
partial_default as partial,
pick_default as pick,
pipe_default as pipe,
promisisfy_default as promisify,
queryParams_default as queryParams,
randomId_default as randomId,
retry_default as retry,
sleep_default as sleep,
snakeCase_default as snakeCase,
storage_default as storage,
template_default as template,
throttle_default as throttle,
truncate_default as truncate,
uniqueBy_default as uniqueBy
};