Skip to content

Commit

Permalink
feat(utils): 新增 getEmailUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jun 26, 2023
1 parent e7cb3d1 commit 6eccb18
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/utils/__snapshots__/getEmailUrl.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getEmailUrl ok 1`] = `"mailto:hello@gmail.com?cc=hello2%40gmail.com&bcc=hello3%40gmail.com&subject=hello&body=%E4%BD%A0%E5%A5%BD"`;

exports[`getEmailUrl ok 2`] = `"mailto:hello@gmail.com?cc=hello2%40gmail.com%2Chello22%40gmail.com&bcc=hello3%40gmail.com%2Chello333%40gmail.com&subject=hello&body=%E4%BD%A0%E5%A5%BD"`;
1 change: 1 addition & 0 deletions src/utils/createUrlQueryString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('createUrlQueryString', () => {
'2': 'xxx',
'0': '0 = & ?😁',
'😁🥳': 'hello',
'un': undefined,
}
const query = createUrlQueryString(parameters)
expect(query).toMatchSnapshot()
Expand Down
12 changes: 7 additions & 5 deletions src/utils/createUrlQueryString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ export function createUrlQueryString(
const partSeparator = options?.partSeparator ?? '&'
const parts: string[] = []
for (const key of Object.keys(parameters)) {
parts.push(
`${encodeURIComponent(key)}${pairSeparator}${encodeURIComponent(
parameters[key],
)}`,
)
if (parameters[key] != null) {
parts.push(
`${encodeURIComponent(key)}${pairSeparator}${encodeURIComponent(
parameters[key],
)}`,
)
}
}
return parts.join(partSeparator)
}
34 changes: 34 additions & 0 deletions src/utils/getEmailUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getEmailUrl } from './getEmailUrl'

describe('getEmailUrl', () => {
test('ok', () => {
expect(
getEmailUrl({
to: 'hello@gmail.com',
}),
).toBe('mailto:hello@gmail.com')
expect(
getEmailUrl({
to: ['hello@gmail.com', 'hello2@gmail.com'],
}),
).toBe('mailto:hello@gmail.com,hello2@gmail.com')
expect(
getEmailUrl({
to: 'hello@gmail.com',
cc: 'hello2@gmail.com',
bcc: 'hello3@gmail.com',
subject: 'hello',
body: '你好',
}),
).toMatchSnapshot()
expect(
getEmailUrl({
to: 'hello@gmail.com',
cc: ['hello2@gmail.com', 'hello22@gmail.com'],
bcc: ['hello3@gmail.com', 'hello333@gmail.com'],
subject: 'hello',
body: '你好',
}),
).toMatchSnapshot()
})
})
33 changes: 33 additions & 0 deletions src/utils/getEmailUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createUrlQueryString } from './createUrlQueryString'

export interface GetEmailUrlOptions {
/** 收件人 */
to?: string | string[]
/** 抄送人 */
cc?: string | string[]
/** 密送人 */
bcc?: string | string[]
/** 主题 */
subject?: string
/** 内容 */
body?: string
}

/**
* 获取邮件链接。
*
* @param options 选项
*/
export function getEmailUrl(options: GetEmailUrlOptions): string {
const { to, cc, bcc, subject, body } = options
let url = `mailto:${Array.isArray(to) ? to.join(',') : to}`
if (cc || bcc || subject || body) {
url += `?${createUrlQueryString({
cc: cc && (Array.isArray(cc) ? cc.join(',') : cc),
bcc: bcc && (Array.isArray(bcc) ? bcc.join(',') : bcc),
subject: subject,
body: body,
})}`
}
return url
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export * from './formatBytes'
export * from './formatNumber'
export * from './GeoCoord'
export * from './getCurrentScript'
export * from './getEmailUrl'
export * from './getEnvironment'
export * from './getSmsUrl'
export * from './getWechatPublicAccountQrcodeUrl'
Expand Down

0 comments on commit 6eccb18

Please sign in to comment.