MCPcopy Create free account
hub / github.com/denoland/std / from

Method from

data_structures/deque.ts:672–724  ·  view source on GitHub ↗
(
    collection: ArrayLike<T> | Iterable<T> | Deque<T>,
    options?: {
      map?: (value: T, index: number) => U;
      thisArg?: V;
    },
  )

Source from the content-addressed store, hash-verified

670 },
671 ): Deque<U>;
672 static from<T, U, V>(
673 collection: ArrayLike<T> | Iterable<T> | Deque<T>,
674 options?: {
675 map?: (value: T, index: number) => U;
676 thisArg?: V;
677 },
678 ): Deque<U> {
679 if (
680 collection === null || collection === undefined ||
681 typeof collection !== "object" && typeof collection !== "string" ||
682 !(
683 Symbol.iterator in Object(collection) ||
684 "length" in Object(collection)
685 )
686 ) {
687 throw new TypeError(
688 "Cannot create a Deque: the 'collection' parameter is not iterable or array-like",
689 );
690 }
691 const result = new Deque<U>();
692 let unmappedValues: ArrayLike<T> | Iterable<T>;
693
694 if (collection instanceof Deque) {
695 if (!options?.map) {
696 const capacity = nextPowerOfTwo(collection.#length);
697 result.#buffer = Deque.#copyBuffer(
698 collection,
699 capacity,
700 ) as (U | undefined)[];
701 result.#head = 0;
702 result.#length = collection.#length;
703 result.#mask = capacity - 1;
704 return result;
705 }
706 unmappedValues = collection.toArray();
707 } else {
708 unmappedValues = collection;
709 }
710
711 const mapped: U[] = options?.map
712 ? Array.from(unmappedValues, options.map, options.thisArg)
713 : Array.from(unmappedValues as ArrayLike<U> & Iterable<U>);
714
715 const capacity = nextPowerOfTwo(mapped.length);
716 result.#buffer = new Array(capacity);
717 for (let i = 0; i < mapped.length; i++) {
718 result.#buffer[i] = mapped[i];
719 }
720 result.#head = 0;
721 result.#length = mapped.length;
722 result.#mask = capacity - 1;
723 return result;
724 }
725
726 /**
727 * Iterate over the deque's elements from front to back. Non-destructive

Calls 3

nextPowerOfTwoFunction · 0.85
#copyBufferMethod · 0.80
toArrayMethod · 0.45

Tested by

no test coverage detected