UNPKG

for-ease

Version:

a simple library for itrating over anything

79 lines (78 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class ReturnsOfCounter { constructor(mixed) { this.mixed = mixed; mixed ? this.createReutrns() : ''; } createReutrns() { this.makeMixedObj(); let arr = this.mixed; let length = arr.length; this.returnObjs = { returns: arr, assign: (array) => { if (!Array.isArray(array)) throw new Error('you can only assign to arrays'); for (var i = 0; i < length; i++) { array[i] = arr[i]; } }, append: (array) => { if (!Array.isArray(array)) throw new Error('you can only assign to arrays'); // if (length === undefined) array.push(arr); // arr here is just a string or number for (var i = 0; i < length; i++) { array.push(arr[i]); } }, prepend: (array) => { if (!Array.isArray(array)) throw new Error('you can only assign to arrays'); // if (length === undefined) array.unshift(arr); // arr here is just a string or number for (var i = 0; i < length; i++) { array.unshift(arr[i]); } } }; } makeMixedObj() { let resultArray = this.initializeArrayAccordingToType(this.mixed, typeof this.mixed); if (resultArray.length !== 0) this.mixed = resultArray; } initializeArrayAccordingToType(mixed, type) { let temp = []; if (type === 'number') { for (var i = 0; i < mixed; i++) { temp.push(i); } } else if (type === 'string') { let length = mixed.length; for (var i = 0; i < length; i++) { temp.push(mixed[i]); } } else if (type === 'object' && !Array.isArray(mixed)) { let keys = Object.keys(mixed); let length = keys.length; for (let i = 0; i < length; i++) { let currentKey = keys[i]; let currentValue = mixed[currentKey]; temp[i] = [currentKey, currentValue]; } } return temp; } // --- public apis getReturns() { return this.returnObjs; } setMixedObj(mixed) { this.mixed = mixed; this.createReutrns(); return this; } } exports.ReturnsOfCounter = ReturnsOfCounter;