Skip to content

Commit

Permalink
Simplify logic further to avoid regex
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoppolo committed Sep 14, 2022
1 parent b46ee00 commit 9723013
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
25 changes: 5 additions & 20 deletions examples/user-settings/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ const users = [...Array(10)].map((_, i) => {
};
});

function sendVerificationEmail(user) {
// replace with a call to your email service
ctx.log('Sending verification email to', user.email);
}

function resetUserPassword(user) {
// replace with database update and a call to send a password reset email
ctx.log('Resetting password for', user.email);
Expand Down Expand Up @@ -66,25 +61,16 @@ const interval = new Interval({
}),
]);

const messages = [];
const helpTexts = [];
const messages = ['update the user'];
let helpText;
if (resetPassword) {
messages.push('reset their password');
helpTexts.push('log the user out all current sessions');
}
if (user.email !== email) {
messages.push('change their email');
helpTexts.push('send an email to verifiy the new email address');
helpText = 'This will log the user out all current sessions.';
}
const confirmed = await io.confirm(
`Are you sure you want to ${messages
.join(', ')
.replace(/,(?!.*,)/gim, messages.length > 2 ? ', and' : ' and')}?`,
`Are you sure you want to ${messages.join(', ')}?`,
{
helpText:
helpTexts.length > 0
? `This will ${helpTexts.join(' and ')}.`
: undefined,
helpText,
}
);

Expand All @@ -101,7 +87,6 @@ const interval = new Interval({
users[users.indexOf(user)] = updatedUser;

if (resetPassword) resetUserPassword(updatedUser);
if (user.email !== email) sendVerificationEmail(updatedUser);

return updatedUser;
},
Expand Down
23 changes: 4 additions & 19 deletions examples/user-settings/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ const users = [...Array(10)].map((_, i) => {
};
});

function sendVerificationEmail(user) {
// replace with a call to your email service
ctx.log('Sending verification email to', user.email);
}

function resetUserPassword(user) {
// replace with database update and a call to send a password reset email
ctx.log('Resetting password for', user.email);
Expand Down Expand Up @@ -67,24 +62,15 @@ const interval = new Interval({
]);

const messages = ['update the user'];
const helpTexts = [];
let helpText;
if (resetPassword) {
messages.push('reset their password');
helpTexts.push('log the user out all current sessions');
}
if (user.email !== email) {
messages.push('change their email');
helpTexts.push('send an email to verifiy the new email address');
helpText = 'This will log the user out all current sessions';
}
const confirmed = await io.confirm(
`Are you sure you want to ${messages
.join(', ')
.replace(/,(?!.*,)/gim, messages.length > 2 ? ', and' : ' and')}?`,
`Are you sure you want to ${messages.join(' and ')}?`,
{
helpText:
helpTexts.length > 0
? `This will ${helpTexts.join(' and ')}.`
: undefined,
helpText,
}
);

Expand All @@ -101,7 +87,6 @@ const interval = new Interval({
users[users.indexOf(user)] = updatedUser;

if (resetPassword) resetUserPassword(updatedUser);
if (user.email !== email) sendVerificationEmail(updatedUser);

return updatedUser;
},
Expand Down

0 comments on commit 9723013

Please sign in to comment.