- Definitions
- Bug Reports
- Feature Requests
- Coding Style
- Naming Convention
- Code of Conduct
- Compiled Assets
To avoid confusion when reading the docs, bellow there are some common terms and their definitions:
- Each
module
represents a collection of related methods to generate data Modules
can contain any amount of methods inside itModules
MUST only be a collection of its own functions, so no code should exists within it besides the expectedexport
/import
- An example of
module
can be found insrc/name/index.ts
// src/myModuleName/index.ts
import myMethod from './myMethod';
import anotherMethod from './anotherMethod';
export default { anotherMethod, myMethod };
- Each
method
represents a specific data generation function Methods
MUST always be functions to allow tree-shakingMethods
MUST always export a single function, but can export another supportinterfaces
,types
and etcMethods
MUST always use thegetLocale
helper in order to determine the localeMethods
CAN receive a valid locale string. You can usesrc/types/locale.ts {Locale}
to type this parameter
⚠️ The code bellow has comments to make it easier to understand, but may not work with all the comments
// src/name/firstName/index.ts
// This is the locale interface
import { Locale } from '../../types/locale';
// Helper responsible for determining which locale to use
import getLocale from '../../helpers/getLocale';
// Your module locale collection
import locales from './locales';
// Helper to get a random element from a given array
import randomArrayElement from '../../helpers/randomArrayElement';
/**
* Return a random first name.
* If a valid locale is given, it will the locale collection.
* If the given locale is not valid, it will fallback to Locale.EN
*
* @export
* @param {Locale} [selectedLocale]
* @returns {string}
*/
export default function firstName(selectedLocale?: Locale): string {
// Use `getLocale` to determine the right locale data collection
const collection = getLocale(locales, selectedLocale);
// Return a random element from collection
return randomArrayElement(collection);
}
- A
locale
is a simple array of all the possible results for a module method in a specific language - Methods will always fallback to
en
when the givenlocale
is not found. So for multi locale methods, you MUST always haveen
set - A
locale
MUST be placed inside a collection oflocales
. This way any method can determine whichlocale
it wants to use
// src/name/firstName/locales/en/collection.ts
const collection: string[] = [
'Aaliyah',
'Aaron',
'Abagail',
'Abbey',
'Abbie',
'Abbigail',
];
export default collection;
⚠️ The code bellow has comments to make it easier to understand, but may not work with all the comments
// src/name/firstName/locales/index.ts
import { LocaleObject } from '../../../types/locale';
// Import each given locale
import en from './en/collection';
import ja from './ja/collection';
import pt_BR from './pt_BR/collection';
// Put them together in an object
const locales: LocaleObject<string[]> = { en, ja, pt_BR };
// Export it
export default locales;
Helpers
are functions meant to be reused within other methods to avoid repetitive tasksHelpers
are supposed to do only one thing and thing only
⚠️ The code bellow has comments to make it easier to understand, but may not work with all the comments
// src/helpers/randomArrayElement.ts
// May import other helpers
import randomNumber from './randomNumber';
/**
* Return a random element from given array
*
* @export
* @template T
* @param {T[]} collection
* @returns {T}
*/
export default function randomArrayElement<T>(collection: T[]): T {
const min = 0;
const max = collection.length - 1;
const index = randomNumber(min, max);
return collection[index];
}
We strongly encourage pull requests, but please do not be afraid to send bug requests as well in the form of issues.
If you want a faster response to your problem, it's usually better to open a issue with a failing test. This way it's easier to explain what its not working
- Everything you code, must be covered by tested. Check the coverage to validate if all your code is tested
- Third party libraries DOES NOT have to be tested. If possible, just consider that they will work, otherwise mock them
❕ A good starting point is the
name
module insrc/name/*
To create a new module
one should:
- Create module main file in
src/myModuleName/index.ts
- Add
module
to be loaded insrc/index.ts
- Add methods to
module
Expected tests are:
src/__tests__/faker.test.ts
src/myModuleName/__tests__/myModuleName.test.ts
❕ A good starting point is the
name
module insrc/name/*
To create a new method
one should:
- Create method main file in
src/myModuleName/myMethodName/index.ts
- Add
method
to be loaded in module atsrc/myModuleName/index.ts
- Add locales to
method
Expected tests are:
src/myModuleName/__tests__/myModuleName.test.ts
src/myModuleName/myMethodName/__tests__/myMethodName.test.ts
❕ A good starting point is the
name
module insrc/name/*
To add locales to a method
one should:
- Create
locale
entry point insrc/myModuleName/myMethodName/locales/index.ts
- Create specific
locale
insrc/myModuleName/myMethodName/locales/myValidLocale/collection.ts
- Register specific
locale
in entry point atsrc/myModuleName/myMethodName/locales/index.ts
Expected tests are:
src/myModuleName/myMethodName/__tests__/myMethodName.test.ts
src/myModuleName/myMethodName/locales/__tests__/locales.test.ts
Faker ES6 has a special helper named parseRegex
which can generate data from a valid RegEx and also accepts replacements.
This allows for modules/methods to use other functions to create new meaninful data.
Here are a few examples:
// src/myModule/myMethod/index.ts
import parseRegex from '../../helpers/parseRegex';
// Renders: 23 81834-9138
const randomPhone = parseRegex(/\d{2} \d{5}-\d{4}/);
// src/myModule/myMethod/index.ts
import parseRegex from '../../helpers/parseRegex';
import firstName from '../../name/firstName';
const data = {
namePlaceholder: firstName(),
};
// Renders: Maria 18
const randomPhone = parseRegex(/:namePlaceholder \d{2}/);
❗ PR should be opened to solve issues. So before submitting your PR, check if there is a issue open about it already or open a new issue
This project uses git-cz to make standard commits, so a lot of useful information is expected to already exists in your commits.
Still, when opening a PR its nice to always make a few things clear:
- Which issue(s) it solves
- Do you have any doubt about a specific part of what you coded? Tell it in the PR so other people can help you out
- Make a small summary of what and how you solve the issue with your PR
To avoid further discussions about how many spaces to use or if it should be space
or tab
, recurring questions regarding this matter will be SET IN STONE bellow:
- Space or Tab? SPACE
- How many spaces? 4 SPACES
- Prefer double or single quote? SINGLE QUOTE
- Use or not template literals? USE WHEREVER IT MAKES SENSE
- Trailing commas for ES5? YES
Please, always focus on the quality of your code, not petty discussions.
Also, please ALWAYS use Prettier (there is a .prettierrc file in the root with general rules) to automatically format your code.
In order to maintain a consistent code base, there are a few rules to be followed when naming your files
Folders
MUST always be written in camelCase
.
Examples:
- MyModuleName - ⛔ BAD
- myModuleName - 🎉 GOOD
❕ A good starting point is the
name
module insrc/name/index.ts
Modules
MUST always be written incamelCase
Modules
MUST always have anindex.ts
fileindex.ts
MUST only work as a wrapper for the methods that it will export
Examples:
- src/myModuleName/myModule.ts - ⛔ BAD
- src/myModuleName/index.ts - 🎉 GOOD
❕ A good starting point is the
name
module insrc/name/firstName/index.ts
Methods
MUST always be written incamelCase
Methods
MUST always have anindex.ts
fileMethods
MUST always be written as function to allow tree shake
Examples:
- src/myModuleName/myMethod/myMethod.ts - ⛔ BAD
- src/myModuleName/myMethod/index.ts - 🎉 GOOD
❕ A good starting point is the
name
module insrc/name/firstName/locales/*
Locales
MUST always be written incamelCase
Locales
folders MUST be a valid locale name- Specific
locales
folders MUST always only have acollection.ts
file - Specific
locales
MUST always be a simple array
Examples:
- src/myModuleName/myMethod/locales/locales.ts - ⛔ BAD
- src/myModuleName/myMethod/locales/index.ts - 🎉 GOOD
- src/myModuleName/myMethod/locales/en.ts - ⛔ BAD
- src/myModuleName/myMethod/locales/en/collection.ts - 🎉 GOOD
❕ A good starting point is the
randomArrayElement
module insrc/helpers/randomArrayElement
Because helpers
are meant to be simple functions, they MUST not have a folder with their names inside helpers
, but MUST be a single file.
Helpers
MUST always be written incamelCase
Helpers
MUST always be a single file- If you need to split the
helper
function, consider creating anotherhelper
Examples:
- src/helpers/myHelper/index.ts - ⛔ BAD
- src/helpers/myHelper.ts - 🎉 GOOD
❕ A good starting point is the
name
module insrc/name/**/__tests__/*
Tests
MUST always be written incamelCase
Tests
MUST always be named after the module/method/file it testsTests
MUST always reside inside a__tests__
folder__tests__
folder MUST must always be in the same level of the file it testsTests
MUST always have a singledescribe
Tests
describe label ofmethods
MUST always be written asMy Module | myMethodName
Examples:
- src/myModuleName/myMethodName/myMethodName.test.ts - ⛔ BAD
- src/myModuleName/myMethodName/__tests__/myMethodName.test.ts - 🎉 GOOD
Any violation of the code of conduct, can be punished by permanent ban from the repository
- Participants will be tolerant of opposing views
- Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks
- When interpreting the words and actions of others, participants should always assume good intentions
- Behavior that can be reasonably considered harassment will not be tolerated
Build files MUST never be commited when contributing to this project.
Because compiled assets are too large and hard to read, it could be used to inject malicious code inside the lib.
Thus to prevent this sort of behaviour, only maintainers are able to build and deploy.