Added counter to lists
This commit is contained in:
10
src/API.ts
10
src/API.ts
@ -977,6 +977,7 @@ export class UserAPI {
|
||||
return this.counters[name].value;
|
||||
};
|
||||
$ = this.counter;
|
||||
count = this.counter;
|
||||
|
||||
// =============================================================
|
||||
// Iterator functions (for loops, with evaluation count, etc...)
|
||||
@ -1737,12 +1738,21 @@ export class UserAPI {
|
||||
* @param step - The step value of the array
|
||||
* @returns An array of values between start and end, with a given step
|
||||
*/
|
||||
function countPlaces(num: number) {
|
||||
var text = num.toString();
|
||||
var index = text.indexOf(".");
|
||||
return index == -1 ? 0 : (text.length - index - 1);
|
||||
}
|
||||
const result: number[] = [];
|
||||
|
||||
if ((end > start && step > 0) || (end < start && step < 0)) {
|
||||
for (let value = start; value <= end; value += step) {
|
||||
result.push(value);
|
||||
}
|
||||
} else if((end > start && step < 0) || (end < start && step > 0)) {
|
||||
for (let value = start; value >= end; value -= step) {
|
||||
result.push(parseFloat(value.toFixed(countPlaces(step))));
|
||||
}
|
||||
} else {
|
||||
console.error("Invalid range or step provided.");
|
||||
}
|
||||
|
||||
@ -120,6 +120,19 @@ beat(1)::sound(['kick', 'fsnare'].dur(3, 1))
|
||||
true,
|
||||
)}
|
||||
|
||||
## Iterating over lists
|
||||
|
||||
- <ic>counter(name,limit?,step?)</ic>: return the next value on the list based on counter value. The limit is optional and defaults to the length of the list. The step is optional and defaults to 1. Setting / changing limit will reset the counter.
|
||||
- <ic>$(name,limit?,step?)</ic>: shorter alias for the counter.
|
||||
|
||||
${makeExample(
|
||||
"Using counter to iterate over a list",
|
||||
`
|
||||
beat(0.5) :: sound("bd").gain(line(0,1,0.01).$("ramp")).out()
|
||||
`,
|
||||
true,
|
||||
)}
|
||||
|
||||
## Manipulating notes and scales
|
||||
|
||||
- <ic>pitch()</ic>: convert a list of integers to pitch classes
|
||||
|
||||
@ -33,6 +33,8 @@ declare global {
|
||||
gen(min: number, max: number, times: number): number[];
|
||||
sometimes(func: Function): number[];
|
||||
apply(func: Function): number[];
|
||||
counter(name: string | number, limit?: number, step?: number): number[]
|
||||
$(name: string | number, limit?: number, step?: number): number[];
|
||||
}
|
||||
}
|
||||
|
||||
@ -398,7 +400,30 @@ export const makeArrayExtensions = (api: UserAPI) => {
|
||||
return this[Math.floor(api.randomGen() * this.length)];
|
||||
};
|
||||
Array.prototype.rand = Array.prototype.random;
|
||||
|
||||
Array.prototype.counter = function(
|
||||
name: string | number,
|
||||
limit?: number,
|
||||
step?: number) {
|
||||
/**
|
||||
* @param n - Returns next item in array until the end, then returns the last value.
|
||||
*
|
||||
* @returns the shifted array
|
||||
*/
|
||||
const idx = api.counter(name,limit,step);
|
||||
if(limit) {
|
||||
return this[idx % this.length];
|
||||
} else if(idx < this.length) {
|
||||
return this[idx];
|
||||
} else {
|
||||
return this[this.length - 1];
|
||||
}
|
||||
};
|
||||
Array.prototype.$ = Array.prototype.counter;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Array.prototype.scale = function (
|
||||
scale: string = "major",
|
||||
|
||||
Reference in New Issue
Block a user