mirror of
https://github.com/shivammathur/setup-php.git
synced 2026-01-23 00:58:57 +07:00
Refactor to use ES2024+ features for Node 24
Use Set for O(1) redirect status code lookup in fetch.ts Use at(-1) and Object.hasOwn() in tools.ts Use for...of with entries() in utils.ts
This commit is contained in:
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
10
src/fetch.ts
10
src/fetch.ts
@@ -1,3 +1,8 @@
|
||||
/**
|
||||
* Redirect status codes set for O(1) lookup
|
||||
*/
|
||||
const REDIRECT_CODES = new Set([301, 302, 303, 307, 308]);
|
||||
|
||||
/**
|
||||
* Function to fetch a URL using native fetch API (Node 24+)
|
||||
*
|
||||
@@ -26,10 +31,7 @@ export async function fetch(
|
||||
if (response.ok) {
|
||||
const data = await response.text();
|
||||
return {data};
|
||||
} else if (
|
||||
[301, 302, 303, 307, 308].includes(response.status) &&
|
||||
redirect_count <= 0
|
||||
) {
|
||||
} else if (REDIRECT_CODES.has(response.status) && redirect_count <= 0) {
|
||||
return {error: `${response.status}: Redirect error`};
|
||||
} else {
|
||||
return {error: `${response.status}: ${response.statusText}`};
|
||||
|
||||
@@ -140,7 +140,7 @@ export async function filterList(tools_list: string[]): Promise<string[]> {
|
||||
case matches[0] == undefined:
|
||||
break;
|
||||
default:
|
||||
composer = matches[matches.length - 1].replace(/v(\d\S*)/, '$1');
|
||||
composer = matches.at(-1)!.replace(/v(\d\S*)/, '$1');
|
||||
break;
|
||||
}
|
||||
tools_list.unshift(composer);
|
||||
@@ -488,7 +488,7 @@ export async function getData(
|
||||
const tool = parts[0];
|
||||
const version = parts[1];
|
||||
let data: RS;
|
||||
if (Object.keys(json_objects).includes(tool)) {
|
||||
if (Object.hasOwn(json_objects, tool)) {
|
||||
data = json_objects[tool];
|
||||
data['tool'] = tool;
|
||||
} else {
|
||||
|
||||
@@ -97,9 +97,8 @@ export async function parseIniFile(ini_file: string): Promise<string> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Async foreach loop
|
||||
* Async foreach loop using modern for...of pattern
|
||||
*
|
||||
* @author https://github.com/Atinux
|
||||
* @param array
|
||||
* @param callback
|
||||
*/
|
||||
@@ -111,8 +110,8 @@ export async function asyncForEach(
|
||||
array: Array<string>
|
||||
) => Promise<void>
|
||||
): Promise<void> {
|
||||
for (let index = 0; index < array.length; index++) {
|
||||
await callback(array[index], index, array);
|
||||
for (const [index, element] of array.entries()) {
|
||||
await callback(element, index, array);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user