guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
52 lines (51 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isMap = void 0;
const generateTypeGuardError_1 = require("./generateTypeGuardError");
/**
* Checks if a value is a Map object.
*
* This type guard validates that a value is a Map instance, which is available
* in all modern JavaScript environments.
*
* @param value - The value to check
* @param config - Optional configuration for error handling
* @returns True if the value is a Map object, false otherwise
*
* @example
* ```typescript
* import { isMap } from 'guardz';
*
* console.log(isMap(new Map())); // true
* console.log(isMap(new Map([['key', 'value']]))); // true
* console.log(isMap({})); // false (plain object)
* console.log(isMap([])); // false (array)
* console.log(isMap('not a map')); // false
*
* // With type narrowing
* const data: unknown = getData();
* if (isMap(data)) {
* // data is now typed as Map<unknown, unknown>
* console.log('Map size:', data.size);
* data.set('newKey', 'newValue');
* }
*
* // With error handling
* const cache: unknown = getCache();
* if (!isMap(cache, { identifier: 'userCache' })) {
* console.error('Invalid cache data structure');
* return;
* }
* // cache is now typed as Map<unknown, unknown>
* ```
*/
const isMap = function (value, config) {
if (!(value instanceof Map)) {
if (config) {
config.callbackOnError((0, generateTypeGuardError_1.generateTypeGuardError)(value, config.identifier, 'Map'));
}
return false;
}
return true;
};
exports.isMap = isMap;