Returns an immutable wrapper around a sorted array. Use wrapUnsorted if not yet sorted.

Functions that change contents return a new wrapped instance.

let w = wrapSorted([ 1, 2, 10 ]);
w.indexOf(1); // 0
w = w.insert(9); // Inserts 9, returning a new wrapper
w = w.remove(9); // Removes 9, returning a new wrapper

You can access the underyling sorted array with the data property. It's important this is not modified since the wrapper assumes its immutable and stays sorted. Use toArray() to get a copy of the array which can be changed.

// A few basic array-like features supported
w.length; // Get length of array
w.at(0); // Get item at index 0
w.data; // Get underlying array
w.toArray(); // Get a copy of the underyling array
  • Type Parameters

    • T

    Parameters

    • sortedData: T[]
    • comparer: Comparer<T> = defaultComparer

    Returns {
        at: ((offset: number) => undefined | T);
        indexOf: ((sought: T, start?: number, end?: number) => number);
        insert: ((toInsert: T) => { indexOf: (sought: T, start?: number, end?: number) => number; insertionIndex: (toInsert: T) => number; insert: (toInsert: T) => ...; remove: (toRemove: T) => ...; at: (offset: number) => T | undefined; readonly length: number; readonly data: T[]; toArray: () => T[]; });
        insertionIndex: ((toInsert: T) => number);
        remove: ((toRemove: T) => { indexOf: (sought: T, start?: number, end?: number) => number; insertionIndex: (toInsert: T) => number; insert: (toInsert: T) => ...; remove: (toRemove: T) => ...; at: (offset: number) => T | undefined; readonly length: number; readonly data: T[]; toArray: () => T[]; });
        toArray: (() => T[]);
        get data(): T[];
        get length(): number;
    }

    • at: ((offset: number) => undefined | T)

      Gets item at a specified offset

        • (offset): undefined | T
        • Parameters

          • offset: number

          Returns undefined | T

    • indexOf: ((sought: T, start?: number, end?: number) => number)

      Returns index of an item, or -1 if not found

        • (sought, start?, end?): number
        • Parameters

          • sought: T

            Item to find

          • start: number = 0
          • end: number = store.length

          Returns number

    • insert: ((toInsert: T) => { indexOf: (sought: T, start?: number, end?: number) => number; insertionIndex: (toInsert: T) => number; insert: (toInsert: T) => ...; remove: (toRemove: T) => ...; at: (offset: number) => T | undefined; readonly length: number; readonly data: T[]; toArray: () => T[]; })

      Inserts an item, returning a new wrapper

        • (toInsert): { indexOf: (sought: T, start?: number, end?: number) => number; insertionIndex: (toInsert: T) => number; insert: (toInsert: T) => ...; remove: (toRemove: T) => ...; at: (offset: number) => T | undefined; readonly length: number; readonly data: T[]; toArray: () => T[]; }
        • Parameters

          • toInsert: T

          Returns { indexOf: (sought: T, start?: number, end?: number) => number; insertionIndex: (toInsert: T) => number; insert: (toInsert: T) => ...; remove: (toRemove: T) => ...; at: (offset: number) => T | undefined; readonly length: number; readonly data: T[]; toArray: () => T[]; }

    • insertionIndex: ((toInsert: T) => number)
        • (toInsert): number
        • Parameters

          • toInsert: T

          Returns number

    • remove: ((toRemove: T) => { indexOf: (sought: T, start?: number, end?: number) => number; insertionIndex: (toInsert: T) => number; insert: (toInsert: T) => ...; remove: (toRemove: T) => ...; at: (offset: number) => T | undefined; readonly length: number; readonly data: T[]; toArray: () => T[]; })

      Removes an item, returning a new wrapper

        • (toRemove): { indexOf: (sought: T, start?: number, end?: number) => number; insertionIndex: (toInsert: T) => number; insert: (toInsert: T) => ...; remove: (toRemove: T) => ...; at: (offset: number) => T | undefined; readonly length: number; readonly data: T[]; toArray: () => T[]; }
        • Parameters

          • toRemove: T

          Returns { indexOf: (sought: T, start?: number, end?: number) => number; insertionIndex: (toInsert: T) => number; insert: (toInsert: T) => ...; remove: (toRemove: T) => ...; at: (offset: number) => T | undefined; readonly length: number; readonly data: T[]; toArray: () => T[]; }

    • toArray: (() => T[])

      Returns a copy of data which is safe to modify.

        • (): T[]
        • Returns T[]

    • get data(): T[]

      Gets underlying data. Be careful not to mutate. Use toArray to get a copy that can be modified.

    • get length(): number

      Gets length