aboutsummaryrefslogtreecommitdiff
path: root/src/helpers/arrayFunctions.ts
diff options
context:
space:
mode:
authorPriyansh <[email protected]>2022-01-20 18:55:01 -0500
committerPriyansh <[email protected]>2022-01-20 18:55:01 -0500
commit90be11f8e8ad16ef2a68d71d4523f813db18e6ef (patch)
tree19b9fced8668b83066f3f98a95af67877e721072 /src/helpers/arrayFunctions.ts
parentd0352ed005d0bceb345368274bff4c4c605ed396 (diff)
downloadizuku.js-90be11f8e8ad16ef2a68d71d4523f813db18e6ef.tar.xz
izuku.js-90be11f8e8ad16ef2a68d71d4523f813db18e6ef.zip
feat: added arrayFunctions and rename Izuku to Frame
Diffstat (limited to 'src/helpers/arrayFunctions.ts')
-rw-r--r--src/helpers/arrayFunctions.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/helpers/arrayFunctions.ts b/src/helpers/arrayFunctions.ts
new file mode 100644
index 0000000..7e78e29
--- /dev/null
+++ b/src/helpers/arrayFunctions.ts
@@ -0,0 +1,56 @@
+/**
+ * isArrayOfType check if the array contains only the specified type
+ * @param array: the array to be checked
+ * @param type: the type to be checked
+ * @returns true if the array contains only the specified type
+ */
+
+export function isArrayOfType(array: any[], type: string): boolean {
+ return array.filter((i) => typeof i === type).length === array.length;
+}
+
+/**
+ * range - returns an array of numbers from start to end
+ * @param start: the start of the range
+ * @param end: the end of the range
+ * @param step: the step of the range
+ * @returns an array of numbers from start to end
+ */
+
+export function range(
+ start: number,
+ end: number,
+ step = 1,
+ remove?: Array<number>
+): Array<number> {
+ // Check if start, end and step are integers
+ if (
+ !Number.isInteger(start) ||
+ !Number.isInteger(end) ||
+ !Number.isInteger(step)
+ ) {
+ throw new Error('range parameters must be integers');
+ }
+ const rangeArray: Array<number> = [];
+ // Start must be less than end
+ if (start > end) {
+ throw new Error('starting value must be less than end value');
+ }
+
+ // Step must be greater than 0
+ if (step <= 0) {
+ throw new Error('step must be greater than 0');
+ }
+
+ // If remove is specified, remove the values from the range
+ if (remove) {
+ for (let i = start; i <= end; i += step) {
+ if (!remove.includes(i)) {
+ rangeArray.push(i);
+ }
+ }
+ }
+
+ // return the range
+ return rangeArray;
+}