/** * Checks that the given data is not an empty object. * @remarks * Returning false means that the data is either an empty object or a falsy value. * * @param ob - the object to check * @returns true of false * */ export const objectIsEmpty = (ob: unknown): boolean => Boolean(ob && typeof ob === `object` && Object.values(ob).length === 0) /** * Checks that the given data is an object and also typeguards it so that TypeScript also knows if it is in fact an object. * @param value - the value to check * @returns true or false */ export const isObject = (value: unknown): value is object => typeof value !== `undefined` && Object.getPrototypeOf(value) === Object.prototype /** * Returns the longest word contained in the given string. * * @remarks * If the string is accompanied by punctuation, it will be included in its word length. ie. "hello worlds" will return "worlds", but "hello-worlds." will return "hello-worlds." — note the hyphen and full stop is included. * * @param text - the text to check * @returns the longest word in the text */ export const longestWord = (text: string): string => { const wordSeparatorPattern = /[ \t\r\n/]/gi return text .replace(wordSeparatorPattern, ` `) .split(` `) .reduce((acc, curr) => { return curr.length > acc.length ? curr : acc }, ``) } /** * Gets a random item of a supplied collection (array, string, etc.). * @param items - a collection of anything * @returns a random one of the collection */ export const getRandomItem = (items: T[] = []): T => { const random = Math.random() // decimal between 0 and 1 const timesLength = random * items.length const index = Math.floor(timesLength) return items[index] }