Skip to content

Droid997/custom-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

custom-array [đź’™]

  • extends Array

  • native + custom methods

  • register callbacks on push, pop, shift, unshift

  • observe array on push, pop, shift, unshift

Install

  • Get the package through CDN

<script src="https://cdn.jsdelivr.net/gh/Droid997/custom-array@1.0/src/customArray.min.js"></script>

Initialize

  • It will be placed in the window object window.customArray

var arr = new customArray();

//returns arr with length 0

Constructor (size, value)


var arr = new customArray(size?,value?);

// size default = 0

  

var arr = new customArray(5,1);

// arr => [1,1,1,1,1]

parameters


size | optional, default = 0;

value | optional;

returns

array = []

Custom Methods

initialize(value)

fills array with the passed value

parameters

value | default = 0

returns

array with filled values

usage


var arr = new customArray(5);

arr.initialize(1);

// arr => [1,1,1,1,1]

extend(newlen)

extends the array length

parameters

newlen | default = current array length

returns

array with extended length

usage


var arr = new customArray(5);

// arr.length => 5;

arr.extend(10)

// arr.length => 10;

reduceSize(newlen)

reduces array size

parameters

newlen | default= current array length

returns

array with reduced length

usage


var arr = new customArray(10);

// arr.length => 10;

arr.reduceSize(5)

// arr.length => 5;

clear()

clears all the values of the array

returns

cleared array

usage


var arr = new customArray(10,10);

// arr[0] => 10;

arr.clear()

// arr[0] => undefined;

valueAt(index)

returns the value at passed index

parameters

index

returns

value at specified index

usage


var arr = new customArray(10,10);

// arr[0] => 10;

arr.clear()

// arr[0] => undefined;

insertAt(index,[items])

inserts elements's at specified index

parameters

index => index at which element should be inserted

items => Array of items

returns

array

usage


var arr = new customArray(5,5);

// arr => [5,5,5,5,5]

arr.insertAt(2,"a","b","c");

// arr => [5,5,a,b,c,5,5,5]

remove(index,count)

remove elements from array

parameters

index => index at which element should be removed

count | default=1 => Number of elements to be removed from index

returns

array

usage


var arr = new customArray(5,5);

// arr => [5,5,5,5,5]

arr.remove(2);

// arr => [5,5,5,5]

observe(fn)

Event is fired when one of the following occours

  • push

  • pop

  • shift

  • unshift

parameters

function => function to be called when event occours

function callback takes array and element as parameters

returns

array

usage


var arr = new customArray(5);

arr.observe(function(array,element){

})

arr.push(2);

arr.observe(function(array,element){

//control here

//array => [5]

//element => 5

})

unobserve()

Removes observe callback if present

returns

true

usage


var arr = new customArray(5);

arr.observe(function(array,element){

})

arr.push(2);

arr.unobserve()

registerEvent(event,fn)

Allows to register custom events on one of the following

  • push

  • pop

  • shift

  • unshift

Note: First observe event is fired first and based on the event type registred function gets called

parameters

event => can be one of the following push , pop, shift, unshift

function => function to be called when event occours

function callback takes array and element as parameters

returns

boolean

usage


var arr = new customArray(5);

arr.registerEvent('push',function(array,element){

})

arr.push(2);

arr.registerEvent('push',function(array,element){

// control here

// array => [2]

// element => 2

})

unregisterEvent(event)

Removes registerEvent callback for event specified if present any.

returns

boolean

usage


var arr = new customArray(5);

arr.registerEvent('push',function(array,element){

})

arr.push(2);

arr.unregisterEvent("push")

fill ( value, start, end )

fills the array with given value from start index to end index .

parameters

value => any value to be filled. start => start index | default = 0. end => end index | default = array length.

returns

array

usage

var arr = new customArray(5);
arr.fill(2);
arr => [2 ,2 ,2 ,2 ,2]

sum ()

returns the sum of numbers in the array .

returns

sum

usage

var arr = new customArray(5,2);
arr => [2 ,2 ,2 ,2 ,2]
arr.push("str");
arr => [2 ,2 ,2 ,2 ,2,"str"]
var sum = arr.sum();
sum => 10

replace (index, value)

index at which the value should be replaced at.

returns

array

usage

var arr = new customArray(5,2);
arr => [2 ,2 ,2 ,2 ,2]
arr.replace(2,"5");
arr => [2 ,2 ,5 ,2 ,2]

copy (from, to)

copy the current array.

parameters

from => from index | default = 0 to => to index | default = array length

returns

copied array

usage

var arr = new customArray(5,2);
arr => [2 ,2 ,2 ,2 ,2]
var copiedArray = arr.copy();
copiedArray => [2 ,2 ,2 ,2 ,2]

count (element)

finds the total number of occurrences of the passed element.

parameters

element => element to find the count of.

returns

count

usage

var arr = new customArray(5,2);
arr => [2 ,2 ,2 ,2 ,2]
var count = arr.count(2);
count => 5

About

customArray class which extends Array with custom methods and enables to register call events

Resources

License

Stars

Watchers

Forks

Packages

No packages published