UNPKG

emvco-qr-sdk

Version:

A robust TypeScript SDK for decoding, validating, and processing EMVCo-compliant QR codes used in digital payment systems.

58 lines (57 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NamedFieldsMapper = void 0; const regions_constant_1 = require("../constants/regions.constant"); const tag_mapping_registry_strategy_1 = require("../strategies/registry/tag-mapping-registry.strategy"); const tlv_mapping_util_1 = require("../utils/mappers/tlv-mapping.util"); const tlv_util_1 = require("../utils/resolvers/tlv.util"); /** * Provides a static mapping function to transform TLV entities into * human-readable key-value objects using tag names and mapping strategies. * * This mapper supports region-specific logic (e.g., Colombia, Global), * and resolves tag/subtag names into meaningful labels. */ class NamedFieldsMapper { /** * Maps an array of TLV entities into a flat object with named keys. * For each TLV entity, it determines if a mapping strategy exists * for the region and applies it, otherwise it attempts generic subtag mapping * or uses the raw value. * * @param tlvs - List of TLV entities to be mapped. * @param resolveTagName - Function that resolves tag IDs to human-readable keys. * @param resolveSubtagName - (Optional) Function that resolves subtag IDs to keys (used if subtags are present). * @returns An object with resolved keys and values for each TLV. */ static map(tlvs, resolveTagName, resolveSubtagName) { var _a; const result = {}; const region = this.resolveRegion(resolveTagName); for (const tlv of tlvs) { const key = resolveTagName(tlv.tag); const strategy = (_a = tag_mapping_registry_strategy_1.TagMappingRegistryStrategy[tlv.tag]) === null || _a === void 0 ? void 0 : _a[region]; result[key] = strategy ? strategy(tlv) : (0, tlv_mapping_util_1.isTlvWithSubTags)(tlv) ? (0, tlv_mapping_util_1.mapSubTags)(tlv.tag, tlv.subTags, resolveSubtagName) : tlv.value; } return result; } /** * Determines the current region (CO, GLOBAL, or DEFAULT) based on * the resolver function provided. This affects how tags are mapped. * * @param resolveTagName - Resolver used to identify tag names. * @returns The matching region code. */ static resolveRegion(resolveTagName) { return resolveTagName === tlv_util_1.TLVUtil.getColombianName ? regions_constant_1.REGIONS.CO : resolveTagName === tlv_util_1.TLVUtil.getStandardName ? regions_constant_1.REGIONS.GLOBAL : regions_constant_1.REGIONS.DEFAULT; } } exports.NamedFieldsMapper = NamedFieldsMapper;