Skip to content

Commit

Permalink
[Refactor] use loops instead of array.prototype.find
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 9, 2024
1 parent 7e992d2 commit d58884b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
19 changes: 7 additions & 12 deletions getCategory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var find = require('array.prototype.find');
var entries = require('object.entries');
var satisfies = require('semver').satisfies;

Expand All @@ -9,17 +8,13 @@ var ranges = require('./ranges');
/** @type {import('./getCategory')} */
module.exports = function getCategory() {
var version = arguments.length > 0 ? arguments[0] : process.version;
/** @type {import('./types').RangePair | undefined} */
var found = find(
entries(ranges),
/** @type {(entry: import('./types').RangePair) => boolean} */
function (entry) {
var range = entry[0];
return satisfies(version, range);
var rangeEntries = entries(ranges);
for (var i = 0; i < rangeEntries.length; i += 1) {
var entry = rangeEntries[i];
if (satisfies(version, entry[0])) {
return entry[1];
}
);
if (!found) {
throw new RangeError('no category found for version ' + version);
}
return found[1];

throw new RangeError('no category found for version ' + version);
};
17 changes: 7 additions & 10 deletions getConditionsForCategory.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
'use strict';

var find = require('array.prototype.find');
var entries = require('object.entries');

var ranges = require('./ranges');

/** @type {import('./getConditionsForCategory')} */
module.exports = function getConditionsForCategory(category) {
/** @type {import('./types').RangePair | undefined} */
var found = find(
entries(ranges),
/** @type {(entry: import('./types').RangePair) => boolean} */
function (entry) {
var cat = entry[1];
return cat === category;
}
);
var rangeEntries = entries(ranges);
var found = false;
for (var i = 0; !found && i < rangeEntries.length; i++) {
var entry = rangeEntries[i];
found = entry[1] === category;
}
if (!found) {
throw new RangeError('invalid category ' + category);
}

var moduleSystem = arguments.length > 1 ? arguments[1] : null;
if (arguments.length > 1 && moduleSystem !== 'import' && moduleSystem !== 'require') {
throw new TypeError('invalid moduleSystem: must be `\'require\'` or `\'import\'` if provided, got' + moduleSystem);
Expand Down
19 changes: 7 additions & 12 deletions getRange.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
'use strict';

var find = require('array.prototype.find');
var entries = require('object.entries');

var ranges = require('./ranges');

/** @type {import('./getRange')} */
module.exports = function getRange(category) {
/** @type {import('./types').RangePair | undefined} */
var found = find(
entries(ranges),
/** @type {(entry: import('./types').RangePair) => boolean} */
function (entry) {
var cat = entry[1];
return cat === category;
var rangeEntries = entries(ranges);
for (var i = 0; i < rangeEntries.length; i += 1) {
var entry = rangeEntries[i];
if (entry[1] === category) {
return entry[0];
}
);
if (!found) {
throw new RangeError('no version range found for category ' + category);
}
return found[0];

throw new RangeError('no version range found for category ' + category);
};
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"homepage": "https://github.com/inspect-js/node-exports-info#readme",
"dependencies": {
"array.prototype.find": "^2.2.3",
"array.prototype.flatmap": "^1.3.2",
"object.entries": "^1.1.8",
"semver": "^6.3.1"
Expand All @@ -65,7 +64,6 @@
"@arethetypeswrong/cli": "^0.16.4",
"@ljharb/eslint-config": "^21.1.1",
"@ljharb/tsconfig": "^0.2.0",
"@types/array.prototype.find": "^2.2.0",
"@types/array.prototype.flatmap": "^1.2.6",
"@types/for-each": "^0.3.3",
"@types/object-inspect": "^1.13.0",
Expand Down

0 comments on commit d58884b

Please sign in to comment.