import { Children, isValidElement, ReactNode, Ref } from 'react' export function concat(prefix: string, ...suffix: string[]) { return [prefix, ...suffix].join('-') } export const isDev = process.env.NODE_ENV !== 'production' /** 得到 ReactNode 中的文本 */ export function getText(node: ReactNode): string { return Children.toArray(node).map(child => { if (typeof child === 'string') { return child } if (typeof child === 'number') { return child + '' } if (isValidElement(child) && child.props) { return getText(child.props.children) } return '' }).join('') } /** 使用给定的值填充给定 ref,支持 ref object 或 callback ref */ export function fillRef(ref: Ref, value: T | null) { if (ref == null) return if (typeof ref === 'function') { ref(value) return } // ref.current 本来是 readonly 的 (ref.current as T | null) = value } export function assert(predict: boolean | (() => boolean), message: string): void { if (typeof predict === 'boolean') { if (!predict) throw Error(message) return } if (!predict()) { throw Error(message) } } export function areArraysShallowEqual(arr1: any[], arr2: any[]) { if (arr1.length !== arr2.length) return false for (const index in arr1) { if (arr1[index] !== arr2[index]) { return false } } return true } let index = 0 export function getId() { return `icecream-${Date.now()}-${++index}` }