Fixing a thousand bugs

This commit is contained in:
2023-10-08 19:05:20 +02:00
parent 89a0f8a0a3
commit 0a23ff442f
3 changed files with 46 additions and 56 deletions

View File

@ -1,6 +1,6 @@
import { type UserAPI } from "./API";
import { safeScale, stepsToScale } from "zifferjs";
export { };
export {};
declare global {
interface Array<T> {
@ -12,13 +12,12 @@ declare global {
random(index: number): T;
rand(index: number): T;
degrade(amount: number): T;
repeatAll(amount: number): T;
repeatPair(amount: number): T;
repeat(amount: number): T;
repeatEven(amount: number): T;
repeatOdd(amount: number): T;
beat(division: number): T;
b(division: number): T;
bar(): T;
pulse(): T;
pick(): T;
loop(index: number): T;
shuffle(): this;
@ -26,7 +25,6 @@ declare global {
scaleArp(scaleName: string): this;
rotate(steps: number): this;
unique(): this;
in(value: T): boolean;
square(): number[];
sqrt(): number[];
gen(min: number, max: number, times: number): number[];
@ -34,18 +32,14 @@ declare global {
}
export const makeArrayExtensions = (api: UserAPI) => {
Array.prototype.in = function <T>(this: T[], value: T): boolean {
return this.includes(value);
};
Array.prototype.square = function(): number[] {
Array.prototype.square = function (): number[] {
/**
* @returns New array with squared values.
*/
return this.map((x: number) => x * x);
};
Array.prototype.sqrt = function(): number[] {
Array.prototype.sqrt = function (): number[] {
/**
* @returns New array with square roots of values. Throws if any element is negative.
*/
@ -54,7 +48,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => Math.sqrt(x));
};
Array.prototype.add = function(amount: number): number[] {
Array.prototype.add = function (amount: number): number[] {
/**
* @param amount - The value to add to each element in the array.
* @returns New array with added values.
@ -62,7 +56,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x + amount);
};
Array.prototype.sub = function(amount: number): number[] {
Array.prototype.sub = function (amount: number): number[] {
/**
* @param amount - The value to subtract from each element in the array.
* @returns New array with subtracted values.
@ -70,7 +64,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x - amount);
};
Array.prototype.mult = function(amount: number): number[] {
Array.prototype.mult = function (amount: number): number[] {
/**
* @param amount - The value to multiply with each element in the array.
* @returns New array with multiplied values.
@ -78,7 +72,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x * amount);
};
Array.prototype.div = function(amount: number): number[] {
Array.prototype.div = function (amount: number): number[] {
/**
* @param amount - The value to divide each element in the array by.
* @returns New array with divided values. Throws if division by zero.
@ -87,7 +81,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x / amount);
};
Array.prototype.pick = function() {
Array.prototype.pick = function () {
/**
* Returns a random element from an array.
*
@ -96,7 +90,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[Math.floor(api.randomGen() * this.length)];
};
Array.prototype.gen = function(min: number, max: number, times: number) {
Array.prototype.gen = function (min: number, max: number, times: number) {
/**
* Returns an array of random numbers.
* @param min - The minimum value of the random numbers
@ -113,25 +107,22 @@ export const makeArrayExtensions = (api: UserAPI) => {
);
};
Array.prototype.bar = function() {
Array.prototype.bar = function (value: number = 1) {
/**
* Returns an element from an array based on the current bar.
*
* @param array - The array of values to pick from
*/
return this[api.app.clock.time_position.bar % this.length];
if (value === 1) {
return this[api.app.clock.time_position.bar % this.length];
} else {
return this[
Math.floor(api.app.clock.time_position.bar / value) % this.length
];
}
};
Array.prototype.pulse = function() {
/**
* Returns an element from an array based on the current pulse.
*
* @param array - The array of values to pick from
*/
return this[api.app.clock.time_position.pulse % this.length];
};
Array.prototype.beat = function(divisor: number = 1) {
Array.prototype.beat = function (divisor: number = 1) {
const chunk_size = divisor; // Get the first argument (chunk size)
const timepos = api.app.clock.pulses_since_origin;
const slice_count = Math.floor(
@ -141,7 +132,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
};
Array.prototype.b = Array.prototype.beat;
Array.prototype.shuffle = function() {
Array.prototype.shuffle = function () {
/**
* Shuffles the array in place.
*
@ -160,7 +151,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.rotate = function(steps: number) {
Array.prototype.rotate = function (steps: number) {
/**
* Rotates the array in place.
*
@ -180,7 +171,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.unique = function() {
Array.prototype.unique = function () {
/**
* Removes duplicate elements from the array in place.
*
@ -213,7 +204,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
if (this.length <= 1) {
return this;
}
for (let i = 0; i < this.length;) {
for (let i = 0; i < this.length; ) {
const rand = api.randomGen() * 100;
if (rand < amount) {
if (this.length > 1) {
@ -228,7 +219,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.repeatAll = function <T>(this: T[], amount: number = 1) {
Array.prototype.repeat = function <T>(this: T[], amount: number = 1) {
/**
* Repeats all elements in the array n times.
*
@ -250,7 +241,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.repeatPair = function <T>(this: T[], amount: number = 1) {
Array.prototype.repeatOdd = function <T>(this: T[], amount: number = 1) {
/**
* Repeats all elements in the array n times, except for the
* elements at odd indexes.
@ -280,7 +271,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.repeatOdd = function <T>(this: T[], amount: number = 1) {
Array.prototype.repeatEven = function <T>(this: T[], amount: number = 1) {
/**
* Repeats all elements in the array n times, except for the
* elements at even indexes.
@ -326,7 +317,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return left_to_right.concat(right_to_left);
};
Array.prototype.loop = function(index: number) {
Array.prototype.loop = function (index: number) {
/**
* Returns an element from the array based on the index.
* The index will wrap over the array.
@ -337,7 +328,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[index % this.length];
};
Array.prototype.random = function() {
Array.prototype.random = function () {
/**
* Returns a random element from the array.
*
@ -348,7 +339,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
Array.prototype.rand = Array.prototype.random;
};
Array.prototype.scale = function(
Array.prototype.scale = function (
scale: string = "major",
base_note: number = 0
) {
@ -372,7 +363,7 @@ Array.prototype.scale = function(
});
};
Array.prototype.scaleArp = function(
Array.prototype.scaleArp = function (
scaleName: string = "major",
boundary: number = 0
) {