1.0.9 • Published 2 years ago

list-map-ts v1.0.9

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

list-map-ts project

This project provides Java-like List and Map functionalities

  1. Install
  2. Import
  3. List
    1. add()
    2. size()
    3. isEmpty()
    4. contains()
    5. remove() - removes an element from list
    6. clear() - removes all elements from the list
    7. get() - gets an element by index
    8. toArray() - converts the list to an array
    9. arrayToList() - converts a provided array to list
    10. forEach() - loops through the entire list
    11. where() - returns all the elements from the collection which satisfy a given condition.
    12. orderBy() - returns an array ordered by the specified property in either descending or ascending order

Usage:

step1: install

npm i list-map-ts

step2: import

import {KeyValueMap, List} from "list-map-ts";

a model to provide an object blue print

// a model
interface IUser {
    id: number
    email: string;
    name: string
}
// this helps to create a new user instance/object
const newUserObject = (u?: Partial<IUser>): IUser => {
    const defaults: IUser = {
        id: 0,
        name: '',
        email: ''
    }
    return {
        ...defaults,
        ...u
    }
}

List

instantiate list

// initialize list
const list = new List<IUser>()

add to list

// 1. add to list
list.add(newUserObject({id: 1, name: 'Daniel', email: 'daniel@example.com'}))
list.add(newUser({id: 2, name: ' Adam', email: 'adam@mail.com'}))

size of list

show how many elements are in list

// 
const size = list.size()
console.log(size) // prints 2

check if list is empty

console.log(list.isEmpty()) // prints false

check if list contains

const flag = list.contains(newUser({id: 1, name: ' Daniel', email: 'danie@mail.com'}))
console.log(flag) // prints true
        

forEach loop

        list.forEach((item: IUser, index: number) => {
            console.log(item, index)
        })

where condition

const t = list.where(item => item.email.includes('danie'))
console.log(t) // produces     [ { id: 1, name: ' Daniel', email: 'danie@mail.com' } ]

orderBy

        const t = list.orderBy('name', true)
        console.log(t)
/**
 * prints
 * 
 [
    { id: 2, name: ' Adam', email: 'adam@mail.com' },
    { id: 1, name: ' Daniel', email: 'danie@mail.com' }
 ]
 */

examples for other methods will be coming soon but everything works fine

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago