UNPKG

modernirc

Version:

IRC library for creating modern IRC bots

81 lines (70 loc) 1.8 kB
export function mergeDeep(target, ...sources) { if (!sources.length) { return target } const source = sources.shift() if (typeof source === 'object' && !Array.isArray(source) && typeof target === 'object' && !Array.isArray(target)) { Object.keys(source).forEach((key) => { if (typeof source[key] === 'object' && !Array.isArray(source[key])) { if (!target[key]) { Object.assign(target, { [key]: {} }) } mergeDeep(target[key], source[key]) } else { Object.assign(target, { [key]: source[key] }) } }) } return target } /** * Cuts a `source` string into an array of strings that are no longer than `length` characters. * @param source {string} * @param length {number} * @returns {string[]} */ export function cutStringLength(source, length = 510) { if (source.length <= length) { return [source] } const out = [] let remainingLength = source.length let page = 0 while (remainingLength > 0) { const cut = source.substring(page * length, (page + 1) * length) if (cut.length > 0) { out.push(cut) remainingLength -= cut.length page++ } else { remainingLength = 0 } } return out } export function oGet(source, path) { if (typeof source !== 'object') { return undefined } if (typeof path !== 'string') { return undefined } const parts = path.split('.') let key = parts[0] if (Array.isArray(source)) { key = parseInt(key, 10) if (isNaN(key)) { return undefined } } if (parts.length === 1) { return source[key] } if (typeof source[key] === 'undefined') { return undefined } if (typeof source[key] === 'object') { return oGet(source[key], parts.slice(1).join('.')) } return undefined }