Skip to content

Commit

Permalink
Merge branch 'main' into docs/nice-string-literals
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Apr 8, 2022
2 parents 7b0dc44 + 3b5a21f commit 28eeb06
Show file tree
Hide file tree
Showing 9 changed files with 2,258 additions and 197 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"build": "run-s build:clean build:code build:types",
"generate:api-docs": "esno ./scripts/apidoc.ts",
"generate:locales": "esno ./scripts/generateLocales.ts",
"copy:mime-types": "esno ./scripts/copyMimeTypes.ts",
"docs:build": "run-s docs:prepare docs:build:run",
"docs:build:run": "vitepress build docs",
"docs:build:ci": "run-s build docs:build",
Expand Down Expand Up @@ -108,6 +109,7 @@
"eslint-plugin-prettier": "~4.0.0",
"esno": "~0.14.1",
"lint-staged": "~12.3.7",
"mime-db": "~1.52.0",
"npm-run-all": "~4.1.5",
"picocolors": "~1.0.0",
"prettier": "2.6.2",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

39 changes: 39 additions & 0 deletions scripts/copyMimeTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'node:fs';
import path from 'node:path';
import type { Options } from 'prettier';
import { format } from 'prettier';
import options from '../.prettierrc.cjs';

const rootPath = path.resolve(__dirname, '..');
const mimeDbPath = path.resolve(rootPath, 'node_modules/mime-db/db.json');
const mimeDbLicencePath = path.resolve(
rootPath,
'node_modules/mime-db/LICENSE'
);
const mimeTypesTsPath = path.resolve(
rootPath,
'src/locales/en/system/mimeTypes.ts'
);
const prettierTsOptions: Options = { ...options, parser: 'typescript' };
fs.readFile(mimeDbPath, 'utf8', (err, data) => {
if (err) {
throw err;
}

const licence = fs.readFileSync(mimeDbLicencePath, { encoding: 'utf8' });
const mimeTypeFileContent = `// This file is generated by scripts/copyMimeTypes.ts\n// Do not edit this file directly. Instead, update mime-db and run \`pnpm run copy:mime-types\`\n\n/*\n${
licence as string
}*/\n\nexport default ${data as string};\n`;

fs.writeFile(
mimeTypesTsPath,
format(mimeTypeFileContent, prettierTsOptions),
(err) => {
if (err) {
throw err;
}

console.log(`Mime types copied to ${mimeTypesTsPath as string}`);
}
);
});
40 changes: 31 additions & 9 deletions src/internet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,64 @@ export class Internet {
* @param firstName The optional first name to use. If not specified, a random one will be chosen.
* @param lastName The optional last name to use. If not specified, a random one will be chosen.
* @param provider The mail provider domain to use. If not specified, a random free mail provider will be chosen.
* @param options The options to use. Defaults to `{ allowSpecialCharacters: false }`.
* @param options.allowSpecialCharacters Whether special characters such as `.!#$%&'*+-/=?^_`{|}~` should be included
* in the email address. Defaults to `false`.
*
* @example
* faker.internet.email() // 'Kassandra4@hotmail.com'
* faker.internet.email('Jeanne', 'Doe') // 'Jeanne63@yahoo.com'
* faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev') // 'Jeanne_Doe88@example.fakerjs.dev'
* faker.internet.email('Jeanne', 'Doe', 'example.fakerjs.dev', { allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.fakerjs.dev'
*/
email(firstName?: string, lastName?: string, provider?: string): string {
email(
firstName?: string,
lastName?: string,
provider?: string,
options?: { allowSpecialCharacters?: boolean }
): string {
provider =
provider ||
this.faker.random.arrayElement(
this.faker.definitions.internet.free_email
);
return (
this.faker.helpers.slugify(
this.faker.internet.userName(firstName, lastName)
) +
'@' +
provider
let localPart: string = this.faker.helpers.slugify(
this.faker.internet.userName(firstName, lastName)
);
if (options?.allowSpecialCharacters) {
const usernameChars: string[] = '._-'.split('');
const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split('');
localPart = localPart.replace(
this.faker.random.arrayElement(usernameChars),
this.faker.random.arrayElement(specialChars)
);
}
return `${localPart}@${provider}`;
}

/**
* Generates an email address using an example mail provider using the given person's name as base.
*
* @param firstName The optional first name to use. If not specified, a random one will be chosen.
* @param lastName The optional last name to use. If not specified, a random one will be chosen.
* @param options The options to use. Defaults to `{ allowSpecialCharacters: false }`.
* @param options.allowSpecialCharacters Whether special characters such as `.!#$%&'*+-/=?^_`{|}~` should be included
* in the email address. Defaults to `false`.
*
* @example
* faker.internet.exampleEmail() // 'Helmer.Graham23@example.com'
* faker.internet.exampleEmail('Jeanne', 'Doe') // 'Jeanne96@example.net'
* faker.internet.exampleEmail('Jeanne', 'Doe', { allowSpecialCharacters: true }) // 'Jeanne%Doe88@example.com'
*/
exampleEmail(firstName?: string, lastName?: string): string {
exampleEmail(
firstName?: string,
lastName?: string,
options?: { allowSpecialCharacters?: boolean }
): string {
const provider = this.faker.random.arrayElement(
this.faker.definitions.internet.example_email
);
return this.email(firstName, lastName, provider);
return this.email(firstName, lastName, provider, options);
}

/**
Expand Down

0 comments on commit 28eeb06

Please sign in to comment.