-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from venturars/181-prototype
#181-prototype
- Loading branch information
Showing
90 changed files
with
2,190 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pnpm-lock.yaml | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const Biblio = require("./contructor"); | ||
|
||
/** | ||
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const Biblio = require("./contructor"); | ||
|
||
/** | ||
* Copies a sequence of elements from one position to another within the Biblio instance. | ||
* | ||
* @param {number} _target - The index of the starting position of the replacement. | ||
* @param {number} _start - The index of the beginning of the sequence to copy. | ||
* @param {number} _end - The index of the end of the sequence to copy (exclusive). | ||
* @returns {Biblio} The modified Biblio instance. | ||
*/ | ||
function copyWithin(_target, _start, _end = this.length) { | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const { expect } = require("chai"); | ||
const Biblio = require("."); | ||
|
||
describe("CopyWithin method", () => { | ||
it("Use copyWithin using target and start 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, 5); | ||
const resultBiblio = biblio.copyWithin(0, 5); | ||
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 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 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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Checks if all elements in the Biblio instance pass the provided test function. | ||
* | ||
* @param {function} callback - The callback function to test each element against. | ||
* @returns {boolean} True if all elements pass the test, false otherwise. | ||
*/ | ||
function every(callback) { | ||
let result = true; | ||
|
||
let i = 0; | ||
while (i < this.length && result) { | ||
if (!callback(this[i])) result = false; | ||
i++; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
module.exports = every; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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 typeof every element with reasonable values", () => { | ||
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 4 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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Fills all elements of the Biblio instance with the specified value. | ||
* | ||
* @param {*} value - The value to fill the elements with. | ||
* @param {number} _start - The index to start filling from. | ||
* @param {number} _end - The index to stop filling at (exclusive). | ||
* @returns {Biblio} The modified Biblio instance. | ||
*/ | ||
function fill(value, _start = 0, _end = this.length) { | ||
const start = _start >= 0 ? _start : this.length + _start; | ||
const end = _end >= 0 ? _end : this.length + _end; | ||
|
||
for (let i = start; i < end; i++) this[i] = value; | ||
|
||
return this; | ||
} | ||
|
||
module.exports = fill; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const { expect } = require("chai"); | ||
const Biblio = require("."); | ||
|
||
describe("Fill method", () => { | ||
it("Use fill with reasonable value parameter", () => { | ||
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.fill(6); | ||
const resultBiblio = biblio.fill(6); | ||
for (let i = 0; i < resultArray.length; i++) | ||
expect(resultBiblio[i]).to.be.equal(resultArray[i]); | ||
expect(biblio).to.be.deep.equal(resultBiblio); | ||
expect(biblio.length).to.be.equal(array.length).to.be.equal(8); | ||
}); | ||
|
||
it("Use fill with value and start 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.fill(5, 1); | ||
const resultBiblio = biblio.fill(5, 1); | ||
for (let i = 0; i < resultArray.length; i++) | ||
expect(resultBiblio[i]).to.be.equal(resultArray[i]); | ||
expect(biblio).to.be.deep.equal(resultBiblio); | ||
expect(biblio.length).to.be.equal(array.length).to.be.equal(8); | ||
}); | ||
|
||
it("Use fill with all 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.fill(0, 2, 4); | ||
const resultBiblio = biblio.fill(0, 2, 4); | ||
for (let i = 0; i < resultArray.length; i++) | ||
expect(resultBiblio[i]).to.be.equal(resultArray[i]); | ||
expect(biblio).to.be.deep.equal(resultBiblio); | ||
expect(biblio.length).to.be.equal(array.length).to.be.equal(8); | ||
}); | ||
|
||
it("Use fill with 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.fill(4, -3, -2); | ||
const resultBiblio = biblio.fill(4, -3, -2); | ||
for (let i = 0; i < resultArray.length; i++) | ||
expect(resultBiblio[i]).to.be.equal(resultArray[i]); | ||
expect(biblio).to.be.deep.equal(resultBiblio); | ||
expect(biblio.length).to.be.equal(array.length).to.be.equal(8); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const Biblio = require("./contructor"); | ||
|
||
/** | ||
* Filters the elements of the Biblio instance based on the provided callback function. | ||
* | ||
* @param {function} callback - The callback function to test each element against. | ||
* @returns {Biblio} A new Biblio instance containing the elements that passed the test. | ||
*/ | ||
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; |
Oops, something went wrong.