@ssibrahimbas/core
v0.0.9
Published
Contains helpful Type-Java-Script constructs.
Downloads
7
Maintainers
Readme
Maintainers
| Maintainer | GitHub | Web | |-----------------------|-------------------------------------------------|---------------------------------------------------------| | Sami Salih İbrahimbaş | ssibrahimbas | @ssibrahimbas |
Installation
With npm
$ npm install @ssibrahimbas/core
Or with yarn:
$ yarn add @ssibrahimbas/core
Usage
For TypeScript
import {Queue, PriorityQueue, Stack} from "@ssibrahimbas/core";
interface IQueueType {
name: string;
}
const myQueue = new Queue<IQueueType>();
interface IPriorityQueueType {
priority: number;
name: string;
}
const myPriorityQueue = new PriorityQueue<IPriorityQueue>((a, b) => a.priority - b.priority);
interface IstackType {
url: string;
}
const pageHistoryStack = new Stack<IstackType>();
for JavaScript
// import {Queue, PriorityQueue, Stack} from "@ssibrahimbas/core"; // for module
const {Queue, PriorityQueue, Stack} = require("@ssibrahimbas/core"); // for commonjs
const myQueue = new Queue();
const myPriorityQueue = new PriorityQueue((a,b) => a - b);
const myStack = new Stack();
API
Queue
Description:
The feature of queue is FIFO. That is, fist in, first out.
Type-Java-Script itself has no queue structure. However, we may want to queue some transactions for various reasons. We can even process according to the importance of these queses, see: PriorityQueue.
That's why this post and you are here. @ssibrahimbas/core It, gives the queue structure to Type-Java-Script.
PriorityQueue
Description
FIFO is a very nice rule, but you may not need it. Instead, the priority may need to be delisted first.
We can compare this to the fact that in any queue (for example, the hospital queue), the elderly and children get ahead of other people first.
While this is a very nice feature, it requires more performance than the Queue itself. Because the list must be reordered every time an element is added. You may have some peace of mind about this. We do the sorting on the insertion with BubbleSort, and it has a total O(logn) performance complexity. If we had used the ``sort``` method instead, this complexity would have increased to O(n log n).
Its usage is exactly the same as Queue except for deletion and cloning. The structure running in the background changes.
Stack
Description:
Another data type not in JavaScript itself is Stack. The feature of stacks is LIFO. That is, last in, first out.
Thanks to this package, you can use the Stack data type on JavaScript.
QueueSize
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.size; // return 0
userQueue.enqueue({name: "sami"}); // enqueue sami
userQueue.size // return 1
Returns the size of the queue
Returns: number
QueueEnqueue
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.enqueue({name: "sami"}); // enqueue sami
Add element to queue
| Param | Type |
|---------------|---------------|
| element
| T - your type |
Returns: void
QueueDequeue
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.enqueue({name: "sami"}); // enqueue sami
userQueue.dequeue(); // remove first element
Remove first element this queue - cf. FIFO
Returns: void
QueueDequeueByIndex
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.enqueue({name: "sami"});
userQueue.enqueue({name: "john"});
userQueue.enqueue({name: "evan"});
userQueue.enqueue({name: "salih"});
userQueue.dequeueByIndex(2); // remove evan
remove element at specific index from this queue
Returns: boolean
QueueIsEmpty
const userQueue = new Queue<any>();
console.log(userQueue.isEmpty())
Is the queue empty?
Returns: boolean
QueuePeek
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.enqueue({name: "sami"}); // enqueue sami
console.log(userQueue.peek())
// result -> {name: 'sami'}
Return first element from this queue
Returns: T
QueueLength
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.enqueue({name: "sami"});
console.log(userQueue.length())
Returns the size of the queue - similar to size
Returns: number
QueueToArray
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.enqueue({name: "sami"});
console.log(userQueue.toArray()) // [{name: 'sami'}]
Converts queue to array
Returns: Array<T>
QueueToString
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.enqueue({name: "sami"});
console.log(userQueue.toString()) // "[{name: 'sami'}]"
QueueClone
type User = {
name: string;
}
const userQueue = new Queue<User>();
userQueue.enqueue({name: "sami"});
const employeeQueue = userQueue.clone();
console.log(employeeQueue.toString()) // "[{name: 'sami'}]"
Clones the queue
Returns: QueueType<T>
QueuePriorityDequeue
type User = {
name: string;
}
const userQueue = new PriorityQueue<User>((a,b) => a.priority - b.priority);
userQueue.enqueue({name: "sami", priority: 2});
userQueue.enqueue({name: "salih", priority: 3});
userQueue.enqueue({name: "mehmet", priority: 1});
userQueue.dequeue(); // remove salih
userQueue.dequeue(1); // remove mehmet
Remove first element or your index this queue - cf. FIFO
| Param | Type |
|-------------|--------|
| index
| number |
Returns: void
QueuePriorityClone
type User = {
name: string;
}
const userQueue = new PriorityQueue<User>((a,b) => a.priority - b.priority);
userQueue.enqueue({name: "sami", priority: 2});
userQueue.enqueue({name: "salih", priority: 3});
const employeeQueue = userQueue.clone();
console.log(employeeQueue.toString()) // "[{name: "salih", priority: 3}, {name: "sami", priority: 2}]"
Clones the queue
Returns: PriorityQueueType<T>
StackPush
type Page = {
url: string;
}
const pageStack = new Stack<Page>();
pageStack.push({url: "www.itemsatis.com"});
Add element to stack
| Param | Type |
|---------------|---------------|
| element
| T - your type |
Returns: void
StackLength
type Page = {
url: string;
}
const pageStack = new Stack<Page>();
pageStack.push({url: "www.itemsatis.com"});
console.log(pageStack.length())
Returns the size of the stack
Returns: number
StackPeek
type Page = {
url: string;
}
const pageStack = new Stack<Page>();
pageStack.push({url: "www.itemsatis.com"});
console.log(pageStack.peek())
// result -> {url: 'www.itemsatis.com'}
Return first element from this stack
Returns: T
StackIsEmpty
const pageStack = new Stack<any>();
console.log(pageStack.isEmpty())
Is the stack empty?
Returns: boolean
StackPop
type Page = {
url: string;
}
const pageStack = new Stack<Page>();
pageStack.push({url: "www.itemsatis.com"});
pageStack.push({url: "www.itempazar.com"});
const last = pageStack.pop(); // return last element and remove
Remove last element this stack - cf. LIFO
Returns: T
StackReverse
type Page = {
url: string;
}
const pageStack = new Stack<Page>();
pageStack.push({url: "www.itemsatis.com"});
pageStack.push({url: "www.itempazar.com"});
const last = pageStack.pop(); // return last element and remove
reverses the stack.
For the above example: the first element declared 'www.itemsatis.com' will be the last element and will appear first in a possible pop
call.
Returns: void
StackToArray
type Page = {
url: string;
}
const pageStack = new Stack<Page>();
pageStack.push({url: "www.itemsatis.com"});
console.log(pageStack.toArray()) // [{url: 'www.itemsatis.com'}]
Converts queue to stack
Returns: Array<T>
StackToString
type Page = {
url: string;
}
const pageStack = new Stack<Page>();
pageStack.push({url: "www.itemsatis.com"});
console.log(pageStack.toString()) // "[{url: "www.itemsatis.com"}]"
Converts queue to string
Returns: string
StackClone
type Page = {
url: string;
}
const pageStack = new Stack<Page>();
pageStack.push({url: "www.itemsatis.com"});
const pageStackClone = pageStack.clone();
console.log(pageStackClone.toString()) // "[{url: "www.itemsatis.com"}]"
Clones the queue
Returns: StackType<T>