Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#195-prototype #196

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions staff/carlos-portal/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
##########################
.DS_Store
thumbs.db
pnpm-lock.yaml
/node_modules
17 changes: 17 additions & 0 deletions staff/carlos-portal/biblio/at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Retrieves the element at the specified index in the Biblio instance.
*
* @param {number} _index - The index of the element to retrieve.
* @returns {*} The element at the specified index, or undefined if the index is out of bounds.  

*/
function at(_index) {
if (!(typeof _index === "number")) return this[0];
if (this.length < Math.abs(_index)) return undefined;

const index = _index >= 0 ? _index : this.length + _index;

return this[index];
}

module.exports = at;
28 changes: 28 additions & 0 deletions staff/carlos-portal/biblio/at.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { expect } = require("chai");
const Biblio = require(".");

describe("At method", () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const biblio = new Biblio(1, 2, 3, 4, 5, 6, 7, 8);

it("Use at method returning value of a reasonable index", () => {
const resultBiblio = biblio.at(3);
const resultArray = array.at(3);
expect(resultBiblio).to.be.equal(resultArray);
});
it("Use at for not positive value", () => {
const resultBiblio = biblio.at(-2);
const resultArray = array.at(-2);
expect(resultBiblio).to.be.equal(resultArray);
});
it("Use at for out of range index", () => {
const resultBiblio = biblio.at(20);
const resultArray = array.at(20);
expect(resultBiblio).to.be.equal(resultArray);
});
it("Use at for out of range index non positive case", () => {
const resultBiblio = biblio.at(-30);
const resultArray = array.at(-30);
expect(resultBiblio).to.be.equal(resultArray);
});
});
28 changes: 28 additions & 0 deletions staff/carlos-portal/biblio/concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Biblio = require("./constructor");

/**
* Concatenates the current Biblio instance with the provided arguments.
*
* @param {...Biblio|*} args - The elements or Biblio instances to concatenate.
* @returns {Biblio} A new Biblio instance containing the concatenated elements.
*/
function concat() {
const result = new Biblio();
while (result.length < this.length) {
result[result.length] = this[result.length];
result.length++;
}

for (let i = 0; i < arguments.length; i++) {
let element = arguments[i];
if (!(element instanceof Biblio)) element = new Biblio(element);
for (let j = 0; j < element.length; j++) {
result[result.length] = element[j];
result.length++;
}
}

return result;
}

module.exports = concat;
38 changes: 38 additions & 0 deletions staff/carlos-portal/biblio/concat.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { expect } = require("chai");
const Biblio = require(".");

describe("Concat method", () => {
const array = [1, 2];
const biblio = new Biblio(1, 2);

it("Use concat with only one Biblio parameter", () => {
const resultArray = array.concat([3, 4]);
const resultBiblio = biblio.concat(new Biblio(3, 4));
for (let i = 0; i < resultArray.length; i++)
expect(resultBiblio[i]).to.be.equal(resultArray[i]);
expect(biblio.length).to.be.equal(array.length).to.be.equal(2);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(4);
});

it("Use concat with only one not Biblio parameter", () => {
const resultArray = array.concat("foo");
const resultBiblio = biblio.concat("foo");
for (let i = 0; i < resultArray.length; i++)
expect(resultBiblio[i]).to.be.equal(resultArray[i]);
expect(biblio.length).to.be.equal(array.length).to.be.equal(2);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(3);
});

it("Use concat with only nultiple and mixed types parameters", () => {
const resultArray = array.concat([3, 4], [5, 6], Infinity);
const resultBiblio = biblio.concat(
new Biblio(3, 4),
new Biblio(5, 6),
Infinity
);
for (let i = 0; i < resultArray.length; i++)
expect(resultBiblio[i]).to.be.equal(resultArray[i]);
expect(biblio.length).to.be.equal(array.length).to.be.equal(2);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(7);
});
});
22 changes: 22 additions & 0 deletions staff/carlos-portal/biblio/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Creates an instance of `Biblio`, representing an array-like object with a `length` property.
*
* The `Biblio` constructor calculates the length based on the number of arguments passed
* and assigns each argument to the instance as array-like properties.
*
* @constructor
* @param {...*} value - Any number of values to be stored in the `Biblio` instance as array-like properties.
* @property {number} length - The number of arguments passed to the constructor.
*/
function Biblio() {
let _length = 0;
while (arguments[_length] !== undefined) _length++;
this.length = _length;

for (let i = 0; i < arguments.length; i++) {
this[i] = arguments[i];
}
}

module.exports = Biblio;

36 changes: 36 additions & 0 deletions staff/carlos-portal/biblio/constructor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { expect } = require("chai");
const Biblio = require(".");

describe("Constructor", function () {
describe("", () => {
let biblioInstance;

beforeEach(() => {
biblioInstance = new Biblio(1, "two", true);
});

it("Should calculate the correct length of arguments", () => {
expect(biblioInstance.length).to.equal(3);
});

it("Should store each argument as an indexed property", () => {
expect(biblioInstance[0]).to.equal(1);
expect(biblioInstance[1]).to.equal("two");
expect(biblioInstance[2]).to.equal(true);
});
});

it("Should handle no arguments correctly", () => {
const emptyBiblio = new Biblio();
expect(emptyBiblio.length).to.equal(0);
});

it("Should allow various data types as arguments", () => {
const mixedBiblio = new Biblio(42, { key: "value" }, [1, 2, 3], null);
expect(mixedBiblio.length).to.equal(4);
expect(mixedBiblio[0]).to.equal(42);
expect(mixedBiblio[1]).to.deep.equal({ key: "value" });
expect(mixedBiblio[2]).to.deep.equal([1, 2, 3]);
expect(mixedBiblio[3]).to.equal(null);
});
});
24 changes: 24 additions & 0 deletions staff/carlos-portal/biblio/copy-within.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Biblio = require("./constructor.js");

