arrayTools.ts 706 B

123456789101112131415161718
  1. /**
  2. * Class containing a set of static utilities functions for arrays.
  3. */
  4. export class ArrayTools {
  5. /**
  6. * Returns an array of the given size filled with element built from the given constructor and the paramters
  7. * @param size the number of element to construct and put in the array
  8. * @param itemBuilder a callback responsible for creating new instance of item. Called once per array entry.
  9. * @returns a new array filled with new objects
  10. */
  11. public static BuildArray<T>(size: number, itemBuilder: () => T): Array<T> {
  12. const a: T[] = [];
  13. for (let i = 0; i < size; ++i) {
  14. a.push(itemBuilder());
  15. }
  16. return a;
  17. }
  18. }