Skip to content

spiritmong3r/sleekify

Repository files navigation

Sublime's custom image

Typescript library that offers elegant and powerful utility functions.


npm version codecov spiritmong3r

Why sleekify ?

Sleekify provides powerful features that make your code more lightweight.

  • Fluent iterable API

    Offers many operations thanks to both List and MutableList, which are iterable implementations.

  • Type safe

    The whole librairy is implemented in typescript.

  • Powerful utility function

    when function is a switch-like expression, it is more elegant than traditional conditional statements.

Getting start

Installation

npm i sleekify

Usage

import {List, MutableList, listOf, mutableListOf, when} from 'sleekify';

API documentation


List

List is an array wrapper that offers many operations. It is an immutable iterable.

all

Check if every element matches the predicate, if that's the case then returns true, else false.

example :

const values = new List([1, 2, 3, 4, 5]);
values.all((value) => !isNaN(value)); // returns true
values.all((value) => isNaN(value)); // returns false

any

Check if there's at least one element matching the predicate, if that's the case then returns true, else false.

Alias for some function.

example :

const values = new List([1, 2, 3, 4, 5]);
values.any((value) => value === 3); // returns true
values.any((value) => value === 0); // returns false

contains

Check if there's at least one element matching the given entry, if that's the case then returns true, else false.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.contains({name: 'Jo', age: 22}); // returns true
values.contains({name: 'Jo', age: 23}); // returns false

containsAll

Check if the given entries are presents in the list, if that's the case then returns true, else false.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.containsAll([
    {name: 'Bob', age: 18},
    {name: 'Jo', age: 22},
]); // returns true
values.containsAll({name: 'Bob', age: 18}, {name: 'Jo', age: 23}); // returns false

count

Returns the number of elements matching the given predicate. If no predicate then behaves just like length

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.count(); // returns 2
values.count((value) => value.age === 18); // returns 1

distinct

Returns a new List without any duplicates. If a predicate is given then only duplicates among the matching elements will be removed

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo2: Person = {name: 'Jo', age: 22};
const jo3: Person = {name: 'Jo', age: 23};

const values = new List([bob, jo, jo2, jo3]);
values.distinct(); // returns List([bob, jo, jo3])
values.distinct((value) => value.name === 'Jo'); // returns List([bob, jo])

drop

Returns a new List without the n first elements

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.drop(1); // returns List([jo])

dropLast

Returns a new List without the n last elements

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.dropLast(1); // returns List([bob])

filter

Returns a new List with only the elements matching the predicate

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.filter((value) => value.age === 18); // returns List([bob])

find

Returns the first element matching the predicate

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new List([bob, jo, jo1]);
values.first((value) => value.name === 'jo'); // returns jo
values.first((value) => value.name === 'jane'); // returns undefined

first

Returns the first element matching the predicate, throw an error if there's not matching

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new List([bob, jo, jo1]);
values.first((value) => value.name === 'jo'); // returns jo
values.first((value) => value.name === 'jane'); // throw an error 'No value matches the predicate'

firstOrUndefined

Returns the first element matching the predicate, alias for find function

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new List([bob, jo, jo1]);
values.firstOrUndefined((value) => value.name === 'jo'); // returns jo
values.firstOrUndefined((value) => value.name === 'jane'); // returns undefined

flatMap

Returns a new List, apply the given transformer and then flatten (1 level deep) the results

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([[bob], jo]);
values.flatMap((value) => value.name); // returns List(['bob', 'jo'])

flatten

Returns a new List flattened 1 level deep by default, if a depth is specified then apply it

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jane: Person = {name: 'Jo', age: 22};

const values = new List([[bob], jo, [[jane]]]);
values.flatten(); // returns List([bob, jo, [jane]])
values.flatten(2); // returns List([bob, jo, jane])

forEach

void function that applies a given action on every elements of the List

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.forEach((value) => (value.age = 18)); // returns nothing but every Person of the List are now 18

get

Returns the element at the given index or undefined if the index doesn't exists

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.get(0); // returns bob
values.get(2); // returns undefined

groupBy

