Skip to content

Commit

Permalink
feat: add optional prefix and suffix to the output
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 24, 2016
1 parent 1c7eb98 commit 2fec575
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class Dependency {
/**
* Serialize dependency as a string.
*
* @param {string} joiner Optional character used to join all the lines.
* @param {string} prefix Optional prefix prepended to the output string.
* @param {suffix} suffix Optional suffix appended to the output string.
* @param {string} joiner Optional character used to join all the lines.
* @return {string} The dependency correctly formatted.
*/
text(joiner = EOL, prefix = '', suffix = '') {
return `${prefix}${formatDependency(this, joiner)}${suffix}`;
text(prefix = '', suffix = '', joiner = EOL) {
return formatDependency(this, prefix, suffix, joiner);
}
}

Expand Down Expand Up @@ -116,12 +116,12 @@ function parseDependency(pkg) {
* Format dependency data to a single string.
*
* @param {Object} dependency Dependency to format.
* @param {string} joiner Optional character used to join all the lines.
* @param {string} prefix Optional prefix prepended to the output string.
* @param {suffix} suffix Optional suffix appended to the output string.
* @param {string} joiner Optional character used to join all the lines.
* @return {string} The output string.
*/
function formatDependency(dependency, joiner = EOL, prefix = '', suffix = '') {
function formatDependency(dependency, prefix = '', suffix = '', joiner = EOL) {
const lines = [];

lines.push(`${prefix}Name: ${dependency.name}${suffix}`);
Expand All @@ -142,14 +142,14 @@ function formatDependency(dependency, joiner = EOL, prefix = '', suffix = '') {
}

if (dependency.author) {
lines.push(`${prefix}Author: ${dependency.author.text(prefix, suffix)}${suffix}`);
lines.push(`${prefix}Author: ${dependency.author.text()}${suffix}`);
}

if (dependency.contributors) {
lines.push(`${prefix}Contributors:${suffix}`);

const allContributors = _.chain(dependency.contributors)
.map((contributor) => contributor.text(prefix, suffix))
.map((contributor) => contributor.text())
.map((line) => `${prefix} ${line}${suffix}`)
.value();

Expand Down
32 changes: 32 additions & 0 deletions test/dependency.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,36 @@ describe('Dependency', () => {
` ${pkg.contributors[1].name}`
);
});

it('should format dependency with prefix and suffix', () => {
const pkg = {
name: 'foo',
version: '1.0.0',
license: 'MIT',
description: 'Desc',
homepage: 'https://github.com/mjeanroy',
repository: {type: 'GIT', url: 'git@github.com/mjeanroy'},
author: {name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'},
contributors: [
{name: 'Mickael Jeanroy', email: 'mickael.jeanroy@gmail.com'},
{name: 'John Doe'},
],
};

const dependency = new Dependency(pkg);

expect(dependency.text(' * ', ' --')).toEqual(
` * Name: ${pkg.name} --\n` +
` * Version: ${pkg.version} --\n` +
` * License: ${pkg.license} --\n` +
` * Private: false --\n` +
` * Description: ${pkg.description} --\n` +
` * Repository: ${pkg.repository.url} --\n` +
` * Homepage: ${pkg.homepage} --\n` +
` * Author: ${pkg.author.name} <${pkg.author.email}> --\n` +
` * Contributors: --\n` +
` * ${pkg.contributors[0].name} <${pkg.contributors[0].email}> --\n` +
` * ${pkg.contributors[1].name} --`
);
});
});
12 changes: 12 additions & 0 deletions test/person.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,16 @@ describe('Person', () => {
'Mickael Jeanroy <mickael.jeanroy@gmail.com> (https://mjeanroy.com)'
);
});

it('should format person with a suffix and a prefix', () => {
const person = new Person({
name: 'Mickael Jeanroy',
email: 'mickael.jeanroy@gmail.com',
url: 'https://mjeanroy.com',
});

expect(person.text('-- ', ' --')).toBe(
'-- Mickael Jeanroy <mickael.jeanroy@gmail.com> (https://mjeanroy.com) --'
);
});
});

0 comments on commit 2fec575

Please sign in to comment.