npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-yield-from

v0.1.6

Published

Proper rendering tool to handle arrays, array-like objects, iterators, iterables, generators and more!

Downloads

6

Readme

React Yield/From

Coverage Status Gitlab pipeline status (branch)

Proper rendering tool to handle arrays, array-like objects, iterators, iterables, generators and more!

Includes TypeScript definitions

Quick Start

npm install --save react-yield-from

Simple pattern:

import Yield from "react-yield-from";

<Yield from={/* source */}>{/* Handler yielded result */}</Yield>

Props and types

Yield

| Name | Type | Required | Description | | -------- | ------------------------------------------------------------------------------------------------------------------- | -------- | -------------------------------------------------------------------------- | | from | Array|ArrayLike|Iterable|Iterator|Generator|GeneratorFunction|(() => Array|ArrayLike|Iterable|Iterator|Generator) | true | Source that will be iterated through by the Yield component | | children | (result => ReactNode) | ReactNode | true | Transformer function or component that will be rendered for each iteration |

Yielded Component

When React component is used instead of function as a child, yielded value will be propagated to it. Interface YieldedComponentProps might be used in TypeScript. When using any non-object child (not being ReactElement) no value gets propagated, ie. children like string, Array or null;

| Name | Type | Required | Description | | ------- | ----- | -------- | --------------------------------------- | | yielded | any | false | Propagated value of the iteration cycle |

Handling and transforming

Results recovered from the source will be propagated to the children. Propagated value can be either handled by function or by React component.

<Yield from={["Joe", "Billy"]}>
	{name => <span>{name}</span>}
</Yield>

When using React component, the value will be injected to the props as yielded prop.

const MyComponent = ({ yielded }) => <span>{yielded}</span>;

<Yield from={["Joe", "Billy"]}>
	<MyComponent />
</Yield>

Component can even ignore propagated value. This might be handy when using Yield/From as a while loop.

Sources

Yield/From combines power of for ... of and Array.from. Thus Yield/From is capable of processing almost anything in some way.

Arrays

<div>
	<h2>Current score is:</h2>
	<Yield from={[{name: "Bob", score: 50}, {name: "John", score: 25]}>
		{({name, score}) => <span>{name}:{score}</span>}
	</Yield>
</div>

Array Likes

Array like object are those with length property and indexed keys. Native arguments and NodeList are one of the most common array like objects.

const myArrayLike = {
	0: "Joe",
	1: "Hans",
	length: 2
};

<Yield from={myArrayLike}>
	{name => <span>{name}</span>}
</Yield>

Iterables

Iterables are objects that implement iterable protocol by having [Symbol.iterator] method. Native example might me array or string.

Iterators

Iterators are objects implementing iterator protocol. Method [Symbol.iterator] returns iterator object. Iterator objects are those who implement next method.

const myIterator = {
	next: () => ({
		value: 'foo',
		done: false
	})
}

<Yield from={myIterator}>
	{name => <span>{name}</span>}
</Yield>

Generators

Generators are objects that are created by invoking function* (function declared with asterisk symbol). Generators implement iterator and iterable protocol.

function* MyGenerator() {
	yield 10;
	yield 50;
}

const myGenerator = MyGenerator();

<Yield from={myGenerator}>
	{name => <span>{name}</span>}
</Yield>

Factories and Generator functions

Any of the previous types can be also used as source when wrapped in function. If function is passed in from props, it will get invoked without any arguments.

function Factory() {
	return [5, 10];
}

<Yield from={Factory}>
	{name => <span>{name}</span>}
</Yield>

Generator function can be used in this fashion as well.

function* MyGenerator() {
	yield 10;
	yield 50;
}

<Yield from={MyGenerator}>
	{name => <span>{name}</span>}
</Yield>

Built in loops

Yield/From comes with few prebuilt iterators in order to simulate behaviour of loops such as for, while or do ... while as well as for creating value ranges. They are available through exported object Iterators;

Range

Creates range between two values including min and excluding max.

import Yield, { Iterators } from "react-yield-from";

<Yield from={Iterators.range(0,5,1)}>
	{name => <span>{name}</span>}
</Yield>

Arguments as follows: min, max and step.

For Loop

Simulates behaviour of for(let i = 0; i<5; i++)

import Yield, { Iterators } from "react-yield-from";

<Yield from={Iterators.forLoop(0, i => i < 5 , i => i + 1)}>
	{name => <span>{name}</span>}
</Yield>

Arguments as follows: start, condition and raise.

While Loop

Simulates behaviour of while(condition)

import Yield, { Iterators } from "react-yield-from";

<Yield from={Iterators.whileLoop(() => shouldContinue())}>
	{name => <span>{name}</span>}
</Yield>

Arguments as follows: condition.

Do While Loop

Simulates behaviour of do ... while(condition)

import Yield, { Iterators } from "react-yield-from";

<Yield from={Iterators.doWhileLoop(() => shouldContinue())}>
	{name => <span>{name}</span>}
</Yield>

Arguments as follows: condition.