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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ngx-track-by-property

v0.0.2

Published

[![npm](https://img.shields.io/npm/v/ngx-track-by-property/latest.svg)](https://www.npmjs.com/package/ngx-track-by-property) [![Netlify Status](https://api.netlify.com/api/v1/badges/11932620-4735-4ea2-ba4d-e544ae9cfeb6/deploy-status)](https://app.netlify.

Downloads

11

Readme

NgxTrackByProperty

npm Netlify Status

An Angular pipe that makes it more convenient to use common trackByFunctions to improve performance of the *ngFor structural directive.

This is a little more complete version of @bennadels pipe he wrote about in his blog here and here.

Instead of declaring a trackByFunction in the component like this:

example.component.html

<div *ngFor="let item of items; trackBy: myTrackByFunction;">
    <p>{{ item }}</p>
</div>

example.component.ts

public myTrackByFunction(index: number, item: Item) {
    return item.id;
}

we just have to write code in the template:

example.component.html

<div *ngFor="let item of items; trackBy: 'id' | ngxTrackByProperty: items;">
    <p>{{ item }}</p>
</div>

The later solution is:

  • less verbose
  • as performant as the usual way
  • (mostly) equally type safe

Install

Make sure you are using Angular 12.

Install ngx-track-by-property from npm:

npm install ngx-track-by-property --save

Include lodash-es in your package.json. This package makes only use of its get method.

Add to your NgModule imports:

import { NgxTrackByPropertyModule } from 'ngx-track-by-property';

@NgModule({
  ...
  imports: [NgxTrackByPropertyModule,...]
  ...
})

You can then use this pipe in your component as follows:

<div *ngFor="let item of items; trackBy: '$index' | ngxTrackByProperty: items;">
    <p>{{ item }}</p>
</div>

API

This pipe has two parameters:

propertyNames

 Path[] | Path | '$index' | '$value',

The pipe returns a trackBy function that will track by the specified propertyNames. There are four different ways to specify the propertyNames:

  1. the literal '$index': track by the position of the item in the array use case example: readonly items: ReadonlyArray<any>
  2. the literal '$value': track by the value of the item. You should use this when the array is made up of primitives. The items can be objects too. But they are compared by value, not by reference. So if the items are objects they should be small, to still make this somewhat performant. use case example: items: (number | string | boolean | null | undefined)
  3. a path: track by the value of the item at the specified path. The path is specified as a string where the properties are separated by a '.' like 'user.id'. use case example: items: { id: number }[] - path: 'id' use case example: items: { user: { id: number; lastName: string }; house: any }[]- path: 'user.id'
  4. an array of paths: track by the combination of the values of the items at the specified paths. use case example: items: { firstName: string; lastName: string }[] - paths: ['firstName', 'lastName'] use case example: items: { user: { firstName: string; lastName: string }; house: any }[]- paths: '['user.firstName', 'user.lastName']'

If your trackByFunction is more complex, you should do it the usual way and write a function in the component.ts.

items

The array that is iterated over in the *ngFor directive. The items doesn't serve any runtime purpose. It is only used as a workaround for a type parameters in angular pipes, to provide better TypeScript typings. It is optional to provide.

TypeSafety

For the best typesafety you should provide the items parameter.

The property path is currently only checked one level deep. So in case case of: type Item = { user: { id: number; lastName: string }; house: any } - the path: 'user' would be typesafe (= you get a type error if you remove user from Item), but not the path 'user.id' (= you could also write user.abc without a type error).

Code scaffolding

Run ng generate component component-name --project ngx-track-by-property to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module --project ngx-track-by-property.

Note: Don't forget to add --project ngx-track-by-property or else it will be added to the default project in your angular.json file.

Build

Run ng build ngx-track-by-property to build the project. The build artifacts will be stored in the dist/ directory.

Publishing

After building your library with ng build ngx-track-by-property, go to the dist folder cd dist/ngx-track-by-property and run npm publish.

Running unit tests

Run ng test ngx-track-by-property to execute the unit tests via Karma.