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-dictionary-pipe

v1.2.2

Published

Angular 8 pipe for easy dictionary/master data ID to name mapping.

Downloads

3

Readme

ngx-dictionary-pipe

Angular 8 pipe for easy dictionary/master data ID to name mapping.

This module is unit-tested. If there is a failing case, please submit an issue so someone can fill a test and fix it.

Badges and other stuff are pending, check TODO list further below.

Installation

Add NgxDictionaryPipeModule to your module imports like this:

// import it
import { NgxDictionaryPipeModule } from 'ngx-dictionary-pipe';

@NgModule({
  declarations: [ ... ],
  imports: [
    ...,
    NgxDictionaryPipeModule // add to module
  ],
})
export class AppModule { }

How to use

Define a variable "masterData" with the following data:

{
  "books": [
    {
      "id": 1,
      "name": "The dawn of the night"
    },
    {
      "id": 2,
      "name": "The dawn of the morning"
    }
  ]
}

Define a variable "myBooksIds" with the following data:

[ 1, 3 ]

Then, use the dictionary pipe to replace ID occurrences, like this:

<ul>
   <li *ngFor="let bookId in myBooksIds">
      {{ bookId | dictionary : masterData : ['books[]', 'id'] : 'name' }}
   </li>
</ul>

Alternatively, ['books[]', 'id'] can be written as 'books[].id'.

This can be understood as:

const valueToSearch = bookId;
const dictionary = masterData;
const path = ['books[]'];
const valueKey = 'id'; // must equal valueToSearch
const nameKey = 'name'; // resulting readable name

This will render the following output:

<ul>
   <li>
      The dawn of the night
   </li>
   <li>
      ???
   </li>
</ul>

Troubleshooting

Pipe is printing a blank string

This value is returned if there was an error on the dictionary search. Try setting debug = true when invoking the pipe for displaying debug errors ?, ?? and ???.

e.g. instead of {{ bookId | dictionary : masterData : ['books[]', 'id'] : 'name' }},

use {{ bookId | dictionary : masterData : ['books[]', 'id'] : 'name' : true }},

If debug is enabled, and it is still printing a blank string, this means the value to search was undefined, null or empty. It's probably the most preferred behaviour.

Pipe is printing ?

This means the path was not found, e.g. masterData.bookse in the previous example.

Pipe is printing ??

This means the id key was not found, e.g. masterData.books.ide in the previous example.

Pipe is printing ???

This means an item with the specified value was not found, e.g. 3 in the previous example.

Is this useful?

Personally speaking, it greatly simplifies many problems I regularly face when I need to replace IDs with names.

If I had to say, it is.

TODO:

  • Document abbreviated syntax, e.g. stores.books{} instead of ['stores', 'books'].
  • Document [] operator for a loop search, e.g. stores[].books.uuid.
  • Document {} operator for a map search, e.g. stores.books{}.
  • Document support for value inside [] and {} operators to specify a key to lookup, e.g. stores[1].books{b}.authors and stores[id:1].books{name:b}.authors.
  • To consider: Accepting a function instead of nameKey = name for custom browsing, e.g. myFunction(value: any, ...pathItems: any[]) where pathItems is the transversal stack.
  • Add "bdictionary" for a binary search based dictionary (performance).
  • Add debug flag if required.
  • Simplify repository for NPM publish.
  • Allow for simple and quick inheritance for enums handling, e.g. {{ statusId | status }}.
  • Document support for [] operator at the end of query to collect multiple results instead of returning immediately (filtering).
  • In consideration: Add collecting to map support, e.g. books.id{} would make a map { [id]: item }, while books.id{prop} would group (?).