diff options
| author | Eric Cheng <[email protected]> | 2022-06-05 06:22:40 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-06-05 10:22:40 +0000 |
| commit | d75d07970b44bde066de0a765c169809ee8f6b74 (patch) | |
| tree | b631897f390cce5cfdca8a4d0dd82638a3a036b2 /src/modules | |
| parent | c401f1fc4b7ef9e73656bb08fc6aea26eff4eaa3 (diff) | |
| download | faker-d75d07970b44bde066de0a765c169809ee8f6b74.tar.xz faker-d75d07970b44bde066de0a765c169809ee8f6b74.zip | |
feat: science module (#1014)
Co-authored-by: ST-DDT <[email protected]>
Co-authored-by: Shinigami <[email protected]>
Co-authored-by: pkuczynski <[email protected]>
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/science/index.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/modules/science/index.ts b/src/modules/science/index.ts new file mode 100644 index 00000000..1f5e042a --- /dev/null +++ b/src/modules/science/index.ts @@ -0,0 +1,71 @@ +import type { Faker } from '../..'; + +/** + * The possible definitions related to elements. + */ +export interface ChemicalElement { + /** + * The symbol for the element (e.g. `'He'`). + */ + symbol: string; + /** + * The name for the element (e.g. `'Cerium'`). + */ + name: string; + /** + * The atomic number for the element (e.g. `52`). + */ + atomicNumber: number; +} + +export interface Unit { + /** + * The long version of the unit (e.g. `meter`). + */ + name: string; + /** + * The short version/abbreviation of the element (e.g. `Pa`). + */ + symbol: string; +} + +/** + * Module to generate science related entries. + */ +export class Science { + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Science.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + } + + /** + * Returns a random periodic table element. + * + * @example + * faker.science.chemicalElement() // { symbol: 'H', name: 'Hydrogen', atomicNumber: 1 } + * faker.science.chemicalElement() // { symbol: 'Xe', name: 'Xenon', atomicNumber: 54 } + * faker.science.chemicalElement() // { symbol: 'Ce', name: 'Cerium', atomicNumber: 58 } + */ + chemicalElement(): ChemicalElement { + return this.faker.helpers.arrayElement( + this.faker.definitions.science.chemicalElement + ); + } + + /** + * Returns a random scientific unit. + * + * @example + * faker.science.unit() // { name: 'meter', symbol: 'm' } + * faker.science.unit() // { name: 'second', symbol: 's' } + * faker.science.unit() // { name: 'mole', symbol: 'mol' } + */ + unit(): Unit { + return this.faker.helpers.arrayElement(this.faker.definitions.science.unit); + } +} |
