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

timesaver-common-components

v1.0.5

Published

Tony's react component library

Downloads

30

Readme

Tony's timesavers

This is a component library to save me time while working on react projects. Documentation for the components are generated with ChatGPT

Components

ConditionalRender

Description: The ConditionalRender function is a custom React component that provides a way to conditionally render its children based on the access permissions specified in the access object. The function takes in the access object, condition, children, and an optional negated boolean value as props.

Parameters:

  • access: An object of type IOptionRequestResult or null. The object represents the permissions that are required to render the children. If the value of access is null, then nothing is rendered.
  • condition: An enum value of type AccessOptions. This value specifies which permission is required to render the children.
  • children: A single or multiple React elements that will be rendered conditionally based on the access and condition props.
  • negated: An optional boolean value that is used to negate the condition for rendering the children. If negated is true, then the children are rendered when the specified permission is not present in the access object. If negated is false, then the children are rendered only when the specified permission is present in the access object. The default value of negated is false.

Return Value: The ConditionalRender function returns a ReactElement that represents the conditional rendering of its children based on the access permissions specified in the access object.

Example usage:

import React from 'react';
import ConditionalRender, { AccessOptions, IConditionalRenderProps } from './ConditionalRender';

function App() {
const access: IConditionalRenderProps['access'] = {
    canGet: true,
    canPost: false,
    canPut: true,
    canPatch: false,
    canDelete: true,
};

return (
    <div>
    <ConditionalRender access={access} condition={AccessOptions.canGet}>
        <p>You have permission to GET the data</p>
    </ConditionalRender>
    <ConditionalRender access={access} condition={AccessOptions.canPost}>
        <p>You have permission to POST the data</p>
    </ConditionalRender>
    <ConditionalRender access={access} condition={AccessOptions.canPut}>
        <p>You have permission to PUT the data</p>
    </ConditionalRender>
    <ConditionalRender access={access} condition={AccessOptions.canPatch}>
        <p>You have permission to PATCH the data</p>
    </ConditionalRender>
    <ConditionalRender access={access} condition={AccessOptions.canDelete} negated={true}>
        <p>You don't have permission to DELETE the data</p>
    </ConditionalRender>
    </div>
);
}

In the example above, the access object contains boolean values for each permission that is required to access a specific endpoint. The ConditionalRender component is used to render each permission based on the values in the access object. If the permission is not present in the access object, then nothing is rendered. If the permission is present and negated is false, then the corresponding children are rendered. If negated is true, then the corresponding children are rendered only when the permission is not present in the access object.

Hooks

useAsyncFetch

The useAsyncFetch hook is a custom React hook that can be used for making GET requests to an API endpoint asynchronously.

Parameters:

  • requestFunction: a function that returns a Promise representing the GET request to the API endpoint. The function should return null if the request should not be made (e.g. if a condition is not met).
  • skip: an optional boolean parameter indicating whether the initial fetch should be skipped. If skip is true, the hook will immediately set the status to ComponentStatus.loaded. Otherwise, the hook will fetch the data when the component mounts.

Return Value The useAsyncFetch hook returns a tuple containing four elements:

  • error - an object representing any error that occurred during the request, or null if there was no error.
  • status - an enum value indicating the status of the request, which can be one of the following:
    • ComponentStatus.fetching - the request is currently being fetched.
    • ComponentStatus.loaded - the request has been successfully fetched.
    • ComponentStatus.error - an error occurred while fetching the request.
    • ComponentStatus.reFetching - the request is being re-fetched after an error occurred.
  • data - the data returned by the API endpoint, or null if the data has not yet been fetched.
  • retry - a function that can be called to retry the request if an error occurred.

Utility functions

String utils

  • isStringNullOrWhiteSpace - Checks if string is null or whitespace
  • capitalizeFirstLetter - Capitalizes first letter of string
  • joinStringsWithAnd - Joins a list of strings into one string separated with comma and 'and'

Date utils

  • formatDateToString - formats date to string in the specified date format
  • sortByDate - Comparator for dates
  • isValidDate - Checks if date is a valid date object

Object utils

  • checkObjectEquality - Deep check for equality in two specified objects with specified types
  • checkEquality - Deep check for equality in two specified objects with unspecified types