Skip to content

Commit

Permalink
feat(metadata): add method to access metadata (#2143)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmayer authored Jun 17, 2023
1 parent 19635a7 commit fd8cfe5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LocaleDefinition } from './definitions';
import type { LocaleDefinition, MetadataDefinition } from './definitions';
import { FakerError } from './errors/faker-error';
import { deprecated } from './internal/deprecated';
import type { Mersenne } from './internal/mersenne/mersenne';
Expand Down Expand Up @@ -458,6 +458,19 @@ export class Faker {
return seed;
}

/**
* Returns an object with metadata about the current locale.
*
* @example
* import { faker, fakerES_MX } from '@faker-js/faker';
* // const { faker, fakerES_MX } = require("@faker-js/faker")
* faker.getMetadata(); // { title: 'English', code: 'en', language: 'en', endonym: 'English', dir: 'ltr', script: 'Latn' }
* fakerES_MX.getMetadata(); // { title: 'Spanish (Mexico)', code: 'es_MX', language: 'es', endonym: 'Español (México)', dir: 'ltr', script: 'Latn', country: 'MX' }
*/
getMetadata(): MetadataDefinition {
return this.rawDefinitions.metadata ?? {};
}

// Pure JS backwards compatibility

/**
Expand Down
8 changes: 8 additions & 0 deletions test/faker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('faker', () => {
}
});

describe('getMetadata()', () => {
it('should return metadata for the locale', () => {
expect(faker.getMetadata()).toBeDefined();
expect(faker.getMetadata().title).toBeTypeOf('string');
// Not all properties are tested here, see locale-imports.spec.ts for full tests
});
});

describe('rawDefinitions', () => {
it('locale rawDefinition accessibility', () => {
// Metadata
Expand Down

0 comments on commit fd8cfe5

Please sign in to comment.