Code Monkey home page Code Monkey logo

jscollection's Introduction

JsCollection

###Simplify your object array Build Status

Js Collection is an object collection class used to simplify using array of objects. This pattern or methodology rather, is commonly used in oo designs. Together with iterator functionality it simplifies traversal and manipulation of objects during the iteration and adds commonality to your arrays.

Usage

Example

myCollection = new JsCollection(myArrayOfObjects);

while(myCollection.hasNext()) {
    element = testCollection.next();
    element.functionCall();
}

myCollection.orderBy('name');
myArrayOfObjects = myCollection.getAll();

####Add elements to your collection #####Within a loop Adds a single object at the end of the array collection

myCollection.add(myObject);

#####Complete array You can set an array of objects directly

myCollection.setArray(myArrayOfObjects);

####Iteration You can iterate the collection in multiple ways:

each

myCollection.each(function(key, element) {}):

while

while(myCollection.hasNext()) {
    element = testCollection.next();
}

for

for(var i = 0; i < testCollection.count(); i++) {
    element = testCollection.getByKey(i);
}

Iterator functions

Function Description
count() Returns number of objects in the collection
position() Returns the current iterator position
first() Returns the first element in the collection
last() Returns the last element in the collection
current() Returns current element according to the iterator position
next() Returns current element then increases the iterator position by one
hasNext() Returns true or false if the collection has more elements
rewind() Resets the iterator position and returns the first element in the collection

####Ordering

myCollection.orderBy('name', 'desc');

Functions

  • add(element): Add an element to the collection
  • get(key|null): If key is defined get element by key, else get current element
  • getAll(): Get all elements currently in the collection
  • count(): Returns number of elements in the collection
  • getKeys(): Returns an array of the collection keys
  • containsKey(key): Check if a key exists or not
  • remove(key|null): If key is defined remove element by key, else remove current element
  • replace(key, element): Replace object with key
  • first(): Get the first element in the collection
  • last(): Get the last element in the collection
  • position(): Returns current iterator position
  • next(): Returns current object and increases the iterator
  • hasNext(): Returns true or false if the collection has more elements
  • current(): Returns current element
  • rewind(): Resets the iterator
  • clear(): Clear the collection, remove all elements
  • setArray(): Fill the collection with a array
  • each(callback(key, value)): Iterate through all elements
  • orderBy(property, direction): Order elements by element property and direction

jscollection's People

Contributors

oligus avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.