0.0.2 • Published 8 years ago

typescript-collection-framework v0.0.2

Weekly downloads
2
License
BSD
Repository
github
Last release
8 years ago

typescript-collection-framework

A small framework with some helper methods for Javascript/Typescript.

Build

Run the following command once: npm install

Then run grunt to build the lib and run all tests.

You can also run grunt watch to build again if a file changes.

Usage

Just include the tcf.js in your code:<script type="text/javascript" src="tcf.js"></script>.

Then you can wrap an array var arrHelper = new tcf.ArrayHelper([1, 2, 3, 4, 5, 6]) or create a new one with no arguments var arrHelper = new tcf.ArrayHelper().

You always can access the native array with arrHelper.array`.

Access the first or last element

new tcf.ArrayHelper([1, 2, 3, 4, 5, 6]).first

Why optional ? Optional helps to prevent null pointers. So if you want to access you have to use the method getOrElse() with adefault value, or you use the doIfPresent() .

getOrElse()

You cannot just access the content of the optional. You have to pass a default value, which will be used if the optional is null.

new tcf.ArrayHelper([1, 2, 3, 4, 5, 6]).first.getOrElse(3); // will return 1

new tcf.ArrayHelper().first.getOrElse(3); // will return 3

isNull()

You can check if the optional has a value inside by calling isNull().

new tcf.ArrayHelper([1, 2, 3, 4, 5, 6]).first.isNull(); // will return false

new tcf.ArrayHelper().first.isNull() // will return true

doIfPresent()

If the content of the optional is not null, then the passed function with the content of the optional.

new tcf.ArrayHelper([1, 2, 3, 4, 5, 6]).last.doIfPresent(function(lastElement){
    lastElement // last element is 6
});

new tcf.ArrayHelper().last.doIfPresent(function(lastElement){
    lastElement // the function is not called
});

removeFirst()

Removes the first equal element from the array.

new tcf.ArrayHelper([ "hello", "world", "hello", "whats up" ]).removeFirst("hello"); 
// the array is now [ "world", "hello", "whats up" ]

removeLast()

Removes the last equal element from the array.

new tcf.ArrayHelper([ "hello", "world", "hello", "whats up" ]).removeLast("hello"); 
// the array is now [ "hello", "world", "whats up" ]

removeAll()

Removes all equal elements from the array.

new tcf.ArrayHelper([ "hello", "world", "hello", "whats up" ]).removeLast("hello"); 
// the array is now [ "world", "whats up" ]

findWhere()

Finds all element which have all the properties of the passed object.

var testArray = [
  {id:1, name:"test1", points:4},
  {id:2, name:"test2", points:2},
  {id:3, name:"test3", points:2},
  {id:4, name:"test4", points:1}
];
var arrHelper = tcf.ArrayHelper(testArray);
var foundValues = arrayHelper.findWhere({points : 2}); 
/*
Returns [{id:2, name:"test2", points:2}, {id:3, name:"test3", points:2}] wrapped in a array helper
*/

firstWhere()

Finds the first element which has all the properties of the passed object.

var testArray = [
            {id : 1, name : "User1"},
            {id : 2, name : "User2"},
            {id : 3, name : "User3"},
            {id : 4, name : "User4"},
            {id : 3, name : "User5"}
        ];
        
var arrHelper = tcf.ArrayHelper(testArray);
arrHelper.firstWhere({id:4}).doIfPresent(function(result){ 
    console.log("hello " + result.name); // prints "hello User4"
});

count()

Count all elements (array.length) or if you pass a element if counts all occurrences.

var testArray = [1, 2, 1, 3, 1, 4, 1, 5];
var arrHelper = tcf.ArrayHelper(testArray);
arrHelper.count(1); // will return 4

insertAtIndex()

You can insert elements to a specific position. The First argument is the position followed by elements to insert.

var array = [ 1, 5 ];
var arrayHelper = new tcf.ArrayHelper(array);
arrayHelper.insertAtIndex(1, 2, 3, 4);
// array is now [1,2,3,4,5]