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

ngx-pipes-toolkit

v0.2.1

Published

Complementary pipes library for Angular

Downloads

58

Readme

NGX Pipes Toolkit

A complementary pipes library for Angular

Table of contents

Motivation

ngx-pipes-toolkit library was born from the need to "wrap" the utility functions of Lodash for a direct usage in the templates, especially the mathematical ones.

Typically, we manipulate collections of objects fetched from a REST API, and we want to display to the users some statistics based on a property of those objects. For example, we might want to display the mean age for a collection of users.

However, native Angular pipes or ones from other NPM packages like NGX Pipes or Angular Pipes handle only data from native types (i.e. number, string, or boolean). Consequently, it is not possible to compute directly in the template the mean age. Therefore, we need to declare a derived value from the fetched collection in the component class (signals are used for convenience):

import { meanBy } from 'lodash';

export type User = {
  id: string;
  age: number;
};

@Component({
  template: `
    <p>Mean age: {{ $meanAge() }}
  `,
})
export class DashboardComponent {
  readonly $users: Signal<User[]> = this.userApiService.fetchUsers();

  readonly $meanAge = computed<number>(() => {
    return meanBy(this.$users(), 'age');
  });
}

Even if this derived value $meanAge is relatively simple, it adds an extra layer for a straightforward computation using utility functions.

The primary motivation behind ngx-pipes-toolkit is to allow direct manipulation of collection of objets without declaring intermediate fields in the component class, thereby simplifying the code:

import { MeanByPipe } from 'ngx-pipes-toolkit';

export type User = {
  id: string;
  age: number;
};

@Component({
  imports: [MeanByPipe],
  template: `
    <p>Mean age: {{ $users() | meanBy:'age' }}
  `,
})
export class DashboardComponent {
  readonly $users: Signal<User[]> = this.userApiService.fetchUsers();
}

Moreover, the aim of ngx-pipes-toolkit is not to replace other libraries, but to complement them. That's why there are no duplicate pipes with the other main Angular pipes library: NGX Pipes.

Installation

Use NPM to install the package

$ npm install ngx-pipes-toolkit --save

Usage

All pipes in ngx-pipes-toolkit are implemented as standalone pipes. There are no Angular modules that wrap the pipes. Therefore, you can directly use them in your components or templates by importing them where needed. Furthermore, all pipes are pure.

  • In templates:
<p>{{ 1_000 | convert:'m':'km' }}</p> <!-- Returns "1" -->
  • In components:
import { ConvertPipe } from 'ngx-pipes-toolkit';

@Component({
  providers: [ConvertPipe],
})
export class MyComponent {
  readonly convertPipe = inject(ConvertPipe);

  readonly conversion = this.convertPipe.transform(1_000, 'm', 'km'); // Returns "1"
}

Content

Array

Enumerate

Allows to use *ngFor directive or @for control flow block like a classic for loop (sometimes we just want to render something in the template a certain number of times, independently from a collection of objets)

@for (index of $loadingCount() | enumerate; track index) {
  <!-- Display skeletons while loading... -->
}
<ngx-skeleton *ngFor="let index of $loadingCount() | enumerate" />

Math

Convert

Converts a value with its current unit to the final unit

<p>{{ 1_000 | convert:'m':'km' }}</p> <!-- Output: "1" -->
<p>{{ 32 | convert:'F':'C' }}</p> <!-- Output: "0" -->

Mean by

Calculates the mean of an array of objects based on one of their number, parsable string or Date property

const users = [
  { name: 'Alice', age: 20 },
  { name: 'Bob', age: 30 },
]
const games = [
  { name: 'Mass Effect 1', price: 30 }
  { name: 'Mass Effect Andromeda', price: 40 }
]
<p>{{ users | meanBy:'age' }}</p> <!-- Output: "25" -->
<p>{{ games | meanBy:'price' }}</p> <!-- Output: "35" -->

Sum by

Calculates the sum of an array of objects based on one of their number, parsable string or Date property

const users = [
  { name: 'Alice', age: 20 },
  { name: 'Bob', age: 30 },
]
const games = [
  { name: 'Mass Effect 1', price: 30 }
  { name: 'Mass Effect Andromeda', price: 40 }
]
<p>{{ users | sumBy:'age' }}</p> <!-- Output: "50" -->
<p>{{ games | sumBy:'price' }}</p> <!-- Output: "70" -->

Min by

Returns the item with the smallest value of an array of object based on one of their number, parsable string or Date property

const users = [
  { name: 'Alice', age: 20 },
  { name: 'Bob', age: 30 },
]
const games = [
  { name: 'Mass Effect 1', price: 30 }
  { name: 'Mass Effect Andromeda', price: 40 }
]
<p>{{ users | minBy:'age' }}</p> <!-- Output: "{ name: 'Alice', age: 20 }" -->
<p>{{ games | minBy:'price' }}</p> <!-- Output: "{ name: 'Mass Effect 1', price: 30 }" -->

Max by

Returns the item with the largest value of an array of object based on one of their number, parsable string or Date property

const users = [
  { name: 'Alice', age: 20 },
  { name: 'Bob', age: 30 },
]
const games = [
  { name: 'Mass Effect 1', price: 30 }
  { name: 'Mass Effect Andromeda', price: 40 }
]
<p>{{ users | maxBy:'age' }}</p> <!-- Output: "{ name: 'Bob', age: 30 }" -->
<p>{{ games | maxBy:'price' }}</p> <!-- Output: "{ name: 'Mass Effect Andromeda', price: 40 }" -->

Contributing

  • Any contribution is appreciated
  • If you are planning to add a new pipe (or any new other feature) or make a fix, please open an issue before
  • Follow the conventional commit message format
  1. Clone the project
$ git clone https://github.com/SlyTed/ngx-pipes-toolkit.git
  1. Install the dependencies
$ npm install
  1. Make your changes in a new branch
$ git checkout -b <my-branch>
  1. Add appropriate tests (cover all cases) and make sure everything is running properly (don't forget to lint)
$ npm run test:coverage
$ npm run lint
  1. Push your branch

  2. Make a pull request