Returns a Map object where the key is provided by the given selector and value is an array of all the elements matching this key

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jane: Person = {name: 'Jane', age: 22};

const values = new List([bob, jo, jane]);
values.groupBy((value) => value.age);
// returns a map like so:
// Map([
//    [18, [{name: 'Bob', age: 18}]],
//    [22, [{name: 'Jo', age: 22}, {name: 'Jane', age: 22}]]
// ])

indexOf

Find the first index at which a given element can be found in the array

example :

const values = new List([1, 2, 3, 4, 5]);
values.indexOf(5); // returns 4
values.indexOf(11); // returns -1

isEmpty

Check if the List is empty or not

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

new List([bob, jo]).isEmpty(); // returns false
new List().isEmpty(); // returns true

isNotEmpty

Check if the List is empty or not

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

new List([bob, jo]).isNotEmpty(); // returns true
new List().isNotEmpty(); // returns false

join

Returns a string resulting from converting each element of the List to a string and then concatenating them together

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.join({separator: ' / '}, (value) => value.name); // returns 'bob / jo'

last

Returns the last element matching the predicate, throw an error if there's not matching

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new List([bob, jo, jo1]);
values.last((value) => value.name === 'jo'); // returns jo1
values.last((value) => value.name === 'jane'); // throw an error 'No value matches the predicate'

lastOrUndefined

Returns the last element matching the predicate, or undefined if no matching

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new List([bob, jo, jo1]);
values.lastOrUndefined((value) => value.name === 'jo'); // returns jo1
values.lastOrUndefined((value) => value.name === 'jane'); // returns undefined

map

Returns a new List where a given transformer is applied on every elements

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.map((value) => {
    value.age = 18;
    return value;
}); // returns a new List similar to values but where every Person is now 18

max

Returns the max value or object according to the given selector.

If no selector, then just returns the max among all values. The array must consist of numbers only, otherwise an error is thrown.

examples :

const values = new List([1, 2, 3, 4, 5]);
values.max(); // returns 5
const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.max((value) => value.age); // returns jo
values.max(); // throw an Error 'Type of array is not number'

min

Returns the min value or object according to the given selector.

If no selector, then just returns the min among all values. The array must consist of numbers only, otherwise an error is thrown.

examples :

const values = new List([1, 2, 3, 4, 5]);
values.min(); // returns 1
const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.min((value) => value.age); // returns bob
values.min(); // throw an Error 'Type of array is not number'

none

Check if there's no element matching the predicate, if that's the case then returns true, else false

example :

const values = new List([1, 2, 3, 4, 5]);
values.none((value) => value === 3); // returns false
values.none((value) => value === 0); // returns true

onEach

Returns a new List where a given action is applied on every elements, the selector silently returns this.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.onEach((value) => (value.age = 18)); // returns a new List similar to values but where every Person is now 18

reduce

Returns a value obtained after an operation (accumulator) is applied on every element of the List.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.reduce((acc, value) => acc + value.age, 0); // returns 40

reverse

Returns a new List where all elements are reversed: first element become last, last become first and so on.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.reverse(); // returns List([jo, bob])

size

Returns the number of elements in the List.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.size(); // returns 2

some

Check if there's at least one element matching the predicate, if that's the case then returns true, else false.

example :

const values = new List([1, 2, 3, 4, 5]);
values.some((value) => value === 3); // returns true
values.some((value) => value === 0); // returns false

sort

Returns a new List where elements are sorted according to the selector if given.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([jo, bob]);
values.sort((value) => value.age); // returns List([bob, jo])

subList

Create a new list containing the elements between the given indexes.

example :

const values = new List([1, 2, 3, 4, 5]);
values.subList(1, 3); // returns List([2, 3]);
values.subList(2); // returns List([3, 4, 5]);
values.subList(-2); // returns List([4, 5]);

sum

Calculate the sum of the array according to the selector if given.

If no selector is given, the List must be composed of numbers otherwise an error will be thrown.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.sum((value) => value.age); // returns 40
values.sum(); // throw an error 'Type of array is not number'

take

