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

flexible-chain

v0.0.6

Published

This library will help you quickly and clearly build your code by building a chain of trees.

Downloads

44

Readme

Flexible chain GitHub license npm version Build Status Coverage Status Size devDependencies Status

Install

Install from the NPM repository using yarn or npm:

yarn add flexible-chain

Or

npm install flexible-chain

Demo with React

Motivation

We have a lot of repetitive code and the chore of writing different combinations. Also, over time, we forget what a piece of code should do and spend more time understanding it. This utility helps to make your code more flexible, understandable and transparent.

Solution

Flexible chain is a small but powerful utility for creating tree chains and obtaining results after combining it.

Example

Simple

Simple use for creating various chains

import { combine } from "flexible-chain";

const colorProps = {
    Red: {
        color: 'red',
    },
    /* ... */
};

const sizeProps = {
    L: {
        fontSize: 16,
    },
    /* ... */
};

const initialFontTree = { ...colorProps, ...sizeProps };

export const fontResult = {
    View: style => style,
    Color: style => style.color,
    Size: style => style.fontSize,
};

const Font = combine(initialFontTree, fontResult);

console.log(Font.Red.View); // {color: 'red'}
console.log(Font.Red.L.View); // {color: 'red', fontSize: 16}

console.log(Font.Red.L.Color); // 'red'
console.log(Font.Red.L.Size); // 16

Custom

More flexible and specific use of combine for your tasks. For example, when you want the state to be not an object (by default), but a different data type.

import { combine } from "flexible-chain";

const mainLanguageKey = {
    Hello: 'Hello',
    Welcome: 'Welcome',
};

const secondaryLanguageKey = {
    Dear: 'dear',
    To: 'to',
};

const initialLanguageTree = { ...mainLanguageKey, ...secondaryLanguageKey };

const customResultTree = {
    Key: keys => keys.join('.'),
    Keys: keys => keys,
    Name: keys => name => `${[...keys, name].join(' ')}!`,
    Length: keys => keys.length,
};

const customConcatenationFn = (prev, key) => [...prev, key];

const customInitialState = [];

const Salutation = combine(initialLanguageTree, customResultTree, customConcatenationFn, customInitialState);

console.log(Salutation.Hello.Dear.Key); // 'Hello.dear'
console.log(Salutation.Hello.Dear.Keys); // ['Hello', 'dear']

console.log(Salutation.Hello.Length); // 1
console.log(Salutation.Hello.Dear.Length); // 2
/* ... N ... */

console.log(Salutation.Hello.Name('Kris')); // 'Hello Kris!'
console.log(Salutation.Welcome.To.Name('flexible-chain')); // 'Welcome to flexible-chain!';

React & React-Native

You can also make beautiful chains for react components.

React style

import { combine, combineComponent } from "flexible-chain";

const SpanComponent = props => <span {...props} />;
const ParagraphComponent = props => <p {...props} />;

const componentColors = {
    White: {
        color: 'white',
    },
    Red: {
        color: 'red',
    },
    /* ... */
};

const componentFontStyle = {
    Italic: {
        fontStyle: 'italic',
    },
    Oblique: {
        fontStyle: 'oblique',
    },
    /* ... */
};

const componentFontWeight = {
    Bold: {
        fontWeight: 'bold',
    },
    /* ... */
};

const componentStyles = { ...componentColors, ...componentFontStyle, ...componentFontWeight };

const componentResult = {
    Text: combineComponent(SpanComponent, 'style'),
    Paragraph: combineComponent(ParagraphComponent, 'style'),
};

const UserInfo = combine(componentStyles, componentResult);

const UserProfile = () => (
    <>
        <UserInfo.White.Bold.Text>User Name</UserInfo.White.Bold.Text>
        <UserInfo.White.Bold.Italic.Text>User Surname</UserInfo.White.Bold.Italic.Text>
        <UserInfo.Red.Oblique.Paragraph>User Role</UserInfo.Red.Oblique.Paragraph>
        /* ... */
    </>
);

const HeaderUserInfo = () => (
    <>
        <UserInfo.White.Bold.Paragraph>User Name</UserInfo.White.Bold.Paragraph>
        <UserInfo.Red.Italic.Paragraph>User Role</UserInfo.Red.Italic.Paragraph>
        /* ... */
    </>
);

console.log(UserProfile); // ->
/* 
    <span style={{color: 'white', fontWeight: 'bold'}}>User Name</span>
    <span style={{color: 'white', fontWeight: 'bold', fontStyle: 'italic'}}>User Surname</span>
    <p style={{color: 'red', fontStyle: 'oblique'}}>User Role</p>

*/

React classes

import { combine, combineComponent } from "flexible-chain";
import cs from 'classnames';

import styles from './style.module.css';

/* ... */

const SpanComponent = ({classes, ...props}) => {
   return <span className={cs(classes)} {...props} />;
}

const componentResult = {
    Text: combineComponent(SpanComponent, 'classes'),
};

const UserInfo = combine(styles, componentResult);

/* ... */

P.S.

Above are the most simple and commonly used options. You can combine more complex and dynamic connections for easier reuse.