@orpc/shared
Version:
> [!WARNING] > > `@orpc/shared` is an internal dependency of oRPC packages. It does not follow semver and may change at any time without notice. > Please do not use it in your project.
153 lines (143 loc) • 3.83 kB
JavaScript
export { group, guard, mapEntries, mapValues, omit } from 'radash';
function resolveMaybeOptionalOptions(rest) {
return rest[0] ?? {};
}
function toArray(value) {
return Array.isArray(value) ? value : value === void 0 || value === null ? [] : [value];
}
function splitInHalf(arr) {
const half = Math.ceil(arr.length / 2);
return [arr.slice(0, half), arr.slice(half)];
}
function once(fn) {
let cached;
return () => {
if (cached) {
return cached.result;
}
const result = fn();
cached = { result };
return result;
};
}
function onStart(callback) {
return async (options, ...rest) => {
await callback(options, ...rest);
return await options.next();
};
}
function onSuccess(callback) {
return async (options, ...rest) => {
const result = await options.next();
await callback(result, options, ...rest);
return result;
};
}
function onError(callback) {
return async (options, ...rest) => {
try {
return await options.next();
} catch (error) {
await callback(error, options, ...rest);
throw error;
}
};
}
function onFinish(callback) {
let state;
return async (options, ...rest) => {
try {
const result = await options.next();
state = [null, result, true];
return result;
} catch (error) {
state = [error, void 0, false];
throw error;
} finally {
await callback(state, options, ...rest);
}
};
}
async function intercept(interceptors, options, main) {
const next = async (options2, index) => {
const interceptor = interceptors[index];
if (!interceptor) {
return await main(options2);
}
return await interceptor({
...options2,
next: (newOptions = options2) => next(newOptions, index + 1)
});
};
return next(options, 0);
}
function isAsyncIteratorObject(maybe) {
if (!maybe || typeof maybe !== "object") {
return false;
}
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
}
function parseEmptyableJSON(text) {
if (!text) {
return void 0;
}
return JSON.parse(text);
}
function stringifyJSON(value) {
return JSON.stringify(value);
}
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
if (check(payload)) {
maps.push(segments);
values.push(payload);
} else if (Array.isArray(payload)) {
payload.forEach((v, i) => {
findDeepMatches(check, v, [...segments, i], maps, values);
});
} else if (isObject(payload)) {
for (const key in payload) {
findDeepMatches(check, payload[key], [...segments, key], maps, values);
}
}
return { maps, values };
}
function isObject(value) {
if (!value || typeof value !== "object") {
return false;
}
const proto = Object.getPrototypeOf(value);
return proto === Object.prototype || !proto || !proto.constructor;
}
function isTypescriptObject(value) {
return !!value && (typeof value === "object" || typeof value === "function");
}
function clone(value) {
if (Array.isArray(value)) {
return value.map(clone);
}
if (isObject(value)) {
const result = {};
for (const key in value) {
result[key] = clone(value[key]);
}
return result;
}
return value;
}
function get(object, path) {
let current = object;
for (const key of path) {
if (!isTypescriptObject(current)) {
return void 0;
}
current = current[key];
}
return current;
}
function value(value2, ...args) {
if (typeof value2 === "function") {
return value2(...args);
}
return value2;
}
export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, splitInHalf, stringifyJSON, toArray, value };