diff --git a/src/dependency.js b/src/dependency.js index 780e9ed1..f5aef0de 100644 --- a/src/dependency.js +++ b/src/dependency.js @@ -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); } } @@ -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}`); @@ -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(); diff --git a/test/dependency.spec.js b/test/dependency.spec.js index 88483b6e..07ccb5f2 100644 --- a/test/dependency.spec.js +++ b/test/dependency.spec.js @@ -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} --` + ); + }); }); diff --git a/test/person.spec.js b/test/person.spec.js index 53815977..e1e54c13 100644 --- a/test/person.spec.js +++ b/test/person.spec.js @@ -110,4 +110,16 @@ describe('Person', () => { 'Mickael Jeanroy (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 (https://mjeanroy.com) --' + ); + }); });