🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@autometa/asserters

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@autometa/asserters - npm Package Compare versions

Comparing version
0.1.8
to
1.0.0-rc.0
+16
dist/assert-defined.d.ts
/**
* Asserts that a value is not null or undefined, narrowing its type.
*
* @param value - The value to check
* @param context - Optional context for error messages
* @throws {AutomationError} If value is null or undefined
*
* @example
* ```ts
* const maybeValue: string | undefined = getValue();
* assertDefined(maybeValue, "getValue");
* // maybeValue is now typed as string
* console.log(maybeValue.toUpperCase());
* ```
*/
export declare function assertDefined<T>(value: T, context?: string): asserts value is NonNullable<T>;
/**
* Asserts that a value matches an expected type or instance.
*
* @param value - The value to check
* @param expected - The expected type (string, constructor, or value)
* @param context - Optional context for error messages
* @throws {AutomationError} If value doesn't match expected type
*
* @example
* ```ts
* // Type checking
* assertIs(value, "string");
* assertIs(value, "number");
*
* // Instance checking
* assertIs(error, Error);
* assertIs(date, Date);
*
* // Value checking
* assertIs(status, 200);
* assertIs(flag, true);
* ```
*/
export declare function assertIs<T>(value: unknown, expected: T, context?: string): asserts value is T;
import { AutomationError } from "@autometa/errors";
/**
* Custom error for invalid object key access with helpful suggestions.
*/
export declare class InvalidKeyError<T extends object> extends AutomationError {
readonly key: string;
readonly target: T;
readonly suggestions: string[];
constructor(key: string, target: T, suggestions: string[], context?: string);
}
/**
* Asserts that a key exists on an object, narrowing the key type.
*
* @param target - The object to check
* @param key - The key to verify
* @param context - Optional context for error messages
* @throws {InvalidKeyError} If key doesn't exist on target
*
* @example
* ```ts
* const config: Record<string, unknown> = getConfig();
* assertKey(config, "apiKey");
* // "apiKey" is now typed as keyof typeof config
* const key = config.apiKey;
* ```
*/
export declare function assertKey<T extends object>(target: T, key: unknown, context?: string): asserts key is keyof T;
/**
* Type guard to check if a key exists on an object.
* Non-throwing version of assertKey.
*
* @param target - The object to check
* @param key - The key to verify
* @returns true if key exists on target
*
* @example
* ```ts
* if (confirmKey(config, "apiKey")) {
* // TypeScript knows apiKey exists
* console.log(config.apiKey);
* }
* ```
*/
export declare function confirmKey<T extends object>(target: T, key: unknown): key is keyof T;
/**
* Safely retrieves a value from an object, asserting the key exists.
*
* @param target - The object to access
* @param key - The key to retrieve
* @param context - Optional context for error messages
* @returns The value at the key
* @throws {InvalidKeyError} If key doesn't exist on target
*
* @example
* ```ts
* const value = getKey(config, "apiKey");
* // TypeScript knows the value type from the object
* ```
*/
export declare function getKey<T extends object, K extends keyof T>(target: T, key: K, context?: string): T[K];
export declare function getKey<T extends object>(target: T, key: string, context?: string): unknown;
type LengthLike = {
length: number;
};
/**
* Asserts that an array or string has an exact length.
*
* @param value - Array or string to check
* @param expectedLength - Expected length
* @param context - Optional context for error messages
* @throws {AutomationError} If length doesn't match
*
* @example
* ```ts
* assertLength(args, 3, "function arguments");
* assertLength(username, 5, "username");
* ```
*/
export declare function assertLength<T extends LengthLike>(value: T, expectedLength: number, context?: string): asserts value is T & {
length: typeof expectedLength;
};
/**
* Asserts that an array or string has at least a minimum length.
*
* @param value - Array or string to check
* @param minLength - Minimum required length
* @param context - Optional context for error messages
* @throws {AutomationError} If length is too short
*
* @example
* ```ts
* assertMinLength(password, 8, "password");
* assertMinLength(items, 1, "cart items");
* ```
*/
export declare function assertMinLength<T extends LengthLike>(value: T, minLength: number, context?: string): void;
/**
* Asserts that an array or string has at most a maximum length.
*
* @param value - Array or string to check
* @param maxLength - Maximum allowed length
* @param context - Optional context for error messages
* @throws {AutomationError} If length is too long
*
* @example
* ```ts
* assertMaxLength(description, 280, "tweet");
* assertMaxLength(items, 10, "cart limit");
* ```
*/
export declare function assertMaxLength<T extends LengthLike>(value: T, maxLength: number, context?: string): void;
export {};
'use strict';
var errors = require('@autometa/errors');
var closestMatch = require('closest-match');
// src/assert-defined.ts
function assertDefined(value, context) {
if (value === null || value === void 0) {
const prefix = context ? `[${context}] ` : "";
throw new errors.AutomationError(
`${prefix}Expected value to be defined, but got ${value}`
);
}
}
function assertIs(value, expected, context) {
const prefix = context ? `[${context}] ` : "";
if (typeof expected === "function") {
if (value instanceof expected) {
return;
}
const expectedName = expected.name ?? "Constructor";
throw new errors.AutomationError(
`${prefix}Expected instance of ${expectedName}, but got ${getTypeName(value)}`
);
}
const expectedType = typeof expected;
const actualType = typeof value;
if (expectedType === actualType) {
if (value === void 0 && expected === void 0) {
return;
}
if (value === null && expected === null) {
return;
}
if (isPrimitive(expected) && value === expected) {
return;
}
if (actualType === "object" && value !== null) {
return;
}
}
if (typeof expected === "string" && actualType === expected) {
return;
}
throw new errors.AutomationError(
`${prefix}Expected ${formatExpected(expected)}, but got ${formatActual(value)}`
);
}
function isPrimitive(value) {
const type = typeof value;
return type === "string" || type === "number" || type === "boolean" || type === "symbol" || type === "bigint";
}
function getTypeName(value) {
if (value === null)
return "null";
if (value === void 0)
return "undefined";
if (typeof value === "object") {
return value.constructor?.name ?? "Object";
}
return typeof value;
}
function formatExpected(expected) {
if (typeof expected === "string") {
return `type "${expected}"`;
}
return `value ${JSON.stringify(expected)}`;
}
function formatActual(value) {
const type = getTypeName(value);
if (value === null || value === void 0) {
return type;
}
try {
const str = JSON.stringify(value);
return `${type} ${str}`;
} catch {
return type;
}
}
var InvalidKeyError = class extends errors.AutomationError {
constructor(key, target, suggestions, context) {
const prefix = context ? `[${context}] ` : "";
const targetType = target.constructor?.name ?? "Object";
const suggestionText = suggestions.length > 0 ? `
Did you mean one of these?
${suggestions.join("\n ")}` : "";
super(
`${prefix}Key "${key}" does not exist on ${targetType}.${suggestionText}`
);
this.key = key;
this.target = target;
this.suggestions = suggestions;
}
};
function assertKey(target, key, context) {
if (target === null || target === void 0) {
const prefix = context ? `[${context}] ` : "";
throw new errors.AutomationError(
`${prefix}Cannot check key "${String(key)}" on ${target}`
);
}
if (typeof key !== "string" && typeof key !== "symbol" && typeof key !== "number") {
const prefix = context ? `[${context}] ` : "";
throw new errors.AutomationError(
`${prefix}Key must be string, symbol, or number, got ${typeof key}`
);
}
if (key in target) {
return;
}
const suggestions = typeof key === "string" ? findSimilarKeys(key, target) : [];
throw new InvalidKeyError(String(key), target, suggestions, context);
}
function confirmKey(target, key) {
if (target === null || target === void 0) {
return false;
}
if (typeof key !== "string" && typeof key !== "symbol" && typeof key !== "number") {
return false;
}
return key in target;
}
function getKey(target, key, context) {
assertKey(target, key, context);
return target[key];
}
function findSimilarKeys(searchKey, target) {
const keys = Object.keys(target);
if (keys.length === 0) {
return [];
}
const matches = closestMatch.closestMatch(searchKey, keys, true);
if (!matches) {
return [];
}
return Array.isArray(matches) ? matches.slice(0, 3) : [matches];
}
function assertLength(value, expectedLength, context) {
if (!hasLength(value)) {
const prefix = context ? `[${context}] ` : "";
throw new errors.AutomationError(
`${prefix}Cannot check length on ${getTypeName2(value)}`
);
}
if (value.length !== expectedLength) {
const prefix = context ? `[${context}] ` : "";
const typeName = Array.isArray(value) ? "array" : "string";
throw new errors.AutomationError(
`${prefix}Expected ${typeName} length ${expectedLength}, but got ${value.length}`
);
}
}
function assertMinLength(value, minLength, context) {
if (!hasLength(value)) {
const prefix = context ? `[${context}] ` : "";
throw new errors.AutomationError(
`${prefix}Cannot check length on ${getTypeName2(value)}`
);
}
if (value.length < minLength) {
const prefix = context ? `[${context}] ` : "";
const typeName = Array.isArray(value) ? "array" : "string";
throw new errors.AutomationError(
`${prefix}Expected ${typeName} length >= ${minLength}, but got ${value.length}`
);
}
}
function assertMaxLength(value, maxLength, context) {
if (!hasLength(value)) {
const prefix = context ? `[${context}] ` : "";
throw new errors.AutomationError(
`${prefix}Cannot check length on ${getTypeName2(value)}`
);
}
if (value.length > maxLength) {
const prefix = context ? `[${context}] ` : "";
const typeName = Array.isArray(value) ? "array" : "string";
throw new errors.AutomationError(
`${prefix}Expected ${typeName} length <= ${maxLength}, but got ${value.length}`
);
}
}
function hasLength(value) {
return value !== null && value !== void 0 && typeof value.length === "number";
}
function getTypeName2(value) {
if (value === null)
return "null";
if (value === void 0)
return "undefined";
if (Array.isArray(value))
return "array";
return typeof value;
}
// src/type-cast.ts
function lie(value) {
}
function unsafeCast(value) {
return value;
}
exports.InvalidKeyError = InvalidKeyError;
exports.assertDefined = assertDefined;
exports.assertIs = assertIs;
exports.assertKey = assertKey;
exports.assertLength = assertLength;
exports.assertMaxLength = assertMaxLength;
exports.assertMinLength = assertMinLength;
exports.confirmKey = confirmKey;
exports.getKey = getKey;
exports.lie = lie;
exports.unsafeCast = unsafeCast;
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.cjs.map
{"version":3,"sources":["../src/assert-defined.ts","../src/assert-is.ts","../src/assert-key.ts","../src/assert-length.ts","../src/type-cast.ts"],"names":["AutomationError","getTypeName"],"mappings":";AAAA,SAAS,uBAAuB;AAiBzB,SAAS,cACd,OACA,SACiC;AACjC,MAAI,UAAU,QAAQ,UAAU,QAAW;AACzC,UAAM,SAAS,UAAU,IAAI,OAAO,OAAO;AAC3C,UAAM,IAAI;AAAA,MACR,GAAG,MAAM,yCAAyC,KAAK;AAAA,IACzD;AAAA,EACF;AACF;;;AC3BA,SAAS,mBAAAA,wBAAuB;AAyBzB,SAAS,SACd,OACA,UACA,SACoB;AACpB,QAAM,SAAS,UAAU,IAAI,OAAO,OAAO;AAG3C,MAAI,OAAO,aAAa,YAAY;AAElC,QAAI,iBAAkB,UAAuB;AAC3C;AAAA,IACF;AACA,UAAM,eAAgB,SAA+B,QAAQ;AAC7D,UAAM,IAAIA;AAAA,MACR,GAAG,MAAM,wBAAwB,YAAY,aAAa,YAAY,KAAK,CAAC;AAAA,IAC9E;AAAA,EACF;AAGA,QAAM,eAAe,OAAO;AAC5B,QAAM,aAAa,OAAO;AAE1B,MAAI,iBAAiB,YAAY;AAE/B,QAAI,UAAU,UAAa,aAAa,QAAW;AACjD;AAAA,IACF;AACA,QAAI,UAAU,QAAQ,aAAa,MAAM;AACvC;AAAA,IACF;AAEA,QAAI,YAAY,QAAQ,KAAK,U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AAAA,IACvD;AAAA,EACF;AAEA,MAAI,MAAM,SAAS,WAAW;AAC5B,UAAM,SAAS,UAAU,IAAI,OAAO,OAAO;AAC3C,UAAM,WAAW,MAAM,QAAQ,KAAK,IAAI,UAAU;AAClD,UAAM,IAAID;AAAA,MACR,GAAG,MAAM,YAAY,QAAQ,cAAc,SAAS,aAAa,MAAM,MAAM;AAAA,IAC/E;AAAA,EACF;AACF;AAEA,SAAS,UAAU,OAAqC;AACtD,SACE,UAAU,QACV,UAAU,UACV,OAAQ,MAAqB,WAAW;AAE5C;AAEA,SAASC,aAAY,OAAwB;AAC3C,MAAI,UAAU;AAAM,WAAO;AAC3B,MAAI,UAAU;AAAW,WAAO;AAChC,MAAI,MAAM,QAAQ,KAAK;AAAG,WAAO;AACjC,SAAO,OAAO;AAChB;;;AC5GO,SAAS,IAAO,OAAoC;AAE3D;AAeO,SAAS,WAAc,OAAmB;AAC/C,SAAO;AACT","sourcesContent":["import { AutomationError } from \"@autometa/errors\";\n\n/**\n * Asserts that a value is not null or undefined, narrowing its type.\n * \n * @param value - The value to check\n * @param context - Optional context for error messages\n * @throws {AutomationError} If value is null or undefined\n * \n * @example\n * ```ts\n * const maybeValue: string | undefined = getValue();\n * assertDefined(maybeValue, \"getValue\");\n * // maybeValue is now typed as string\n * console.log(maybeValue.toUpperCase());\n * ```\n */\nexport function assertDefined<T>(\n value: T,\n context?: string\n): asserts value is NonNullable<T> {\n if (value === null || value === undefined) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Expected value to be defined, but got ${value}`\n );\n }\n}\n","import { AutomationError } from \"@autometa/errors\";\n\n/**\n * Asserts that a value matches an expected type or instance.\n * \n * @param value - The value to check\n * @param expected - The expected type (string, constructor, or value)\n * @param context - Optional context for error messages\n * @throws {AutomationError} If value doesn't match expected type\n * \n * @example\n * ```ts\n * // Type checking\n * assertIs(value, \"string\");\n * assertIs(value, \"number\");\n * \n * // Instance checking\n * assertIs(error, Error);\n * assertIs(date, Date);\n * \n * // Value checking\n * assertIs(status, 200);\n * assertIs(flag, true);\n * ```\n */\nexport function assertIs<T>(\n value: unknown,\n expected: T,\n context?: string\n): asserts value is T {\n const prefix = context ? `[${context}] ` : \"\";\n \n // Handle constructor/class checks\n if (typeof expected === \"function\") {\n // eslint-disable-next-line @typescript-eslint/ban-types\n if (value instanceof (expected as Function)) {\n return;\n }\n const expectedName = (expected as { name?: string }).name ?? \"Constructor\";\n throw new AutomationError(\n `${prefix}Expected instance of ${expectedName}, but got ${getTypeName(value)}`\n );\n }\n \n // Handle primitive type checks\n const expectedType = typeof expected;\n const actualType = typeof value;\n \n if (expectedType === actualType) {\n // Special case: undefined and null can only match themselves\n if (value === undefined && expected === undefined) {\n return;\n }\n if (value === null && expected === null) {\n return;\n }\n // For primitives, check both type and value\n if (isPrimitive(expected) && value === expected) {\n return;\n }\n // For objects (not null), just check type match\n if (actualType === \"object\" && value !== null) {\n return;\n }\n }\n \n // Handle typeof string checks (e.g., \"string\", \"number\")\n if (typeof expected === \"string\" && actualType === expected) {\n return;\n }\n \n throw new AutomationError(\n `${prefix}Expected ${formatExpected(expected)}, but got ${formatActual(value)}`\n );\n}\n\nfunction isPrimitive(value: unknown): value is string | number | boolean | symbol | bigint {\n const type = typeof value;\n return type === \"string\" || type === \"number\" || type === \"boolean\" || \n type === \"symbol\" || type === \"bigint\";\n}\n\nfunction getTypeName(value: unknown): string {\n if (value === null) return \"null\";\n if (value === undefined) return \"undefined\";\n if (typeof value === \"object\") {\n return value.constructor?.name ?? \"Object\";\n }\n return typeof value;\n}\n\nfunction formatExpected(expected: unknown): string {\n if (typeof expected === \"string\") {\n return `type \"${expected}\"`;\n }\n return `value ${JSON.stringify(expected)}`;\n}\n\nfunction formatActual(value: unknown): string {\n const type = getTypeName(value);\n if (value === null || value === undefined) {\n return type;\n }\n try {\n const str = JSON.stringify(value);\n return `${type} ${str}`;\n } catch {\n return type;\n }\n}\n","import { AutomationError } from \"@autometa/errors\";\nimport { closestMatch } from \"closest-match\";\n\n/**\n * Custom error for invalid object key access with helpful suggestions.\n */\nexport class InvalidKeyError<T extends object> extends AutomationError {\n constructor(\n public readonly key: string,\n public readonly target: T,\n public readonly suggestions: string[],\n context?: string\n ) {\n const prefix = context ? `[${context}] ` : \"\";\n const targetType = target.constructor?.name ?? \"Object\";\n const suggestionText = suggestions.length > 0\n ? `\\n\\nDid you mean one of these?\\n ${suggestions.join(\"\\n \")}`\n : \"\";\n \n super(\n `${prefix}Key \"${key}\" does not exist on ${targetType}.${suggestionText}`\n );\n }\n}\n\n/**\n * Asserts that a key exists on an object, narrowing the key type.\n * \n * @param target - The object to check\n * @param key - The key to verify\n * @param context - Optional context for error messages\n * @throws {InvalidKeyError} If key doesn't exist on target\n * \n * @example\n * ```ts\n * const config: Record<string, unknown> = getConfig();\n * assertKey(config, \"apiKey\");\n * // \"apiKey\" is now typed as keyof typeof config\n * const key = config.apiKey;\n * ```\n */\nexport function assertKey<T extends object>(\n target: T,\n key: unknown,\n context?: string\n): asserts key is keyof T {\n // Validate target\n if (target === null || target === undefined) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Cannot check key \"${String(key)}\" on ${target}`\n );\n }\n \n // Validate key type\n if (typeof key !== \"string\" && typeof key !== \"symbol\" && typeof key !== \"number\") {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Key must be string, symbol, or number, got ${typeof key}`\n );\n }\n \n // Check if key exists\n if (key in target) {\n return;\n }\n \n // Generate helpful suggestions for string keys\n const suggestions = typeof key === \"string\"\n ? findSimilarKeys(key, target)\n : [];\n \n throw new InvalidKeyError(String(key), target, suggestions, context);\n}\n\n/**\n * Type guard to check if a key exists on an object.\n * Non-throwing version of assertKey.\n * \n * @param target - The object to check\n * @param key - The key to verify\n * @returns true if key exists on target\n * \n * @example\n * ```ts\n * if (confirmKey(config, \"apiKey\")) {\n * // TypeScript knows apiKey exists\n * console.log(config.apiKey);\n * }\n * ```\n */\nexport function confirmKey<T extends object>(\n target: T,\n key: unknown\n): key is keyof T {\n if (target === null || target === undefined) {\n return false;\n }\n if (typeof key !== \"string\" && typeof key !== \"symbol\" && typeof key !== \"number\") {\n return false;\n }\n return key in target;\n}\n\n/**\n * Safely retrieves a value from an object, asserting the key exists.\n * \n * @param target - The object to access\n * @param key - The key to retrieve\n * @param context - Optional context for error messages\n * @returns The value at the key\n * @throws {InvalidKeyError} If key doesn't exist on target\n * \n * @example\n * ```ts\n * const value = getKey(config, \"apiKey\");\n * // TypeScript knows the value type from the object\n * ```\n */\nexport function getKey<T extends object, K extends keyof T>(\n target: T,\n key: K,\n context?: string\n): T[K];\nexport function getKey<T extends object>(\n target: T,\n key: string,\n context?: string\n): unknown;\nexport function getKey<T extends object>(\n target: T,\n key: string,\n context?: string\n): unknown {\n assertKey(target, key, context);\n return target[key as keyof T];\n}\n\n/**\n * Finds similar keys in an object for helpful error messages.\n */\nfunction findSimilarKeys<T extends object>(searchKey: string, target: T): string[] {\n const keys = Object.keys(target);\n if (keys.length === 0) {\n return [];\n }\n \n const matches = closestMatch(searchKey, keys, true);\n if (!matches) {\n return [];\n }\n \n return Array.isArray(matches) ? matches.slice(0, 3) : [matches];\n}\n","import { AutomationError } from \"@autometa/errors\";\n\ntype LengthLike = { length: number };\n\n/**\n * Asserts that an array or string has an exact length.\n * \n * @param value - Array or string to check\n * @param expectedLength - Expected length\n * @param context - Optional context for error messages\n * @throws {AutomationError} If length doesn't match\n * \n * @example\n * ```ts\n * assertLength(args, 3, \"function arguments\");\n * assertLength(username, 5, \"username\");\n * ```\n */\nexport function assertLength<T extends LengthLike>(\n value: T,\n expectedLength: number,\n context?: string\n): asserts value is T & { length: typeof expectedLength } {\n if (!hasLength(value)) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Cannot check length on ${getTypeName(value)}`\n );\n }\n \n if (value.length !== expectedLength) {\n const prefix = context ? `[${context}] ` : \"\";\n const typeName = Array.isArray(value) ? \"array\" : \"string\";\n throw new AutomationError(\n `${prefix}Expected ${typeName} length ${expectedLength}, but got ${value.length}`\n );\n }\n}\n\n/**\n * Asserts that an array or string has at least a minimum length.\n * \n * @param value - Array or string to check\n * @param minLength - Minimum required length\n * @param context - Optional context for error messages\n * @throws {AutomationError} If length is too short\n * \n * @example\n * ```ts\n * assertMinLength(password, 8, \"password\");\n * assertMinLength(items, 1, \"cart items\");\n * ```\n */\nexport function assertMinLength<T extends LengthLike>(\n value: T,\n minLength: number,\n context?: string\n): void {\n if (!hasLength(value)) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Cannot check length on ${getTypeName(value)}`\n );\n }\n \n if (value.length < minLength) {\n const prefix = context ? `[${context}] ` : \"\";\n const typeName = Array.isArray(value) ? \"array\" : \"string\";\n throw new AutomationError(\n `${prefix}Expected ${typeName} length >= ${minLength}, but got ${value.length}`\n );\n }\n}\n\n/**\n * Asserts that an array or string has at most a maximum length.\n * \n * @param value - Array or string to check\n * @param maxLength - Maximum allowed length\n * @param context - Optional context for error messages\n * @throws {AutomationError} If length is too long\n * \n * @example\n * ```ts\n * assertMaxLength(description, 280, \"tweet\");\n * assertMaxLength(items, 10, \"cart limit\");\n * ```\n */\nexport function assertMaxLength<T extends LengthLike>(\n value: T,\n maxLength: number,\n context?: string\n): void {\n if (!hasLength(value)) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Cannot check length on ${getTypeName(value)}`\n );\n }\n \n if (value.length > maxLength) {\n const prefix = context ? `[${context}] ` : \"\";\n const typeName = Array.isArray(value) ? \"array\" : \"string\";\n throw new AutomationError(\n `${prefix}Expected ${typeName} length <= ${maxLength}, but got ${value.length}`\n );\n }\n}\n\nfunction hasLength(value: unknown): value is LengthLike {\n return (\n value !== null &&\n value !== undefined &&\n typeof (value as LengthLike).length === \"number\"\n );\n}\n\nfunction getTypeName(value: unknown): string {\n if (value === null) return \"null\";\n if (value === undefined) return \"undefined\";\n if (Array.isArray(value)) return \"array\";\n return typeof value;\n}\n","/**\n * Type assertion function that lies to TypeScript about a value's type.\n * Use with caution - no runtime checks are performed.\n * \n * @param value - Value to cast\n * @returns The same value, typed as T\n * \n * @example\n * ```ts\n * const data: unknown = JSON.parse(response);\n * lie<User>(data);\n * // TypeScript now thinks data is User, but no validation occurred!\n * ```\n */\nexport function lie<T>(value: unknown): asserts value is T {\n // Intentionally empty - this is a type assertion only\n}\n\n/**\n * Unsafely casts a value to a different type without runtime checks.\n * Prefer using proper type guards and assertions when possible.\n * \n * @param value - Value to cast\n * @returns The same value, typed as T\n * \n * @example\n * ```ts\n * const config = unsafeCast<Config>(rawData);\n * // Use with caution - no validation!\n * ```\n */\nexport function unsafeCast<T>(value: unknown): T {\n return value as T;\n}\n"]}
export { InvalidKeyError } from "./assert-key.js";
/**
* Type assertion function that lies to TypeScript about a value's type.
* Use with caution - no runtime checks are performed.
*
* @param value - Value to cast
* @returns The same value, typed as T
*
* @example
* ```ts
* const data: unknown = JSON.parse(response);
* lie<User>(data);
* // TypeScript now thinks data is User, but no validation occurred!
* ```
*/
export declare function lie<T>(value: unknown): asserts value is T;
/**
* Unsafely casts a value to a different type without runtime checks.
* Prefer using proper type guards and assertions when possible.
*
* @param value - Value to cast
* @returns The same value, typed as T
*
* @example
* ```ts
* const config = unsafeCast<Config>(rawData);
* // Use with caution - no validation!
* ```
*/
export declare function unsafeCast<T>(value: unknown): T;
+12
-34

