Skip to content

Commit 2e291b2

Browse files
committed
addMInsertAt() method added
1 parent 1991034 commit 2e291b2

8 files changed

Lines changed: 188 additions & 5 deletions

File tree

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.

index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,37 @@ <h4><code>arrInsertAt(key, index, value)</code></h4>
562562

563563
<hr>
564564

565+
<!-- arrMInsertAt() -->
566+
<a name="arrMInsertAt"></a>
567+
<h4><code>arrMInsertAt(key, index, value)</code></h4>
568+
569+
<p>This will insert multiple <code>value</code> at a given <code>index</code>
570+
in an array referred by <code>key</code> in the cache.</p>
571+
572+
<p><code>index</code> is the index at which the new elements are inserted.</p>
573+
574+
<p><code>value</code> is an array of elements.
575+
Each element can be a number, string, array or object.</p>
576+
577+
<p>On success return true. Otherwise false.</p>
578+
579+
<pre><code>obj.arrMInsertAt('users', 1, [ {id: 9999, name: 'yusufshakeel'}, 10, 'happy', [100] ]);</code></pre>
580+
581+
<div>
582+
<a class="btn btn-primary"
583+
role="button"
584+
data-toggle="collapse"
585+
href="#collapse-arrMInsertAt"
586+
aria-expanded="false"
587+
aria-controls="collapse-arrMInsertAt">Output</a>
588+
</div>
589+
<div class="collapse" id="collapse-arrMInsertAt">
590+
</div>
591+
592+
<!-- arrMInsertAt() ends here -->
593+
594+
<hr>
595+
565596
<!-- arrUpdateElem() -->
566597
<a name="arrUpdateElem"></a>
567598
<h4><code>arrUpdateElem(key, index, value)</code></h4>