Returns a new List with only the n first elements.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.take(1); // returns List([bob])

takeLast

Returns a new List with only the n last elements.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.takeLast(1); // returns List([jo])

toArray

Returns an array out of the List.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.toArray(); // returns [bob, jo]

MutableList

MutableList is an array wrapper that offers many operations. It is a mutable iterable.

add

Add a new element to the current MutableList and returns this.

Mutable operation.

example :

const values = new MutableList([1, 2, 3, 4, 5]);
values.add(6); // returns MutableList([1, 2, 3, 4, 5, 6])

all

Check if every element matches the predicate, if that's the case then returns true, else false.

example :

const values = new MutableList([1, 2, 3, 4, 5]);
values.all((value) => !isNaN(value)); // returns true
values.all((value) => isNaN(value)); // returns false

any

Check if there's at least one element matching the predicate, if that's the case then returns true, else false.

Alias for some function.

example :

const values = new MutableList([1, 2, 3, 4, 5]);
values.any((value) => value === 3); // returns true
values.any((value) => value === 0); // returns false

clear

Remove all elements from the current list.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new List([bob, jo]);
values.clear(); // returns an empty MutableList

contains

Check if there's at least one element matching the given entry, if that's the case then returns true, else false.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.contains({name: 'Jo', age: 22}); // returns true
values.contains({name: 'Jo', age: 23}); // returns false

containsAll

Check if the given entries are presents in the list, if that's the case then returns true, else false.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.containsAll([
    {name: 'Bob', age: 18},
    {name: 'Jo', age: 22},
]); // returns true
values.containsAll({name: 'Bob', age: 18}, {name: 'Jo', age: 23}); // returns false

count

Returns the number of elements matching the given predicate. If no predicate then behaves just like length

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.count(); // returns 2
values.count((value) => value.age === 18); // returns 1

distinct

Returns a new MutableList without any duplicates. If a predicate is given then only duplicates among the matching elements will be removed

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo2: Person = {name: 'Jo', age: 22};
const jo3: Person = {name: 'Jo', age: 23};

const values = new MutableList([bob, jo, jo2, jo3]);
values.distinct(); // returns MutableList([bob, jo, jo3])
values.distinct((value) => value.name === 'Jo'); // returns MutableList([bob, jo])

drop

Returns a new MutableList without the n first elements

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.drop(1); // returns MutableList([jo])

dropLast

Returns a new MutableList without the n last elements

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.dropLast(1); // returns MutableList([bob])

filter

Returns a new MutableList with only the elements matching the predicate

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.filter((value) => value.age === 18); // returns MutableList([bob])

find

Returns the first element matching the predicate

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new MutableList([bob, jo, jo1]);
values.first((value) => value.name === 'jo'); // returns jo
values.first((value) => value.name === 'jane'); // returns undefined

first

Returns the first element matching the predicate, throw an error if there's not matching

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new MutableList([bob, jo, jo1]);
values.first((value) => value.name === 'jo'); // returns jo
values.first((value) => value.name === 'jane'); // throw an error 'No value matches the predicate'

firstOrUndefined

Returns the first element matching the predicate, alias for find function

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new MutableList([bob, jo, jo1]);
values.firstOrUndefined((value) => value.name === 'jo'); // returns jo
values.firstOrUndefined((value) => value.name === 'jane'); // returns undefined

flatMap

Returns a new MutableList, apply the given transformer and then flatten (1 level deep) the results

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([[bob], jo]);
values.flatMap((value) => value.name); // returns MutableList(['bob', 'jo'])

flatten

Returns a new MutableList flattened 1 level deep by default, if a depth is specified then apply it

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jane: Person = {name: 'Jo', age: 22};

const values = new MutableList([[bob], jo, [[jane]]]);
values.flatten(); // returns MutableList([bob, jo, [jane]])
values.flatten(2); // returns MutableList([bob, jo, jane])

forEach

void function that applies a given action on every elements of the MutableList

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.forEach((value) => (value.age = 18)); // returns nothing but every Person of the MutableList are now 18

get

Returns the element at the given index or undefined if the index doesn't exists

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.get(0); // returns bob
values.get(2); // returns undefined