@@ -1,34 +0,12 @@

import { AutomationError } from '@autometa/errors';
declare function FromKey<TObj, TReturn>(item: TObj, key: string): TReturn;
declare function AssertKey<TObj>(item: TObj, key: string | keyof TObj, context?: string): asserts key is keyof TObj;
declare function ConfirmKey<TObj>(item: TObj, key: string | keyof TObj): key is keyof TObj;
declare class InvalidKeyError<T> extends AutomationError {
readonly key: string;
readonly item: T;
readonly context?: string | undefined;
bestMatches: string | string[];
constructor(key: string, item: T, context?: string | undefined);
}
type ExtractLiteralFromObject<TObj extends Record<string, unknown>, TString extends string> = TObj[TString] extends infer T ? T : never;
declare function AssertLength<TObj extends Array<unknown> | string>(item: TObj, length: number, context?: string): asserts length is TObj["length"];
declare function ConfirmLength<TObj extends Array<unknown> | string>(item: TObj, length: number): length is TObj["length"];
declare function AssertLengthAtLeast<TObj extends Array<unknown> | string>(item: TObj, length: number, context?: string): void;
declare function ConfirmLengthAtLeast<TObj extends Array<unknown> | string>(item: TObj, length: number): boolean;
declare function AssertLengthAtMost<TObj extends Array<unknown> | string>(item: TObj, length: number, context?: string): void;
declare function ConfirmLengthAtMost<TObj extends Array<unknown> | string>(item: TObj, length: number): boolean;
declare function AssertDefined<TObj>(item: TObj | null | undefined, context?: string): asserts item is TObj;
declare function ConfirmDefined<TObj>(item: TObj | null | undefined): item is TObj;
declare function AssertIs<TIsType>(value: unknown, type: TIsType, context?: string): asserts value is TIsType;
declare function fib<T>(value: unknown): T;
declare function lie<T>(value: unknown): asserts value is T;
export { AssertDefined, AssertIs, AssertKey, AssertLength, AssertLengthAtLeast, AssertLengthAtMost, ConfirmDefined, ConfirmKey, ConfirmLength, ConfirmLengthAtLeast, ConfirmLengthAtMost, ExtractLiteralFromObject, FromKey, InvalidKeyError, fib, lie };
/**
* @autometa/asserters - Type-safe runtime assertions for Autometa
*
* Provides assertion utilities that throw AutomationError on failure and
* narrow TypeScript types on success.
*/
export { assertDefined } from "./assert-defined.js";
export { assertIs } from "./assert-is.js";
export { assertKey, confirmKey, getKey } from "./assert-key.js";
export { assertLength, assertMinLength, assertMaxLength } from "./assert-length.js";
export { InvalidKeyError } from "./invalid-key-error.js";
export { lie, unsafeCast } from "./type-cast.js";

