2023-06-11 02:59:33 +00:00
|
|
|
import { customAlphabet, urlAlphabet } from 'nanoid'
|
|
|
|
|
|
|
|
import * as types from './types'
|
|
|
|
|
2023-06-15 03:30:16 +00:00
|
|
|
/**
|
2023-06-15 14:05:52 +00:00
|
|
|
* Extracts a JSON object string from a given string.
|
2023-06-15 03:30:16 +00:00
|
|
|
*
|
|
|
|
* @param text - string from which to extract the JSON object
|
|
|
|
* @returns extracted JSON object string, or `undefined` if no JSON object is found
|
|
|
|
*/
|
2023-06-14 04:39:19 +00:00
|
|
|
export function extractJSONObjectFromString(text: string): string | undefined {
|
2023-06-15 14:05:52 +00:00
|
|
|
return text.match(/\{(.|\n)*\}/gm)?.[0] // FIXME: This breaks if there are multiple JSON objects in the string
|
2023-06-14 04:39:19 +00:00
|
|
|
}
|
2023-05-24 06:15:59 +00:00
|
|
|
|
2023-06-15 03:30:16 +00:00
|
|
|
/**
|
2023-06-15 14:05:52 +00:00
|
|
|
* Extracts a JSON array string from a given string.
|
2023-06-15 03:30:16 +00:00
|
|
|
*
|
|
|
|
* @param text - string from which to extract the JSON array
|
|
|
|
* @returns extracted JSON array string, or `undefined` if no JSON array is found
|
|
|
|
*/
|
2023-06-14 04:39:19 +00:00
|
|
|
export function extractJSONArrayFromString(text: string): string | undefined {
|
2023-06-15 14:05:52 +00:00
|
|
|
return text.match(/\[(.|\n)*\]/gm)?.[0] // FIXME: This breaks if there are multiple JSON arrays in the string
|
2023-06-14 04:39:19 +00:00
|
|
|
}
|
2023-06-07 01:03:39 +00:00
|
|
|
|
2023-06-15 03:30:16 +00:00
|
|
|
/**
|
|
|
|
* Pauses the execution of a function for a specified time.
|
|
|
|
*
|
|
|
|
* @param ms - number of milliseconds to pause
|
|
|
|
* @returns promise that resolves after the specified number of milliseconds
|
|
|
|
*/
|
2023-06-14 04:39:19 +00:00
|
|
|
export function sleep(ms: number) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
|
|
}
|
2023-06-11 02:59:33 +00:00
|
|
|
|
2023-06-15 03:30:16 +00:00
|
|
|
/**
|
|
|
|
* A default ID generator function that uses a custom alphabet based on URL safe symbols.
|
|
|
|
*/
|
2023-06-11 02:59:33 +00:00
|
|
|
export const defaultIDGeneratorFn: types.IDGeneratorFunction =
|
|
|
|
customAlphabet(urlAlphabet)
|
2023-06-14 04:39:19 +00:00
|
|
|
|
|
|
|
const taskNameRegex = /^[a-zA-Z_][a-zA-Z0-9_-]{0,63}$/
|
|
|
|
export function isValidTaskIdentifier(id: string): boolean {
|
|
|
|
return !!id && taskNameRegex.test(id)
|
|
|
|
}
|
2023-06-15 03:00:02 +00:00
|
|
|
|
|
|
|
/**
|
2023-06-15 03:30:16 +00:00
|
|
|
* Chunks a string into an array of chunks.
|
2023-06-15 03:00:02 +00:00
|
|
|
*
|
|
|
|
* @param text - string to chunk
|
2023-06-15 03:30:16 +00:00
|
|
|
* @param maxLength - maximum length of each chunk
|
|
|
|
* @returns array of chunks
|
2023-06-15 03:00:02 +00:00
|
|
|
*/
|
2023-06-15 03:30:16 +00:00
|
|
|
export const chunkString = (text: string, maxLength: number) => {
|
2023-06-15 03:00:02 +00:00
|
|
|
const words = text.split(' ')
|
|
|
|
const chunks: string[] = []
|
|
|
|
let chunk = ''
|
|
|
|
|
|
|
|
for (const word of words) {
|
2023-06-15 03:30:16 +00:00
|
|
|
if (word.length > maxLength) {
|
2023-06-15 03:00:02 +00:00
|
|
|
// Truncate the word if it's too long and indicate that it was truncated:
|
2023-06-15 03:30:16 +00:00
|
|
|
chunks.push(word.substring(0, maxLength - 3) + '...')
|
|
|
|
} else if ((chunk + word + 1).length > maxLength) {
|
|
|
|
// +1 accounts for the space between words
|
2023-06-15 03:00:02 +00:00
|
|
|
chunks.push(chunk.trim())
|
|
|
|
chunk = word
|
|
|
|
} else {
|
2023-06-15 03:30:16 +00:00
|
|
|
chunk += (chunk ? ' ' : '') + word
|
2023-06-15 03:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunk) {
|
|
|
|
chunks.push(chunk.trim())
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks
|
|
|
|
}
|