groupBy

Returns a Map object where the key is provided by the given selector and value is an array of all the elements matching this key

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jane: Person = {name: 'Jane', age: 22};

const values = new MutableList([bob, jo, jane]);
values.groupBy((value) => value.age);
// returns a map like so:
// Map([
//    [18, [{name: 'Bob', age: 18}]],
//    [22, [{name: 'Jo', age: 22}, {name: 'Jane', age: 22}]]
// ])

isEmpty

Check if the MutableList is empty or not

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

new MutableList([bob, jo]).isEmpty(); // returns false
new MutableList().isEmpty(); // returns true

isNotEmpty

Check if the MutableList is empty or not

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

new MutableList([bob, jo]).isNotEmpty(); // returns true
new MutableList().isNotEmpty(); // returns false

join

Returns a string resulting from converting each element of the MutableList to a string and then concatenating them together

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.join({separator: ' / '}, (value) => value.name); // returns 'bob / jo'

last

Returns the last element matching the predicate, throw an error if there's not matching

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new MutableList([bob, jo, jo1]);
values.last((value) => value.name === 'jo'); // returns jo1
values.last((value) => value.name === 'jane'); // throw an error 'No value matches the predicate'

lastOrUndefined

Returns the last element matching the predicate, or undefined if no matching

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jo1: Person = {name: 'Jo', age: 23};

const values = new MutableList([bob, jo, jo1]);
values.lastOrUndefined((value) => value.name === 'jo'); // returns jo1
values.lastOrUndefined((value) => value.name === 'jane'); // returns undefined

map

Returns a new MutableList where a given transformer is applied on every elements

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.map((value) => {
    value.age = 18;
    return value;
}); // returns a new List similar to values but where every Person is now 18

max

Returns the max value or object according to the given selector.

If no selector, then just returns the max among all values. The array must consist of numbers only, otherwise an error is thrown.

examples :

const values = new MutableList([1, 2, 3, 4, 5]);
values.max(); // returns 5
const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.max((value) => value.age); // returns jo
values.max(); // throw an Error 'Type of array is not number'

min

Returns the min value or object according to the given selector.

If no selector, then just returns the min among all values. The array must consist of numbers only, otherwise an error is thrown.

examples :

const values = new MutableList([1, 2, 3, 4, 5]);
values.min(); // returns 1
const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.min((value) => value.age); // returns bob
values.min(); // throw an Error 'Type of array is not number'

none

Check if there's no element matching the predicate, if that's the case then returns true, else false

example :

const values = new MutableList([1, 2, 3, 4, 5]);
values.none((value) => value === 3); // returns false
values.none((value) => value === 0); // returns true

onEach

Returns a new MutableList where a given action is applied on every elements, the selector silently returns this.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.onEach((value) => (value.age = 18)); // returns a new MutableList similar to values but where every Person is now 18

reduce

Returns a value obtained after an operation (accumulator) is applied on every element of the MutableList.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.reduce((acc, value) => acc + value.age, 0); // returns 40

remove

Remove the element at the given index from the current MutableList and returns this.

Mutable operation.

example :

const values = new MutableList([1, 2, 3, 4, 5, 6]);
values.remove(5); // returns MutableList([1, 2, 3, 4, 5])

removeAll

If the parameter is a predicate, remove all elements from the array matching this predicate.

Otherwise remove all occurences of the given element from the array.

Mutable operation.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};
const jane: Person = {name: 'Jane', age: 22};

const values = new MutableList([bob, jo, jane]);
values.removeAll((value) => value.age === 22); // returns MutableList([bob])
values.removeAll(bob); // returns MutableList([jo, jane])

removeAt

Remove the element at the given index from the current MutableList and returns this.

Mutable operation.

example :

const values = new MutableList([1, 2, 3, 4, 5, 6]);
values.removeAt(5); // returns MutableList([1, 2, 3, 4, 5])

removeFirst

Remove the first element the current MutableList and returns this.

Mutable operation.

example :

