Skip to content

Commit

Permalink
Fix subtitle for some package results (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrik-csak authored and sindresorhus committed Nov 12, 2018
1 parent 0a54a3a commit bf63e9c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
5 changes: 3 additions & 2 deletions index.js
@@ -1,6 +1,7 @@
'use strict';
const alfy = require('alfy');
const dateFormat = require('date-format');

const cmdSubtitle = require('./source/cmd-subtitle');

// Do not boost exact matches by default, unless specified by the input
const q = /boost-exact:[^\s]+/.test(alfy.input) ? alfy.input : `${alfy.input} boost-exact:false`;
Expand All @@ -26,7 +27,7 @@ alfy.fetch('https://api.npms.io/v2/search', {
subtitle: 'Open the npm page instead of the GitHub repo'
},
cmd: {
subtitle: `${pkg.version} published at ${dateFormat('yyyy-dd-MM', new Date(pkg.date))} by ${(pkg.author && pkg.author.name) || pkg.publisher.username}`
subtitle: cmdSubtitle(pkg)
}
},
quicklookurl: pkg.links.repository && `${pkg.links.repository}#readme`
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -38,6 +38,7 @@
"devDependencies": {
"alfy-test": "^0.3.0",
"ava": "*",
"semver-regex": "^2.0.0",
"xo": "*"
}
}
23 changes: 23 additions & 0 deletions source/cmd-subtitle.js
@@ -0,0 +1,23 @@
const dateFormat = require('date-format');

/**
* @param {object} pgk - A single package from the npms API
* @returns {string} - The command-modifier subtitle for the package
*/
const cmdSubtitle = ({author, date, publisher, version}) => {
let subtitle = `${version}`;

if (date) {
subtitle += ` published at ${dateFormat('yyyy-dd-MM', new Date(date))}`;
}

if (author) {
subtitle += ` by ${(author && author.name)}`;
} else if (publisher) {
subtitle += ` by ${publisher.username}`;
}

return subtitle;
};

module.exports = cmdSubtitle;
23 changes: 21 additions & 2 deletions test.js
@@ -1,15 +1,16 @@
import test from 'ava';
import alfyTest from 'alfy-test';
import semverRegex from 'semver-regex';

test(async t => {
const alfy = alfyTest();

const result = await alfy('boost-exact:true chalk'); // Ensure chalk is first
const result = await alfy('boost-exact:true chalk'); // Ensure chalk is first
delete result[0].mods.cmd;

t.deepEqual(result[0], {
title: 'chalk',
subtitle: 'Terminal string styling done right. Much color',
subtitle: 'Terminal string styling done right',
arg: 'https://github.com/chalk/chalk',
mods:
{
Expand All @@ -22,3 +23,21 @@ test(async t => {
quicklookurl: 'https://github.com/chalk/chalk#readme'
});
});

test('command-modifier subtitle with date and author', async t => {
const alfy = alfyTest();

const result = await alfy('boost-exact:true chalk');
const {subtitle} = result[0].mods.cmd;

t.regex(subtitle, new RegExp(`${semverRegex().source} published at.*by.*`));
});

test('command-modifier subtitle without date and author', async t => {
const alfy = alfyTest();

const result = await alfy('boost-exact:true @types/consola');
const {subtitle} = result[0].mods.cmd;

t.regex(subtitle, semverRegex());
});

0 comments on commit bf63e9c

Please sign in to comment.