mirror of
https://github.com/affaan-m/ECC.git
synced 2026-09-11 04:07:54 +02:00
test(pack): select the requested package
This commit is contained in:
@@ -3,19 +3,23 @@ function isPackEntry(value) {
|
||||
}
|
||||
|
||||
function getNpmPackEntry(output, packageName) {
|
||||
const matchesPackage = value => (
|
||||
isPackEntry(value) && value.name === packageName
|
||||
);
|
||||
|
||||
if (Array.isArray(output)) {
|
||||
return output.find(isPackEntry);
|
||||
return output.find(matchesPackage);
|
||||
}
|
||||
|
||||
if (!isPackEntry(output)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (isPackEntry(output[packageName])) {
|
||||
if (matchesPackage(output[packageName])) {
|
||||
return output[packageName];
|
||||
}
|
||||
|
||||
return Object.values(output).find(isPackEntry);
|
||||
return Object.values(output).find(matchesPackage);
|
||||
}
|
||||
|
||||
module.exports = { getNpmPackEntry };
|
||||
|
||||
@@ -18,6 +18,7 @@ function test(name, fn) {
|
||||
|
||||
test('reads the npm 11 array response', () => {
|
||||
const entry = getNpmPackEntry([
|
||||
{ name: 'unrelated-package', filename: 'unrelated-package-1.0.0.tgz' },
|
||||
{ name: 'ecc-universal', filename: 'ecc-universal-2.2.0.tgz' },
|
||||
], 'ecc-universal');
|
||||
|
||||
@@ -35,10 +36,31 @@ test('reads the npm 12 package-keyed response', () => {
|
||||
assert.strictEqual(entry.filename, 'ecc-universal-2.2.0.tgz');
|
||||
});
|
||||
|
||||
test('finds a requested package in a generic object response', () => {
|
||||
const entry = getNpmPackEntry({
|
||||
unrelated: { name: 'unrelated-package', filename: 'unrelated-package-1.0.0.tgz' },
|
||||
target: { name: 'ecc-universal', filename: 'ecc-universal-2.2.0.tgz' },
|
||||
}, 'ecc-universal');
|
||||
|
||||
assert.strictEqual(entry.filename, 'ecc-universal-2.2.0.tgz');
|
||||
});
|
||||
|
||||
test('returns undefined for empty or malformed responses', () => {
|
||||
assert.strictEqual(getNpmPackEntry([], 'ecc-universal'), undefined);
|
||||
assert.strictEqual(getNpmPackEntry({}, 'ecc-universal'), undefined);
|
||||
assert.strictEqual(getNpmPackEntry(null, 'ecc-universal'), undefined);
|
||||
assert.strictEqual(
|
||||
getNpmPackEntry([
|
||||
{ name: 'unrelated-package', filename: 'unrelated-package-1.0.0.tgz' },
|
||||
], 'ecc-universal'),
|
||||
undefined
|
||||
);
|
||||
assert.strictEqual(
|
||||
getNpmPackEntry({
|
||||
unrelated: { name: 'unrelated-package', filename: 'unrelated-package-1.0.0.tgz' },
|
||||
}, 'ecc-universal'),
|
||||
undefined
|
||||
);
|
||||
});
|
||||
|
||||
console.log(`\nPassed: ${passed}`);
|
||||
|
||||
Reference in New Issue
Block a user