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

@simpli/resource-collection

v1.1.7

Published

A data-structure library to work with ResourceCollections (with Id and Tag) and PageCollections (search and order as well)

Downloads

46

Readme

Resource-Collection

A data-structure library to work with ResourceCollections (with Id and Tag) and PageCollections (search and order as well)

Install

npm i @simpli/resource-collection class-transformer

Usage

ResourceCollection

ResourceCollection is a Collection of items containing id and tag

Import

import { ResourceCollection, IResource } from '@simpli/resource-collection'

Create your resource class

Your class must implement IResource with $id and $tag props or getters

class MyResource implements IResource {
  get $id (){ return this.myId }
  get $tag () { return this.myName }

  myId: number
  myName: string

  constructor(myId: number, myName: string) {
    this.myId = myId
    this.myName = myName
  }
}

Instantiate a ResourceCollection

You must informing the resource type and optionally an array of items


 const subject = new ResourceCollection(MyResource, [
  new MyResource(1, 'first'),
  new MyResource(2, 'second'),
  new MyResource(3, 'third'),
  new MyResource(4, 'fourth')
])

ResourceCollection basic methods

subject.size() // returns 4
subject.isEmpty() // returns false
subject.first().$tag // returns 'first'
subject.last().$tag // returns 'fourth'

const allWPH = subject.allWithPlaceholder('hint')
allWPH[0].$tag // returns 'hint'

const allWPHnull = subject.allWithPlaceholder()
allWPHnull[0] // returns null

subject.getById(2).$tag // returns 'second'

const twoAnd3 = subject.getManyIds([2, 3])
twoAnd3[0].$tag // returns 'second'
twoAnd3[1].$tag // returns 'third'

const thebad = new MyResource(666, 'from hell')
const thegood = new MyResource(777, 'mighty one')

subject.add(thebad) // adds in the last position
subject.add(thegood, 0) // adds in the first position

subject.remove(thebad) // thebad was removed
subject.removeById(3) // removed item with id 3
subject.remove(new MyResource(123212, 'unexistent entry')) // not found, no item was removed
subject.removeById(123212) // not found, no item was removed

subject.clear() // remove all items

ResourceCollection has 'filter' features

subject.addFilter({ abc: 123, ignoreMe: null, you: 'and me' })
subject.params // ignores null fields and returns { abc: 123, you: 'and me' }
subject.clearFilters() // remove all fields from filter

PageCollection

PageCollection is useful to work with paginated collections, it extends ResourceCollection

Import

import { PageCollection, IResource } from '@simpli/resource-collection'

Create your PageCollection

Differently of ResourceCollection, you must create a class for your collection

class MyPageCollection extends PageCollection<MyResource> {
 constructor() {
   super(MyResource)
 }

 async queryAsPage() {
   // implement how to update itself with API calls
   // send the PageCollection properties to the server
   // { search, currentPage, perPage, orderBy, asc }
   // and set other PageCollection properties on response
   // this.total = <the amount of all items, as if it wasn't paginated>
   // this.items = <the items on the current page>
   // the class is prepared to be used with class-transformer library
   
   // example using @simpli/serialized-request:
   return await Request.get(`/mything`, {params: this.params})
         .as(this)
         .getResponse()
 }

}

PageCollection props and methods

const subject = new MyPageCollection()

subject.setPerPage(20)
subject.lastPage
// if total is 90 the lastPage will be 4. It contains 5 pages, the last page with only 10 items

subject.setCurrentPage(4)
subject.isLastPage // returns true

await subject.queryCurrentPage(3)
// will change the page securely and call queryAsPage

await subject.queryPrevPage()
// will change the page securely and call queryAsPage

await subject.queryNextPage()
// will change the page securely and call queryAsPage

subject.noPagination() // to remove pagination properties
subject.currentPage // returns null
subject.perPage // returns null

await subject.querySearch()
// will call queryAsPage IF the search prop null, empty
// or >= PageCollection.defaultMinCharToSearch, which is 2 by default
// to avoid searching with insufficient caracters

await subject.queryOrderBy('column')
//sets the orderBy field, toggle asc and call queryAsPage
subject.orderBy // returns 'column'
subject.asc // returns true
await subject.queryOrderBy('column')
subject.asc // returns false

Create your ExpansibleCollection

This collection can concat new items. Note this class expands PageCollection, so this can be used as a regular pagination too.

class MyExpansibleCollection extends ExpansibleCollection<MyResource> {
 constructor() {
   super(MyResource)
 }

 customFilter: string | null = null  

 async queryAsPage() {
   // implement how to update itself with API calls
   // send the PageCollection properties to the server
   // { search, currentPage, perPage, orderBy, asc }
   // and set other PageCollection properties on response
   // this.total = <the amount of all items, as if it wasn't paginated>
   // this.items = <the items on the current page>
   // the class is prepared to be used with class-transformer library
   
   // example using @simpli/serialized-request:
   return await Request.get(`/mything`, {params: this.params})
         .as(this)
         .getResponse()
 }

}

ResourceCollection props and methods

const subject = new MyExpansibleCollection()

subject.setPerPage(20)

await subject.expand() // showing 20 items
await subject.expand() // showing more 20 items; total = 40
await subject.expand() // showing more 20 items; total = 60

subject.customFilter = 'foo'

await subject.update() // updates the information after the filter; still 60 items
// or
await subject.queryToFirstPage() // reset collection to its initial state; showing 20 items

await subject.queryOrderBy('bar') // apply orderby filter