UNPKG

magicbell

Version:
78 lines 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasOwn = void 0; exports.isString = isString; exports.isBoolean = isBoolean; exports.isArray = isArray; exports.isStringArray = isStringArray; exports.isNumber = isNumber; exports.isObject = isObject; exports.compact = compact; exports.sleep = sleep; exports.joinAnd = joinAnd; exports.joinOr = joinOr; exports.joinUrlSegments = joinUrlSegments; exports.tryParse = tryParse; const hasOwn = (obj, prop) => obj && Object.prototype.hasOwnProperty.call(obj, prop); exports.hasOwn = hasOwn; function isString(value) { return typeof value === 'string'; } function isBoolean(value) { return typeof value === 'boolean'; } function isArray(value) { return Array.isArray(value); } function isStringArray(value) { return Array.isArray(value) && value.every((x) => typeof x === 'string'); } function isNumber(value) { return typeof value === 'number'; } function isObject(value) { return value && typeof value === 'object'; } function compact(obj, dropEmptyString = false) { if (typeof obj !== 'object') { throw new Error('Argument must be an object'); } const result = {}; for (const key of Object.keys(obj)) { if (obj[key] == null) continue; if (dropEmptyString && obj[key] === '') continue; result[key] = obj[key]; } return result; } async function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } function joinAnd(...parts) { if (parts.length <= 2) return parts.join(' and '); const last = parts.slice(-1)[0]; const others = parts.slice(0, -1); return [others.join(', '), last].join(', and '); } function joinOr(...parts) { if (parts.length <= 2) return parts.join(' or '); const last = parts.slice(-1)[0]; const others = parts.slice(0, -1); return [others.join(', '), last].join(', or '); } function joinUrlSegments(...segments) { return ['/', ...segments].join('/').replace(/\/+/g, '/').replace(/\/$/, ''); } function tryParse(obj) { try { return JSON.parse(String(obj)); } catch { return obj; } } //# sourceMappingURL=utils.js.map