Skip to content

Commit

Permalink
Merge pull request #200 from angelabernaldz/194-prototype
Browse files Browse the repository at this point in the history
#194-prototype
  • Loading branch information
rucev authored Oct 23, 2024
2 parents 0734055 + 002ee6a commit bf1af11
Show file tree
Hide file tree
Showing 84 changed files with 2,498 additions and 0 deletions.
2 changes: 2 additions & 0 deletions staff/angela-bernaldez/biblio/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pnpm-lock.yaml
/node_modules
20 changes: 20 additions & 0 deletions staff/angela-bernaldez/biblio/at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Biblio = require("./constructor.js")

function at(index) {

if (!(this instanceof Biblio)) {
throw new TypeError('The provided object needs to have been created with Biblio constructor')
}

if (index == null) index = 0

if (typeof index === 'boolean') index = Number(index)

if (index < - this.length || index >= this.length) return undefined

if (index < 0) index += this.length

return this[index]
}

module.exports = at
28 changes: 28 additions & 0 deletions staff/angela-bernaldez/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]
const biblio = new Biblio(1, 2, 3, 4, 5, 6)

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(-4);
const resultArray = array.at(-4);
expect(resultBiblio).to.be.equal(resultArray);
});
it("Use at for out of range index", () => {
const resultBiblio = biblio.at(10);
const resultArray = array.at(10);
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);
});
});
27 changes: 27 additions & 0 deletions staff/angela-bernaldez/biblio/concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const Biblio = require("./constructor.js")

function concat() {

if (!(this instanceof Biblio)) {
throw new TypeError('The provided object needs to have been created with Biblio constructor')
}

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];
// need to manually update length property
result.length++;
}
}
return result;
}

module.exports = concat
38 changes: 38 additions & 0 deletions staff/angela-bernaldez/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 passing only one argument to Biblio", () => {
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 passing not only one argument to Biblio", () => {
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 passing multiple and mixed type arguments to Biblio", () => {
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);
});
});
13 changes: 13 additions & 0 deletions staff/angela-bernaldez/biblio/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function Biblio(array) {

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/angela-bernaldez/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);
});
});
28 changes: 28 additions & 0 deletions staff/angela-bernaldez/biblio/copy-within.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Biblio = require("./constructor.js")

function copyWithin(_target, _start, _end = this.length) {

if (!(this instanceof Biblio)) {
throw new TypeError('The provided object needs to have been created with Biblio constructor')
}

const elementsToCopy = new Biblio();

const target = _target >= 0 ? _target : this.length + _target;
const start = _start >= 0 ? _start : this.length + _start;
const end = _end >= 0 ? _end : this.length + _end;

let i = 0;
while (elementsToCopy.length < end - start) {
elementsToCopy[i] = this[start + i];
elementsToCopy.length += 1;
i++;
}

for (let i = 0; i < elementsToCopy.length; i++)
this[target + i] = elementsToCopy[i];

return this;
}

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

describe("CopyWithin method", () => {
it("Use copyWithin using target, start and end parameters with reasonable values", () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const biblio = new Biblio(1, 2, 3, 4, 5, 6, 7, 8);
const resultArray = array.copyWithin(0, 3, 4);
const resultBiblio = biblio.copyWithin(0, 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(8);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(8);
expect(biblio).to.be.deep.equal(resultBiblio);
});

it("Use copyWithin target and start with reasonable values", () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const biblio = new Biblio(1, 2, 3, 4, 5, 6, 7, 8);
const resultArray = array.copyWithin(1, 3);
const resultBiblio = biblio.copyWithin(1, 3);
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(8);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(8);
expect(biblio).to.be.deep.equal(resultBiblio);
});

it("Use copyWithin using all parameters with negative values", () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const biblio = new Biblio(1, 2, 3, 4, 5, 6, 7, 8);
const resultArray = array.copyWithin(-2, -3, -1);
const resultBiblio = biblio.copyWithin(-2, -3, -1);
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(8);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(8);
expect(biblio).to.be.deep.equal(resultBiblio);
});

