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

ts-util-721

v0.0.4

Published

Functional programming and Dependency injection for Typescript

Downloads

125

Readme

ts-util-721

  1. Functional Programming

    1. Either

      Use left(l: L) or right(r: R) static functions in Either class to create Either for erroneous and valid values correspondingly.

      export class HttpsError{
        constructor(private message: string) {
        }
            
        get getError(){
            return this.message
        }
      }  
      export class HttpsResponse{
        constructor(public response: any) {
        }
      }  
      import axios from "axios";
      import {Either} from "ts-util-721";
      export class AxiosDatasource{
            ...
        async get(): Promise<Either<RestResponseFailure, RestResponse>> {
            try{
                const response = awiat this.axios.get(this.url)
                return Either.right(new HttpsResponse(response))
            }catch(error){
                return Either.left(new HttpsError(error.message))
            }
        }  
      }

      The result of the above function(Either type) can be consumed using functions of the Either instance; fold, foldLeft, foldRight, getOrElse, getLeft or getRight.

      import {AxiosDatasource} from "./axios_datasource"
            
      const someFunction = () => {
         const response = await new AxiosDatasource().get()
         response.fold(async l => {
            // Display or log error using l.getError
         }, async r => {
            // Consume valid value using r.response
         })        
    2. Option

      You can use option similar to the Either
      You can use Option.none() and Option.some() to create Option object.
      Similar functions to consume the value of an Option is also present here.

  2. Dependency Injection

    You can register all your instances globally using the DependencyProvider class. You can register both singletons and lazy singletons. The provider class is a singleton on its own. It has a getInstance static function for instantiation, but you won't have to use it because it is instantiated internally. You just need to import that.
    import {provider} from "ts-util-721"
       
    provider.registerSingleton(
         "unique indentifier",
          new AxiosDatasource()
    )
       
    provider.registerLazySingleton(
         "unique indentifier",
         () => new AxiosDatasource()
    )   
    The first usage is better suited for instances that are more likely to be used or are necessary during instantiation. The second usage is better for instances that may not be needed during the lifecycle of the application or are not need during instantiation. You can fetch the instance using the get function.
    import {provider} from "ts-util-721"
       
    const axiosDatasource = provider.get<AxiosDatasource>("unique indentifier")
    You should specify the type of the instance you are fetching if you read values of the instance since it can't induce the type of the instance automatically.