src/js/dyCache.forTest.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,35 @@ var dyCache = /** @class */ (function () {
236236
this._cache[key].splice(index, 0, value);
237237
return true;
238238
};
239+
/**
240+
* This will insert multiple values at a given index in the array
241+
* referred by key in the cache.
242+
*
243+
* On success return true, otherwise false.
244+
*
245+
* @param {string} key
246+
* @param {number} index
247+
* @param value
248+
* @returns {boolean}
249+
*/
250+
dyCache.prototype.arrMInsertAt = function (key, index, value) {
251+
// if key does not exists in the cache
252+
if (!this.exists(key)) {
253+
return false;
254+
}
255+
// if index is invalid
256+
if (this.arrLength(key) < index || index < 0) {
257+
return false;
258+
}
259+
// if value does not exists
260+
if (typeof value === "undefined") {
261+
return false;
262+
}
263+
for (var i = 0; i < value.length; i++) {
264+
this._cache[key].splice(index + i, 0, value[i]);
265+
}
266+
return true;
267+
};
239268
/**
240269
* This will update the value at given index in an array
241270
* referred by key in the cache.

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.

src/js/dyCache.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,35 @@ var dyCache = /** @class */ (function () {
236236
this._cache[key].splice(index, 0, value);
237237
return true;
238238
};
239+
/**
240+
* This will insert multiple values at a given index in the array
241+
* referred by key in the cache.
242+
*
243+
* On success return true, otherwise false.
244+
*
245+
* @param {string} key
246+
* @param {number} index
247+
* @param value
248+
* @returns {boolean}
249+
*/
250+
dyCache.prototype.arrMInsertAt = function (key, index, value) {
251+
// if key does not exists in the cache
252+
if (!this.exists(key)) {
253+
return false;
254+
}
255+
// if index is invalid
256+
if (this.arrLength(key) < index || index < 0) {
257+
return false;
258+
}
259+
// if value does not exists
260+
if (typeof value === "undefined") {
261+
return false;
262+
}
263+
for (var i = 0; i < value.length; i++) {
264+
this._cache[key].splice(index + i, 0, value[i]);
265+
}
266+
return true;
267+
};
239268
/**
240269
* This will update the value at given index in an array
241270
* referred by key in the cache.

src/js/index-page-example.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,17 @@ output("Fetch all the elements of the array referred by <code>users</code> key i
156156
outputJSON(obj.arrGet('users'), 'collapse-arrLPop');
157157

158158
// arrInsertAt();
159-
output("insert a value at given <code>index</code> in a array referred by key <code>users</code> in the cache.", 'collapse-arrInsertAt');
159+
output("Insert a value at given <code>index</code> in a array referred by key <code>users</code> in the cache.", 'collapse-arrInsertAt');
160160
outputJSON(obj.arrInsertAt('users', 3, {username: 'foo', points: 99}), 'collapse-arrInsertAt');
161161
output("Fetch all the elements of the array referred by <code>users</code> key in the cache.", 'collapse-arrInsertAt');
162162
outputJSON(obj.arrGet('users'), 'collapse-arrInsertAt');
163163

164+
// arrMInsertAt();
165+
output("Insert multiple values at given <code>index</code> in a array referred by key <code>users</code> in the cache.", 'collapse-arrMInsertAt');
166+
outputJSON(obj.arrMInsertAt('users', 1, [ {id: 9999, name: 'yusufshakeel'}, 10, 'happy', [100] ]), 'collapse-arrMInsertAt');
167+
output("Fetch all the elements of the array referred by <code>users</code> key in the cache.", 'collapse-arrMInsertAt');
168+
outputJSON(obj.arrGet('users'), 'collapse-arrMInsertAt');
169+
164170
// arrUpdateElem();
165171
output("Update value of an element at index 0 in the array referred by key <code>users</code> in the cache.", 'collapse-arrUpdateElem');
166172
outputJSON(obj.arrUpdateElem('users', 0, { username: 'tintin', points: 50 }), 'collapse-arrUpdateElem');

src/ts/dyCache.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,40 @@ class dyCache {
263263
return true;
264264
}
265265

266+
/**
267+
* This will insert multiple values at a given index in the array
268+
* referred by key in the cache.
269+
*
270+
* On success return true, otherwise false.
271+
*
272+
* @param {string} key
273+
* @param {number} index
274+
* @param value
275+
* @returns {boolean}
276+
*/
277+
public arrMInsertAt(key: string, index: number, value: any): boolean {
278+
279+
// if key does not exists in the cache
280+
if (!this.exists(key)) {
281+
return false;
282+
}
283+
284+
// if index is invalid
285+
if (this.arrLength(key) < index || index < 0) {
286+
return false;
287+
}
288+
289+
// if value does not exists
290+
if (typeof value === "undefined") {
291+
return false;
292+
}
293+
294+
for (let i = 0; i < value.length; i++) {
295+
this._cache[key].splice(index + i, 0, value[i]);
296+
}
297+
return true;
298+
}
299+
266300
/**
267301
* This will update the value at given index in an array
268302
* referred by key in the cache.

test/src/js/dyCache.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,60 @@ describe('Testing dyCacheJS', function () {
377377

378378
});
379379

380+
describe('Testing arrMInsertAt()', () => {
381+
382+
it('should insert multiple number values at a given index', function () {
383+
for (let i = 1; i <= 3; i++) {
384+
obj.arrPush('arr', i);
385+
}
386+
assert.equal(obj.arrMInsertAt('arr', 1, [10, 20]), true);
387+
assert.deepEqual(obj.arrGet('arr'), [1, 10, 20, 2, 3]);
388+
});
389+
390+
it('should insert multiple string values at a given index', function () {
391+
for (let i = 1; i <= 3; i++) {
392+
obj.arrPush('arr', i);
393+
}
394+
assert.equal(obj.arrMInsertAt('arr', 1, ['a', 'b']), true);
395+
assert.deepEqual(obj.arrGet('arr'), [1, 'a', 'b', 2, 3]);
396+
});
397+
398+
it('should insert multiple array values at a given index', function () {
399+
for (let i = 1; i <= 3; i++) {
400+
obj.arrPush('arr', i);
401+
}
402+
assert.equal(obj.arrMInsertAt('arr', 1, [['a', 10], ['b', 20]]), true);
403+
assert.deepEqual(obj.arrGet('arr'), [1, ['a', 10], ['b', 20], 2, 3]);
404+
});
405+
406+
it('should insert multiple object values at a given index', function () {
407+
for (let i = 1; i <= 3; i++) {
408+
obj.arrPush('arr', i);
409+
}
410+
assert.equal(obj.arrMInsertAt('arr', 1, [{a: 1}, {b: 2}]), true);
411+
assert.deepEqual(obj.arrGet('arr'), [1, {a: 1}, {b: 2}, 2, 3]);
412+
});
413+
414+
it('should return false for non-existing key', function () {
415+
assert.equal(obj.arrMInsertAt('arr', 1, [1, 2]), false);
416+
});
417+
418+
it('should return false for inserting at invalid index', function () {
419+
for (let i = 1; i <= 10; i++) {
420+
obj.arrPush('arr', i);
421+
}
422+
assert.equal(obj.arrMInsertAt('arr', 99, [1, 2]), false);
423+
});
424+
425+
it('should return false when value is missing', function () {
426+
for (let i = 1; i <= 10; i++) {
427+
obj.arrPush('arr', i);
428+
}
429+
assert.equal(obj.arrMInsertAt('arr', 99), false);
430+
});
431+
432+
});
433+
380434
describe('Testing arrUpdateElem()', () => {
381435

382436
it('should update an element at a given index in the array', function () {

0 commit comments

Comments
 (0)