UNPKG

@technobuddha/library

Version:
15 lines (14 loc) 548 B
import { empty } from '../constants'; import isFinite from 'lodash/isFinite'; /** * Break a string into equal sized segments of characters * * @param input The string to break apart * @param length The length of each segment * @returns Array of segments */ export function chop(input, length, { truncate = false } = {}) { // eslint-disable-next-line @typescript-eslint/prefer-regexp-exec return length > 0 && isFinite(length) ? input.match(new RegExp(`.{${truncate ? empty : '1,'}${length}}`, 'gu')) : [input]; } export default chop;