anchor/ts/src/utils/common.ts

22 lines
650 B
TypeScript

/**
* Returns true if being run inside a web browser,
* false if in a Node process or electron app.
*/
export const isBrowser =
process.env.BROWSER ||
(typeof window !== "undefined" && !window.process?.hasOwnProperty("type"));
/**
* Splits an array into chunks
*
* @param array Array of objects to chunk.
* @param size The max size of a chunk.
* @returns A two dimensional array where each T[] length is < the provided size.
*/
export function chunks<T>(array: T[], size: number): T[][] {
return Array.apply(
0,
new Array(Math.ceil(array.length / size))
).map((_, index) => array.slice(index * size, (index + 1) * size));
}