function copyWithin(target, start, end) {
const elementsToCopy = new Biblio();

// Guardamos los elementos que luego necesitaremos para copiar en otra parte del array, en un array temporal.
let i = start;
while (i < end) {
elementsToCopy[elementsToCopy.length] = this[i];
i++;
elementsToCopy.length++;
}

/* Recorremos el array temporal y lo copiamos en el array original.*/
let j = 0;
while (j < elementsToCopy.length) {
this[target + j] = elementsToCopy[j];
j++;
}

return this;
}

module.exports = copyWithin;
16 changes: 16 additions & 0 deletions staff/carlos-portal/biblio/copy-within.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { assert } = require("chai");
const Biblio = require(".");

describe("Copy-within method", () => {
it("Use of copy-within method using regular range of length.", () => {
const arr1 = [1, 2, 3, 4, 5, 6];
const result1 = arr1.copyWithin(2, 0, 3);
const biblio1 = new Biblio(1, 2, 3, 4, 5, 6);
const resultBiblio1 = biblio1.copyWithin(2, 0, 3);
assert.equal(
resultBiblio1[0],
result1[0],
"The copy-within method from Biblio does not work as the Array copy-within method."
);
});
});
17 changes: 17 additions & 0 deletions staff/carlos-portal/biblio/every.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Biblio = require("./constructor.js");

function every(callback) {
// Establecemos resultado como true
let result = true;

let i = 0;
while (i < this.length && result) {
// Si el resultado de aplicar la función al elemento es false, establecemos el resultado como false.
if (!callback(this[i])) result = false;

i++;
}
return result;
}

module.exports = every;
13 changes: 13 additions & 0 deletions staff/carlos-portal/biblio/every.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {assert} = require("chai")
const Biblio = require(".")

describe("Every Method", ()=> {
it("Using of every method",()=>{
const array = [1,2,3,4,5,6,7,8,9]
const biblioArray = [1,2,3,4,5,6,7,8,9]
const result1 = array.every((value) => value<10);
const biblio1 = new Biblio(...biblioArray);
const resultBiblio1 = biblio1.every((currentValue) => currentValue < 40)

assert.equal(result1,resultBiblio1,"The Method from Biblio does not work as eht regular one ")});
})
13 changes: 13 additions & 0 deletions staff/carlos-portal/biblio/fill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Biblio = require("./constructor")

function fill(value,start= 0,end = this.length,array){
start = start < 0 ? this.length + start : start;
end = end < 0 ? this.length + end : end;

for (let i = start; i < end; i++) this[i] = value;

return this;
}

module.exports = fill;

30 changes: 30 additions & 0 deletions staff/carlos-portal/biblio/fill.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const {assert, expect} = require ("chai")
const Biblio = require(".")

describe ("Testing the fill method ",()=>{
it("Basic Usage",()=>{
const array = [1,2,3,4,5,6,7]
const value = 5
const biblio = new Biblio(1,2,3,4,5,6,7)
const resultArray = array.fill(0,1,2);
const resultBiblio = biblio.fill(0,1,2);
for (let i = 0; i < resultArray.length; i++) {
expect(resultArray[i]).to.be.equal(resultBiblio[i])
expect(biblio).to.be.deep.equal(resultBiblio)


}




})








})
17 changes: 17 additions & 0 deletions staff/carlos-portal/biblio/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Biblio = require("./constructor.js");

function filter(callback) {
let result = new Biblio();

for (let i = 0; i < this.length; i++) {
const element = this[i];

if (callback(element)) {
result[result.length] = element;
result.length += 1;
}
}
return result;
}

module.exports = filter;
36 changes: 36 additions & 0 deletions staff/carlos-portal/biblio/filter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { assert } = require("chai");
const Biblio = require(".");

describe("Filter method", () => {
it("Use a filter method returning only numbers", () => {
const array1 = [1, 2, "g", 4, "j"];
const biblio1 = new Biblio(1, 2, "g", 4, "j");

const resultArray1 = array1.filter((item) => typeof item === "number");
const resultBiblio1 = biblio1.filter((item) => typeof item === "number");

const resultBiblioAsArray = Array.from(resultBiblio1);

assert.deepEqual(
resultArray1,
resultBiblioAsArray,
"The filter method from Biblio does not work as the Array filter method."
);
});
it("Use a filter method returning only stgrings", () => {
const array2 = ["Alboroto", 10, 2, "l"];
const biblio2 = new Biblio("Alboroto", 10, 2, "l");

const resultArray2 = array2.filter((item) => typeof item === "string");
const resultBiblio2 = biblio2.filter((item) => typeof item === "string");


const resultBiblioAsArray = Array.from(resultBiblio2);

assert.deepEqual(
resultArray2,
resultBiblioAsArray,
"The filter method from Biblio does not work as the Array filter method."
);
});
});
20 changes: 20 additions & 0 deletions staff/carlos-portal/biblio/find-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


const Biblio = require("./constructor.js");

function findIndex(callback) {
let result = -1;

let i = 0;

while (i < this.length && result < 0) {
const element = this[i];

if (callback(element)) result = i;

i++;
}
return result;
}

module.exports = findIndex;
Loading