autotel-cloudflare
Version:
The #1 OpenTelemetry package for Cloudflare Workers - complete bindings coverage, native CF OTel integration, advanced sampling
108 lines (107 loc) • 4.41 kB
JavaScript
//#region src/values.ts
/** The value as an object, or undefined when it is anything else. */
function asRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
}
/** The string the value carries, or undefined when it carries anything else. */
function asString(value) {
return typeof value === "string" ? value : void 0;
}
/** The number the value carries, or undefined when it carries anything else. */
/** A string with something in it - a blank one counts as absent. */
function nonEmptyString(value) {
const text = asString(value);
return text !== void 0 && text.trim() !== "" ? text : void 0;
}
function asNumber(value) {
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
/** The boolean the value carries, or undefined when it carries anything else. */
function asBoolean(value) {
return typeof value === "boolean" ? value : void 0;
}
/** Whether the value is callable. */
function isFunction(value) {
return typeof value === "function";
}
/** The value as a callable, or undefined when it is not one. */
function asFunction(value) {
if (typeof value !== "function") return void 0;
return value;
}
/** One field of a value, when the value is an object at all. */
function readProperty(source, key) {
return asRecord(source)?.[key];
}
/** Whether an object carries a callable under this name. */
function hasMethod(source, name) {
return isFunction(readProperty(source, name));
}
/** Whether an object carries a callable under every one of these names. */
function hasMethods(source, names) {
return names.every((name) => hasMethod(source, name));
}
/**
* Call the function a proxy trap is wrapping, with the arguments the caller
* actually passed.
*
* SAFETY: an apply trap receives the argument list of the very call it is
* intercepting, so those arguments are the wrapped function's own parameter
* tuple and its result is that function's return. TypeScript types a trap's
* `argArray` as `any[]` and cannot carry either fact, so it is stated here
* once rather than at every trap in the package.
*/
function applyTrap(fn, thisArg, args) {
return fn.apply(thisArg, args);
}
/**
* Read a member off a proxy target, by whatever key the trap was asked for.
*
* SAFETY: a get trap forwards every key, including ones the target's declared
* type does not name. The value is returned as unread, so nothing here claims
* to know what it is.
*/
function member(target, key) {
return target[key];
}
/**
* The arguments a proxy trap intercepted, read as the wrapped call's own
* parameters.
*
* SAFETY: as with applyTrap - an apply trap receives the argument list of the
* very call it is intercepting, which is that function's parameter tuple.
* TypeScript types a trap's `argArray` as `any[]` and cannot carry that.
*/
function trapArgs(args) {
return args;
}
/** A field of a value's field - `readProperty(readProperty(v, a), b)`, said once. */
function readPath(source, ...keys) {
return keys.reduce((value, key) => readProperty(value, key), source);
}
/** The number at this path, when the value carries one there. */
function numberAt(source, ...keys) {
const value = readPath(source, ...keys);
return asNumber(value) ?? asNumber(Number(asString(value)));
}
/** The scalar the value carries - string, number or boolean - or undefined. */
function asScalar(value) {
return asString(value) ?? asNumber(value) ?? asBoolean(value);
}
/** A homogeneous list of scalars, or undefined when the value is not one. */
function asScalarArray(value) {
if (!Array.isArray(value)) return void 0;
if (value.every((v) => asString(v) !== void 0)) return value.map(String);
if (value.every((v) => asNumber(v) !== void 0)) return value.map(Number);
if (value.every((v) => asBoolean(v) !== void 0)) return value.map(Boolean);
}
/**
* What a value is, for a diagnostic attribute: the same words `typeof` uses,
* plus `null` and `array`, which are the two cases `typeof` answers uselessly.
*/
function describeValue(value) {
if (value === null) return "null";
return Array.isArray(value) ? "array" : typeof value;
}
//#endregion
export { trapArgs as _, asRecord as a, asString as c, hasMethods as d, member as f, readProperty as g, readPath as h, asNumber as i, describeValue as l, numberAt as m, asBoolean as n, asScalar as o, nonEmptyString as p, asFunction as r, asScalarArray as s, applyTrap as t, hasMethod as u };