same.js
v1.0.4
Published
A Class of Parallel Arrays Manipulation
Downloads
4
Maintainers
Readme
Installation
npm i same.js
Example Usage
Initialize:
const { Same } = require("Same");
var same = new Same();
insert() and over()
same.insert(1, "one"); // inserts 1 in the first array and "one" in the second array
console.log(same.over(1) + "\n" + same.over("one")); // outputs the overwritten element for the index of the value 1 and the overwritten element for the index of the value "one"
has()
async function main() {
same.insert(1, "one"); //
same.insert(2, "two"); // inserts many values in the Same
same.insert("uno", 1); //
console.log(await same.has(1)); // checks if the Same has the value 1 and outputs an array of objects containing the index and the number of array where the value 1 is stored
}
main() // calls an async function (THE MODULE has() MUST BE AWAITED)
replaceUnd()
async function main() {
let ar1 = [0, 1, 2, 3]; // creates a 4-indexed array
let ar2 = ["zero", "one", "two"]; // creates a 3-indexed array
same.insert(ar1, ar2); // inserts the arrays in the Same; since ar2 has one value less than ar1, in the second array there will be an empty space
await same.replaceUnd(["three"]) // the empty space is overwritten with the string "three"
console.log(same);
}
main() // calls the async function (THE MODULE replaceUnd() MUST BE AWAITED)
replace()
same.insert(1, "one");
same.insert(2, "two");
same.replace(1, false, "one", 1); // replaces the indexes in the Same of the value 1 with "one" in the first array and 1 in the second
same.replace(1, true, "two", 2); // replaces the indexes 1 in the Same with "two" in the first array and 2 in the second
console.log(same);
cut()
for (let i = 0; i < 10; i++) {
same.insert(i, i - i * 2); // inserts in the first array every number from 0 to 9 and in the second array every number from 0 to -9
}
console.log(same);
console.log("\n\n\n");
console.log(same.cut(2, 4)); // outputs the object with two arrays with every element of the first and second array inside the indexes 2 and 4
remove()
for (let i = 0; i < 10; i++) {
same.insert(i, i - i * 2); // inserts in the first array every number from 0 to 9 and in the second array every number from 0 to -9
}
same.insert("hello", "ciao") // inserts in the first array the string "hello" and in the second array the string "ciao"
console.log(same);
console.log("\n\n\n");
same.remove(0); // removes the indexes 0 for both arrays
same.remove("hello") // removes the indexes of the value "hello" for both arrays
same.remove([1, 3, 5, 7, 9], false) // removes every odd number from the arrays
console.log(same);
forEach()
async function main() {
for (let i = 0; i < 10; i++) {
same.insert(i, i - i * 2); // inserts in the first array every number from 0 to 9 and in the second array every number from 0 to -9
}
await same.forEach((element) => { // starts to loop throught every element in the same
console.log(element) // every time the loop executes it outputs the element
})
}
main() // calls and async function (THE MODULE forEach() MUST BE AWAITED)
charNumbInsert()
async function main() {
await same.charNumbInsert(-4, 9); // inserts in the first array every number from -4 to 9, translates them into spoken language and inserts the strings in the second array
await same.forEach((element) => {
console.log(element); // outputs every element in the Same
})
}
main() // calls an async function (THE MODULE charNumbInsert() MUST BE AWAITED)
Complete Documentation
Same
The deposit from where the parallel arrays manipulation starts
Constructor
new Same();
Properties = {
first,
second,
timestamp
}
Methods = {
insert(),
over(),
has(),
replaceUnd(),
replace(),
cut(),
remove(),
forEach(),
cherNumbInsert(),
NoValue(),
NoFn(),
_0Value()
}
Properties
- .first
The first array, where the variables are stored
Type: Array
- .second
The second array, where the variables are stored
Type: Array
- .timestamp
The moment when the Same was created
Type: Date
Methods
- .insert(firstValue, secondValue)
Inserts in the Same two values
To Note: When both firstValue and secondValue are objects or arrays, insert() will automatically add every property or element in the next indexes
Parameter | Type | Description --------- | ---- | ----------- firstValue | any | The value stored in the first array of the Same secondValue | any | The value stored in the second array of the SameReturns: number <The new Same length>
- .over(value)
Overwrites the value into the corresponding one; if there are more indexes with the same value, it returns an array of its overwritten values
To Note: Value will be read only as element inside the Same
Parameter | Type | Description --------- | ---- | ----------- value | any | The value in the same index of its correspodingReturns: any <The corresponding value of the first parameter>
- .has(value)
Loops thorught every element in the Same and checks if there's the value to search for
To Note: has() does not return true, instead, a object[]
Parameter | Type | Description --------- | ---- | ----------- value | any | The value to checkReturns: boolean | object[] <If the value is not in the same returns false, else returns more objects containing infos about the position of the value found>
- .replaceUnd(elements)
Replaces every undefined variable inside the Same (FROM THE FIRST ARRAY TO THE SECOND)
Parameter | Type | Description --------- | ---- | ----------- elements | any[] | The array of elements to replace in orderReturns: number <The new Same length>
- .replace(index, isIndex, firstElement, secondElement)
Replaces the variables in the Same from the index
Parameter | Type | Description --------- | ---- | ----------- index | number or any | The index of the variables to replace (it can be one of the values itself) isIndex | boolean | Checks if the number inserted as index is an index or a variable inside the Same firstElement | any | The value to replace in the first array secondElement | any | The value to replace in the second arrayReturns: any[] <An array of the replaced elements>
- .cut(firstIndex, lastIndex)
Only returns an Object with an array of a part from the first and second arrays
To Note: cut() does not delete any elements from the same
Parameter | Type | Description --------- | ---- | ----------- firstIndex | number or any | The index of the Same from where the selection starts (it may be the value of the index itself) lastIndex | number or any | The index of the Same form where the selection ends (it may be the value of the index itself)Returns: object <The container of the elements cut in the first and second array>
- .remove(value, isIndex, spaces)
Removes one or more elements at the indexes
Parameter | Type | Description --------- | ---- | ----------- value | number or any | The index from where the selection starts; it can be the value of the index itself; if it's an array of values, spaces can be omitted isIndex | boolean | Decides if the value must be an index of the array or the value of an element spaces | number | The number of indexes that will be removed after the first one (default: 1)Returns: number <The new Same length>
- .length()
Reads how long is the first array of the Same
Returns: number <The Same length>
- .forEach(callback)
Executes a code for every elements inside the first array of the Same first, and then in the second one
Parameter | Type | Description --------- | ---- | ----------- callback | Function | The code that will be executed with every elements in the Same as parameterReturns: Promise <Nothing.>
- .charNumbInsert(from, to)
Translates every number between the parameters (included) from decimal to spoken language and inserts the numbers in the first array, and the translations in the second one
To Note: min value: -9999, max value: 9999
Parameter | Type | Description --------- | ---- | ----------- from | number | The number from where the translation will start to | number | The number from where the translation will endReturns: number <The new Same length>
- .NoValue()
Generates and thorws an error where the value to overwrite was not in the Same
- .NoFn()
Generates and throws an error where the argument was not a Function to execute
- ._0Value()
Generates and throws an error where the argument was not a number
Author
cheeseburger006