Skip to content

Commit a34804e

Browse files
committed
stack methods added
1 parent 73cfcdf commit a34804e

7 files changed

Lines changed: 578 additions & 38 deletions

File tree

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@ Cache data using JavaScript in the browser.
55
### Status
66

77
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yusufshakeel/dyCacheJS)
8-
[![npm version](https://img.shields.io/badge/npm-1.1.0-blue.svg)](https://www.npmjs.com/package/dycachejs)
8+
[![npm version](https://img.shields.io/badge/npm-1.3.0-blue.svg)](https://www.npmjs.com/package/dycachejs)
99
[![Build Status](https://travis-ci.org/yusufshakeel/dyCacheJS.svg?branch=master)](https://travis-ci.org/yusufshakeel/dyCacheJS)
10-
10+
[![](https://data.jsdelivr.com/v1/package/npm/dycachejs/badge)](https://www.jsdelivr.com/package/npm/dycachejs)
1111

1212
### Getting Started
1313
* Download the [latest release](https://github.com/yusufshakeel/dyCacheJS/releases) of the project.
1414
* Clone the repo: `git clone https://github.com/yusufshakeel/dyCacheJS.git`
1515
* Install via npm: `npm install --save dycachejs`
16-
* Use from jsDelivr CDN: `https://cdn.jsdelivr.net/npm/dycachejs@version/dist/js/dyCache.min.js`
17-
18-
Note! For jsDelive CDN, replace `@version` with the version you want to use like `@1.1.0`.
19-
20-
Example `https://cdn.jsdelivr.net/npm/dycachejs@1.1.0/dist/js/dyCache.min.js`
16+
* Use from jsDelivr CDN: `https://www.jsdelivr.com/package/npm/dycachejs`
2117

2218

2319
### Documentation

dist/js/dyCache.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/dyCache.forTest.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,135 @@ var dyCache = /** @class */ (function () {
436436
}
437437
return -1;
438438
};
439+
/**
440+
* This will initialise a new stack in the cache and will refer it by key.
441+
* @param {string} key
442+
*/
443+
dyCache.prototype.stackInit = function (key) {
444+
this._cache[key] = [];
445+
};
446+
/**
447+
* This will create a stack in the cache and will refer it by key.
448+
*
449+
* @param {string} key
450+
* @param value
451+
*/
452+
dyCache.prototype.stackPush = function (key, value) {
453+
// create array for the key if not exists
454+
if (!this.exists(key)) {
455+
this._cache[key] = [];
456+
}
457+
this._cache[key].push(value);
458+
};
459+
/**
460+
* This will pop the last element from the right side of the stack
461+
* referred by key in the cache.
462+
*
463+
* On success return the element. Otherwise undefined.
464+
*
465+
* @param {string} key
466+
* @returns {any}
467+
*/
468+
dyCache.prototype.stackPop = function (key) {
469+
// if stack referred by the key exists in the cache
470+
if (typeof key !== 'undefined' && typeof this._cache[key] !== "undefined") {
471+
return this._cache[key].pop();
472+
}
473+
return undefined;
474+
};
475+
/**
476+
* This will check if stack referred by the key in the cache exists.
477+
*
478+
* On success return true. Otherwise false.
479+
*
480+
* @param {string} key
481+
* @returns {boolean}
482+
*/
483+
dyCache.prototype.stackExists = function (key) {
484+
return typeof this._cache[key] !== "undefined";
485+
};
486+
/**
487+
* This will return the last element in the stack referred by key
488+
* in the cache.
489+
*
490+
* On success return the value. Otherwise, null.
491+
*
492+
* @param {string} key
493+
* @returns {any}
494+
*/
495+
dyCache.prototype.stackPeek = function (key) {
496+
// if stack referred by the key exists in the cache
497+
if (typeof key !== 'undefined' && typeof this._cache[key] !== "undefined") {
498+
// return just the element like: 10
499+
// and not in an array like: [10]
500+
return this._cache[key].slice(-1)[0];
501+
}
502+
return null;
503+
};
504+
/**
505+
* This will return total number of elements in the stack referred by
506+
* key in the cache.
507+
*
508+
* On success return a number representing total number of elements.
509+
* Otherwise, -1.
510+
*
511+
* @param {string} key
512+
* @returns {number}
513+
*/
514+
dyCache.prototype.stackLength = function (key) {
515+
// if stack referred by the key exists in the cache
516+
if (typeof key !== 'undefined' && typeof this._cache[key] !== "undefined") {
517+
return Object.keys(this._cache[key]).length;
518+
}
519+
return -1;
520+
};
521+
/**
522+
* This will return true if stack referred by key in the cache is empty.
523+
* Otherwise false.
524+
*
525+
* @param {string} key
526+
* @returns {boolean}
527+
*/
528+
dyCache.prototype.stackIsEmpty = function (key) {
529+
// if stack referred by key does not exists then return false
530+
if (!this.stackExists(key)) {
531+
return undefined;
532+
}
533+
return this.stackLength(key) === 0;
534+
};
535+
/**
536+
* This will remove all the elements from the stack referred by key
537+
* in the cache.
538+
*
539+
* On success return true. Otherwise, false.
540+
*
541+
* @param {string} key
542+
* @returns {boolean}
543+
*/
544+
dyCache.prototype.stackPurge = function (key) {
545+
// if stack referred by key does not exists then return false
546+
if (!this.stackExists(key)) {
547+
return false;
548+
}
549+
this.stackInit(key);
550+
return true;
551+
};
552+
/**
553+
* This will delete the stack from the cache.
554+
*
555+
* On success return true. Otherwise false.
556+
*
557+
* @param {string} key
558+
* @returns {boolean}
559+
*/
560+
dyCache.prototype.stackDelete = function (key) {
561+
// if stack referred by key does not exists then return false
562+
if (!this.stackExists(key)) {
563+
return false;
564+
}
565+
this.del(key);
566+
return true;
567+
};
439568
return dyCache;
440569
}());
441570

src/js/dyCache.forTest.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)