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

@ao-framework/gates

v0.0.3

Published

Conditional logic

Downloads

4

Readme

What is Gates?

Gates is a library with many functions to make writing logic less fatiguing and more stable. The functions are broken into five categories:

  • Ensure Functions
  • Is Functions
  • Returning Functions
  • Throwing Functions
  • When Functions

Installation

npm install --save @ao-framework/gates

Typescript types are included.

How Do I Import Gates?

// You can import a single function for tree shaking
import { isFunction } from "@ao-framework/gates"

// You can import everything
import * as gates from "@ao-framework/gates"

// You can import by using Old Faithful.
const gates = require("@ao-framework/gates")

**A common use case code example is provided below each description for visual code learners.

Ensure Functions

This category is used to ensure that variables are what you think they are.

import { ensureObject, ensureFunction } from "@ao-framework/gates"

function myLibrary(options: iMyOptions) {
    const defaultOptions = { x: 0, y: 0 }
    const finalOptions = ensureObject(options, defaultOptions, false);
    ensureFunction(finalOptions.loaded)()
}
  • ensureObject
  • ensureArray
  • ensureFunction

Is Functions

This category is used to assert that a variable is a particular type.

import { isString, isNumber } from "@ao-framework/gates"

function doSomething(v: string | number) {
    if(isString(v)) { /** do something */ }
    if(isNumber(v)) { /** do something */ }
}

For Typescript users, please note that Type Guards were used so that intellisense knowledge is maintained.

  • isObjectLike
  • isObject
  • isString
  • isStringWithLength
  • isFunction
  • isBoolean
  • isNumber
  • isArray
  • isUndefined
  • isNull
  • isNill
  • isBigInt
  • isSymbol
  • constructedFrom

Is Functions include the negations

  • isNotObjectLike
  • isNotObject
  • isNotString
  • isNotStringWithLength
  • isNotFunction
  • isNotBoolean
  • isNotNumber
  • isNotArray
  • isNotUndefined
  • isNotNull
  • isNotNill
  • isNotBigInt
  • isNotSymbol

Returning Functions

This category is used to make sure that you have either the correct type assigned or nothing at all.

import { returnStringWithLengthOrNothing } from "@ao-framework/gates"

function createUser(data: iCreateUserRequest) {
    const user = new User();
    user.name = returnStringWithLengthOrNothing(data.name);
    user.email = returnStringWithLengthOrNothing(data.email);
}
  • returnObjectLikeOrNothing
  • returnObjectOrNothing
  • returnStringOrNothing
  • returnStringWithLengthOrNothing
  • returnFunctionOrNothing
  • returnBooleanOrNothing
  • returnNumberOrNothing
  • returnArrayOrNothing
  • returnNullOrNothing
  • returnBigIntOrNothing
  • returnSymbolOrNothing

Throwing Functions

This category is used to streamline throwing exceptions when variables are not needed types.

import { throwWhenNotObject, throwWhenNotStringWithLength } from "@ao-framework/gates"

function validateUser(user: User) {
    throwWhenNotObject(user, "User object was not created", SystemException)
    throwWhenNotStringWithLength(user.name, "User must have a name", BadInputException)
    throwWhenNotStringWithLength(user.email, "User must have an email", BadInputException)
}
  • throwException
  • throwWhenObjectLike
  • throwWhenObject
  • throwWhenString
  • throwWhenStringWithLength
  • throwWhenFunction
  • throwWhenBoolean
  • throwWhenNumber
  • throwWhenArray
  • throwWhenUndefined
  • throwWhenNull
  • throwWhenNill
  • throwWhenBigInt
  • throwWhenSymbol

Throwing Functions include the negations

  • throwWhenNotObjectLike
  • throwWhenNotObject
  • throwWhenNotString
  • throwWhenNotStringWithLength
  • throwWhenNotFunction
  • throwWhenNotBoolean
  • throwWhenNotNumber
  • throwWhenNotArray
  • throwWhenNotUndefined
  • throwWhenNotNull
  • throwWhenNotNill
  • throwWhenNotBigInt
  • throwWhenNotSymbol

When Functions

This category is used to be a short replacement for if statements.

import { when, whenString } from "@ao-framework/gates"

when(something === true)(fnThatCanBeCalled)
whenString(nameOfUser)(fnThatCanBeCalledExpectingString);
  • when
  • whenObjectLike
  • whenObject
  • whenString
  • whenStringWithLength
  • whenFunction
  • whenBoolean
  • whenNumber
  • whenArray
  • whenUndefined
  • whenNull
  • whenNill
  • whenBigInt
  • whenSymbol

When Functions include the negations

  • whenNotObjectLike
  • whenNotObject
  • whenNotString
  • whenNotStringWithLength
  • whenNotFunction
  • whenNotBoolean
  • whenNotNumber
  • whenNotArray
  • whenNotUndefined
  • whenNotNull
  • whenNotNill
  • whenNotBigInt
  • whenNotSymbol

At the End of the Day

You can turn something like this...

import { cleanString } from "./commons"

function createPost(request: iPostRequest) {

    // validation & construction are together
    // and not easily refactorable.

    if(typeof request === "object" && request !== null) {
         const post = new Post();
        if(typeof request.title === "string") { 
            post.title = cleanString(request.title)
        } else { 
            throw new Error("Post title must be a string") 
        }
        if(typeof request.content === "string") { 
            post.content = cleanString(request.content) 
        } else { 
            throw new Error("Post content must be a string") 
        }
        if(Array.isArray(request.tags)) {
            request.tags.forEach(tag => {
                if(typeof tag !== "string") {
                    throw new Error("Post tag must be a string")
                }
            })
        } else {
            throw new Error("Post tags must be an array")
        }
        return post;
    } else {
        throw new Error("Post request data is corrupt")
    }
}

To something more refactorable...

import { cleanString } from "./commons"

function createPost(request: iPostRequest) {

    // validation -> easily refactorable

    assert(typeof request === "object" && request !== null, 
        "Post request data is corrupt")

    assert(typeof request.title === "string", 
        "Post title must be a string")
    
    assert(typeof request.content === "string", 
        "Post content must be a string")
    
    assert(Array.isArray(request.tags), 
        "Post tags must be an array")
    
    request.tags.forEach(tag => 
        assert(typeof tag === "string", 
            "Post tag must be a string"))

    // construction -> feeling secure

    const post = new Post();
    post.title = cleanString(request.title)
    post.content = cleanString(request.content)
    post.tags = request.tags.map(tag => cleanString(tag))
    return post;
}

Now something more readable

import { cleanString } from "./commons"

function createPost(request: iPostRequest) {
    
    // validation -> easily refactorable

    throwWhenNotObject(request, "Post request data is corrupt")
    throwWhenNotString(request.title, "Post title must be a string")
    throwWhenNotString(request.content, "Post content must be a string")
    throwWhenNotArray(request.tags, "Post tags must be an array")
    request.tags.forEach(tag => throwWhenNotString(tag, "Post tag must be a string"))

    // construction -> feeling secure

    const post = new Post();
    post.title = cleanString(request.title)
    post.content = cleanString(request.content)
    post.tags = request.tags.map(tag => cleanString(tag))
    return post;
}