UNPKG

@donation-alerts/common

Version:

Common utils and types that are used in other @donation-alerts packages.

39 lines (38 loc) 1.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractUserId = extractUserId; exports.extractUserName = extractUserName; const NUMERIC_STRING_REGEX = /^\d+$/u; /** * Extracts the user ID from an argument that is possibly an object containing that ID. * * @param user The user ID or object. */ function extractUserId(user) { if (typeof user === 'number') { return user; } else if (typeof user === 'string') { if (NUMERIC_STRING_REGEX.test(user)) { return parseInt(user, 10); } throw new TypeError('User ID must be integer or numeric string'); } else { if (typeof user.id === 'number') { return user.id; } if (NUMERIC_STRING_REGEX.test(user.id)) { return parseInt(user.id, 10); } throw new TypeError('User ID must be integer or numeric string'); } } /** * Extracts the username from an argument that is possibly an object containing that name. * * @param user The username or object containing `name` field with username. */ function extractUserName(user) { return typeof user === 'string' ? user : user.name; }