Skip to content

Commit

Permalink
refactor: use flatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed May 15, 2024
1 parent 8607e2d commit a18e736
Showing 1 changed file with 34 additions and 48 deletions.
82 changes: 34 additions & 48 deletions packages/markdown/tests/plugins/linksPlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
'[readme1](readme.md)',
'[readme2](../foo/bar/readme.md)',
]
.map((item) => {
.flatMap((item) => {
const link = /([^)]*)/.exec(item)![1]

return [
Expand All @@ -162,7 +162,6 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
item.replace(link, `${link}?a=1&b=2#hash`),
]
})
.flat()
.join('\n\n')

const expectRelativeLinks = [
Expand Down Expand Up @@ -226,15 +225,13 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
relative: '../foo/bar/readme.md',
absolute: null,
},
]
.map(({ raw, ...rest }) => [
{ raw, ...rest },
{ raw: `${raw}#hash`, ...rest },
{ raw: `${raw}?a=1&b=2`, ...rest },
{ raw: `${raw}#hash?a=1&b=2`, ...rest },
{ raw: `${raw}?a=1&b=2#hash`, ...rest },
])
.flat()
].flatMap(({ raw, ...rest }) => [
{ raw, ...rest },
{ raw: `${raw}#hash`, ...rest },
{ raw: `${raw}?a=1&b=2`, ...rest },
{ raw: `${raw}#hash?a=1&b=2`, ...rest },
{ raw: `${raw}?a=1&b=2#hash`, ...rest },
])

it('should render to <a> tag correctly', () => {
const md = MarkdownIt({ html: true }).use(linksPlugin, {
Expand All @@ -259,7 +256,7 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
'<a href="index.html">readme1</a>',
'<a href="../foo/bar/">readme2</a>',
]
.map((item) => {
.flatMap((item) => {
const link = /href="([^"]*)"/.exec(item)![1]

return [
Expand All @@ -270,7 +267,6 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
item.replace(link, `${link}?a=1&amp;b=2#hash`),
]
})
.flat()
.map((a) => `<p>${a}</p>`)
.join('\n') + '\n',
)
Expand Down Expand Up @@ -299,7 +295,7 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
'<RouteLink to="index.html">readme1</RouteLink>',
'<RouteLink to="../foo/bar/">readme2</RouteLink>',
]
.map((item) => {
.flatMap((item) => {
const link = /to="([^"]*)"/.exec(item)![1]

return [
Expand All @@ -310,7 +306,6 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
item.replace(link, `${link}?a=1&amp;b=2#hash`),
]
})
.flat()
.map((a) => `<p>${a}</p>`)
.join('\n') + '\n',
)
Expand Down Expand Up @@ -341,7 +336,7 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
'<RouteLink to="/path/to/">readme1</RouteLink>',
'<RouteLink to="/path/foo/bar/">readme2</RouteLink>',
]
.map((item) => {
.flatMap((item) => {
const link = /to="([^"]*)"/.exec(item)![1]

return [
Expand All @@ -352,7 +347,6 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
item.replace(link, `${link}?a=1&amp;b=2#hash`),
]
})
.flat()
.map((a) => `<p>${a}</p>`)
.join('\n') + '\n',
)
Expand Down Expand Up @@ -419,15 +413,13 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
relative: 'path/foo/bar/readme.md',
absolute: '/path/foo/bar/readme.md',
},
]
.map(({ raw, ...rest }) => [
{ raw, ...rest },
{ raw: `${raw}#hash`, ...rest },
{ raw: `${raw}?a=1&b=2`, ...rest },
{ raw: `${raw}#hash?a=1&b=2`, ...rest },
{ raw: `${raw}?a=1&b=2#hash`, ...rest },
])
.flat(),
].flatMap(({ raw, ...rest }) => [
{ raw, ...rest },
{ raw: `${raw}#hash`, ...rest },
{ raw: `${raw}?a=1&b=2`, ...rest },
{ raw: `${raw}#hash?a=1&b=2`, ...rest },
{ raw: `${raw}?a=1&b=2#hash`, ...rest },
]),
)
})

Expand Down Expand Up @@ -456,7 +448,7 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
`<RouteLink to="/${encoded中}/${encoded文}/">readme1</RouteLink>`,
`<RouteLink to="/${encoded中}/foo/bar/">readme2</RouteLink>`,
]
.map((item) => {
.flatMap((item) => {
const link = /to="([^"]*)"/.exec(item)![1]

return [
Expand All @@ -467,7 +459,6 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
item.replace(link, `${link}?a=1&amp;b=2#hash`),
]
})
.flat()
.map((a) => `<p>${a}</p>`)
.join('\n') + '\n',
)
Expand Down Expand Up @@ -534,15 +525,13 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
relative: `${encoded中}/foo/bar/readme.md`,
absolute: `/${encoded中}/foo/bar/readme.md`,
},
]
.map(({ raw, ...rest }) => [
{ raw, ...rest },
{ raw: `${raw}#hash`, ...rest },
{ raw: `${raw}?a=1&b=2`, ...rest },
{ raw: `${raw}#hash?a=1&b=2`, ...rest },
{ raw: `${raw}?a=1&b=2#hash`, ...rest },
])
.flat(),
].flatMap(({ raw, ...rest }) => [
{ raw, ...rest },
{ raw: `${raw}#hash`, ...rest },
{ raw: `${raw}?a=1&b=2`, ...rest },
{ raw: `${raw}#hash?a=1&b=2`, ...rest },
{ raw: `${raw}?a=1&b=2#hash`, ...rest },
]),
)
})

Expand Down Expand Up @@ -570,7 +559,7 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
'<RouteLink to="/path/to/">readme1</RouteLink>',
'<RouteLink to="/path/foo/bar/">readme2</RouteLink>',
]
.map((item) => {
.flatMap((item) => {
const link = /to="([^"]*)"/.exec(item)![1]

return [
Expand All @@ -581,7 +570,6 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
item.replace(link, `${link}?a=1&amp;b=2#hash`),
]
})
.flat()
.map((a) => `<p>${a}</p>`)
.join('\n') + '\n',
)
Expand Down Expand Up @@ -648,15 +636,13 @@ describe('@vuepress/markdown > plugins > linksPlugin', () => {
relative: 'path/foo/bar/readme.md',
absolute: '/path/path/foo/bar/readme.md',
},
]
.map(({ raw, ...rest }) => [
{ raw, ...rest },
{ raw: `${raw}#hash`, ...rest },
{ raw: `${raw}?a=1&b=2`, ...rest },
{ raw: `${raw}#hash?a=1&b=2`, ...rest },
{ raw: `${raw}?a=1&b=2#hash`, ...rest },
])
.flat(),
].flatMap(({ raw, ...rest }) => [
{ raw, ...rest },
{ raw: `${raw}#hash`, ...rest },
{ raw: `${raw}?a=1&b=2`, ...rest },
{ raw: `${raw}#hash?a=1&b=2`, ...rest },
{ raw: `${raw}?a=1&b=2#hash`, ...rest },
]),
)
})
})
Expand Down

0 comments on commit a18e736

Please sign in to comment.