MCPcopy Index your code
hub / github.com/immutable-js/immutable-js / flattenFactory

Function flattenFactory

src/Operations.js:724–777  ·  view source on GitHub ↗
(collection, depth, useKeys)

Source from the content-addressed store, hash-verified

722}
723
724export function flattenFactory(collection, depth, useKeys) {
725 const flatSequence = makeSequence(collection);
726 flatSequence.__iterateUncached = function (fn, reverse) {
727 if (reverse) {
728 return this.cacheResult().__iterate(fn, reverse);
729 }
730 let iterations = 0;
731 let stopped = false;
732 function flatDeep(iter, currentDepth) {
733 iter.__iterate((v, k) => {
734 if ((!depth || currentDepth < depth) && isCollection(v)) {
735 flatDeep(v, currentDepth + 1);
736 } else {
737 iterations++;
738 if (fn(v, useKeys ? k : iterations - 1, flatSequence) === false) {
739 stopped = true;
740 }
741 }
742 return !stopped;
743 }, reverse);
744 }
745 flatDeep(collection, 0);
746 return iterations;
747 };
748 flatSequence.__iteratorUncached = function (type, reverse) {
749 if (reverse) {
750 return this.cacheResult().__iterator(type, reverse);
751 }
752 let iterator = collection.__iterator(type, reverse);
753 const stack = [];
754 let iterations = 0;
755 return new Iterator(() => {
756 while (iterator) {
757 const step = iterator.next();
758 if (step.done !== false) {
759 iterator = stack.pop();
760 continue;
761 }
762 let v = step.value;
763 if (type === ITERATE_ENTRIES) {
764 v = v[1];
765 }
766 if ((!depth || stack.length < depth) && isCollection(v)) {
767 stack.push(iterator);
768 iterator = v.__iterator(type, reverse);
769 } else {
770 return useKeys ? step : iteratorValue(type, iterations++, v, step);
771 }
772 }
773 return iteratorDone();
774 });
775 };
776 return flatSequence;
777}
778
779export function flatMapFactory(collection, mapper, context) {
780 const coerce = collectionClass(collection);

Callers 1

flattenFunction · 0.90

Calls 11

isCollectionFunction · 0.90
iteratorValueFunction · 0.90
iteratorDoneFunction · 0.90
makeSequenceFunction · 0.85
flatDeepFunction · 0.85
nextMethod · 0.80
cacheResultMethod · 0.65
popMethod · 0.65
pushMethod · 0.65
__iterateMethod · 0.45
__iteratorMethod · 0.45

Tested by

no test coverage detected