it("Use copyWithin using some parameters with values outside range", () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const biblio = new Biblio(1, 2, 3, 4, 5, 6, 7, 8);
const resultArray = array.copyWithin(10, 14, 0);
const resultBiblio = biblio.copyWithin(10, 14, 0);
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(8);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(8);
expect(biblio).to.be.deep.equal(resultBiblio);
});
});
22 changes: 22 additions & 0 deletions staff/angela-bernaldez/biblio/every.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Biblio = require("./constructor.js")

function every(callback) {

if (!(this instanceof Biblio)) {
throw new TypeError('The provided object needs to have been created with Biblio constructor')
}

if (typeof callback !== 'function') {
throw TypeError('Argument of every needs to be a function')
}

let result = true

for (let i = 0; i < this.length; i++) {
if (!callback(this[i])) return false
}

return result
}

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

describe("Every 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 every evaluating each element type", () => {
const resultArray = array.every((item) => typeof item === "number");
const resultBiblio = biblio.every((item) => typeof item === "number");
expect(resultBiblio).to.be.equal(resultArray).to.be.true;
expect(biblio.length).to.be.equal(array.length).to.be.equal(8);
});

it("Use every evaluating if every element is equal to a specific number", () => {
const resultArray = array.every((item) => item === 4);
const resultBiblio = biblio.every((item) => item === 4);
expect(resultBiblio).to.be.equal(resultArray).to.be.false;
expect(biblio.length).to.be.equal(array.length).to.be.equal(8);
});

it("Use every evaluating if every element is even", () => {
const resultArray = array.every((item) => item % 2 === 0);
const resultBiblio = biblio.every((item) => item % 2 === 0);
expect(resultBiblio).to.be.equal(resultArray).to.be.false;
expect(biblio.length).to.be.equal(array.length).to.be.equal(8);
});

it("Use every evaluating if every element is positive", () => {
const resultArray = array.every((item) => item > 0);
const resultBiblio = biblio.every((item) => item > 0);
expect(resultBiblio).to.be.equal(resultArray).to.be.true;
expect(biblio.length).to.be.equal(array.length).to.be.equal(8);
});
});
24 changes: 24 additions & 0 deletions staff/angela-bernaldez/biblio/fill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Biblio = require("./constructor.js")

function fill(value, start = 0, end = this.length) {

if (!(this instanceof Biblio)) {
throw new TypeError('The provided object needs to have been created with Biblio constructor')
}

start = start < 0 ? Math.max(start + this.length, 0) : start;
end = end < 0 ? end + this.length : Math.min(end, this.length);

// Return the original array if start is greater than or equal to end
if (start >= end) return this

// loop through array to change corresponding elements according to start and end if provided
// otherwise, change the whole array
for (let i = start; i < end; i++) {
this[i] = value
}

return this
}

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

describe("Fill 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 fill passing no values for start and end positions", () => {
const resultArray = array.fill(0);
const resultBiblio = biblio.fill(0);
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(8);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(8);
expect(biblio).to.be.deep.equal(resultBiblio);
});

it("Use fill passing positive values for start and end positions", () => {
const resultArray = array.fill(10, 2, 8);
const resultBiblio = biblio.fill(10, 2, 8);
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(8);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(8);
expect(biblio).to.be.deep.equal(resultBiblio);
});

it("Use fill passing negative values for both start and end positions", () => {
const resultArray = array.fill(10, -4, -2);
const resultBiblio = biblio.fill(10, -4, -2);
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(8);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(8);
expect(biblio).to.be.deep.equal(resultBiblio);
});

it("Use fill passing start position greater than end position", () => {
const resultArray = array.fill(10, -2, -4);
const resultBiblio = biblio.fill(10, -2, -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(8);
expect(resultBiblio.length).to.be.equal(resultArray.length).to.be.equal(8);
expect(biblio).to.be.deep.equal(resultBiblio);
});
});
Loading

0 comments on commit bf1af11

Please sign in to comment.