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

chai-latte

v0.2.3

Published

Build expressive & readable fluent interface libraries.

Downloads

37

Readme

✨ What is chai-latte?

It's a tool for building expressive fluent interface libraries. Think of it as a simple tool to buid libraries à la chaijs but not necessarly related to testing. It can be about anyhting :).

🤔 Motivation

🔌 Installation

It's available on npm. To install it, type:

npm i chai-latte

or

yarn add chai-latte

:rocket: Quick Start!

Lets create a library that will be able to create arrays and add number to it. I know... The example is not exiting 😅. But let's create it anyway! The library we'll create should be able to execute the command below:

import example from './the/library/we/will/create';

const { create, add } = example;

const arr = create.an.array();
add(4).to.this.array(arr)
add(5).to(arr)

const arr2 = create.a.copy.of(arr);
add(5).to(arr2);
  1. Let's create our first expression. The expression function takes two arguments. The first argument should be a function that shows how to use the expression. The second argument is the callback. See example below:
import { expression } from 'chai-latte';

const ourFirstExpression = expression(
  ({ create }) => create.an.array(), 
  () => {
    console.log('Array Created!');
    return [];
  }
);
  1. Make the expression executable. expression alone just returns a configuration object. we should compile it in order to use it. See example:
import { expression, compile } from 'chai-latte';

const ourLibrary = compile(
  expression(
    ({ create }) => create.an.array(), 
    () => {
      console.log('Array Created!');
      return [];
    }
  )
);

// 

const { create } = ourLibrary;
const arr = create.an.array(); // [] and logged 'Array Created!'.
  1. Add the other expressions! In order to do so, just add them as extra argument to the compile function. See example:
const addNumToArray = (num: number, arr: any[]) => arr.push(num);
const createArray = () : any[]=> [];
const cloneArray = (arr: any[]) : any[] => [...arr];

const ourLibrary = compile(
  expression(({ create }) => create.an.array(), createArray),
  expression(({ create }) => create.a.copy.of(Array), cloneArray),
  expression(({ add }) => add(Number).to.this.array(Array), addNumToArray),
  expression(({ add }) => add(Number).to(Array), addNumToArray),
);
  1. Congratulations!! 🎉 You can now use your library! See example:
const { create, add } = ourLibrary;

const arr = create.an.array();
add(4).to.this.array(arr)
add(5).to(arr)
console.log(arr) // logs [4, 5]

const arr2 = create.a.copy.of(arr);
add(12).to(arr2);
console.log(arr2) // logs [4, 5, 12]

📚 Typescript

To make your library type-safe, you can generate the corresponding typings using the command line. It will create a new file called generated.ts that will export the same fluent interface but fully typed. Then simply use this new file as your entry file! To create the generated.ts type this in the terminal:

npx chai-latte ./index.ts

🙈 Things to know & Limitations

  1. When creating expressions, you have to provide a class for each callable parts. This is how the engine avoid conflicts.
// example
class Human {}
class Child extends Human {}

the(Human).can.jump(); // expression 1
the(Child).will.play(); // expression 2

// if we try to call
const human = new Human();
const child = new Child();
the(child).can.jump(); // <-- will work since Child inherits Human
the(human).will.play(); // <- won't work because .will.play() isn't defined for expression 1
  1. The last word on an expression has to be function. It can accept an argument.
// example
the(Human).works.up.until(Number);
the(Human).can.jump();
the(Human).is.alive // <- won't compile because it doesn't end with a function
  1. Expressions that share the exact same code path and argument types cannot co-exist. Here is an example of 3 expressions to illustrate the issue:
// example
the(Human).can.say('Hi!');
the(Baby).can.say('Hi!'); // <- will work, because Human and Baby are different classes
the(Baby).can.say('Hi!').and.say('I\'m hungry'); // <- won't compile

📚 Typescript

You can generate the corresponding typings using the command line. It will create a new file called generated.ts next to index.ts. This file will export yout same fluent interface but fully typed. Use this new file as your entry file to enjoy the typngs! Ensure ts-node is installed locally then type this in the terminal:

npx chai-latte

:handshake: Contributing

Thank you very much for considering to contribute! Please don't hesitate to create issues and PRs. Your contribution is very much welcome!.

:book: License

The MIT License

Copyright (c) 2022 Chai-Latte

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.