const values = new MutableList([1, 2, 3, 4, 5, 6]);
values.removeFirst(); // returns MutableList([2, 3, 4, 5, 6])

removeLast

Remove the last element the current MutableList and returns this.

Mutable operation.

example :

const values = new MutableList([1, 2, 3, 4, 5, 6]);
values.removeLast(); // returns MutableList([1, 2, 3, 4, 5])

reverse

Returns a new MutableList where all elements are reversed: first element become last, last become first and so on.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.reverse(); // returns MutableList([jo, bob])

size

Returns the number of elements in the MutableList.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.size(); // returns 2

some

Check if there's at least one element matching the predicate, if that's the case then returns true, else false.

example :

const values = new MutableList([1, 2, 3, 4, 5]);
values.some((value) => value === 3); // returns true
values.some((value) => value === 0); // returns false

sort

Returns a new MutableList where elements are sorted according to the selector if given.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([jo, bob]);
values.sort((value) => value.age); // returns MutableList([bob, jo])

subList

Create a new list containing the elements between the given indexes.

example :

const values = new MutableList([1, 2, 3, 4, 5]);
values.subList(1, 3); // returns MutableList([2, 3]);
values.subList(2); // returns MutableList([3, 4, 5]);
values.subList(-2); // returns MutableList([4, 5]);

sum

Calculate the sum of the array according to the selector if given.

If no selector is given, the MutableList must be composed of numbers otherwise an error will be thrown.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.sum((value) => value.age); // returns 40
values.sum(); // throw an error 'Type of array is not number'

take

Returns a new MutableList with only the n first elements.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.take(1); // returns MutableList([bob])

takeLast

Returns a new MutableList with only the n last elements.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.takeLast(1); // returns MutableList([jo])

toArray

Returns an array out of the MutableList.

example :

const bob: Person = {name: 'Bob', age: 18};
const jo: Person = {name: 'Jo', age: 22};

const values = new MutableList([bob, jo]);
values.toArray(); // returns [bob, jo]

listOf

listOf is a utility fonction to instantiate a List.

examples

const values = listOf(1, 2, 3, 4, 5);

mutableListOf

mutableListOf is a utility fonction to instantiate a MutableList.

examples

const values = mutableListOf(1, 2, 3, 4, 5);

when
when is similar to the switch statement, it defines a conditional expression with multiples branches. Every branch condition is checked sequentially until a matching is met.

It can also be used without argument. In this case it is similar to the if/else statement.

when is a function, which means it's an expression unlike switch or if/else, which means that when returns a value that you can directly attribuate to a variable for example.

examples

with argument without argument
const colorName = getRandomColor();

const color = when(colorName, [
    'orange', () => new Orange(),
    ['red', 'redish'], () => new Red(),
    'green', () => new Green(),
    () => undefined, // default value
]);
const darkColorName = getRandomColor();
const lightColorName = getRandomColor();

const color = when([
    darkColorName === 'black', () => new Black(),
    lightColorName === 'white', () => new White(),
    lightColorName === 'yellow', () => new Yellow(),
    () => undefined, // default value
]);

when function is more lightweight in term of code than the other conditional options :

when if/else with brackets if/else without brackets switch
const colorName = getRandomColor();

const color = when(colorName, [
    'orange', () => new Orange(),
    ['red', 'redish'], () => new Red(),
    'green', () => new Green(),
    () => undefined
]);
const colorName = getRandomColor();

let color = undefined;
if (colorName === 'orange') {
    color = new Orange();
} else if (colorName === 'red' || colorName === 'redish') {
    color = new Red();
} else if (colorName === 'green') {
    color = new Green();
}
const colorName = getRandomColor();

let color;
if (colorName === 'orange') color = new Orange();
else if (colorName === 'red' || colorName === 'redish') color = new Red();
else if (colorName === 'green') color = new Green();
else color = undefined;
const colorName = getRandomColor();

let color;
switch (color) {
    case 'orange':
        color = new Orange();
        break;
    case 'red':
    case 'redish':
        color = new Red();
        break;
    case 'green':
        color = new Green();
        break;
    default:
        color = undefined;
        break;
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published