From 3ad2a4a2d48b17653eda6e70fd80cf611cfc30c5 Mon Sep 17 00:00:00 2001 From: Justin Dalrymple Date: Wed, 28 Jan 2026 22:56:21 -0500 Subject: [PATCH] Removing lodash dep # Conflicts: # src/util/git.js # Conflicts: # src/generate/index.js # src/init/init-content.js --- package.json | 2 +- src/contributors/add.js | 29 +++++--- src/contributors/index.js | 3 +- src/contributors/prompt.js | 42 +++++------ src/generate/__tests__/format-badge.js | 11 +-- src/generate/__tests__/format-contributor.js | 11 +-- src/generate/format-badge.js | 4 +- src/generate/format-contribution-type.js | 10 +-- src/generate/format-contributor.js | 40 +++++----- src/generate/index.js | 78 +++++++++++--------- src/init/init-content.js | 15 ++-- src/init/prompt.js | 7 +- src/util/config-file.js | 3 +- src/util/contribution-types.js | 3 +- src/util/git.js | 12 +-- src/util/index.js | 1 + src/util/template.js | 10 +++ 17 files changed, 155 insertions(+), 126 deletions(-) create mode 100644 src/util/template.js diff --git a/package.json b/package.json index e8769d4e..17bbf84b 100644 --- a/package.json +++ b/package.json @@ -49,9 +49,9 @@ "async": "^3.1.0", "chalk": "^5.6.2", "didyoumean": "^1.2.1", + "eta": "^4.5.0", "inquirer": "^7.3.3", "json-fixer": "^1.6.8", - "lodash": "^4.11.2", "node-fetch": "^2.6.0", "pify": "^5.0.0", "yargs": "^18.0.0" diff --git a/src/contributors/add.js b/src/contributors/add.js index d45a2d6f..a34827f0 100644 --- a/src/contributors/add.js +++ b/src/contributors/add.js @@ -1,11 +1,13 @@ -const _ = require('lodash/fp') - function uniqueTypes(contribution) { return contribution.type || contribution } function formatContributions(options, existing = [], types) { - const same = _.intersectionBy(uniqueTypes, existing, types) + const same = types.filter(type => + existing.some( + existingType => uniqueTypes(existingType) === uniqueTypes(type), + ), + ) const remove = types.length < existing.length && same.length if (options.url) { @@ -20,17 +22,23 @@ function formatContributions(options, existing = [], types) { return same } - return _.uniqBy(uniqueTypes, existing.concat(types)) + const combined = existing.concat(types) + return combined.filter( + (item, index, arr) => + index === + arr.findIndex(other => uniqueTypes(other) === uniqueTypes(item)), + ) } function updateContributor(options, contributor, contributions) { - return _.assign(contributor, { + return { + ...contributor, contributions: formatContributions( options, contributor.contributions, contributions, ), - }) + } } function updateExistingContributor(options, username, contributions) { @@ -48,9 +56,10 @@ function updateExistingContributor(options, username, contributions) { function addNewContributor(options, username, contributions, infoFetcher) { return infoFetcher(username, options.repoType, options.repoHost).then( userData => { - const contributor = _.assign(userData, { + const contributor = { + ...userData, contributions: formatContributions(options, [], contributions), - }) + } return options.contributors.concat(contributor) }, ) @@ -63,12 +72,12 @@ module.exports = function addContributor( infoFetcher, ) { // case insensitive find - const exists = _.find(contributor => { + const exists = options.contributors.find(contributor => { return ( contributor.login && contributor.login.toLowerCase() === username.toLowerCase() ) - }, options.contributors) + }) if (exists) { return Promise.resolve( diff --git a/src/contributors/index.js b/src/contributors/index.js index 3e3544c9..b7dbb275 100644 --- a/src/contributors/index.js +++ b/src/contributors/index.js @@ -1,11 +1,10 @@ -const _ = require('lodash/fp') const util = require('../util') const repo = require('../repo') const add = require('./add') const prompt = require('./prompt') function isNewContributor(contributorList, username) { - return !_.find({login: username}, contributorList) + return !contributorList.find(contributor => contributor.login === username) } module.exports = function addContributor(options, username, contributions) { diff --git a/src/contributors/prompt.js b/src/contributors/prompt.js index 58fdd90c..e112051b 100644 --- a/src/contributors/prompt.js +++ b/src/contributors/prompt.js @@ -1,21 +1,18 @@ -const _ = require('lodash/fp') const inquirer = require('inquirer') const util = require('../util') const repo = require('../repo') -const contributionChoices = _.flow( - util.contributionTypes, - _.toPairs, - _.sortBy(pair => { - return pair[1].description - }), - _.map(pair => { - return { - name: `${pair[1].symbol} ${pair[1].description}`, - value: pair[0], - } - }), -) +function contributionChoices(options) { + const types = util.contributionTypes(options) + const pairs = Object.entries(types) + const sorted = pairs.sort((a, b) => + a[1].description.localeCompare(b[1].description), + ) + return sorted.map(pair => ({ + name: `${pair[1].symbol} ${pair[1].description}`, + value: pair[0], + })) +} function getQuestions(options, username, contributions) { return [ @@ -68,7 +65,10 @@ function getQuestions(options, username, contributions) { if (!input.length) { return 'Use space to select at least one contribution type.' - } else if (_.isEqual(input, previousContributions)) { + } else if ( + JSON.stringify(input.sort()) === + JSON.stringify(previousContributions.sort()) + ) { return 'Nothing changed, use space to select contribution types.' } return true @@ -81,15 +81,15 @@ function getValidUserContributions(options, contributions) { const validContributionTypes = util.contributionTypes(options) const userContributions = contributions && contributions.split(',') - const validUserContributions = _.filter( + const validUserContributions = userContributions.filter( userContribution => validContributionTypes[userContribution] !== undefined, - )(userContributions) + ) - const invalidUserContributions = _.filter( + const invalidUserContributions = userContributions.filter( userContribution => validContributionTypes[userContribution] === undefined, - )(userContributions) + ) - if (_.isEmpty(validUserContributions)) { + if (validUserContributions.length === 0) { throw new Error( `${invalidUserContributions.toString()} is/are invalid contribution type(s)`, ) @@ -106,5 +106,5 @@ module.exports = function prompt(options, username, contributions) { : getValidUserContributions(options, contributions), } const questions = getQuestions(options, username, contributions) - return inquirer.prompt(questions).then(_.assign(defaults)) + return inquirer.prompt(questions).then(answers => ({...defaults, ...answers})) } diff --git a/src/generate/__tests__/format-badge.js b/src/generate/__tests__/format-badge.js index 1f13e65a..913107b9 100644 --- a/src/generate/__tests__/format-badge.js +++ b/src/generate/__tests__/format-badge.js @@ -1,4 +1,3 @@ -import _ from 'lodash/fp' import formatBadge from '../format-badge' test('return badge with the number of contributors', () => { @@ -8,8 +7,8 @@ test('return badge with the number of contributors', () => { const expected16 = '[![All Contributors](https://img.shields.io/badge/all_contributors-16-orange.svg?style=flat-square)](#contributors-)' - expect(formatBadge(options, _.times(_.constant({}), 8))).toBe(expected8) - expect(formatBadge(options, _.times(_.constant({}), 16))).toBe(expected16) + expect(formatBadge(options, Array(8).fill({}))).toBe(expected8) + expect(formatBadge(options, Array(16).fill({}))).toBe(expected16) }) test('be able to specify custom badge template', () => { @@ -17,10 +16,8 @@ test('be able to specify custom badge template', () => { badgeTemplate: 'We have <%= contributors.length %> contributors', } - expect(formatBadge(options, _.times(_.constant({}), 8))).toBe( - 'We have 8 contributors', - ) - expect(formatBadge(options, _.times(_.constant({}), 16))).toBe( + expect(formatBadge(options, Array(8).fill({}))).toBe('We have 8 contributors') + expect(formatBadge(options, Array(16).fill({}))).toBe( 'We have 16 contributors', ) }) diff --git a/src/generate/__tests__/format-contributor.js b/src/generate/__tests__/format-contributor.js index e9ebde86..ab2b00c6 100644 --- a/src/generate/__tests__/format-contributor.js +++ b/src/generate/__tests__/format-contributor.js @@ -1,4 +1,3 @@ -import _ from 'lodash/fp' import formatContributor from '../format-contributor' import contributors from './fixtures/contributors.json' @@ -14,9 +13,10 @@ function fixtures() { } test('format a simple contributor', () => { - const contributor = _.assign(contributors.kentcdodds, { + const contributor = { + ...contributors.kentcdodds, contributions: ['review'], - }) + } const {options} = fixtures() const expected = @@ -46,9 +46,10 @@ test('format contributor using custom template', () => { }) test('default image size to 100', () => { - const contributor = _.assign(contributors.kentcdodds, { + const contributor = { + ...contributors.kentcdodds, contributions: ['review'], - }) + } const {options} = fixtures() delete options.imageSize diff --git a/src/generate/format-badge.js b/src/generate/format-badge.js index f03730ca..51fd71e1 100644 --- a/src/generate/format-badge.js +++ b/src/generate/format-badge.js @@ -1,10 +1,10 @@ -const _ = require('lodash/fp') +const util = require('../util') const defaultTemplate = '[![All Contributors](https://img.shields.io/badge/all_contributors-<%= contributors.length %>-orange.svg?style=flat-square)](#contributors-)' module.exports = function formatBadge(options, contributors) { - return _.template(options.badgeTemplate || defaultTemplate)({ + return util.template(options.badgeTemplate || defaultTemplate)({ contributors, }) } diff --git a/src/generate/format-contribution-type.js b/src/generate/format-contribution-type.js index b54ac774..42a31b54 100644 --- a/src/generate/format-contribution-type.js +++ b/src/generate/format-contribution-type.js @@ -1,8 +1,6 @@ -const _ = require('lodash/fp') - const util = require('../util') -const linkTemplate = _.template( +const linkTemplate = util.template( '<%= symbol %>', ) @@ -28,7 +26,7 @@ module.exports = function formatContribution( const templateData = { symbol: type.symbol, - description: type.description, + description: type.description || '', contributor, options, } @@ -38,10 +36,10 @@ module.exports = function formatContribution( if (contribution.url) { url = contribution.url } else if (type.link) { - url = _.template(type.link)(templateData) + url = util.template(type.link)(templateData) } - return linkTemplate(_.assign({url}, templateData)) + return linkTemplate({url, ...templateData}) } function getUrl(contribution, contributor) { diff --git a/src/generate/format-contributor.js b/src/generate/format-contributor.js index 5a86e83f..8e2c8bc8 100644 --- a/src/generate/format-contributor.js +++ b/src/generate/format-contributor.js @@ -1,16 +1,16 @@ -const _ = require('lodash/fp') +const util = require('../util') const formatContributionType = require('./format-contribution-type') -const avatarTemplate = _.template( +const avatarTemplate = util.template( '<%= name %>', ) -const avatarBlockTemplate = _.template( +const avatarBlockTemplate = util.template( '<%= avatar %>
<%= name %>
', ) -const avatarBlockTemplateNoProfile = _.template( +const avatarBlockTemplateNoProfile = util.template( '<%= avatar %>
<%= name %>', ) -const contributorTemplate = _.template( +const contributorTemplate = util.template( '<%= avatarBlock %>
<%= contributions %>', ) @@ -20,18 +20,15 @@ function defaultTemplate(templateData) { const rawName = templateData.contributor.name || templateData.contributor.login const name = escapeName(rawName) - const avatar = avatarTemplate( - _.assign(templateData, { - name, - }), - ) - const avatarBlockTemplateData = _.assign( - { - name, - avatar, - }, - templateData, - ) + const avatar = avatarTemplate({ + ...templateData, + name, + }) + const avatarBlockTemplateData = { + name, + avatar, + ...templateData, + } let avatarBlock = null if (templateData.contributor.profile) { @@ -40,7 +37,7 @@ function defaultTemplate(templateData) { avatarBlock = avatarBlockTemplateNoProfile(avatarBlockTemplateData) } - return contributorTemplate(_.assign({avatarBlock}, templateData)) + return contributorTemplate({avatarBlock, ...templateData}) } function escapeName(name) { @@ -50,14 +47,15 @@ function escapeName(name) { } module.exports = function formatContributor(options, contributor) { - const formatter = _.partial(formatContributionType, [options, contributor]) + const formatter = contribution => + formatContributionType(options, contributor, contribution) const contributions = contributor.contributions.map(formatter).join(' ') const templateData = { contributions, contributor, - options: _.assign({imageSize: defaultImageSize}, options), + options: {imageSize: defaultImageSize, ...options}, } const customTemplate = - options.contributorTemplate && _.template(options.contributorTemplate) + options.contributorTemplate && util.template(options.contributorTemplate) return (customTemplate || defaultTemplate)(templateData) } diff --git a/src/generate/index.js b/src/generate/index.js index 88925b4c..8b4c1f62 100644 --- a/src/generate/index.js +++ b/src/generate/index.js @@ -1,8 +1,15 @@ -const _ = require('lodash/fp') -const floor = require('lodash/floor') +const util = require('../util') const formatBadge = require('./format-badge') const formatContributor = require('./format-contributor') +function chunk(array, size) { + const chunks = [] + for (let i = 0; i < array.length; i += size) { + chunks.push(array.slice(i, i + size)) + } + return chunks +} + function injectListBetweenTags(newContent) { return function (previousContent) { const tagToLookFor = `