Skip to content

Commit

Permalink
feat: add many to many conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay-Karia committed Aug 18, 2024
1 parent 4984af0 commit 875b86a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 16 deletions.
14 changes: 10 additions & 4 deletions dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -636,18 +636,24 @@ var convertMultiple = function (str, to) {
var commands = [];
// one to many
if (typeof str === 'string' && Array.isArray(to)) {
to.forEach(function (t, index) {
commands.push(convert(str, to[index]));
to.forEach(function (t) {
commands.push(convert(str, t));
});
}
// many to one
else if (Array.isArray(str) && typeof to === 'string') {
str.forEach(function (s, index) {
str.forEach(function (s) {
commands.push(convert(s, to));
});
}
// many to many
else ;
else if (Array.isArray(str) && Array.isArray(to)) {
to.forEach(function (t) {
str.forEach(function (s) {
commands.push(convert(s, t));
});
});
}
return commands;
};

Expand Down
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions dist/npm-to-yarn.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/npm-to-yarn.umd.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,23 @@ export const convertMultiple = (str: string | string[], to: Command | Command[])

// one to many
if (typeof str === 'string' && Array.isArray(to)) {
to.forEach((t, index) => {
commands.push(convert(str, to[index]))
to.forEach((t) => {
commands.push(convert(str, t))
})
}
// many to one
else if (Array.isArray(str) && typeof to === 'string') {
str.forEach((s, index) => {
str.forEach((s) => {
commands.push(convert(s, to))
})
}
// many to many
else if (Array.isArray(str) && Array.isArray(to)) {
to.forEach((t) => {
str.forEach((s) => {
commands.push(convert(s, t))
})
})
}

return commands
Expand Down
42 changes: 39 additions & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ describe('Yarn to NPM tests', () => {
})

describe("Multiple Convert Tests", () => {
// one to many
const oneToManyTests: {
command: string,
managers: Command[],
Expand Down Expand Up @@ -462,7 +461,6 @@ describe("Multiple Convert Tests", () => {
},
]

// many to one
const manyToOneTests: {
command: string[],
managers: Command,
Expand Down Expand Up @@ -494,7 +492,39 @@ describe("Multiple Convert Tests", () => {
result: ["pnpm outdated", "pnpm outdated react"]
},
]
// many to many


const manyToManyTests: {
command: string[],
managers: Command[],
result: string[]
}[] = [
{
command: ["npm install react", "npm install react-dom"],
managers: ["yarn", "pnpm"],
result: ["yarn add react", "yarn add react-dom", "pnpm add react", "pnpm add react-dom"]
},
{
command: ["npm install squirrelly --save", "npm install react --save-optional"],
managers: ["yarn", "bun"],
result: ["yarn add squirrelly", "yarn add react --optional", "bun add squirrelly", "bun add react --optional"]
},
{
command: ["npm install squirrelly -E", "npm install react --save-prod"],
managers: ["pnpm", "bun"],
result: ["pnpm add squirrelly -E", "pnpm add react --save-prod", "bun add squirrelly --exact", "bun add react --production"]
},
{
command: ["npm install squirrelly --save-optional", "npm install react --save-prod", "npm install squirrelly --save-prod"],
managers: ["yarn", "pnpm"],
result: ["yarn add squirrelly --optional", "yarn add react --production", "yarn add squirrelly --production", "pnpm add squirrelly --save-optional", "pnpm add react --save-prod", "pnpm add squirrelly --save-prod"]
},
{
command: ["npm outdated", "npm outdated react"],
managers: ["yarn", "pnpm"],
result: ["yarn outdated", "yarn outdated react", "pnpm outdated", "pnpm outdated react"]
}
]

describe("One to Many", () => {
it.each(oneToManyTests)('%s', (test) => {
Expand All @@ -507,4 +537,10 @@ describe("Multiple Convert Tests", () => {
expect(convertMultiple(test.command, test.managers)).toEqual(test.result)
})
})

describe("Many to Many", () => {
it.each(manyToManyTests)('%s', (test) => {
expect(convertMultiple(test.command, test.managers)).toEqual(test.result)
})
})
})

0 comments on commit 875b86a

Please sign in to comment.