@@ -1,228 +0,205 @@

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
import { AutomationError } from '@autometa/errors';
import { closestMatch } from 'closest-match';
// src/index.ts
var src_exports = {};
__export(src_exports, {
AssertDefined: () => AssertDefined,
AssertIs: () => AssertIs,
AssertKey: () => AssertKey,
AssertLength: () => AssertLength,
AssertLengthAtLeast: () => AssertLengthAtLeast,
AssertLengthAtMost: () => AssertLengthAtMost,
ConfirmDefined: () => ConfirmDefined,
ConfirmKey: () => ConfirmKey,
ConfirmLength: () => ConfirmLength,
ConfirmLengthAtLeast: () => ConfirmLengthAtLeast,
ConfirmLengthAtMost: () => ConfirmLengthAtMost,
FromKey: () => FromKey,
InvalidKeyError: () => InvalidKeyError,
fib: () => fib,
lie: () => lie
});
module.exports = __toCommonJS(src_exports);
// src/assert-key.ts
var import_errors2 = require("@autometa/errors");
// src/invalid-key-error.ts
var import_errors = require("@autometa/errors");
var import_closest_match = require("closest-match");
var InvalidKeyError = class extends import_errors.AutomationError {
constructor(key, item, context) {
const prefix = context ? `${context}: ` : "";
const matches = (0, import_closest_match.closestMatch)(key, Object.keys(item), true) ?? [];
super(
`${prefix}Key ${String(key)} does not exist on target ${item}.
These keys are similar. Did you mean one of these?:
${Array.isArray(matches) ? matches.join("\n") : matches}`
// src/assert-defined.ts
function assertDefined(value, context) {
if (value === null || value === void 0) {
const prefix = context ? `[${context}] ` : "";
throw new AutomationError(
`${prefix}Expected value to be defined, but got ${value}`
);
this.key = key;
this.item = item;
this.context = context;
this.bestMatches = matches;
}
};
// src/assert-key.ts
function AssertKey(item, key, context) {
const prefix = context ? `${context}: ` : "";
if (item === null || item === void 0) {
throw new import_errors2.AutomationError(
`${prefix}Item cannot be null or undefined if indexing for values. ${String(
key
)} is not a valid property of ${item}`
}
function assertIs(value, expected, context) {
const prefix = context ? `[${context}] ` : "";
if (typeof expected === "function") {
if (value instanceof expected) {
return;
}
const expectedName = expected.name ?? "Constructor";
throw new AutomationError(
`${prefix}Expected instance of ${expectedName}, but got ${getTypeName(value)}`
);
}
if (!(typeof item === "object" || typeof item === "function")) {
throw new import_errors2.AutomationError(
`${prefix}A key can only be valid for a value whose type is object or function: Type ${typeof item} is not valid`
);
const expectedType = typeof expected;
const actualType = typeof value;
if (expectedType === actualType) {
if (value === void 0 && expected === void 0) {
return;
}
if (value === null && expected === null) {
return;
}
if (isPrimitive(expected) && value === expected) {
return;
}
if (actualType === "object" && value !== null) {
return;
}
}
if (key && typeof key == "string" && key in item) {
if (typeof expected === "string" && actualType === expected) {
return;
}
throw new InvalidKeyError(key, item);
throw new AutomationError(
`${prefix}Expected ${formatExpected(expected)}, but got ${formatActual(value)}`
);
}
// src/from-key.ts
function FromKey(item, key) {
AssertKey(item, key);
return item[key];
function isPrimitive(value) {
const type = typeof value;
return type === "string" || type === "number" || type === "boolean" || type === "symbol" || type === "bigint";
}
// src/confirm-key.ts
function ConfirmKey(item, key) {
if (item === null || item === void 0) {
return false;
function getTypeName(value) {
if (value === null)
return "null";
if (value === void 0)
return "undefined";
if (typeof value === "object") {
return value.constructor?.name ?? "Object";
}
if (!(typeof item === "object" || typeof item === "function")) {
return false;
return typeof value;
}
function formatExpected(expected) {
if (typeof expected === "string") {
return `type "${expected}"`;
}
if (key && typeof key == "string" && key in item) {
return true;
return `value ${JSON.stringify(expected)}`;
}
function formatActual(value) {
const type = getTypeName(value);
if (value === null || value === void 0) {
return type;
}
return false;
try {
const str = JSON.stringify(value);
return `${type} ${str}`;
} catch {
return type;
}
}
var InvalidKeyError = class extends AutomationError {
constructor(key, target, suggestions, context) {
const prefix = context ? `[${context}] ` : "";
const targetType = target.constructor?.name ?? "Object";
const suggestionText = suggestions.length > 0 ? `
// src/assert-length.ts
var import_errors3 = require("@autometa/errors");
function AssertLength(item, length, context) {
AssertKey(item, "length", context);
const prefix = context ? `${context}: ` : "";
if (item.length !== length) {
throw new import_errors3.AutomationError(
`${prefix}Array was expected to have length ${length} but was ${item.length}. Full Array: ${JSON.stringify(item, null, 2)}`
Did you mean one of these?
${suggestions.join("\n ")}` : "";
super(
`${prefix}Key "${key}" does not exist on ${targetType}.${suggestionText}`
);
this.key = key;
this.target = target;
this.suggestions = suggestions;
}
}
function ConfirmLength(item, length) {
AssertKey(item, "length");
if (item.length !== length) {
return false;
};
function assertKey(target, key, context) {
if (target === null || target === void 0) {
const prefix = context ? `[${context}] ` : "";
throw new AutomationError(
`${prefix}Cannot check key "${String(key)}" on ${target}`
);
}
return true;
}
function AssertLengthAtLeast(item, length, context) {
AssertKey(item, "length", context);
const prefix = context ? `${context}: ` : "";
if (item.length < length) {
throw new import_errors3.AutomationError(
`${prefix}Array was expected to have at least length ${length} but was ${item.length}. Full Array: ${JSON.stringify(item, null, 2)}`
if (typeof key !== "string" && typeof key !== "symbol" && typeof key !== "number") {
const prefix = context ? `[${context}] ` : "";
throw new AutomationError(
`${prefix}Key must be string, symbol, or number, got ${typeof key}`
);
}
if (key in target) {
return;
}
const suggestions = typeof key === "string" ? findSimilarKeys(key, target) : [];
throw new InvalidKeyError(String(key), target, suggestions, context);
}
function ConfirmLengthAtLeast(item, length) {
AssertKey(item, "length");
if (item.length < length) {
function confirmKey(target, key) {
if (target === null || target === void 0) {
return false;
}
return true;
}
function AssertLengthAtMost(item, length, context) {
AssertKey(item, "length", context);
const prefix = context ? `${context}: ` : "";
if (item.length > length) {
throw new import_errors3.AutomationError(
`${prefix}Array was expected to have at least length ${length} but was ${item.length}. Full Array: ${JSON.stringify(item, null, 2)}`
);
if (typeof key !== "string" && typeof key !== "symbol" && typeof key !== "number") {
return false;
}
return key in target;
}
function ConfirmLengthAtMost(item, length) {
AssertKey(item, "length");
if (item.length > length) {
return false;
function getKey(target, key, context) {
assertKey(target, key, context);
return target[key];
}
function findSimilarKeys(searchKey, target) {
const keys = Object.keys(target);
if (keys.length === 0) {
return [];
}
return true;
const matches = closestMatch(searchKey, keys, true);
if (!matches) {
return [];
}
return Array.isArray(matches) ? matches.slice(0, 3) : [matches];
}
// src/assert-defined.ts
var import_errors4 = require("@autometa/errors");
function AssertDefined(item, context) {
const prefix = context ? `${context}: ` : "";
if (item === null || item === void 0) {
throw new import_errors4.AutomationError(
`${prefix}Item was expected to be defined but was ${item}. Full Item: ${JSON.stringify(
item,
null,
2
)}`
function assertLength(value, expectedLength, context) {
if (!hasLength(value)) {
const prefix = context ? `[${context}] ` : "";
throw new AutomationError(
`${prefix}Cannot check length on ${getTypeName2(value)}`
);
}
}
function ConfirmDefined(item) {
if (item === null || item === void 0) {
return false;
if (value.length !== expectedLength) {
const prefix = context ? `[${context}] ` : "";
const typeName = Array.isArray(value) ? "array" : "string";
throw new AutomationError(
`${prefix}Expected ${typeName} length ${expectedLength}, but got ${value.length}`
);
}
return true;
}
// src/assert-is.ts
var import_errors5 = require("@autometa/errors");
function AssertIs(value, type, context) {
const prefix = context ? `${context}: ` : "";
const message = `${prefix}Expected ${type} but got ${value}`;
if (value !== type && typeof value !== type) {
throw new import_errors5.AutomationError(message);
function assertMinLength(value, minLength, context) {
if (!hasLength(value)) {
const prefix = context ? `[${context}] ` : "";
throw new AutomationError(
`${prefix}Cannot check length on ${getTypeName2(value)}`
);
}
if (value !== type && typeof value !== typeof type) {
throw new import_errors5.AutomationError(message);
if (value.length < minLength) {
const prefix = context ? `[${context}] ` : "";
const typeName = Array.isArray(value) ? "array" : "string";
throw new AutomationError(
`${prefix}Expected ${typeName} length >= ${minLength}, but got ${value.length}`
);
}
if (type !== null && value === null) {
throw new import_errors5.AutomationError(message);
}
function assertMaxLength(value, maxLength, context) {
if (!hasLength(value)) {
const prefix = context ? `[${context}] ` : "";
throw new AutomationError(
`${prefix}Cannot check length on ${getTypeName2(value)}`
);
}
if (type !== void 0 && value === void 0) {
throw new import_errors5.AutomationError(message);
}
if (typeof type === "function") {
if (value instanceof type) {
return;
}
throw new import_errors5.AutomationError(
`${prefix}Expected ${type} to be instance of ${value}`
if (value.length > maxLength) {
const prefix = context ? `[${context}] ` : "";
const typeName = Array.isArray(value) ? "array" : "string";
throw new AutomationError(
`${prefix}Expected ${typeName} length <= ${maxLength}, but got ${value.length}`
);
}
}
function hasLength(value) {
return value !== null && value !== void 0 && typeof value.length === "number";
}
function getTypeName2(value) {
if (value === null)
return "null";
if (value === void 0)
return "undefined";
if (Array.isArray(value))
return "array";
return typeof value;
}
// src/lie.ts
function fib(value) {
// src/type-cast.ts
function lie(value) {
}
function unsafeCast(value) {
return value;
}
function lie(value) {
return;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
AssertDefined,
AssertIs,
AssertKey,
AssertLength,
AssertLengthAtLeast,
AssertLengthAtMost,
ConfirmDefined,
ConfirmKey,
ConfirmLength,
ConfirmLengthAtLeast,
ConfirmLengthAtMost,
FromKey,
InvalidKeyError,
fib,
lie
});
export { InvalidKeyError, assertDefined, assertIs, assertKey, assertLength, assertMaxLength, assertMinLength, confirmKey, getKey, lie, unsafeCast };
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"sources":["../src/index.ts","../src/assert-key.ts","../src/invalid-key-error.ts","../src/from-key.ts","../src/confirm-key.ts","../src/assert-length.ts","../src/assert-defined.ts","../src/assert-is.ts","../src/lie.ts"],"sourcesContent":["export * from \"./from-key\";\nexport * from \"./assert-key\";\nexport * from \"./confirm-key\";\nexport * from \"./invalid-key-error\";\nexport * from \"./types\";\nexport * from \"./assert-length\";\nexport * from \"./assert-defined\";\nexport * from \"./assert-is\";\nexport * from './lie'","import { AutomationError } from \"@autometa/errors\";\nimport { InvalidKeyError } from \"./invalid-key-error\";\n\nexport function AssertKey<TObj>(\n item: TObj,\n key: string | keyof TObj,\n context?: string\n): asserts key is keyof TObj {\n const prefix = context ? `${context}: ` : \"\";\n if (item === null || item === undefined) {\n throw new AutomationError(\n `${prefix}Item cannot be null or undefined if indexing for values. ${String(\n key\n )} is not a valid property of ${item}`\n );\n }\n if (!(typeof item === \"object\" || typeof item === \"function\")) {\n throw new AutomationError(\n `${prefix}A key can only be valid for a value whose type is object or function: Type ${typeof item} is not valid`\n );\n }\n if (key && typeof key == \"string\" && key in item) {\n return;\n }\n throw new InvalidKeyError(key as string, item);\n}\n","import { AutomationError } from \"@autometa/errors\";\nimport { closestMatch } from \"closest-match\";\n\nexport class InvalidKeyError<\n T\n> extends AutomationError {\n bestMatches: string | string[];\n constructor(\n readonly key: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n readonly item: T,\n readonly context?: string\n ) {\n const prefix = context ? `${context}: ` : \"\";\n const matches = closestMatch(key as string, Object.keys(item as object), true) ?? [];\n super(\n `${prefix}Key ${String(key)} does not exist on target ${item}.\n These keys are similar. Did you mean one of these?: \n ${Array.isArray(matches) ? matches.join(\"\\n\") : matches}`\n );\n this.bestMatches = matches;\n }\n}\n","import { AssertKey } from \"./assert-key\";\nexport function FromKey<TObj, TReturn>(item: TObj, key: string) {\n AssertKey(item, key);\n return item[key] as TReturn;\n}\n","export function ConfirmKey<TObj>(\n item: TObj,\n key: string | keyof TObj\n): key is keyof TObj {\n if (item === null || item === undefined) {\n return false;\n }\n if (!(typeof item === \"object\" || typeof item === \"function\")) {\n return false;\n }\n if (key && typeof key == \"string\" && key in item) {\n return true;\n }\n return false;\n}\n","import { AutomationError } from \"@autometa/errors\";\nimport { AssertKey } from \".\";\nexport function AssertLength<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number,\n context?: string\n): asserts length is TObj[\"length\"] {\n AssertKey(item, \"length\", context);\n const prefix = context ? `${context}: ` : \"\";\n if (item.length !== length) {\n throw new AutomationError(\n `${prefix}Array was expected to have length ${length} but was ${\n item.length\n }. Full Array: ${JSON.stringify(item, null, 2)}`\n );\n }\n}\nexport function ConfirmLength<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number\n): length is TObj[\"length\"] {\n AssertKey(item, \"length\");\n if (item.length !== length) {\n return false;\n }\n return true;\n}\nexport function AssertLengthAtLeast<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number,\n context?: string\n) {\n AssertKey(item, \"length\", context);\n const prefix = context ? `${context}: ` : \"\";\n if (item.length < length) {\n throw new AutomationError(\n `${prefix}Array was expected to have at least length ${length} but was ${\n item.length\n }. Full Array: ${JSON.stringify(item, null, 2)}`\n );\n }\n}\nexport function ConfirmLengthAtLeast<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number,\n) {\n AssertKey(item, \"length\");\n if (item.length < length) {\n return false;\n }\n return true;\n}\n\nexport function AssertLengthAtMost<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number,\n context?: string\n) {\n AssertKey(item, \"length\", context);\n const prefix = context ? `${context}: ` : \"\";\n if (item.length > length) {\n throw new AutomationError(\n `${prefix}Array was expected to have at least length ${length} but was ${\n item.length\n }. Full Array: ${JSON.stringify(item, null, 2)}`\n );\n }\n}\n\nexport function ConfirmLengthAtMost<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number\n) {\n AssertKey(item, \"length\");\n if (item.length > length) {\n return false;\n }\n return true;\n}\n","import { AutomationError } from \"@autometa/errors\";\n\nexport function AssertDefined<TObj>(\n item: TObj | null | undefined,\n context?: string\n): asserts item is TObj {\n const prefix = context ? `${context}: ` : \"\";\n if (item === null || item === undefined) {\n throw new AutomationError(\n `${prefix}Item was expected to be defined but was ${item}. Full Item: ${JSON.stringify(\n item,\n null,\n 2\n )}`\n );\n }\n}\nexport function ConfirmDefined<TObj>(\n item: TObj | null | undefined\n): item is TObj {\n if (item === null || item === undefined) {\n return false;\n }\n return true;\n}\n","import { AutomationError } from \"@autometa/errors\";\nexport function AssertIs<TIsType>(\n value: unknown,\n type: TIsType,\n context?: string\n): asserts value is TIsType {\n const prefix = context ? `${context}: ` : \"\";\n const message = `${prefix}Expected ${type} but got ${value}`;\n if (value !== type && typeof value !== type) {\n throw new AutomationError(message);\n }\n if (value !== type && typeof value !== typeof type) {\n throw new AutomationError(message);\n }\n if (type !== null && value === null) {\n throw new AutomationError(message);\n }\n if (type !== undefined && value === undefined) {\n throw new AutomationError(message);\n }\n if (typeof type === \"function\") {\n if (value instanceof type) {\n return;\n }\n throw new AutomationError(\n `${prefix}Expected ${type} to be instance of ${value}`\n );\n }\n}\n","export function fib<T>(value: unknown): T{\n return value as T;\n}\n\nexport function lie<T>(value: unknown): asserts value is T{\n return;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,iBAAgC;;;ACAhC,oBAAgC;AAChC,2BAA6B;AAEtB,IAAM,kBAAN,cAEG,8BAAgB;AAAA,EAExB,YACW,KAEA,MACA,SACT;AACA,UAAM,SAAS,UAAU,GAAG,OAAO,OAAO;AAC1C,UAAM,cAAU,mCAAa,KAAe,OAAO,KAAK,IAAc,GAAG,IAAI,KAAK,CAAC;AACnF;AAAA,MACE,GAAG,MAAM,OAAO,OAAO,GAAG,CAAC,6BAA6B,IAAI;AAAA;AAAA,IAE9D,MAAM,QAAQ,OAAO,IAAI,QAAQ,KAAK,IAAI,IAAI,OAAO;AAAA,IACrD;AAXS;AAEA;AACA;AAST,SAAK,cAAc;AAAA,EACrB;AACF;;;ADnBO,SAAS,UACd,MACA,KACA,SAC2B;AAC3B,QAAM,SAAS,UAAU,GAAG,OAAO,OAAO;AAC1C,MAAI,SAAS,QAAQ,SAAS,QAAW;AACvC,UAAM,IAAI;AAAA,MACR,GAAG,MAAM,4DAA4D;AAAA,QACnE;AAAA,MACF,CAAC,+BAA+B,IAAI;AAAA,IACtC;AAAA,EACF;AACA,MAAI,EAAE,OAAO,SAAS,YAAY,OAAO,SAAS,aAAa;AAC7D,UAAM,IAAI;AAAA,MACR,GAAG,MAAM,8EAA8E,OAA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U,MAAM;AACnC,UAAM,IAAI,+BAAgB,OAAO;AAAA,EACnC;AACA,MAAI,SAAS,UAAa,UAAU,QAAW;AAC7C,UAAM,IAAI,+BAAgB,OAAO;AAAA,EACnC;AACA,MAAI,OAAO,SAAS,YAAY;AAC9B,QAAI,iBAAiB,MAAM;AACzB;AAAA,IACF;AACA,UAAM,IAAI;AAAA,MACR,GAAG,MAAM,YAAY,IAAI,sBAAsB,KAAK;AAAA,IACtD;AAAA,EACF;AACF;;;AC5BO,SAAS,IAAO,OAAkB;AACrC,SAAO;AACX;AAEO,SAAS,IAAO,OAAmC;AACtD;AACJ;","names":["import_errors","import_errors","import_errors","import_errors"]}
{"version":3,"sources":["../src/assert-defined.ts","../src/assert-is.ts","../src/assert-key.ts","../src/assert-length.ts","../src/type-cast.ts"],"names":["AutomationError","getTypeName"],"mappings":";AAAA,SAAS,uBAAuB;AAiBzB,SAAS,cACd,OACA,SACiC;AACjC,MAAI,UAAU,QAAQ,UAAU,QAAW;AACzC,UAAM,SAAS,UAAU,IAAI,OAAO,OAAO;AAC3C,UAAM,IAAI;AAAA,MACR,GAAG,MAAM,yCAAyC,KAAK;AAAA,IACzD;AAAA,EACF;AACF;;;AC3BA,SAAS,mBAAAA,wBAAuB;AAyBzB,SAAS,SACd,OACA,UACA,SACoB;AACpB,QAAM,SAAS,UAAU,IAAI,OAAO,OAAO;AAG3C,MAAI,OAAO,aAAa,YAAY;AAElC,QAAI,iBAAkB,UAAuB;AAC3C;AAAA,IACF;AACA,UAAM,eAAgB,SAA+B,QAAQ;AAC7D,UAAM,IAAIA;AAAA,MACR,GAAG,MAAM,wBAAwB,YAAY,aAAa,YAAY,KAAK,CAAC;AAAA,IAC9E;AAAA,EACF;AAGA,QAAM,eAAe,OAAO;AAC5B,QAAM,aAAa,OAAO;AAE1B,MAAI,iBAAiB,YAAY;AAE/B,QAAI,UAAU,UAAa,aAAa,QAAW;AACjD;AAAA,IACF;AACA,QAAI,UAAU,QAAQ,aAAa,MAAM;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AAA,MACR,GAAG,MAAM,0BAA0BC,aAAY,KAAK,CAAC;AAAA,IACvD;AAAA,EACF;AAEA,MAAI,MAAM,SAAS,WAAW;AAC5B,UAAM,SAAS,UAAU,IAAI,OAAO,OAAO;AAC3C,UAAM,WAAW,MAAM,QAAQ,KAAK,IAAI,UAAU;AAClD,UAAM,IAAID;AAAA,MACR,GAAG,MAAM,YAAY,QAAQ,cAAc,SAAS,aAAa,MAAM,MAAM;AAAA,IAC/E;AAAA,EACF;AACF;AAEA,SAAS,UAAU,OAAqC;AACtD,SACE,UAAU,QACV,UAAU,UACV,OAAQ,MAAqB,WAAW;AAE5C;AAEA,SAASC,aAAY,OAAwB;AAC3C,MAAI,UAAU;AAAM,WAAO;AAC3B,MAAI,UAAU;AAAW,WAAO;AAChC,MAAI,MAAM,QAAQ,KAAK;AAAG,WAAO;AACjC,SAAO,OAAO;AAChB;;;AC5GO,SAAS,IAAO,OAAoC;AAE3D;AAeO,SAAS,WAAc,OAAmB;AAC/C,SAAO;AACT","sourcesContent":["import { AutomationError } from \"@autometa/errors\";\n\n/**\n * Asserts that a value is not null or undefined, narrowing its type.\n * \n * @param value - The value to check\n * @param context - Optional context for error messages\n * @throws {AutomationError} If value is null or undefined\n * \n * @example\n * ```ts\n * const maybeValue: string | undefined = getValue();\n * assertDefined(maybeValue, \"getValue\");\n * // maybeValue is now typed as string\n * console.log(maybeValue.toUpperCase());\n * ```\n */\nexport function assertDefined<T>(\n value: T,\n context?: string\n): asserts value is NonNullable<T> {\n if (value === null || value === undefined) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Expected value to be defined, but got ${value}`\n );\n }\n}\n","import { AutomationError } from \"@autometa/errors\";\n\n/**\n * Asserts that a value matches an expected type or instance.\n * \n * @param value - The value to check\n * @param expected - The expected type (string, constructor, or value)\n * @param context - Optional context for error messages\n * @throws {AutomationError} If value doesn't match expected type\n * \n * @example\n * ```ts\n * // Type checking\n * assertIs(value, \"string\");\n * assertIs(value, \"number\");\n * \n * // Instance checking\n * assertIs(error, Error);\n * assertIs(date, Date);\n * \n * // Value checking\n * assertIs(status, 200);\n * assertIs(flag, true);\n * ```\n */\nexport function assertIs<T>(\n value: unknown,\n expected: T,\n context?: string\n): asserts value is T {\n const prefix = context ? `[${context}] ` : \"\";\n \n // Handle constructor/class checks\n if (typeof expected === \"function\") {\n // eslint-disable-next-line @typescript-eslint/ban-types\n if (value instanceof (expected as Function)) {\n return;\n }\n const expectedName = (expected as { name?: string }).name ?? \"Constructor\";\n throw new AutomationError(\n `${prefix}Expected instance of ${expectedName}, but got ${getTypeName(value)}`\n );\n }\n \n // Handle primitive type checks\n const expectedType = typeof expected;\n const actualType = typeof value;\n \n if (expectedType === actualType) {\n // Special case: undefined and null can only match themselves\n if (value === undefined && expected === undefined) {\n return;\n }\n if (value === null && expected === null) {\n return;\n }\n // For primitives, check both type and value\n if (isPrimitive(expected) && value === expected) {\n return;\n }\n // For objects (not null), just check type match\n if (actualType === \"object\" && value !== null) {\n return;\n }\n }\n \n // Handle typeof string checks (e.g., \"string\", \"number\")\n if (typeof expected === \"string\" && actualType === expected) {\n return;\n }\n \n throw new AutomationError(\n `${prefix}Expected ${formatExpected(expected)}, but got ${formatActual(value)}`\n );\n}\n\nfunction isPrimitive(value: unknown): value is string | number | boolean | symbol | bigint {\n const type = typeof value;\n return type === \"string\" || type === \"number\" || type === \"boolean\" || \n type === \"symbol\" || type === \"bigint\";\n}\n\nfunction getTypeName(value: unknown): string {\n if (value === null) return \"null\";\n if (value === undefined) return \"undefined\";\n if (typeof value === \"object\") {\n return value.constructor?.name ?? \"Object\";\n }\n return typeof value;\n}\n\nfunction formatExpected(expected: unknown): string {\n if (typeof expected === \"string\") {\n return `type \"${expected}\"`;\n }\n return `value ${JSON.stringify(expected)}`;\n}\n\nfunction formatActual(value: unknown): string {\n const type = getTypeName(value);\n if (value === null || value === undefined) {\n return type;\n }\n try {\n const str = JSON.stringify(value);\n return `${type} ${str}`;\n } catch {\n return type;\n }\n}\n","import { AutomationError } from \"@autometa/errors\";\nimport { closestMatch } from \"closest-match\";\n\n/**\n * Custom error for invalid object key access with helpful suggestions.\n */\nexport class InvalidKeyError<T extends object> extends AutomationError {\n constructor(\n public readonly key: string,\n public readonly target: T,\n public readonly suggestions: string[],\n context?: string\n ) {\n const prefix = context ? `[${context}] ` : \"\";\n const targetType = target.constructor?.name ?? \"Object\";\n const suggestionText = suggestions.length > 0\n ? `\\n\\nDid you mean one of these?\\n ${suggestions.join(\"\\n \")}`\n : \"\";\n \n super(\n `${prefix}Key \"${key}\" does not exist on ${targetType}.${suggestionText}`\n );\n }\n}\n\n/**\n * Asserts that a key exists on an object, narrowing the key type.\n * \n * @param target - The object to check\n * @param key - The key to verify\n * @param context - Optional context for error messages\n * @throws {InvalidKeyError} If key doesn't exist on target\n * \n * @example\n * ```ts\n * const config: Record<string, unknown> = getConfig();\n * assertKey(config, \"apiKey\");\n * // \"apiKey\" is now typed as keyof typeof config\n * const key = config.apiKey;\n * ```\n */\nexport function assertKey<T extends object>(\n target: T,\n key: unknown,\n context?: string\n): asserts key is keyof T {\n // Validate target\n if (target === null || target === undefined) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Cannot check key \"${String(key)}\" on ${target}`\n );\n }\n \n // Validate key type\n if (typeof key !== \"string\" && typeof key !== \"symbol\" && typeof key !== \"number\") {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Key must be string, symbol, or number, got ${typeof key}`\n );\n }\n \n // Check if key exists\n if (key in target) {\n return;\n }\n \n // Generate helpful suggestions for string keys\n const suggestions = typeof key === \"string\"\n ? findSimilarKeys(key, target)\n : [];\n \n throw new InvalidKeyError(String(key), target, suggestions, context);\n}\n\n/**\n * Type guard to check if a key exists on an object.\n * Non-throwing version of assertKey.\n * \n * @param target - The object to check\n * @param key - The key to verify\n * @returns true if key exists on target\n * \n * @example\n * ```ts\n * if (confirmKey(config, \"apiKey\")) {\n * // TypeScript knows apiKey exists\n * console.log(config.apiKey);\n * }\n * ```\n */\nexport function confirmKey<T extends object>(\n target: T,\n key: unknown\n): key is keyof T {\n if (target === null || target === undefined) {\n return false;\n }\n if (typeof key !== \"string\" && typeof key !== \"symbol\" && typeof key !== \"number\") {\n return false;\n }\n return key in target;\n}\n\n/**\n * Safely retrieves a value from an object, asserting the key exists.\n * \n * @param target - The object to access\n * @param key - The key to retrieve\n * @param context - Optional context for error messages\n * @returns The value at the key\n * @throws {InvalidKeyError} If key doesn't exist on target\n * \n * @example\n * ```ts\n * const value = getKey(config, \"apiKey\");\n * // TypeScript knows the value type from the object\n * ```\n */\nexport function getKey<T extends object, K extends keyof T>(\n target: T,\n key: K,\n context?: string\n): T[K];\nexport function getKey<T extends object>(\n target: T,\n key: string,\n context?: string\n): unknown;\nexport function getKey<T extends object>(\n target: T,\n key: string,\n context?: string\n): unknown {\n assertKey(target, key, context);\n return target[key as keyof T];\n}\n\n/**\n * Finds similar keys in an object for helpful error messages.\n */\nfunction findSimilarKeys<T extends object>(searchKey: string, target: T): string[] {\n const keys = Object.keys(target);\n if (keys.length === 0) {\n return [];\n }\n \n const matches = closestMatch(searchKey, keys, true);\n if (!matches) {\n return [];\n }\n \n return Array.isArray(matches) ? matches.slice(0, 3) : [matches];\n}\n","import { AutomationError } from \"@autometa/errors\";\n\ntype LengthLike = { length: number };\n\n/**\n * Asserts that an array or string has an exact length.\n * \n * @param value - Array or string to check\n * @param expectedLength - Expected length\n * @param context - Optional context for error messages\n * @throws {AutomationError} If length doesn't match\n * \n * @example\n * ```ts\n * assertLength(args, 3, \"function arguments\");\n * assertLength(username, 5, \"username\");\n * ```\n */\nexport function assertLength<T extends LengthLike>(\n value: T,\n expectedLength: number,\n context?: string\n): asserts value is T & { length: typeof expectedLength } {\n if (!hasLength(value)) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Cannot check length on ${getTypeName(value)}`\n );\n }\n \n if (value.length !== expectedLength) {\n const prefix = context ? `[${context}] ` : \"\";\n const typeName = Array.isArray(value) ? \"array\" : \"string\";\n throw new AutomationError(\n `${prefix}Expected ${typeName} length ${expectedLength}, but got ${value.length}`\n );\n }\n}\n\n/**\n * Asserts that an array or string has at least a minimum length.\n * \n * @param value - Array or string to check\n * @param minLength - Minimum required length\n * @param context - Optional context for error messages\n * @throws {AutomationError} If length is too short\n * \n * @example\n * ```ts\n * assertMinLength(password, 8, \"password\");\n * assertMinLength(items, 1, \"cart items\");\n * ```\n */\nexport function assertMinLength<T extends LengthLike>(\n value: T,\n minLength: number,\n context?: string\n): void {\n if (!hasLength(value)) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Cannot check length on ${getTypeName(value)}`\n );\n }\n \n if (value.length < minLength) {\n const prefix = context ? `[${context}] ` : \"\";\n const typeName = Array.isArray(value) ? \"array\" : \"string\";\n throw new AutomationError(\n `${prefix}Expected ${typeName} length >= ${minLength}, but got ${value.length}`\n );\n }\n}\n\n/**\n * Asserts that an array or string has at most a maximum length.\n * \n * @param value - Array or string to check\n * @param maxLength - Maximum allowed length\n * @param context - Optional context for error messages\n * @throws {AutomationError} If length is too long\n * \n * @example\n * ```ts\n * assertMaxLength(description, 280, \"tweet\");\n * assertMaxLength(items, 10, \"cart limit\");\n * ```\n */\nexport function assertMaxLength<T extends LengthLike>(\n value: T,\n maxLength: number,\n context?: string\n): void {\n if (!hasLength(value)) {\n const prefix = context ? `[${context}] ` : \"\";\n throw new AutomationError(\n `${prefix}Cannot check length on ${getTypeName(value)}`\n );\n }\n \n if (value.length > maxLength) {\n const prefix = context ? `[${context}] ` : \"\";\n const typeName = Array.isArray(value) ? \"array\" : \"string\";\n throw new AutomationError(\n `${prefix}Expected ${typeName} length <= ${maxLength}, but got ${value.length}`\n );\n }\n}\n\nfunction hasLength(value: unknown): value is LengthLike {\n return (\n value !== null &&\n value !== undefined &&\n typeof (value as LengthLike).length === \"number\"\n );\n}\n\nfunction getTypeName(value: unknown): string {\n if (value === null) return \"null\";\n if (value === undefined) return \"undefined\";\n if (Array.isArray(value)) return \"array\";\n return typeof value;\n}\n","/**\n * Type assertion function that lies to TypeScript about a value's type.\n * Use with caution - no runtime checks are performed.\n * \n * @param value - Value to cast\n * @returns The same value, typed as T\n * \n * @example\n * ```ts\n * const data: unknown = JSON.parse(response);\n * lie<User>(data);\n * // TypeScript now thinks data is User, but no validation occurred!\n * ```\n */\nexport function lie<T>(value: unknown): asserts value is T {\n // Intentionally empty - this is a type assertion only\n}\n\n/**\n * Unsafely casts a value to a different type without runtime checks.\n * Prefer using proper type guards and assertions when possible.\n * \n * @param value - Value to cast\n * @returns The same value, typed as T\n * \n * @example\n * ```ts\n * const config = unsafeCast<Config>(rawData);\n * // Use with caution - no validation!\n * ```\n */\nexport function unsafeCast<T>(value: unknown): T {\n return value as T;\n}\n"]}
{
"name": "@autometa/asserters",
"version": "0.1.8",
"main": "dist/index.js",
"module": "dist/esm/index.js",
"version": "1.0.0-rc.0",
"description": "Type-safe assertion utilities for Autometa",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"files": [
"dist"
],
"exports": {
"import": "./dist/esm/index.js",
"require": "./dist/index.js",
"default": "./dist/esm/index.js",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js",
"types": "./dist/index.d.ts"

@@ -17,34 +21,35 @@ },

"devDependencies": {
"@autometa/types": "0.4.1",
"@cucumber/cucumber-expressions": "^16.1.2",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"@vitest/coverage-istanbul": "^0.31.0",
"eslint": "^8.37.0",
"eslint-config-custom": "0.6.0",
"eslint-config-prettier": "^8.3.0",
"istanbul": "^0.4.5",
"prettier": "^2.8.3",
"reflect-metadata": "^0.1.13",
"rimraf": "^4.1.2",
"tsconfig": " *",
"tsup": "^7.2.0",
"typescript": "^4.9.5",
"vitest": "0.34.6"
"vitest": "1.4.0",
"eslint-config-custom": "0.6.0",
"tsconfig": "0.7.0",
"tsup-config": "0.1.0"
},
"dependencies": {
"@autometa/errors": "^0.2.2",
"closest-match": "^1.3.3"
"closest-match": "^1.3.3",
"@autometa/errors": "^1.0.0-rc.0"
},
"scripts": {
"test": "vitest run",
"coverage": "vitest run --coverage",
"prettify": "prettier --config .prettierrc 'src/**/*.ts' --write",
"type-check": "tsc --noEmit -p tsconfig.dev.json",
"type-check:watch": "tsc --noEmit --watch -p tsconfig.dev.json",
"build": "tsup && pnpm run build:types",
"build:types": "rimraf tsconfig.types.tsbuildinfo && tsc --build tsconfig.types.json",
"build:watch": "tsup --watch",
"dev": "tsup --watch",
"test": "vitest run --passWithNoTests",
"test:watch": "vitest --passWithNoTests",
"test:ui": "vitest --ui --passWithNoTests",
"coverage": "vitest run --coverage --passWithNoTests",
"lint": "eslint . --max-warnings 0",
"lint:fix": "eslint . --fix",
"clean": "rimraf dist",
"build": "tsup"
},
"readme": "# Errors\n\nErrors & Exceptions implementation for @autometa.\n"
"prettify": "prettier --config .prettierrc 'src/**/*.ts' --write",
"clean": "rimraf dist"
}
}
+150
-2

@@ -1,3 +0,151 @@

# Errors
# @autometa/asserters
Errors & Exceptions implementation for @autometa.
Type-safe runtime assertions for the Autometa ecosystem.
## Features
- **Type narrowing** - Assertions narrow TypeScript types automatically
- **Clear error messages** - Actionable errors with context
- **Helpful suggestions** - Get similar key names when accessing invalid properties
- **Composable utilities** - Small, focused functions that work together
- **Zero dependencies** - Except `@autometa/errors` and `closest-match`
## Installation
\`\`\`bash
npm install @autometa/asserters
# or
pnpm add @autometa/asserters
\`\`\`
## API
### assertDefined
Asserts a value is not \`null\` or \`undefined\`.
\`\`\`typescript
import { assertDefined } from "@autometa/asserters";
const maybeValue: string | undefined = getValue();
assertDefined(maybeValue, "user.name");
// maybeValue is now typed as string
console.log(maybeValue.toUpperCase());
\`\`\`
### assertKey / confirmKey / getKey
Work with object keys safely.
\`\`\`typescript
import { assertKey, confirmKey, getKey } from "@autometa/asserters";
const config: Record<string, unknown> = getConfig();
// Assert key exists (throws on failure)
assertKey(config, "apiKey");
const key = config.apiKey; // TypeScript knows this is safe
// Check if key exists (type guard)
if (confirmKey(config, "optionalKey")) {
console.log(config.optionalKey);
}
// Get key value (throws on failure with helpful suggestions)
const apiKey = getKey(config, "apiKey");
\`\`\`
When accessing an invalid key, you get helpful suggestions:
\`\`\`typescript
assertKey(config, "firstname"); // Did you mean: firstName?
\`\`\`
### assertLength / assertMinLength / assertMaxLength
Assert array or string length constraints.
\`\`\`typescript
import { assertLength, assertMinLength, assertMaxLength } from "@autometa/asserters";
// Exact length
assertLength(args, 3, "function arguments");
// Minimum length
assertMinLength(password, 8, "password");
// Maximum length
assertMaxLength(description, 280, "tweet");
\`\`\`
### assertIs
Assert type or instance checks.
\`\`\`typescript
import { assertIs } from "@autometa/asserters";
// Type checking
assertIs(value, "string");
assertIs(count, "number");
// Instance checking
assertIs(error, Error);
assertIs(date, Date);
// Value checking
assertIs(status, 200);
assertIs(flag, true);
\`\`\`
### lie / unsafeCast
**Use with extreme caution--filter @autometa/asserters type-check* These bypass runtime checks.
\`\`\`typescript
import { lie, unsafeCast } from "@autometa/asserters";
// Type assertion (no runtime effect)
const data: unknown = getData();
lie<User>(data);
// TypeScript thinks data is User, but no validation occurred!
// Unsafe cast (returns typed value)
const config = unsafeCast<Config>(rawData);
\`\`\`
## Error Handling
All assertions throw \`AutomationError\` from \`@autometa/errors\`:
\`\`\`typescript
import { assertKey, InvalidKeyError } from "@autometa/asserters";
try {
assertKey(config, "missing");
} catch (error) {
if (error instanceof InvalidKeyError) {
console.log(error.suggestions); // ["missng", "mising"]
}
}
\`\`\`
## Best Practices
1. **Always provide context** - Makes debugging easier
\`\`\`typescript
assertDefined(value, "config.database.host");
\`\`\`
2. **Use type guards for optional checks** - Prefer \`confirmKey\` over try/catch
\`\`\`typescript
if (confirmKey(obj, "optionalProp")) {
// Use obj.optionalProp
}
\`\`\`
3. **Avoid unsafe casts** - Use proper validation instead of \`lie\`/\`unsafeCast\`
## License
MIT

Sorry, the diff of this file is not supported yet

module.exports = {
root: true,
extends: ["custom"],
};
# Gherkin
## 0.1.8
### Patch Changes
- Updated dependencies [3fe2ad4]
- @autometa/errors@0.2.2
## 0.1.7
### Patch Changes
- Updated dependencies [3493bb6]
- @autometa/errors@0.2.1
## 0.1.6
### Patch Changes
- 0c070cb: fix: export 'lie'
## 0.1.5
### Patch Changes
- Updated dependencies [b5ce008]
- @autometa/errors@0.2.0
## 0.1.4
### Patch Changes
- 04ed85d: feat: added HTP client based on axios
- Updated dependencies [04ed85d]
- @autometa/errors@0.1.4
## 0.1.3
### Patch Changes
- 53f958e1: Fix: steps not executing onStepEnded event when an error was thrown
- Updated dependencies [53f958e1]
- @autometa/errors@0.1.3
## 0.1.2
### Patch Changes
- Updated dependencies [12bd4b1e]
- @autometa/errors@0.1.2
## 0.1.1
### Patch Changes
- Updated dependencies [e8f02f3a]
- @autometa/errors@0.1.1
## 0.1.0
### Minor Changes
- 554b77e: Releasing packages
### Patch Changes
- Updated dependencies [554b77e]
- @autometa/errors@0.1.0
## 0.3.1
### Patch Changes
- 6a4a9ac: Swapped project type to "composite", unified build system for most projects
## 0.3.0
### Minor Changes
- b48f577: Added initial implemention of scopes and updated `overloads`
## 0.2.1
### Patch Changes
- cfc35f4: Update build systems on packages to use tsup
## 0.2.0
### Minor Changes
- 0a27508: Created "gherkin" package to help split up cucumber-runner
// src/assert-key.ts
import { AutomationError as AutomationError2 } from "@autometa/errors";
// src/invalid-key-error.ts
import { AutomationError } from "@autometa/errors";
import { closestMatch } from "closest-match";
var InvalidKeyError = class extends AutomationError {
constructor(key, item, context) {
const prefix = context ? `${context}: ` : "";
const matches = closestMatch(key, Object.keys(item), true) ?? [];
super(
`${prefix}Key ${String(key)} does not exist on target ${item}.
These keys are similar. Did you mean one of these?:
${Array.isArray(matches) ? matches.join("\n") : matches}`
);
this.key = key;
this.item = item;
this.context = context;
this.bestMatches = matches;
}
};
// src/assert-key.ts
function AssertKey(item, key, context) {
const prefix = context ? `${context}: ` : "";
if (item === null || item === void 0) {
throw new AutomationError2(
`${prefix}Item cannot be null or undefined if indexing for values. ${String(
key
)} is not a valid property of ${item}`
);
}
if (!(typeof item === "object" || typeof item === "function")) {
throw new AutomationError2(
`${prefix}A key can only be valid for a value whose type is object or function: Type ${typeof item} is not valid`
);
}
if (key && typeof key == "string" && key in item) {
return;
}
throw new InvalidKeyError(key, item);
}
// src/from-key.ts
function FromKey(item, key) {
AssertKey(item, key);
return item[key];
}
// src/confirm-key.ts
function ConfirmKey(item, key) {
if (item === null || item === void 0) {
return false;
}
if (!(typeof item === "object" || typeof item === "function")) {
return false;
}
if (key && typeof key == "string" && key in item) {
return true;
}
return false;
}
// src/assert-length.ts
import { AutomationError as AutomationError3 } from "@autometa/errors";
function AssertLength(item, length, context) {
AssertKey(item, "length", context);
const prefix = context ? `${context}: ` : "";
if (item.length !== length) {
throw new AutomationError3(
`${prefix}Array was expected to have length ${length} but was ${item.length}. Full Array: ${JSON.stringify(item, null, 2)}`
);
}
}
function ConfirmLength(item, length) {
AssertKey(item, "length");
if (item.length !== length) {
return false;
}
return true;
}
function AssertLengthAtLeast(item, length, context) {
AssertKey(item, "length", context);
const prefix = context ? `${context}: ` : "";
if (item.length < length) {
throw new AutomationError3(
`${prefix}Array was expected to have at least length ${length} but was ${item.length}. Full Array: ${JSON.stringify(item, null, 2)}`
);
}
}
function ConfirmLengthAtLeast(item, length) {
AssertKey(item, "length");
if (item.length < length) {
return false;
}
return true;
}
function AssertLengthAtMost(item, length, context) {
AssertKey(item, "length", context);
const prefix = context ? `${context}: ` : "";
if (item.length > length) {
throw new AutomationError3(
`${prefix}Array was expected to have at least length ${length} but was ${item.length}. Full Array: ${JSON.stringify(item, null, 2)}`
);
}
}
function ConfirmLengthAtMost(item, length) {
AssertKey(item, "length");
if (item.length > length) {
return false;
}
return true;
}
// src/assert-defined.ts
import { AutomationError as AutomationError4 } from "@autometa/errors";
function AssertDefined(item, context) {
const prefix = context ? `${context}: ` : "";
if (item === null || item === void 0) {
throw new AutomationError4(
`${prefix}Item was expected to be defined but was ${item}. Full Item: ${JSON.stringify(
item,
null,
2
)}`
);
}
}
function ConfirmDefined(item) {
if (item === null || item === void 0) {
return false;
}
return true;
}
// src/assert-is.ts
import { AutomationError as AutomationError5 } from "@autometa/errors";
function AssertIs(value, type, context) {
const prefix = context ? `${context}: ` : "";
const message = `${prefix}Expected ${type} but got ${value}`;
if (value !== type && typeof value !== type) {
throw new AutomationError5(message);
}
if (value !== type && typeof value !== typeof type) {
throw new AutomationError5(message);
}
if (type !== null && value === null) {
throw new AutomationError5(message);
}
if (type !== void 0 && value === void 0) {
throw new AutomationError5(message);
}
if (typeof type === "function") {
if (value instanceof type) {
return;
}
throw new AutomationError5(
`${prefix}Expected ${type} to be instance of ${value}`
);
}
}
// src/lie.ts
function fib(value) {
return value;
}
function lie(value) {
return;
}
export {
AssertDefined,
AssertIs,
AssertKey,
AssertLength,
AssertLengthAtLeast,
AssertLengthAtMost,
ConfirmDefined,
ConfirmKey,
ConfirmLength,
ConfirmLengthAtLeast,
ConfirmLengthAtMost,
FromKey,
InvalidKeyError,
fib,
lie
};
//# sourceMappingURL=index.js.map
{"version":3,"sources":["../../src/assert-key.ts","../../src/invalid-key-error.ts","../../src/from-key.ts","../../src/confirm-key.ts","../../src/assert-length.ts","../../src/assert-defined.ts","../../src/assert-is.ts","../../src/lie.ts"],"sourcesContent":["import { AutomationError } from \"@autometa/errors\";\nimport { InvalidKeyError } from \"./invalid-key-error\";\n\nexport function AssertKey<TObj>(\n item: TObj,\n key: string | keyof TObj,\n context?: string\n): asserts key is keyof TObj {\n const prefix = context ? `${context}: ` : \"\";\n if (item === null || item === undefined) {\n throw new AutomationError(\n `${prefix}Item cannot be null or undefined if indexing for values. ${String(\n key\n )} is not a valid property of ${item}`\n );\n }\n if (!(typeof item === \"object\" || typeof item === \"function\")) {\n throw new AutomationError(\n `${prefix}A key can only be valid for a value whose type is object or function: Type ${typeof item} is not valid`\n );\n }\n if (key && typeof key == \"string\" && key in item) {\n return;\n }\n throw new InvalidKeyError(key as string, item);\n}\n","import { AutomationError } from \"@autometa/errors\";\nimport { closestMatch } from \"closest-match\";\n\nexport class InvalidKeyError<\n T\n> extends AutomationError {\n bestMatches: string | string[];\n constructor(\n readonly key: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n readonly item: T,\n readonly context?: string\n ) {\n const prefix = context ? `${context}: ` : \"\";\n const matches = closestMatch(key as string, Object.keys(item as object), true) ?? [];\n super(\n `${prefix}Key ${String(key)} does not exist on target ${item}.\n These keys are similar. Did you mean one of these?: \n ${Array.isArray(matches) ? matches.join(\"\\n\") : matches}`\n );\n this.bestMatches = matches;\n }\n}\n","import { AssertKey } from \"./assert-key\";\nexport function FromKey<TObj, TReturn>(item: TObj, key: string) {\n AssertKey(item, key);\n return item[key] as TReturn;\n}\n","export function ConfirmKey<TObj>(\n item: TObj,\n key: string | keyof TObj\n): key is keyof TObj {\n if (item === null || item === undefined) {\n return false;\n }\n if (!(typeof item === \"object\" || typeof item === \"function\")) {\n return false;\n }\n if (key && typeof key == \"string\" && key in item) {\n return true;\n }\n return false;\n}\n","import { AutomationError } from \"@autometa/errors\";\nimport { AssertKey } from \".\";\nexport function AssertLength<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number,\n context?: string\n): asserts length is TObj[\"length\"] {\n AssertKey(item, \"length\", context);\n const prefix = context ? `${context}: ` : \"\";\n if (item.length !== length) {\n throw new AutomationError(\n `${prefix}Array was expected to have length ${length} but was ${\n item.length\n }. Full Array: ${JSON.stringify(item, null, 2)}`\n );\n }\n}\nexport function ConfirmLength<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number\n): length is TObj[\"length\"] {\n AssertKey(item, \"length\");\n if (item.length !== length) {\n return false;\n }\n return true;\n}\nexport function AssertLengthAtLeast<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number,\n context?: string\n) {\n AssertKey(item, \"length\", context);\n const prefix = context ? `${context}: ` : \"\";\n if (item.length < length) {\n throw new AutomationError(\n `${prefix}Array was expected to have at least length ${length} but was ${\n item.length\n }. Full Array: ${JSON.stringify(item, null, 2)}`\n );\n }\n}\nexport function ConfirmLengthAtLeast<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number,\n) {\n AssertKey(item, \"length\");\n if (item.length < length) {\n return false;\n }\n return true;\n}\n\nexport function AssertLengthAtMost<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number,\n context?: string\n) {\n AssertKey(item, \"length\", context);\n const prefix = context ? `${context}: ` : \"\";\n if (item.length > length) {\n throw new AutomationError(\n `${prefix}Array was expected to have at least length ${length} but was ${\n item.length\n }. Full Array: ${JSON.stringify(item, null, 2)}`\n );\n }\n}\n\nexport function ConfirmLengthAtMost<TObj extends Array<unknown> | string>(\n item: TObj,\n length: number\n) {\n AssertKey(item, \"length\");\n if (item.length > length) {\n return false;\n }\n return true;\n}\n","import { AutomationError } from \"@autometa/errors\";\n\nexport function AssertDefined<TObj>(\n item: TObj | null | undefined,\n context?: string\n): asserts item is TObj {\n const prefix = context ? `${context}: ` : \"\";\n if (item === null || item === undefined) {\n throw new AutomationError(\n `${prefix}Item was expected to be defined but was ${item}. Full Item: ${JSON.stringify(\n item,\n null,\n 2\n )}`\n );\n }\n}\nexport function ConfirmDefined<TObj>(\n item: TObj | null | undefined\n): item is TObj {\n if (item === null || item === undefined) {\n return false;\n }\n return true;\n}\n","import { AutomationError } from \"@autometa/errors\";\nexport function AssertIs<TIsType>(\n value: unknown,\n type: TIsType,\n context?: string\n): asserts value is TIsType {\n const prefix = context ? `${context}: ` : \"\";\n const message = `${prefix}Expected ${type} but got ${value}`;\n if (value !== type && typeof value !== type) {\n throw new AutomationError(message);\n }\n if (value !== type && typeof value !== typeof type) {\n throw new AutomationError(message);\n }\n if (type !== null && value === null) {\n throw new AutomationError(message);\n }\n if (type !== undefined && value === undefined) {\n throw new AutomationError(message);\n }\n if (typeof type === \"function\") {\n if (value instanceof type) {\n return;\n }\n throw new AutomationError(\n `${prefix}Expected ${type} to be instance of ${value}`\n );\n }\n}\n","export function fib<T>(value: unknown): T{\n return value as T;\n}\n\nexport function lie<T>(value: unknown): asserts value is T{\n return;\n}"],"mappings":";AAAA,SAAS,mBAAAA,wBAAuB;;;ACAhC,SAAS,uBAAuB;AAChC,SAAS,oBAAoB;AAEtB,IAAM,kBAAN,cAEG,gBAAgB;AAAA,EAExB,YACW,KAEA,MACA,SACT;AACA,UAAM,SAAS,UAAU,GAAG,OAAO,OAAO;AAC1C,UAAM,UAAU,aAAa,KAAe,OAAO,KAAK,IAAc,GAAG,IAAI,KAAK,CAAC;AACnF;AAAA,MACE,GAAG,MAAM,OAAO,OAAO,GAAG,CAAC,6BAA6B,IAAI;AAAA;AAAA,IAE9D,MAAM,QAAQ,OAAO,IAAI,QAAQ,KAAK,IAAI,IAAI,OAAO;AAAA,IACrD;AAXS;AAEA;AACA;AAST,SAAK,cAAc;AAAA,EACrB;AACF;;;ADnBO,SAAS,UACd,MACA,KACA,SAC2B;AAC3B,QAAM,SAAS,UAAU,GAAG,OAAO,OAAO;AAC1C,MAAI,SAAS,QAAQ,SAAS,QAAW;AACvC,UAAM,IAAIC;AAAA,MACR,GAAG,MAAM,4DAA4D;AAAA,QACnE;AAAA,MACF,CAAC,+BAA+B,IAAI;AAAA,IACtC;AAAA,EACF;AACA,MAAI,EAAE,OAAO,SAAS,YAAY,OAAO,SAAS,aAAa;AAC7D,UAAM,IAAIA;AAAA,MACR,GAAG,MAAM,8EAA8E,OAAO,IAAI;AAAA,IACpG;AAAA,EACF;AACA,MAAI,OAAO,OAAO,OAAO,YAAY,OAAO,MAAM;AAChD;AAAA,EACF;AACA,QAAM,IAAI,gBAAgB,KAAe,IAAI;AAC/C;;;AExBO,SAAS,QAAuB,MAAY,KAAa;AAC9D,YAAU,MAAM,GAAG;AACnB,SAAO,KAAK,GAAG;AACjB;;;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,IACF;AACA,UAAM,IAAIA;AAAA,MACR,GAAG,MAAM,YAAY,IAAI,sBAAsB,KAAK;AAAA,IACtD;AAAA,EACF;AACF;;;AC5BO,SAAS,IAAO,OAAkB;AACrC,SAAO;AACX;AAEO,SAAS,IAAO,OAAmC;AACtD;AACJ;","names":["AutomationError","AutomationError","AutomationError","AutomationError","AutomationError","AutomationError"]}
import { AutomationError } from '@autometa/errors';
declare function FromKey<TObj, TReturn>(item: TObj, key: string): TReturn;
declare function AssertKey<TObj>(item: TObj, key: string | keyof TObj, context?: string): asserts key is keyof TObj;
declare function ConfirmKey<TObj>(item: TObj, key: string | keyof TObj): key is keyof TObj;
declare class InvalidKeyError<T> extends AutomationError {
readonly key: string;
readonly item: T;
readonly context?: string | undefined;
bestMatches: string | string[];
constructor(key: string, item: T, context?: string | undefined);
}
type ExtractLiteralFromObject<TObj extends Record<string, unknown>, TString extends string> = TObj[TString] extends infer T ? T : never;
declare function AssertLength<TObj extends Array<unknown> | string>(item: TObj, length: number, context?: string): asserts length is TObj["length"];
declare function ConfirmLength<TObj extends Array<unknown> | string>(item: TObj, length: number): length is TObj["length"];
declare function AssertLengthAtLeast<TObj extends Array<unknown> | string>(item: TObj, length: number, context?: string): void;
declare function ConfirmLengthAtLeast<TObj extends Array<unknown> | string>(item: TObj, length: number): boolean;
declare function AssertLengthAtMost<TObj extends Array<unknown> | string>(item: TObj, length: number, context?: string): void;
declare function ConfirmLengthAtMost<TObj extends Array<unknown> | string>(item: TObj, length: number): boolean;
declare function AssertDefined<TObj>(item: TObj | null | undefined, context?: string): asserts item is TObj;
declare function ConfirmDefined<TObj>(item: TObj | null | undefined): item is TObj;
declare function AssertIs<TIsType>(value: unknown, type: TIsType, context?: string): asserts value is TIsType;
declare function fib<T>(value: unknown): T;
declare function lie<T>(value: unknown): asserts value is T;
export { AssertDefined, AssertIs, AssertKey, AssertLength, AssertLengthAtLeast, AssertLengthAtMost, ConfirmDefined, ConfirmKey, ConfirmLength, ConfirmLengthAtLeast, ConfirmLengthAtMost, ExtractLiteralFromObject, FromKey, InvalidKeyError, fib, lie };
import { defineConfig } from "tsup";
export default defineConfig({
clean: true, // clean up the dist folder
dts: true, // generate dts files
sourcemap:true, // generate sourcemaps
format: ["cjs", "esm"], // generate cjs and esm files
skipNodeModulesBundle: true,
entryPoints: ["src/index.ts"],
target: "es2020",
outDir: "dist",
legacyOutput: true,
external: ["dist"],
});