5.4.0 • Published 1 day ago

collecty v5.4.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 day ago

Collecty

This is an amazing library that allows you to create collections and iterate over them, allowing you to create functionality for the collections and the elements it iterates over.

instalation

npm install collecty

basic usge

for example, maybe you could have an array of authors as a json format:

const authorsArray = [
    {
        "name": "Arthur",
        "gender": "male",
        "age": 15
    },
    {
        "name": "Veronica",
        "gender": "female",
        "age": 40
    },
    {
        "name": "Johnson",
        "gender": "male",
        "age": 33
    }
]

import the Collection

If you are using ECMAScript Modules:

import { Collection } from 'collecty';

If you are using CommonJS:

const Collection = require('collecty').Collection;

If you are using the web browser:

<script src="https://unpkg.com/collecty@latest/dist/umd/index.umd.cjs"></script>
<script>

    const Collection = Collecty.Collection
    // ...

</script>

in the next code, as you can see, you can create your own collection, allowing you to transform each element that it has from an object to your custom Author class

class AuthorCollection extends Collection {

    /**
     * each iteration will return an Author
     * 
     **/
    public item(item:any): Author {
        return new Author(item)
    }

    /**
     * You can create your custom filters for your own collection
     * 
     **/
    public malePeople(): AuthorCollection{
        // ... code
    }

    public femalePeople(): AuthorCollection {
        // ... code
    }
}

class Author {
    public item;

    constructor(item:any) {
        this.item = item
    }

    public gender(): boolean {
        // ... code
    }

    public isLegalAge(): boolean {
        // .. code
    }
}

const authors = new AuthorCollection(authorsArray)

In the code above, you can easily get the male or female people with an easy-to-read-syntax:

authors.malePeople()

and you can know if each person is legal age or not:

for (const author of authors) {
    console.log(author.isLegalAge())
}

available methods

concat( array: any[] ): void

const collection = new AuthorCollection([
    {
        "name": "Arthur",
        "gender": "male",
        "age": 15
    },
    {
        "name": "Veronica",
        "gender": "female",
        "age": 40
    },
    {
        "name": "Johnson",
        "gender": "male",
        "age": 33
    }
])

collection.concat([{
    "name": "Carl",
    "gender": "male",
    "age": 56
}])

console.log(collection.toArray())

// output
>   [{
        "name": "Arthur",
        "gender": "male",
        "age": 15
    },
    {
        "name": "Veronica",
        "gender": "female",
        "age": 40
    },
    {
        "name": "Johnson",
        "gender": "male",
        "age": 33
    }, {
        "name": "Carl",
        "gender": "male",
        "age": 56
    }]  

contains( callback : (item:any) => any ): boolean

checks if the collection contains at least one item which matches with the callback

const collection = new AuthorCollection([{
    "name": "Johnson",
    "gender": "male",
    "age": 33
}, {
    "name": "Carl",
    "gender": "male",
    "age": 56
}])

collection.contains( (author: Author) => author.age() > 50 )

// output
> true

collection.contains((author: Author) => {
    return author.age() < 30
})

// output
> false

count(): number

gets total items in the collection

const collection = new AuthorCollection([{
    "name": "Johnson",
    "gender": "male",
    "age": 33
}, {
    "name": "Carl",
    "gender": "male",
    "age": 56
}]);

console.log("total", collection.count())

// output
> total 3

filter( callback: (any) => any ): any

returns a new collection with the items that match with the callback given

let collection = new Collection([1, 2, 3])

let newCollection = collection.filter((item: number) => {
   return item <= 2
})

console.log(newCollection.toArray())
// output
> [1,2]

first(): any

gets the first item in the collection

const collection = new Collection([1,2,3])
console.log("first element", collection.first())

// output
> first element 1

firstWhere( callback: (any) => any ): any

gets the first item which match with the callback condition

let persons = new PersonCollection([{
        'name': 'rix'
 }, {
     'name': 'roger'
 }])

 const person = persons.firstWhere((person: Person) => {
     return person.name() == "roger"
 })

// output
> Person { item { name: "roger" } }

fromJson( json: string ): Collection

creates a new collection from a json

collection = Collection.fromJson("[1,2,3]")

get(): any

gets the item acording to the given index

const collection = new Collection([1, 2, 3])
console.log(collection.get(1))
 
 \\ output
> 2

isEmpty(): boolean

checks if the collection has at least one item

const collection = new Collection([1,2,3])
console.log(collection.isEmpty())

// output
> false
const collection = new Collection([])
console.log(collection.isEmpty())

// output
> true

map( callback: (any) => any ): Collection

returns a collection with the data mapped for each element

class PersonCollection extends Collection {
    item(item: any): Person {
        return new Person(item)
    }
}

class Person {

    private item;

    constructor(item: any) {
        this.item = item
    }

    public name() {
        return this.item.name
    }
}

let persons = new PersonCollection([{
    'name': 'rix'
}, {
    'name': 'roger'
}])

const names = persons.map((person: Person) => {
    return person.name()
})

pop(item:any): void

let collection = new Collection([1,2,3])

console.log(collection.pop())
// output
> 3

console.log(collection.torray())
// output
> [1,2]

push(item:any): void

push new item to the collection

let collection = new Collection([1,2,3])

collection.push(4)

collection.toArray()

// output
> [1,2,3,4]

random(): any

gets a random item

collection = new Collection([1,2,3])

console.log(collection.random())

//output
> 3 (obtained randomly)

toArray(): any[]

Transforms the collection to a javascript native array

collection = new Collection([1,2,3])

console.log(collection.toArray())

// output
> [1,2,3]

TO-DO list

  • last method
  • flatten method
  • notContains method
  • merge method
  • StringCollection class
  • ObjectCollection class
  • NumberCollection class

Are you a collaborator?

if you whish to collaborate to Collecty, you can pull request to the repository: click here to go to the repository

How to publish the package?

  1. commit the changes:
git commit -am <message>
  1. Change the version
npm version <patch | minor | major>
  1. Login (if not already logged in)
npm login
  1. Publish the package
npm publish
5.3.0

2 days ago

5.4.0

1 day ago

5.2.0

8 days ago

5.1.0

29 days ago

5.0.1

1 month ago

5.0.0

1 month ago

4.1.0

2 months ago

4.0.2

2 months ago

4.0.1

2 months ago

4.0.0

2 months ago

2.2.0

4 months ago

2.6.1

4 months ago

3.0.0

4 months ago

2.3.0

4 months ago

2.0.14

4 months ago

2.0.12

4 months ago

2.1.0

4 months ago

2.0.3

6 months ago

2.0.5

6 months ago

2.0.4

6 months ago

2.0.11

6 months ago

2.0.7

6 months ago

2.0.6

6 months ago

2.0.9

6 months ago

2.0.10

6 months ago

2.0.8

6 months ago

2.0.1

6 months ago

2.0.0

6 months ago

1.14.0

1 year ago

1.13.2

1 year ago

1.13.1

1 year ago

1.13.0

1 year ago

1.12.0

1 year ago

1.11.0

1 year ago

1.10.0

1 year ago

1.9.3

1 year ago

1.9.2

1 year ago

1.9.1

1 year ago

1.9.0

1 year ago

1.8.0

1 year ago

1.7.2

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

1.6.0

1 year ago

1.5.0

1 year ago

1.4.0

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago