function isObject(value: any) { return typeof value === 'object' && !Array.isArray(value) } // 比较两个对象,并提取出不同的部分 function getTwoObjectDiff(originalObj: Record, diffObj: Record): Record { if (!isObject(originalObj) || !isObject(diffObj)) { return diffObj } const diff: Record = {} for (const key in diffObj) { const originalValue = originalObj[key] const diffValue = diffObj[key] if (JSON.stringify(originalValue) !== JSON.stringify(diffValue)) { if (isObject(originalValue) && isObject(diffValue)) { const nestedDiff = getTwoObjectDiff(originalValue, diffValue) if (Object.keys(nestedDiff).length > 0) { diff[key] = nestedDiff } } else { diff[key] = diffValue } } } return diff } /** * 在指定范围内生成指定数量的随机整数 * @param count 指定数量 * @param max 指定范围 * * 示例: 生成10以内的4个随机整数 * let randomNumbers = generateRandomNumbers(4, 10) */ function generateRandomNumbers(count: number, max: number): number[] { const numbers = new Set() while (numbers.size < count) { numbers.add(Math.floor(Math.random() * max) + 1) } return Array.from(numbers) as number[] } /** * 毫秒数转时分秒 * @param fmtSeconds */ function formatSeconds(fmtSeconds: number): string { const hours = Math.floor(fmtSeconds / (1000 * 60 * 60)) const minutes = Math.floor((fmtSeconds % (1000 * 60 * 60)) / (1000 * 60)) const seconds = Math.floor((fmtSeconds % (1000 * 60)) / 1000) if (hours === 0) { if (hours === 0 && minutes === 0) { return `${seconds.toString().padStart(2, '0')}秒` } else { return `${minutes.toString().padStart(2, '0')}分 ${seconds.toString().padStart(2, '0')}秒` } } return `${hours.toString().padStart(2, '0')}时 ${minutes.toString().padStart(2, '0')}分 ${seconds.toString().padStart(2, '0')}秒` } function isAndroidOrIos(): 'android' | 'ios' | 'unknown' { const userAgent = navigator.userAgent.toLowerCase() if (/iphone|ipad|ipod/.test(userAgent)) { return 'ios' } else if (/android/.test(userAgent)) { return 'android' } return 'unknown' } export { getTwoObjectDiff, generateRandomNumbers, formatSeconds, isAndroidOrIos, }