@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function bindMethodFactory

    • generates a factory function that binds a class-prototype-method func (by reference) to the passed object S (which should be an instance of the class).

      Type Parameters

      • T
      • A extends any[]
      • B extends any[]
      • R

      Parameters

      • func: BindableFunction<T, A, B, R>

        the method to generate the binding for

      • ...args: A

        partial tuple of the first few arguments that should be passed in by default

      Returns <S>(thisArg: S) => (...args: B) => R

      a function that can bind any object obj: S to the said method

      const bind_map_set = bindMethodFactory(Map.prototype.set)
      type ID = number
      const graph_edges = new Map<ID, Set<ID>>()
      const set_graph_edge = bind_map_set(graph_edges) // automatic type inference will correctly assign it the type: `(key: number, value: Set<number>) => Map<number, Set<number>>`
      const edges: [ID, ID[]][] = [[1, [1,2,3]], [2, [3,5]], [3, [4, 7]], [4, [4,5]], [5, [7]]]
      for (const [id, adjacent_ids] of edges) { set_graph_edge(id, new Set(adjacent_ids)) }

      example with assigned default arguments

      import { assertEquals } from "jsr:@std/assert"

      const bind_queue_delete_bottom_n_elements = bindMethodFactory(Array.prototype.splice, 0)
      const queue = [1, 2, 3, 7, 7, 7, 9, 9, 9]
      const release_from_queue = bind_queue_delete_bottom_n_elements(queue) // automatic type inference will correctly assign it the type: `(deleteCount: number, ...items: number[]) => number[]`
      const test_arr: number[][] = []
      while (queue.length > 0) { test_arr.push(release_from_queue(3)) }
      assertEquals(test_arr, [
      [1, 2, 3],
      [7, 7, 7],
      [9, 9, 9],
      ])