From 3fc92310492c4215b2cbe7f806f79c3d632e5650 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 13:28:59 -0600 Subject: [PATCH 01/72] feat: add seperate color module and definition --- src/color.ts | 23 +++++++++++++++++++ src/commerce.ts | 12 ---------- src/definitions/color.ts | 16 +++++++++++++ src/definitions/commerce.ts | 5 ---- src/definitions/index.ts | 1 + src/faker.ts | 2 ++ src/index.ts | 1 + .../en/{commerce/color.ts => color/human.ts} | 0 src/locales/en/color/index.ts | 12 ++++++++++ src/locales/en/commerce/index.ts | 2 -- src/locales/en/index.ts | 2 ++ src/random.ts | 2 +- src/vehicle.ts | 2 +- test/commerce.spec.ts | 11 --------- 14 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 src/color.ts create mode 100644 src/definitions/color.ts rename src/locales/en/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/en/color/index.ts diff --git a/src/color.ts b/src/color.ts new file mode 100644 index 00000000000..6d81cc8d41e --- /dev/null +++ b/src/color.ts @@ -0,0 +1,23 @@ +import type { Faker } from '.'; + +export class Color { + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Color.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + } + + /** + * Returns a human readable color name. + * + * @example + * faker.color.human() // 'red' + */ + human(): string { + return this.faker.random.arrayElement(this.faker.definitions.color.human); + } +} diff --git a/src/commerce.ts b/src/commerce.ts index 79c485b2333..c24ff328836 100644 --- a/src/commerce.ts +++ b/src/commerce.ts @@ -14,18 +14,6 @@ export class Commerce { } } - /** - * Returns a human readable color name. - * - * @example - * faker.commerce.color() // 'red' - */ - color(): string { - return this.faker.random.arrayElement( - this.faker.definitions.commerce.color - ); - } - /** * Returns a department inside a shop. * diff --git a/src/definitions/color.ts b/src/definitions/color.ts new file mode 100644 index 00000000000..ad56d21dab8 --- /dev/null +++ b/src/definitions/color.ts @@ -0,0 +1,16 @@ +import { allOf } from './utils'; + +/** + * The possible definitions related to color. + */ +export interface ColorDefinitions { + /** + * Human readable color names + */ + human: string[]; +} + +/** + * Internal: A list of all keys for the ColorDefinitions. + */ +export const COLOR = allOf()('human'); diff --git a/src/definitions/commerce.ts b/src/definitions/commerce.ts index dbe22f69949..86f04795653 100644 --- a/src/definitions/commerce.ts +++ b/src/definitions/commerce.ts @@ -4,10 +4,6 @@ import { allOf } from './utils'; * The possible definitions related to commerce. */ export interface CommerceDefinitions { - /** - * Human readable color names - */ - color: string[]; /** * Department names inside a shop. */ @@ -44,7 +40,6 @@ export interface CommerceProductNameDefinitions { * Internal: A list of all keys for the CommerceDefinitions. */ export const COMMERCE = allOf()( - 'color', 'department', 'product_name', 'product_description' diff --git a/src/definitions/index.ts b/src/definitions/index.ts index 9eb4c3f3ea1..b52f8e982fc 100644 --- a/src/definitions/index.ts +++ b/src/definitions/index.ts @@ -1,5 +1,6 @@ export type { AddressDefinitions } from './address'; export type { AnimalDefinitions } from './animal'; +export type { ColorDefinitions } from './color'; export type { CommerceDefinitions, CommerceProductNameDefinitions, diff --git a/src/faker.ts b/src/faker.ts index 7b74d58c9ac..7a5f0ab73e7 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -1,5 +1,6 @@ import { Address } from './address'; import { Animal } from './animal'; +import { Color } from './color'; import { Commerce } from './commerce'; import { Company } from './company'; import { Database } from './database'; @@ -64,6 +65,7 @@ export class Faker { readonly address: Address = new Address(this); readonly animal: Animal = new Animal(this); + readonly color: Color = new Color(this); readonly commerce: Commerce = new Commerce(this); readonly company: Company = new Company(this); readonly database: Database = new Database(this); diff --git a/src/index.ts b/src/index.ts index 984833f5625..5b697906439 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import allLocales from './locales'; export type { AddressDefinitions, AnimalDefinitions, + ColorDefinitions, CommerceDefinitions, CommerceProductNameDefinitions, CompanyDefinitions, diff --git a/src/locales/en/commerce/color.ts b/src/locales/en/color/human.ts similarity index 100% rename from src/locales/en/commerce/color.ts rename to src/locales/en/color/human.ts diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts new file mode 100644 index 00000000000..69a08fcf09f --- /dev/null +++ b/src/locales/en/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../../definitions'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/en/commerce/index.ts b/src/locales/en/commerce/index.ts index 2f3f890d61b..dab2aa6966f 100644 --- a/src/locales/en/commerce/index.ts +++ b/src/locales/en/commerce/index.ts @@ -3,13 +3,11 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_description from './product_description'; import product_name from './product_name'; const commerce: CommerceDefinitions = { - color, department, product_description, product_name, diff --git a/src/locales/en/index.ts b/src/locales/en/index.ts index ded35323986..75bd0437199 100644 --- a/src/locales/en/index.ts +++ b/src/locales/en/index.ts @@ -8,6 +8,7 @@ import animal from './animal'; import app from './app'; import business from './business'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import company from './company'; import database from './database'; @@ -32,6 +33,7 @@ const en: LocaleDefinition = { app, business, cell_phone, + color, commerce, company, database, diff --git a/src/random.ts b/src/random.ts index 448cf4f8696..5b9d2fd7f1a 100644 --- a/src/random.ts +++ b/src/random.ts @@ -267,7 +267,7 @@ export class Random { this.faker.commerce.productAdjective, this.faker.commerce.productMaterial, this.faker.commerce.product, - this.faker.commerce.color, + this.faker.color.human, this.faker.company.catchPhraseAdjective, this.faker.company.catchPhraseDescriptor, diff --git a/src/vehicle.ts b/src/vehicle.ts index 979631bdddc..c8c1b5eb4ea 100644 --- a/src/vehicle.ts +++ b/src/vehicle.ts @@ -93,7 +93,7 @@ export class Vehicle { * faker.vehicle.color() // 'red' */ color(): string { - return this.faker.commerce.color(); + return this.faker.color.human(); } /** diff --git a/test/commerce.spec.ts b/test/commerce.spec.ts index 64c7d7b324d..b1cfd72df2d 100644 --- a/test/commerce.spec.ts +++ b/test/commerce.spec.ts @@ -5,7 +5,6 @@ const seededRuns = [ { seed: 42, expectations: { - color: 'grey', department: 'Tools', productName: 'Fantastic Soft Sausages', price: '375.00', @@ -19,7 +18,6 @@ const seededRuns = [ { seed: 1337, expectations: { - color: 'black', department: 'Computers', productName: 'Incredible Granite Keyboard', price: '263.00', @@ -33,7 +31,6 @@ const seededRuns = [ { seed: 1211, expectations: { - color: 'azure', department: 'Automotive', productName: 'Unbranded Cotton Salad', price: '929.00', @@ -49,7 +46,6 @@ const seededRuns = [ const NON_SEEDED_BASED_RUN = 5; const functionNames = [ - 'color', 'department', 'productName', 'price', @@ -84,13 +80,6 @@ describe('commerce', () => { faker.seedValue )}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { - describe(`color()`, () => { - it('should return random value from color array', () => { - const actual = faker.commerce.color(); - expect(faker.definitions.commerce.color).toContain(actual); - }); - }); - describe(`department()`, () => { it('should return random value from department array', () => { const department = faker.commerce.department(); From 4f21dee5d46dea8b433dbefd8386bc969d477ab7 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 13:35:20 -0600 Subject: [PATCH 02/72] feat: add color definitions to LocalDefinition --- src/definitions/definitions.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/definitions/definitions.ts b/src/definitions/definitions.ts index d59f84fc859..fe0ffbae018 100644 --- a/src/definitions/definitions.ts +++ b/src/definitions/definitions.ts @@ -2,6 +2,8 @@ import type { AddressDefinitions } from './address'; import { ADDRESS } from './address'; import type { AnimalDefinitions } from './animal'; import { ANIMAL } from './animal'; +import type { ColorDefinitions } from './color'; +import { COLOR } from './color'; import type { CommerceDefinitions } from './commerce'; import { COMMERCE } from './commerce'; import type { CompanyDefinitions } from './company'; @@ -37,6 +39,7 @@ import { WORD } from './word'; interface Definitions { address: AddressDefinitions; animal: AnimalDefinitions; + color: ColorDefinitions; commerce: CommerceDefinitions; company: CompanyDefinitions; database: DatabaseDefinitions; @@ -94,6 +97,7 @@ export const DEFINITIONS: DefinitionTypes = { address: ADDRESS, animal: ANIMAL, + color: COLOR, company: COMPANY, commerce: COMMERCE, database: DATABASE, From 0dbb047320d4fc42d36b4b822e683c1dfe6af270 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 13:43:39 -0600 Subject: [PATCH 03/72] chore: move color definitions for all locales --- src/locales/ar/{commerce/color.ts => color/human.ts} | 0 src/locales/ar/color/index.ts | 12 ++++++++++++ src/locales/ar/commerce/index.ts | 6 ++---- src/locales/az/{commerce/color.ts => color/human.ts} | 0 src/locales/az/color/index.ts | 12 ++++++++++++ src/locales/az/commerce/index.ts | 6 ++---- src/locales/el/{commerce/color.ts => color/human.ts} | 0 src/locales/el/color/index.ts | 12 ++++++++++++ src/locales/el/commerce/index.ts | 6 ++---- src/locales/en/color/index.ts | 2 +- src/locales/es/{commerce/color.ts => color/human.ts} | 0 src/locales/es/color/index.ts | 12 ++++++++++++ src/locales/es/commerce/index.ts | 6 ++---- .../es_MX/{commerce/color.ts => color/human.ts} | 0 src/locales/es_MX/color/index.ts | 12 ++++++++++++ src/locales/es_MX/commerce/index.ts | 6 ++---- src/locales/fa/{commerce/color.ts => color/human.ts} | 0 src/locales/fa/color/index.ts | 12 ++++++++++++ src/locales/fa/commerce/index.ts | 6 ++---- src/locales/he/{commerce/color.ts => color/human.ts} | 0 src/locales/he/color/index.ts | 12 ++++++++++++ src/locales/he/commerce/index.ts | 6 ++---- src/locales/hy/{commerce/color.ts => color/human.ts} | 0 src/locales/hy/color/index.ts | 12 ++++++++++++ src/locales/hy/commerce/index.ts | 12 ------------ src/locales/lv/{commerce/color.ts => color/human.ts} | 0 src/locales/lv/color/index.ts | 12 ++++++++++++ src/locales/lv/commerce/index.ts | 6 ++---- src/locales/nl/commerce/index.ts | 4 ++-- src/locales/pt_BR/commerce/index.ts | 4 ++-- src/locales/pt_PT/commerce/index.ts | 4 ++-- src/locales/ro/address/index.ts | 2 +- src/locales/ru/commerce/index.ts | 4 ++-- src/locales/sv/commerce/index.ts | 4 ++-- src/locales/tr/commerce/index.ts | 4 ++-- src/locales/ur/commerce/index.ts | 4 ++-- 36 files changed, 140 insertions(+), 60 deletions(-) rename src/locales/ar/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/ar/color/index.ts rename src/locales/az/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/az/color/index.ts rename src/locales/el/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/el/color/index.ts rename src/locales/es/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/es/color/index.ts rename src/locales/es_MX/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/es_MX/color/index.ts rename src/locales/fa/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/fa/color/index.ts rename src/locales/he/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/he/color/index.ts rename src/locales/hy/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/hy/color/index.ts delete mode 100644 src/locales/hy/commerce/index.ts rename src/locales/lv/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/lv/color/index.ts diff --git a/src/locales/ar/commerce/color.ts b/src/locales/ar/color/human.ts similarity index 100% rename from src/locales/ar/commerce/color.ts rename to src/locales/ar/color/human.ts diff --git a/src/locales/ar/color/index.ts b/src/locales/ar/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/ar/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/ar/commerce/index.ts b/src/locales/ar/commerce/index.ts index 621c60571bb..361ba7eb115 100644 --- a/src/locales/ar/commerce/index.ts +++ b/src/locales/ar/commerce/index.ts @@ -3,14 +3,12 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { - color, +const commerce = { department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/az/commerce/color.ts b/src/locales/az/color/human.ts similarity index 100% rename from src/locales/az/commerce/color.ts rename to src/locales/az/color/human.ts diff --git a/src/locales/az/color/index.ts b/src/locales/az/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/az/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/az/commerce/index.ts b/src/locales/az/commerce/index.ts index 621c60571bb..361ba7eb115 100644 --- a/src/locales/az/commerce/index.ts +++ b/src/locales/az/commerce/index.ts @@ -3,14 +3,12 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { - color, +const commerce = { department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/el/commerce/color.ts b/src/locales/el/color/human.ts similarity index 100% rename from src/locales/el/commerce/color.ts rename to src/locales/el/color/human.ts diff --git a/src/locales/el/color/index.ts b/src/locales/el/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/el/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/el/commerce/index.ts b/src/locales/el/commerce/index.ts index 621c60571bb..361ba7eb115 100644 --- a/src/locales/el/commerce/index.ts +++ b/src/locales/el/commerce/index.ts @@ -3,14 +3,12 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { - color, +const commerce = { department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index 69a08fcf09f..79cc39f02f3 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -2,7 +2,7 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../../definitions'; +import type { ColorDefinitions } from '../../..'; import human from './human'; const color: ColorDefinitions = { diff --git a/src/locales/es/commerce/color.ts b/src/locales/es/color/human.ts similarity index 100% rename from src/locales/es/commerce/color.ts rename to src/locales/es/color/human.ts diff --git a/src/locales/es/color/index.ts b/src/locales/es/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/es/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/es/commerce/index.ts b/src/locales/es/commerce/index.ts index 621c60571bb..361ba7eb115 100644 --- a/src/locales/es/commerce/index.ts +++ b/src/locales/es/commerce/index.ts @@ -3,14 +3,12 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { - color, +const commerce = { department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/es_MX/commerce/color.ts b/src/locales/es_MX/color/human.ts similarity index 100% rename from src/locales/es_MX/commerce/color.ts rename to src/locales/es_MX/color/human.ts diff --git a/src/locales/es_MX/color/index.ts b/src/locales/es_MX/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/es_MX/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/es_MX/commerce/index.ts b/src/locales/es_MX/commerce/index.ts index 621c60571bb..361ba7eb115 100644 --- a/src/locales/es_MX/commerce/index.ts +++ b/src/locales/es_MX/commerce/index.ts @@ -3,14 +3,12 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { - color, +const commerce = { department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/fa/commerce/color.ts b/src/locales/fa/color/human.ts similarity index 100% rename from src/locales/fa/commerce/color.ts rename to src/locales/fa/color/human.ts diff --git a/src/locales/fa/color/index.ts b/src/locales/fa/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/fa/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/fa/commerce/index.ts b/src/locales/fa/commerce/index.ts index 621c60571bb..361ba7eb115 100644 --- a/src/locales/fa/commerce/index.ts +++ b/src/locales/fa/commerce/index.ts @@ -3,14 +3,12 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { - color, +const commerce = { department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/he/commerce/color.ts b/src/locales/he/color/human.ts similarity index 100% rename from src/locales/he/commerce/color.ts rename to src/locales/he/color/human.ts diff --git a/src/locales/he/color/index.ts b/src/locales/he/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/he/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/he/commerce/index.ts b/src/locales/he/commerce/index.ts index 2f3f890d61b..a643dfa67c0 100644 --- a/src/locales/he/commerce/index.ts +++ b/src/locales/he/commerce/index.ts @@ -3,16 +3,14 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_description from './product_description'; import product_name from './product_name'; -const commerce: CommerceDefinitions = { - color, +const commerce = { department, product_description, product_name, -}; +} as CommerceDefinitions; export default commerce; diff --git a/src/locales/hy/commerce/color.ts b/src/locales/hy/color/human.ts similarity index 100% rename from src/locales/hy/commerce/color.ts rename to src/locales/hy/color/human.ts diff --git a/src/locales/hy/color/index.ts b/src/locales/hy/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/hy/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/hy/commerce/index.ts b/src/locales/hy/commerce/index.ts deleted file mode 100644 index 8ad08ef73db..00000000000 --- a/src/locales/hy/commerce/index.ts +++ /dev/null @@ -1,12 +0,0 @@ -/* - * This file is automatically generated. - * Run 'pnpm run generate:locales' to update. - */ -import type { CommerceDefinitions } from '../../..'; -import color from './color'; - -const commerce: Partial = { - color, -}; - -export default commerce; diff --git a/src/locales/lv/commerce/color.ts b/src/locales/lv/color/human.ts similarity index 100% rename from src/locales/lv/commerce/color.ts rename to src/locales/lv/color/human.ts diff --git a/src/locales/lv/color/index.ts b/src/locales/lv/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/lv/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/lv/commerce/index.ts b/src/locales/lv/commerce/index.ts index 621c60571bb..361ba7eb115 100644 --- a/src/locales/lv/commerce/index.ts +++ b/src/locales/lv/commerce/index.ts @@ -3,14 +3,12 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { - color, +const commerce = { department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/nl/commerce/index.ts b/src/locales/nl/commerce/index.ts index 621c60571bb..39d00182c8b 100644 --- a/src/locales/nl/commerce/index.ts +++ b/src/locales/nl/commerce/index.ts @@ -7,10 +7,10 @@ import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce = { color, department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/pt_BR/commerce/index.ts b/src/locales/pt_BR/commerce/index.ts index 621c60571bb..39d00182c8b 100644 --- a/src/locales/pt_BR/commerce/index.ts +++ b/src/locales/pt_BR/commerce/index.ts @@ -7,10 +7,10 @@ import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce = { color, department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/pt_PT/commerce/index.ts b/src/locales/pt_PT/commerce/index.ts index 621c60571bb..39d00182c8b 100644 --- a/src/locales/pt_PT/commerce/index.ts +++ b/src/locales/pt_PT/commerce/index.ts @@ -7,10 +7,10 @@ import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce = { color, department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/ro/address/index.ts b/src/locales/ro/address/index.ts index 9b4199f6e12..015977a22b5 100644 --- a/src/locales/ro/address/index.ts +++ b/src/locales/ro/address/index.ts @@ -25,10 +25,10 @@ const address = { secondary_address, state, state_abbr, - streets, street_address, street_name, street_suffix, + streets, } as Partial; export default address; diff --git a/src/locales/ru/commerce/index.ts b/src/locales/ru/commerce/index.ts index 621c60571bb..39d00182c8b 100644 --- a/src/locales/ru/commerce/index.ts +++ b/src/locales/ru/commerce/index.ts @@ -7,10 +7,10 @@ import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce = { color, department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/sv/commerce/index.ts b/src/locales/sv/commerce/index.ts index 621c60571bb..39d00182c8b 100644 --- a/src/locales/sv/commerce/index.ts +++ b/src/locales/sv/commerce/index.ts @@ -7,10 +7,10 @@ import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce = { color, department, product_name, -}; +} as Partial; export default commerce; diff --git a/src/locales/tr/commerce/index.ts b/src/locales/tr/commerce/index.ts index 2f3f890d61b..632e6fcc48e 100644 --- a/src/locales/tr/commerce/index.ts +++ b/src/locales/tr/commerce/index.ts @@ -8,11 +8,11 @@ import department from './department'; import product_description from './product_description'; import product_name from './product_name'; -const commerce: CommerceDefinitions = { +const commerce = { color, department, product_description, product_name, -}; +} as CommerceDefinitions; export default commerce; diff --git a/src/locales/ur/commerce/index.ts b/src/locales/ur/commerce/index.ts index 621c60571bb..39d00182c8b 100644 --- a/src/locales/ur/commerce/index.ts +++ b/src/locales/ur/commerce/index.ts @@ -7,10 +7,10 @@ import color from './color'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce = { color, department, product_name, -}; +} as Partial; export default commerce; From 7b979b3b3bcb1cd94297ba141381ab298f954b77 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 13:51:36 -0600 Subject: [PATCH 04/72] feat: update all locales color files --- src/locales/hy/index.ts | 4 ++-- src/locales/nl/{commerce/color.ts => color/human.ts} | 0 src/locales/nl/color/index.ts | 12 ++++++++++++ src/locales/nl/commerce/index.ts | 2 -- .../pt_BR/{commerce/color.ts => color/human.ts} | 0 src/locales/pt_BR/color/index.ts | 12 ++++++++++++ src/locales/pt_BR/commerce/index.ts | 2 -- .../pt_PT/{commerce/color.ts => color/human.ts} | 0 src/locales/pt_PT/color/index.ts | 12 ++++++++++++ src/locales/pt_PT/commerce/index.ts | 2 -- src/locales/ru/{commerce/color.ts => color/human.ts} | 0 src/locales/ru/color/index.ts | 12 ++++++++++++ src/locales/ru/commerce/index.ts | 2 -- src/locales/sv/{commerce/color.ts => color/human.ts} | 0 src/locales/sv/color/index.ts | 12 ++++++++++++ src/locales/sv/commerce/index.ts | 2 -- src/locales/tr/{commerce/color.ts => color/human.ts} | 0 src/locales/tr/color/index.ts | 12 ++++++++++++ src/locales/tr/commerce/index.ts | 2 -- src/locales/ur/{commerce/color.ts => color/human.ts} | 0 src/locales/ur/color/index.ts | 12 ++++++++++++ src/locales/ur/commerce/index.ts | 2 -- 22 files changed, 86 insertions(+), 16 deletions(-) rename src/locales/nl/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/nl/color/index.ts rename src/locales/pt_BR/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/pt_BR/color/index.ts rename src/locales/pt_PT/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/pt_PT/color/index.ts rename src/locales/ru/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/ru/color/index.ts rename src/locales/sv/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/sv/color/index.ts rename src/locales/tr/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/tr/color/index.ts rename src/locales/ur/{commerce/color.ts => color/human.ts} (100%) create mode 100644 src/locales/ur/color/index.ts diff --git a/src/locales/hy/index.ts b/src/locales/hy/index.ts index 2f9da62ced9..1a1fb639a50 100644 --- a/src/locales/hy/index.ts +++ b/src/locales/hy/index.ts @@ -4,7 +4,7 @@ */ import type { LocaleDefinition } from '../..'; import address from './address'; -import commerce from './commerce'; +import color from './color'; import date from './date'; import internet from './internet'; import lorem from './lorem'; @@ -15,7 +15,7 @@ const hy: LocaleDefinition = { title: 'Armenian', separator: ' և ', address, - commerce, + color, date, internet, lorem, diff --git a/src/locales/nl/commerce/color.ts b/src/locales/nl/color/human.ts similarity index 100% rename from src/locales/nl/commerce/color.ts rename to src/locales/nl/color/human.ts diff --git a/src/locales/nl/color/index.ts b/src/locales/nl/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/nl/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/nl/commerce/index.ts b/src/locales/nl/commerce/index.ts index 39d00182c8b..361ba7eb115 100644 --- a/src/locales/nl/commerce/index.ts +++ b/src/locales/nl/commerce/index.ts @@ -3,12 +3,10 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; const commerce = { - color, department, product_name, } as Partial; diff --git a/src/locales/pt_BR/commerce/color.ts b/src/locales/pt_BR/color/human.ts similarity index 100% rename from src/locales/pt_BR/commerce/color.ts rename to src/locales/pt_BR/color/human.ts diff --git a/src/locales/pt_BR/color/index.ts b/src/locales/pt_BR/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/pt_BR/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/pt_BR/commerce/index.ts b/src/locales/pt_BR/commerce/index.ts index 39d00182c8b..361ba7eb115 100644 --- a/src/locales/pt_BR/commerce/index.ts +++ b/src/locales/pt_BR/commerce/index.ts @@ -3,12 +3,10 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; const commerce = { - color, department, product_name, } as Partial; diff --git a/src/locales/pt_PT/commerce/color.ts b/src/locales/pt_PT/color/human.ts similarity index 100% rename from src/locales/pt_PT/commerce/color.ts rename to src/locales/pt_PT/color/human.ts diff --git a/src/locales/pt_PT/color/index.ts b/src/locales/pt_PT/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/pt_PT/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/pt_PT/commerce/index.ts b/src/locales/pt_PT/commerce/index.ts index 39d00182c8b..361ba7eb115 100644 --- a/src/locales/pt_PT/commerce/index.ts +++ b/src/locales/pt_PT/commerce/index.ts @@ -3,12 +3,10 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; const commerce = { - color, department, product_name, } as Partial; diff --git a/src/locales/ru/commerce/color.ts b/src/locales/ru/color/human.ts similarity index 100% rename from src/locales/ru/commerce/color.ts rename to src/locales/ru/color/human.ts diff --git a/src/locales/ru/color/index.ts b/src/locales/ru/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/ru/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/ru/commerce/index.ts b/src/locales/ru/commerce/index.ts index 39d00182c8b..361ba7eb115 100644 --- a/src/locales/ru/commerce/index.ts +++ b/src/locales/ru/commerce/index.ts @@ -3,12 +3,10 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; const commerce = { - color, department, product_name, } as Partial; diff --git a/src/locales/sv/commerce/color.ts b/src/locales/sv/color/human.ts similarity index 100% rename from src/locales/sv/commerce/color.ts rename to src/locales/sv/color/human.ts diff --git a/src/locales/sv/color/index.ts b/src/locales/sv/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/sv/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/sv/commerce/index.ts b/src/locales/sv/commerce/index.ts index 39d00182c8b..361ba7eb115 100644 --- a/src/locales/sv/commerce/index.ts +++ b/src/locales/sv/commerce/index.ts @@ -3,12 +3,10 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; const commerce = { - color, department, product_name, } as Partial; diff --git a/src/locales/tr/commerce/color.ts b/src/locales/tr/color/human.ts similarity index 100% rename from src/locales/tr/commerce/color.ts rename to src/locales/tr/color/human.ts diff --git a/src/locales/tr/color/index.ts b/src/locales/tr/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/tr/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/tr/commerce/index.ts b/src/locales/tr/commerce/index.ts index 632e6fcc48e..a643dfa67c0 100644 --- a/src/locales/tr/commerce/index.ts +++ b/src/locales/tr/commerce/index.ts @@ -3,13 +3,11 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_description from './product_description'; import product_name from './product_name'; const commerce = { - color, department, product_description, product_name, diff --git a/src/locales/ur/commerce/color.ts b/src/locales/ur/color/human.ts similarity index 100% rename from src/locales/ur/commerce/color.ts rename to src/locales/ur/color/human.ts diff --git a/src/locales/ur/color/index.ts b/src/locales/ur/color/index.ts new file mode 100644 index 00000000000..79cc39f02f3 --- /dev/null +++ b/src/locales/ur/color/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { ColorDefinitions } from '../../..'; +import human from './human'; + +const color: ColorDefinitions = { + human, +}; + +export default color; diff --git a/src/locales/ur/commerce/index.ts b/src/locales/ur/commerce/index.ts index 39d00182c8b..361ba7eb115 100644 --- a/src/locales/ur/commerce/index.ts +++ b/src/locales/ur/commerce/index.ts @@ -3,12 +3,10 @@ * Run 'pnpm run generate:locales' to update. */ import type { CommerceDefinitions } from '../../..'; -import color from './color'; import department from './department'; import product_name from './product_name'; const commerce = { - color, department, product_name, } as Partial; From 4fc40a6a64a7e90a653bd38529d7e91b7d7f7110 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 14:02:49 -0600 Subject: [PATCH 05/72] feat: add test file for color module --- test/color.spec.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++ test/vehicle.spec.ts | 2 +- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/color.spec.ts diff --git a/test/color.spec.ts b/test/color.spec.ts new file mode 100644 index 00000000000..d05e9266a7a --- /dev/null +++ b/test/color.spec.ts @@ -0,0 +1,62 @@ +import { afterEach, describe, expect, it } from 'vitest'; +import { faker } from '../src'; + +const seededRuns = [ + { + seed: 42, + expectations: { + human: 'grey', + }, + }, + { + seed: 1337, + expectations: { + human: 'black', + }, + }, + { + seed: 1211, + expectations: { + human: 'azure', + }, + }, +]; + +const NON_SEEDED_BASED_RUN = 5; + +const functionNames = ['human']; + +describe('color', () => { + afterEach(() => { + faker.locale = 'en'; + }); + + for (const { seed, expectations } of seededRuns) { + describe(`seed: ${seed}`, () => { + for (const functionName of functionNames) { + it(`${functionName}()`, () => { + faker.seed(seed); + + const actual = faker.color[functionName](); + expect(actual).toEqual(expectations[functionName]); + }); + } + }); + } + + // Create and log-back the seed for debug purposes + faker.seed(Math.ceil(Math.random() * 1_000_000_000)); + + describe(`random seeded tests for seed ${JSON.stringify( + faker.seedValue + )}`, () => { + for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe(`human()`, () => { + it('should return random human readable color from human color array', () => { + const color = faker.color.human(); + expect(faker.definitions.color.human).toContain(color); + }); + }); + } + }); +}); diff --git a/test/vehicle.spec.ts b/test/vehicle.spec.ts index 28757e6b03a..6ee131db819 100644 --- a/test/vehicle.spec.ts +++ b/test/vehicle.spec.ts @@ -164,7 +164,7 @@ describe('vehicle', () => { expect(color).toBeTruthy(); expect(color).toBeTypeOf('string'); - expect(faker.definitions.commerce.color).toContain(color); + expect(faker.definitions.color.human).toContain(color); }); }); From feadbbbd3d1d3d12d2e9a406af7f5d808be4ed6a Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 15:18:15 -0600 Subject: [PATCH 06/72] feat: add rgb generation in both hex and decimal format --- src/color.ts | 22 ++++++++++++++++++++++ test/color.spec.ts | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/color.ts b/src/color.ts index 6d81cc8d41e..18ae3c42018 100644 --- a/src/color.ts +++ b/src/color.ts @@ -20,4 +20,26 @@ export class Color { human(): string { return this.faker.random.arrayElement(this.faker.definitions.color.human); } + + /** + * Returns a RGB color hex + * + * @example + * faker.color.rgb() // '#ffffff' + */ + rgb(): string { + return this.faker.datatype.hexaDecimal(6); + } + + /** + * Returns a RGB color in decimal format + * + * @example + * faker.color.rgb_numeric() // '[255, 255, 255]' + */ + rgb_numeric(): number[] { + return new Array(3).map(() => + this.faker.datatype.number({ min: 0, max: 255 }) + ); + } } diff --git a/test/color.spec.ts b/test/color.spec.ts index d05e9266a7a..303212e7a6d 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -57,6 +57,24 @@ describe('color', () => { expect(faker.definitions.color.human).toContain(color); }); }); + + describe(`rgb()`, () => { + it('should return a random rgb hex color', () => { + const color = faker.color.rgb(); + expect(color).match(/^(0x[a-fA-F0-9]{6})$/); + }); + }); + + describe(`rgb_numeric()`, () => { + it('should return a random rgb color in decimal format', () => { + const color = faker.color.rgb_numeric(); + expect(color).length(3); + color.forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(255); + }); + }); + }); } }); }); From a634a540c4b7770af37e28b07f50be030e0b9428 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 15:39:18 -0600 Subject: [PATCH 07/72] feat: add generation of rgba in hex and decimal format --- src/color.ts | 29 +++++++++++++++++++++++++++-- test/color.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/color.ts b/src/color.ts index 18ae3c42018..6e777464d5c 100644 --- a/src/color.ts +++ b/src/color.ts @@ -22,13 +22,13 @@ export class Color { } /** - * Returns a RGB color hex + * Returns a RGB color in hex format * * @example * faker.color.rgb() // '#ffffff' */ rgb(): string { - return this.faker.datatype.hexaDecimal(6); + return this.faker.datatype.hexadecimal(6); } /** @@ -42,4 +42,29 @@ export class Color { this.faker.datatype.number({ min: 0, max: 255 }) ); } + + /** + * Return a RGBA color in hex format + * + * @example + * faker.color.rgba() // '#ffffff00' + */ + rgba(): string { + let alpha = Math.round(this.faker.datatype.float({ min: 0, max: 1 }) * 255); + alpha = (alpha + 0x10000).toString(16).substr(-2); + return `${this.faker.color.rgb()}${alpha}`; + } + + /** + * Returns a RGBA color in decimal format + * + * @example + * faker.color.rgba_numeric() // '[255, 255, 255, 0.5]' + */ + rgba_numeric(): number[] { + const result = this.faker.color.rgb_numeric(); + const alpha = this.faker.datatype.float({ min: 0, max: 1, precision: 0.1 }); + result.push(alpha); + return result; + } } diff --git a/test/color.spec.ts b/test/color.spec.ts index 303212e7a6d..9ebace5fded 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -75,6 +75,26 @@ describe('color', () => { }); }); }); + + describe(`rgba()`, () => { + it('should return a random rgba hex color', () => { + const color = faker.color.rgba(); + expect(color).match(/^(0x[a-fA-F0-9]{8})$/); + }); + }); + + describe(`rgba_numeric()`, () => { + it('should return a random rgba color in decimal format', () => { + const color = faker.color.rgba_numeric(); + expect(color).length(4); + color.slice(0, 3).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(255); + }); + expect(color[color.length - 1]).toBeGreaterThanOrEqual(0); + expect(color[color.length - 1]).toBeLessThanOrEqual(1); + }); + }); } }); }); From 2627330829fa04492b0ffaac328bbfc543fbcb85 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 15:49:23 -0600 Subject: [PATCH 08/72] feat: add seeded tests for rgb and rgba methods --- src/color.ts | 18 +++++++++++------- test/color.spec.ts | 18 +++++++++++++++--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/color.ts b/src/color.ts index 6e777464d5c..71e5aff4c0a 100644 --- a/src/color.ts +++ b/src/color.ts @@ -35,10 +35,10 @@ export class Color { * Returns a RGB color in decimal format * * @example - * faker.color.rgb_numeric() // '[255, 255, 255]' + * faker.color.rgbNumeric() // '[255, 255, 255]' */ - rgb_numeric(): number[] { - return new Array(3).map(() => + rgbNumeric(): number[] { + return [0, 0, 0].map(() => this.faker.datatype.number({ min: 0, max: 255 }) ); } @@ -59,11 +59,15 @@ export class Color { * Returns a RGBA color in decimal format * * @example - * faker.color.rgba_numeric() // '[255, 255, 255, 0.5]' + * faker.color.rgbaNumeric() // '[255, 255, 255, 0.5]' */ - rgba_numeric(): number[] { - const result = this.faker.color.rgb_numeric(); - const alpha = this.faker.datatype.float({ min: 0, max: 1, precision: 0.1 }); + rgbaNumeric(): number[] { + const result: number[] = this.faker.color.rgbNumeric(); + const alpha: number = this.faker.datatype.float({ + min: 0, + max: 1, + precision: 0.1, + }); result.push(alpha); return result; } diff --git a/test/color.spec.ts b/test/color.spec.ts index 9ebace5fded..96a0c6c28b6 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -6,25 +6,37 @@ const seededRuns = [ seed: 42, expectations: { human: 'grey', + rgb: '0x8BE4AB', + rgba: '0xBE4ABd5e', + rgbNumeric: [95, 203, 243], + rgbaNumeric: [95, 203, 243, 0.2], }, }, { seed: 1337, expectations: { human: 'black', + rgb: '0x5c346b', + rgba: '0xc346ba42', + rgbNumeric: [67, 143, 40], + rgbaNumeric: [67, 143, 40, 0.2], }, }, { seed: 1211, expectations: { human: 'azure', + rgb: '0xEaDB42', + rgba: '0xaDB42Fed', + rgbNumeric: [237, 117, 228], + rgbaNumeric: [237, 117, 228, 0.8], }, }, ]; const NON_SEEDED_BASED_RUN = 5; -const functionNames = ['human']; +const functionNames = ['human', 'rgb', 'rgba', 'rgbNumeric', 'rgbaNumeric']; describe('color', () => { afterEach(() => { @@ -67,7 +79,7 @@ describe('color', () => { describe(`rgb_numeric()`, () => { it('should return a random rgb color in decimal format', () => { - const color = faker.color.rgb_numeric(); + const color = faker.color.rgbNumeric(); expect(color).length(3); color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); @@ -85,7 +97,7 @@ describe('color', () => { describe(`rgba_numeric()`, () => { it('should return a random rgba color in decimal format', () => { - const color = faker.color.rgba_numeric(); + const color = faker.color.rgbaNumeric(); expect(color).length(4); color.slice(0, 3).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); From 43bbe3a9216561755d7a639151faedf4e2b7d60b Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 16:02:12 -0600 Subject: [PATCH 09/72] feat: add generation of CMYK colors --- src/color.ts | 12 ++++++++++++ test/color.spec.ts | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/color.ts b/src/color.ts index 71e5aff4c0a..fcaa25e69ba 100644 --- a/src/color.ts +++ b/src/color.ts @@ -71,4 +71,16 @@ export class Color { result.push(alpha); return result; } + + /** + * Returns a CMYK color + * + * @example + * faker.color.cmyk() // [0.1, 0.2, 0.3, 0.4] + */ + cmyk(): number[] { + return [0, 0, 0, 0].map(() => + this.faker.datatype.float({ min: 0, max: 1, precision: 0.1 }) + ); + } } diff --git a/test/color.spec.ts b/test/color.spec.ts index 96a0c6c28b6..1fa9b4cb67a 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -77,7 +77,7 @@ describe('color', () => { }); }); - describe(`rgb_numeric()`, () => { + describe(`rgbNumeric()`, () => { it('should return a random rgb color in decimal format', () => { const color = faker.color.rgbNumeric(); expect(color).length(3); @@ -95,7 +95,7 @@ describe('color', () => { }); }); - describe(`rgba_numeric()`, () => { + describe(`rgbaNumeric()`, () => { it('should return a random rgba color in decimal format', () => { const color = faker.color.rgbaNumeric(); expect(color).length(4); @@ -107,6 +107,17 @@ describe('color', () => { expect(color[color.length - 1]).toBeLessThanOrEqual(1); }); }); + + describe(`cmyk()`, () => { + it('should return a random cmyk color', () => { + const color = faker.color.cmyk(); + expect(color).length(4); + color.forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(1); + }); + }); + }); } }); }); From 10a7307c0f658344181b3e3ddab1c5632b9e5cfa Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 16:08:12 -0600 Subject: [PATCH 10/72] feat: simplify generation of rgba hex generation --- src/color.ts | 4 +--- test/color.spec.ts | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/color.ts b/src/color.ts index fcaa25e69ba..812af7a371f 100644 --- a/src/color.ts +++ b/src/color.ts @@ -50,9 +50,7 @@ export class Color { * faker.color.rgba() // '#ffffff00' */ rgba(): string { - let alpha = Math.round(this.faker.datatype.float({ min: 0, max: 1 }) * 255); - alpha = (alpha + 0x10000).toString(16).substr(-2); - return `${this.faker.color.rgb()}${alpha}`; + return this.faker.datatype.hexadecimal(8); } /** diff --git a/test/color.spec.ts b/test/color.spec.ts index 1fa9b4cb67a..dca1b352e8b 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -7,7 +7,7 @@ const seededRuns = [ expectations: { human: 'grey', rgb: '0x8BE4AB', - rgba: '0xBE4ABd5e', + rgba: '0x8BE4ABdd', rgbNumeric: [95, 203, 243], rgbaNumeric: [95, 203, 243, 0.2], }, @@ -17,7 +17,7 @@ const seededRuns = [ expectations: { human: 'black', rgb: '0x5c346b', - rgba: '0xc346ba42', + rgba: '0x5c346ba0', rgbNumeric: [67, 143, 40], rgbaNumeric: [67, 143, 40, 0.2], }, @@ -27,7 +27,7 @@ const seededRuns = [ expectations: { human: 'azure', rgb: '0xEaDB42', - rgba: '0xaDB42Fed', + rgba: '0xEaDB42F0', rgbNumeric: [237, 117, 228], rgbaNumeric: [237, 117, 228, 0.8], }, From b9f438e22e0d7052523469212ebee8555bf62ef8 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 16:21:03 -0600 Subject: [PATCH 11/72] feat: add hsla generation --- src/color.ts | 39 ++++++++++++++++++++++++++++----------- test/color.spec.ts | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/color.ts b/src/color.ts index 812af7a371f..b8592be5c18 100644 --- a/src/color.ts +++ b/src/color.ts @@ -60,14 +60,9 @@ export class Color { * faker.color.rgbaNumeric() // '[255, 255, 255, 0.5]' */ rgbaNumeric(): number[] { - const result: number[] = this.faker.color.rgbNumeric(); - const alpha: number = this.faker.datatype.float({ - min: 0, - max: 1, - precision: 0.1, - }); - result.push(alpha); - return result; + const rgba: number[] = this.faker.color.rgbNumeric(); + rgba.push(this.getPercentage()); + return rgba; } /** @@ -77,8 +72,30 @@ export class Color { * faker.color.cmyk() // [0.1, 0.2, 0.3, 0.4] */ cmyk(): number[] { - return [0, 0, 0, 0].map(() => - this.faker.datatype.float({ min: 0, max: 1, precision: 0.1 }) - ); + return [0, 0, 0, 0].map(() => this.getPercentage()); + } + + /** + * Returns a HSL color + * + * @example + * faker.color.hsl() // [0.1, 0.2, 0.3] + */ + hsl(): number[] { + const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; + for (let i = 0; i < 2; i++) { + hsl.push(this.getPercentage()); + } + return hsl; + } + + hsla(): number[] { + const hsla: number[] = this.faker.color.hsl(); + hsla.push(this.getPercentage()); + return hsla; + } + + private getPercentage(): number { + return this.faker.datatype.float({ min: 0, max: 1, precision: 0.1 }); } } diff --git a/test/color.spec.ts b/test/color.spec.ts index dca1b352e8b..05733fd38ea 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -10,6 +10,8 @@ const seededRuns = [ rgba: '0x8BE4ABdd', rgbNumeric: [95, 203, 243], rgbaNumeric: [95, 203, 243, 0.2], + hsl: [135, 0.8, 1], + hsla: [135, 0.8, 1, 0.2], }, }, { @@ -20,6 +22,8 @@ const seededRuns = [ rgba: '0x5c346ba0', rgbNumeric: [67, 143, 40], rgbaNumeric: [67, 143, 40, 0.2], + hsl: [94, 0.6, 0.1], + hsla: [94, 0.6, 0.1, 0.2], }, }, { @@ -30,13 +34,23 @@ const seededRuns = [ rgba: '0xEaDB42F0', rgbNumeric: [237, 117, 228], rgbaNumeric: [237, 117, 228, 0.8], + hsl: [335, 0.5, 0.9], + hsla: [335, 0.5, 0.9, 0.8], }, }, ]; const NON_SEEDED_BASED_RUN = 5; -const functionNames = ['human', 'rgb', 'rgba', 'rgbNumeric', 'rgbaNumeric']; +const functionNames = [ + 'human', + 'rgb', + 'rgba', + 'rgbNumeric', + 'rgbaNumeric', + 'hsl', + 'hsla', +]; describe('color', () => { afterEach(() => { @@ -118,6 +132,32 @@ describe('color', () => { }); }); }); + + describe(`hsl()`, () => { + it('should return a random hsl color in decimal format', () => { + const color = faker.color.hsl(); + expect(color).length(3); + expect(color[0]).toBeGreaterThanOrEqual(0); + expect(color[0]).toBeLessThanOrEqual(360); + color.slice(1).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(1); + }); + }); + }); + + describe(`hsla()`, () => { + it('should return a random hsla color in decimal format', () => { + const color = faker.color.hsla(); + expect(color).length(4); + expect(color[0]).toBeGreaterThanOrEqual(0); + expect(color[0]).toBeLessThanOrEqual(360); + color.slice(1).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(1); + }); + }); + }); } }); }); From 9eb05ebcc12799684f02833ef0e02c88b4000e38 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 16:32:20 -0600 Subject: [PATCH 12/72] fix: change percentage generation to have percision of two decimal places --- src/color.ts | 21 +++++++++++++++++---- test/color.spec.ts | 18 +++++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/color.ts b/src/color.ts index b8592be5c18..cee127a51ac 100644 --- a/src/color.ts +++ b/src/color.ts @@ -57,7 +57,7 @@ export class Color { * Returns a RGBA color in decimal format * * @example - * faker.color.rgbaNumeric() // '[255, 255, 255, 0.5]' + * faker.color.rgbaNumeric() // '[255, 255, 255, 0.54]' */ rgbaNumeric(): number[] { const rgba: number[] = this.faker.color.rgbNumeric(); @@ -69,7 +69,7 @@ export class Color { * Returns a CMYK color * * @example - * faker.color.cmyk() // [0.1, 0.2, 0.3, 0.4] + * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] */ cmyk(): number[] { return [0, 0, 0, 0].map(() => this.getPercentage()); @@ -79,7 +79,7 @@ export class Color { * Returns a HSL color * * @example - * faker.color.hsl() // [0.1, 0.2, 0.3] + * faker.color.hsl() // [201, 0.23, 0.32] */ hsl(): number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; @@ -89,13 +89,26 @@ export class Color { return hsl; } + /** + * Returns a HSLA color + * + * @example + * faker.color.hsla() // [201, 0.21, 0.31, 0.11] + */ hsla(): number[] { const hsla: number[] = this.faker.color.hsl(); hsla.push(this.getPercentage()); return hsla; } + /** + * Return a percentage value in decimal format betwene 0 and 1 + * with percision of two decimal place + * + * @example + * this.getPercentage() // 0.36 + */ private getPercentage(): number { - return this.faker.datatype.float({ min: 0, max: 1, precision: 0.1 }); + return this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }); } } diff --git a/test/color.spec.ts b/test/color.spec.ts index 05733fd38ea..6e4fd27d23e 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -9,9 +9,9 @@ const seededRuns = [ rgb: '0x8BE4AB', rgba: '0x8BE4ABdd', rgbNumeric: [95, 203, 243], - rgbaNumeric: [95, 203, 243, 0.2], - hsl: [135, 0.8, 1], - hsla: [135, 0.8, 1, 0.2], + rgbaNumeric: [95, 203, 243, 0.18], + hsl: [135, 0.8, 0.96], + hsla: [135, 0.8, 0.96, 0.18], }, }, { @@ -21,9 +21,9 @@ const seededRuns = [ rgb: '0x5c346b', rgba: '0x5c346ba0', rgbNumeric: [67, 143, 40], - rgbaNumeric: [67, 143, 40, 0.2], - hsl: [94, 0.6, 0.1], - hsla: [94, 0.6, 0.1, 0.2], + rgbaNumeric: [67, 143, 40, 0.21], + hsl: [94, 0.56, 0.16], + hsla: [94, 0.56, 0.16, 0.21], }, }, { @@ -33,9 +33,9 @@ const seededRuns = [ rgb: '0xEaDB42', rgba: '0xEaDB42F0', rgbNumeric: [237, 117, 228], - rgbaNumeric: [237, 117, 228, 0.8], - hsl: [335, 0.5, 0.9], - hsla: [335, 0.5, 0.9, 0.8], + rgbaNumeric: [237, 117, 228, 0.78], + hsl: [335, 0.46, 0.9], + hsla: [335, 0.46, 0.9, 0.78], }, }, ]; From 79bf1d9742699f072c4c9e2a62576e40a09312ff Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Thu, 7 Apr 2022 18:16:09 -0600 Subject: [PATCH 13/72] fix: change # in docs to 0x to match method outputs --- src/color.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/color.ts b/src/color.ts index cee127a51ac..fa63312a188 100644 --- a/src/color.ts +++ b/src/color.ts @@ -25,7 +25,7 @@ export class Color { * Returns a RGB color in hex format * * @example - * faker.color.rgb() // '#ffffff' + * faker.color.rgb() // '0xffffff' */ rgb(): string { return this.faker.datatype.hexadecimal(6); @@ -47,7 +47,7 @@ export class Color { * Return a RGBA color in hex format * * @example - * faker.color.rgba() // '#ffffff00' + * faker.color.rgba() // '0xffffff00' */ rgba(): string { return this.faker.datatype.hexadecimal(8); From f731676318b789675ebc15d9f81a04ab1ea7c3b1 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 11:27:21 -0600 Subject: [PATCH 14/72] fix: generate locales again to fix syntax --- src/locales/ar/commerce/index.ts | 4 ++-- src/locales/ar/index.ts | 2 ++ src/locales/az/commerce/index.ts | 4 ++-- src/locales/az/index.ts | 2 ++ src/locales/el/commerce/index.ts | 4 ++-- src/locales/el/index.ts | 2 ++ src/locales/es/commerce/index.ts | 4 ++-- src/locales/es/index.ts | 2 ++ src/locales/es_MX/commerce/index.ts | 4 ++-- src/locales/es_MX/index.ts | 2 ++ src/locales/fa/commerce/index.ts | 4 ++-- src/locales/fa/index.ts | 2 ++ src/locales/he/commerce/index.ts | 4 ++-- src/locales/he/index.ts | 2 ++ src/locales/lv/commerce/index.ts | 4 ++-- src/locales/lv/index.ts | 2 ++ src/locales/nl/commerce/index.ts | 4 ++-- src/locales/nl/index.ts | 2 ++ src/locales/pt_BR/commerce/index.ts | 4 ++-- src/locales/pt_BR/index.ts | 2 ++ src/locales/pt_PT/commerce/index.ts | 4 ++-- src/locales/pt_PT/index.ts | 2 ++ src/locales/ru/commerce/index.ts | 4 ++-- src/locales/ru/index.ts | 2 ++ src/locales/sv/commerce/index.ts | 4 ++-- src/locales/sv/index.ts | 2 ++ src/locales/tr/commerce/index.ts | 4 ++-- src/locales/tr/index.ts | 2 ++ src/locales/ur/commerce/index.ts | 4 ++-- src/locales/ur/index.ts | 2 ++ 30 files changed, 60 insertions(+), 30 deletions(-) diff --git a/src/locales/ar/commerce/index.ts b/src/locales/ar/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/ar/commerce/index.ts +++ b/src/locales/ar/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/ar/index.ts b/src/locales/ar/index.ts index e3e47d94632..643d2823fa6 100644 --- a/src/locales/ar/index.ts +++ b/src/locales/ar/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import date from './date'; import name_ from './name'; @@ -17,6 +18,7 @@ const ar: LocaleDefinition = { separator: ' & ', address, cell_phone, + color, commerce, date, name: name_, diff --git a/src/locales/az/commerce/index.ts b/src/locales/az/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/az/commerce/index.ts +++ b/src/locales/az/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/az/index.ts b/src/locales/az/index.ts index 05b10fd479e..55d7d8077f3 100644 --- a/src/locales/az/index.ts +++ b/src/locales/az/index.ts @@ -4,6 +4,7 @@ */ import type { LocaleDefinition } from '../..'; import address from './address'; +import color from './color'; import commerce from './commerce'; import company from './company'; import date from './date'; @@ -15,6 +16,7 @@ const az: LocaleDefinition = { title: 'Azerbaijani', separator: ' və ', address, + color, commerce, company, date, diff --git a/src/locales/el/commerce/index.ts b/src/locales/el/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/el/commerce/index.ts +++ b/src/locales/el/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/el/index.ts b/src/locales/el/index.ts index 430bdf68246..69b91f40dbc 100644 --- a/src/locales/el/index.ts +++ b/src/locales/el/index.ts @@ -7,6 +7,7 @@ import address from './address'; import app from './app'; import business from './business'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import company from './company'; import finance from './finance'; @@ -24,6 +25,7 @@ const el: LocaleDefinition = { app, business, cell_phone, + color, commerce, company, finance, diff --git a/src/locales/es/commerce/index.ts b/src/locales/es/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/es/commerce/index.ts +++ b/src/locales/es/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/es/index.ts b/src/locales/es/index.ts index dd8a4775b36..b97f68a245d 100644 --- a/src/locales/es/index.ts +++ b/src/locales/es/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import company from './company'; import internet from './internet'; @@ -15,6 +16,7 @@ const es: LocaleDefinition = { title: 'Spanish', address, cell_phone, + color, commerce, company, internet, diff --git a/src/locales/es_MX/commerce/index.ts b/src/locales/es_MX/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/es_MX/commerce/index.ts +++ b/src/locales/es_MX/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/es_MX/index.ts b/src/locales/es_MX/index.ts index 189829fa953..c60536e97d1 100644 --- a/src/locales/es_MX/index.ts +++ b/src/locales/es_MX/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import company from './company'; import internet from './internet'; @@ -18,6 +19,7 @@ const es_MX: LocaleDefinition = { separator: ' & ', address, cell_phone, + color, commerce, company, internet, diff --git a/src/locales/fa/commerce/index.ts b/src/locales/fa/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/fa/commerce/index.ts +++ b/src/locales/fa/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/fa/index.ts b/src/locales/fa/index.ts index 9f53ec55417..b554f801c2b 100644 --- a/src/locales/fa/index.ts +++ b/src/locales/fa/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import company from './company'; import date from './date'; @@ -19,6 +20,7 @@ const fa: LocaleDefinition = { title: 'Farsi', address, cell_phone, + color, commerce, company, date, diff --git a/src/locales/he/commerce/index.ts b/src/locales/he/commerce/index.ts index a643dfa67c0..dab2aa6966f 100644 --- a/src/locales/he/commerce/index.ts +++ b/src/locales/he/commerce/index.ts @@ -7,10 +7,10 @@ import department from './department'; import product_description from './product_description'; import product_name from './product_name'; -const commerce = { +const commerce: CommerceDefinitions = { department, product_description, product_name, -} as CommerceDefinitions; +}; export default commerce; diff --git a/src/locales/he/index.ts b/src/locales/he/index.ts index 1ab59ac8ae1..38a26a29361 100644 --- a/src/locales/he/index.ts +++ b/src/locales/he/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import date from './date'; import lorem from './lorem'; @@ -17,6 +18,7 @@ const he: LocaleDefinition = { separator: 'ו ', address, cell_phone, + color, commerce, date, lorem, diff --git a/src/locales/lv/commerce/index.ts b/src/locales/lv/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/lv/commerce/index.ts +++ b/src/locales/lv/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/lv/index.ts b/src/locales/lv/index.ts index 31fe8253057..527d7c3fcb8 100644 --- a/src/locales/lv/index.ts +++ b/src/locales/lv/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import company from './company'; import date from './date'; @@ -18,6 +19,7 @@ const lv: LocaleDefinition = { separator: ' un ', address, cell_phone, + color, commerce, company, date, diff --git a/src/locales/nl/commerce/index.ts b/src/locales/nl/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/nl/commerce/index.ts +++ b/src/locales/nl/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/nl/index.ts b/src/locales/nl/index.ts index 7dc23561174..6e6fb8a55fa 100644 --- a/src/locales/nl/index.ts +++ b/src/locales/nl/index.ts @@ -4,6 +4,7 @@ */ import type { LocaleDefinition } from '../..'; import address from './address'; +import color from './color'; import commerce from './commerce'; import company from './company'; import date from './date'; @@ -16,6 +17,7 @@ import phone_number from './phone_number'; const nl: LocaleDefinition = { title: 'Dutch', address, + color, commerce, company, date, diff --git a/src/locales/pt_BR/commerce/index.ts b/src/locales/pt_BR/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/pt_BR/commerce/index.ts +++ b/src/locales/pt_BR/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/pt_BR/index.ts b/src/locales/pt_BR/index.ts index a23c5227aba..de563dcd424 100644 --- a/src/locales/pt_BR/index.ts +++ b/src/locales/pt_BR/index.ts @@ -4,6 +4,7 @@ */ import type { LocaleDefinition } from '../..'; import address from './address'; +import color from './color'; import commerce from './commerce'; import company from './company'; import date from './date'; @@ -15,6 +16,7 @@ import phone_number from './phone_number'; const pt_BR: LocaleDefinition = { title: 'Portuguese (Brazil)', address, + color, commerce, company, date, diff --git a/src/locales/pt_PT/commerce/index.ts b/src/locales/pt_PT/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/pt_PT/commerce/index.ts +++ b/src/locales/pt_PT/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/pt_PT/index.ts b/src/locales/pt_PT/index.ts index cd2281d8808..ca3a9f30263 100644 --- a/src/locales/pt_PT/index.ts +++ b/src/locales/pt_PT/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import date from './date'; import internet from './internet'; @@ -15,6 +16,7 @@ const pt_PT: LocaleDefinition = { title: 'Portuguese (Portugal)', address, cell_phone, + color, commerce, date, internet, diff --git a/src/locales/ru/commerce/index.ts b/src/locales/ru/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/ru/commerce/index.ts +++ b/src/locales/ru/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/ru/index.ts b/src/locales/ru/index.ts index b536671482c..d8e94deedea 100644 --- a/src/locales/ru/index.ts +++ b/src/locales/ru/index.ts @@ -4,6 +4,7 @@ */ import type { LocaleDefinition } from '../..'; import address from './address'; +import color from './color'; import commerce from './commerce'; import company from './company'; import date from './date'; @@ -17,6 +18,7 @@ const ru: LocaleDefinition = { title: 'Russian', separator: ' и ', address, + color, commerce, company, date, diff --git a/src/locales/sv/commerce/index.ts b/src/locales/sv/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/sv/commerce/index.ts +++ b/src/locales/sv/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/sv/index.ts b/src/locales/sv/index.ts index dc0f098593d..6c5ffe69f98 100644 --- a/src/locales/sv/index.ts +++ b/src/locales/sv/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import company from './company'; import date from './date'; @@ -17,6 +18,7 @@ const sv: LocaleDefinition = { title: 'Swedish', address, cell_phone, + color, commerce, company, date, diff --git a/src/locales/tr/commerce/index.ts b/src/locales/tr/commerce/index.ts index a643dfa67c0..dab2aa6966f 100644 --- a/src/locales/tr/commerce/index.ts +++ b/src/locales/tr/commerce/index.ts @@ -7,10 +7,10 @@ import department from './department'; import product_description from './product_description'; import product_name from './product_name'; -const commerce = { +const commerce: CommerceDefinitions = { department, product_description, product_name, -} as CommerceDefinitions; +}; export default commerce; diff --git a/src/locales/tr/index.ts b/src/locales/tr/index.ts index 7a7fac2ff9d..70a988b65af 100644 --- a/src/locales/tr/index.ts +++ b/src/locales/tr/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import address from './address'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import internet from './internet'; import lorem from './lorem'; @@ -15,6 +16,7 @@ const tr: LocaleDefinition = { title: 'Turkish', address, cell_phone, + color, commerce, internet, lorem, diff --git a/src/locales/ur/commerce/index.ts b/src/locales/ur/commerce/index.ts index 361ba7eb115..1c8b94caae5 100644 --- a/src/locales/ur/commerce/index.ts +++ b/src/locales/ur/commerce/index.ts @@ -6,9 +6,9 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce = { +const commerce: Partial = { department, product_name, -} as Partial; +}; export default commerce; diff --git a/src/locales/ur/index.ts b/src/locales/ur/index.ts index 49aae08e2a2..5794e793cbb 100644 --- a/src/locales/ur/index.ts +++ b/src/locales/ur/index.ts @@ -8,6 +8,7 @@ import animal from './animal'; import app from './app'; import business from './business'; import cell_phone from './cell_phone'; +import color from './color'; import commerce from './commerce'; import date from './date'; import finance from './finance'; @@ -25,6 +26,7 @@ const ur: LocaleDefinition = { app, business, cell_phone, + color, commerce, date, finance, From 44dd389f01ddb59bc998ca0fc7dd4c48d89b6faf Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 11:47:10 -0600 Subject: [PATCH 15/72] fix: add a period at the end of docs --- src/color.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/color.ts b/src/color.ts index fa63312a188..421a4a156bc 100644 --- a/src/color.ts +++ b/src/color.ts @@ -22,7 +22,7 @@ export class Color { } /** - * Returns a RGB color in hex format + * Returns a RGB color in hex format. * * @example * faker.color.rgb() // '0xffffff' @@ -32,7 +32,7 @@ export class Color { } /** - * Returns a RGB color in decimal format + * Returns a RGB color in decimal format. * * @example * faker.color.rgbNumeric() // '[255, 255, 255]' @@ -44,7 +44,7 @@ export class Color { } /** - * Return a RGBA color in hex format + * Return a RGBA color in hex format. * * @example * faker.color.rgba() // '0xffffff00' @@ -54,7 +54,7 @@ export class Color { } /** - * Returns a RGBA color in decimal format + * Returns a RGBA color in decimal format. * * @example * faker.color.rgbaNumeric() // '[255, 255, 255, 0.54]' @@ -66,7 +66,7 @@ export class Color { } /** - * Returns a CMYK color + * Returns a CMYK color. * * @example * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] @@ -76,7 +76,7 @@ export class Color { } /** - * Returns a HSL color + * Returns a HSL color. * * @example * faker.color.hsl() // [201, 0.23, 0.32] @@ -90,7 +90,7 @@ export class Color { } /** - * Returns a HSLA color + * Returns a HSLA color. * * @example * faker.color.hsla() // [201, 0.21, 0.31, 0.11] @@ -103,7 +103,7 @@ export class Color { /** * Return a percentage value in decimal format betwene 0 and 1 - * with percision of two decimal place + * with percision of two decimal place. * * @example * this.getPercentage() // 0.36 From c7f255ede7316cb79c28eae83da7705d1bbebcc6 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 12:13:34 -0600 Subject: [PATCH 16/72] fix: revert deletion of color method in commerce and add deprecation message to it --- src/commerce.ts | 19 +++++++++++++++++++ test/commerce.spec.ts | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/commerce.ts b/src/commerce.ts index c24ff328836..96ca6ab70ea 100644 --- a/src/commerce.ts +++ b/src/commerce.ts @@ -1,4 +1,5 @@ import type { Faker } from '.'; +import { deprecated } from './internal/deprecated'; /** * Module to generate commerce and product related entries. @@ -14,6 +15,24 @@ export class Commerce { } } + /** + * Returns a human readable color name. + * + * @example + * faker.commerce.color() // 'red' + * + * @deprecated + */ + color(): string { + deprecated({ + deprecated: 'faker.commerce.color()', + proposed: 'faker.color.human()', + since: 'v7.0.0', + until: 'v8.0.0', + }); + return this.faker.random.arrayElement(this.faker.definitions.color.human); + } + /** * Returns a department inside a shop. * diff --git a/test/commerce.spec.ts b/test/commerce.spec.ts index b1cfd72df2d..b387e5844a8 100644 --- a/test/commerce.spec.ts +++ b/test/commerce.spec.ts @@ -5,6 +5,7 @@ const seededRuns = [ { seed: 42, expectations: { + color: 'grey', department: 'Tools', productName: 'Fantastic Soft Sausages', price: '375.00', @@ -18,6 +19,7 @@ const seededRuns = [ { seed: 1337, expectations: { + color: 'black', department: 'Computers', productName: 'Incredible Granite Keyboard', price: '263.00', @@ -31,6 +33,7 @@ const seededRuns = [ { seed: 1211, expectations: { + color: 'azure', department: 'Automotive', productName: 'Unbranded Cotton Salad', price: '929.00', @@ -46,6 +49,7 @@ const seededRuns = [ const NON_SEEDED_BASED_RUN = 5; const functionNames = [ + 'color', 'department', 'productName', 'price', @@ -80,6 +84,13 @@ describe('commerce', () => { faker.seedValue )}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe(`color()`, () => { + it('should return random value from color array', () => { + const actual = faker.commerce.color(); + expect(faker.definitions.color.human).toContain(actual); + }); + }); + describe(`department()`, () => { it('should return random value from department array', () => { const department = faker.commerce.department(); From ffaaad73f39fc7020d56d37558ae32d154468016 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 12:18:39 -0600 Subject: [PATCH 17/72] fix: rename methods that return hex format to have suffix Hex in method name --- src/color.ts | 8 ++++---- test/color.spec.ts | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/color.ts b/src/color.ts index 421a4a156bc..86f3f1c0a06 100644 --- a/src/color.ts +++ b/src/color.ts @@ -25,9 +25,9 @@ export class Color { * Returns a RGB color in hex format. * * @example - * faker.color.rgb() // '0xffffff' + * faker.color.rgbHex() // '0xffffff' */ - rgb(): string { + rgbHex(): string { return this.faker.datatype.hexadecimal(6); } @@ -47,9 +47,9 @@ export class Color { * Return a RGBA color in hex format. * * @example - * faker.color.rgba() // '0xffffff00' + * faker.color.rgbaHex() // '0xffffff00' */ - rgba(): string { + rgbaHex(): string { return this.faker.datatype.hexadecimal(8); } diff --git a/test/color.spec.ts b/test/color.spec.ts index 6e4fd27d23e..dceacacb63b 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -6,8 +6,8 @@ const seededRuns = [ seed: 42, expectations: { human: 'grey', - rgb: '0x8BE4AB', - rgba: '0x8BE4ABdd', + rgbHex: '0x8BE4AB', + rgbaHex: '0x8BE4ABdd', rgbNumeric: [95, 203, 243], rgbaNumeric: [95, 203, 243, 0.18], hsl: [135, 0.8, 0.96], @@ -18,8 +18,8 @@ const seededRuns = [ seed: 1337, expectations: { human: 'black', - rgb: '0x5c346b', - rgba: '0x5c346ba0', + rgbHex: '0x5c346b', + rgbaHex: '0x5c346ba0', rgbNumeric: [67, 143, 40], rgbaNumeric: [67, 143, 40, 0.21], hsl: [94, 0.56, 0.16], @@ -30,8 +30,8 @@ const seededRuns = [ seed: 1211, expectations: { human: 'azure', - rgb: '0xEaDB42', - rgba: '0xEaDB42F0', + rgbHex: '0xEaDB42', + rgbaHex: '0xEaDB42F0', rgbNumeric: [237, 117, 228], rgbaNumeric: [237, 117, 228, 0.78], hsl: [335, 0.46, 0.9], @@ -44,8 +44,8 @@ const NON_SEEDED_BASED_RUN = 5; const functionNames = [ 'human', - 'rgb', - 'rgba', + 'rgbHex', + 'rgbaHex', 'rgbNumeric', 'rgbaNumeric', 'hsl', @@ -84,9 +84,9 @@ describe('color', () => { }); }); - describe(`rgb()`, () => { + describe(`rgbHex()`, () => { it('should return a random rgb hex color', () => { - const color = faker.color.rgb(); + const color = faker.color.rgbHex(); expect(color).match(/^(0x[a-fA-F0-9]{6})$/); }); }); @@ -102,9 +102,9 @@ describe('color', () => { }); }); - describe(`rgba()`, () => { + describe(`rgbaHex()`, () => { it('should return a random rgba hex color', () => { - const color = faker.color.rgba(); + const color = faker.color.rgbaHex(); expect(color).match(/^(0x[a-fA-F0-9]{8})$/); }); }); From 5e8515b828e8648e2992592affb340909aa2bcc9 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 12:47:27 -0600 Subject: [PATCH 18/72] feat: add options for prefix and letter casing to hex colors --- src/color.ts | 69 ++++++++++++++++++++++++++++++++++++++++++---- test/color.spec.ts | 42 ++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 6 deletions(-) diff --git a/src/color.ts b/src/color.ts index 86f3f1c0a06..71a57a490ec 100644 --- a/src/color.ts +++ b/src/color.ts @@ -1,5 +1,36 @@ import type { Faker } from '.'; +/** + * Formats the hex format of a generated color string according + * to options specified by user. + * + * @param hexColor Hex color string to be formated. + * @param options options object. + * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. + * @param options.case Letter case of the generated hex color. Defaults to `mixed`. + */ +function applyHexFormatting( + hexColor: string, + options?: { + prefix?: string; + case?: 'upper' | 'lower' | 'mixed'; + } +): string { + if (options?.prefix) hexColor = hexColor.replace('0x', options.prefix); + switch (options?.case) { + case 'upper': + hexColor = hexColor.toUpperCase(); + break; + case 'lower': + hexColor = hexColor.toLowerCase(); + break; + } + return hexColor; +} + +/** + * Module to generate colors. + */ export class Color { constructor(private readonly faker: Faker) { // Bind `this` so namespaced is working correctly @@ -24,11 +55,24 @@ export class Color { /** * Returns a RGB color in hex format. * + * @param options options object. + * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. + * @param options.case Letter case of the generated hex color. Defaults to `mixed`. + * * @example - * faker.color.rgbHex() // '0xffffff' + * faker.color.rgbHex() // '0xffffFF' + * faker.color.rgbHex({ prefix: '#' }) // '#ffffFF' + * faker.color.rgbHex({ case: 'upper' }) // '0xFFFFFF' + * faker.color.rgbHex({ case: 'lower' }) // '0xffffff' + * faker.color.rgbHex({ prefix: '#', case: 'lower' }) // '#ffffff' */ - rgbHex(): string { - return this.faker.datatype.hexadecimal(6); + rgbHex(options?: { + prefix?: string; + case?: 'upper' | 'lower' | 'mixed'; + }): string { + let color = this.faker.datatype.hexadecimal(6); + if (options) color = applyHexFormatting(color, options); + return color; } /** @@ -46,11 +90,24 @@ export class Color { /** * Return a RGBA color in hex format. * + * @param options options object. + * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. + * @param options.case Letter case of the generated hex color. Defaults to `mixed`. + * * @example - * faker.color.rgbaHex() // '0xffffff00' + * faker.color.rgbaHex() // '0xffffFF00' + * faker.color.rgbaHex({ prefix: '#' }) // '#ffffFF00' + * faker.color.rgbaHex({ case: 'upper' }) // '0xFFFFFF00' + * faker.color.rgbaHex({ case: 'lower' }) // '0xffffff00' + * faker.color.rgbaHex({ prefix: '#', case: 'lower' }) // '#ffffff00' */ - rgbaHex(): string { - return this.faker.datatype.hexadecimal(8); + rgbaHex(options?: { + prefix?: string; + case?: 'upper' | 'lower' | 'mixed'; + }): string { + let color = this.faker.datatype.hexadecimal(8); + if (options) color = applyHexFormatting(color, options); + return color; } /** diff --git a/test/color.spec.ts b/test/color.spec.ts index dceacacb63b..8120f6bd74f 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -91,6 +91,27 @@ describe('color', () => { }); }); + describe(`rgbHex({ prefix: '#' })`, () => { + it('should return a random rgb hex color with # prefix', () => { + const color = faker.color.rgbHex({ prefix: '#' }); + expect(color).match(/^(#[a-fA-F0-9]{6})$/); + }); + }); + + describe(`rgbHex({ prefix: '#', case: 'lower' })`, () => { + it('should return a random rgb hex color with # prefix and lower case only', () => { + const color = faker.color.rgbHex({ prefix: '#', case: 'lower' }); + expect(color).match(/^(#[a-f0-9]{6})$/); + }); + }); + + describe(`rgbHex({ prefix: '#', case: 'upper' })`, () => { + it('should return a random rgb hex color with # prefix and upper case only', () => { + const color = faker.color.rgbHex({ prefix: '#', case: 'upper' }); + expect(color).match(/^(#[A-F0-9]{6})$/); + }); + }); + describe(`rgbNumeric()`, () => { it('should return a random rgb color in decimal format', () => { const color = faker.color.rgbNumeric(); @@ -109,6 +130,27 @@ describe('color', () => { }); }); + describe(`rgbaHex({ prefix: '#' })`, () => { + it('should return a random rgba hex color with # prefix', () => { + const color = faker.color.rgbaHex({ prefix: '#' }); + expect(color).match(/^(#[a-fA-F0-9]{8})$/); + }); + }); + + describe(`rgbaHex({ prefix: '#', case: 'lower' })`, () => { + it('should return a random rgb hex color with # prefix and lower case only', () => { + const color = faker.color.rgbaHex({ prefix: '#', case: 'lower' }); + expect(color).match(/^(#[a-f0-9]{8})$/); + }); + }); + + describe(`rgbaHex({ prefix: '#', case: 'upper' })`, () => { + it('should return a random rgba hex color with # prefix and upper case only', () => { + const color = faker.color.rgbaHex({ prefix: '#', case: 'upper' }); + expect(color).match(/^(#[A-F0-9]{8})$/); + }); + }); + describe(`rgbaNumeric()`, () => { it('should return a random rgba color in decimal format', () => { const color = faker.color.rgbaNumeric(); From dc452807665362a28c2a383db78dcb51a48a4d11 Mon Sep 17 00:00:00 2001 From: Harsohail Brar <47438886+harsohailB@users.noreply.github.com> Date: Fri, 8 Apr 2022 14:23:33 -0600 Subject: [PATCH 19/72] fix: change commerce color method to call color API Co-authored-by: Shinigami --- src/commerce.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commerce.ts b/src/commerce.ts index 96ca6ab70ea..b951d7f8c69 100644 --- a/src/commerce.ts +++ b/src/commerce.ts @@ -30,7 +30,7 @@ export class Commerce { since: 'v7.0.0', until: 'v8.0.0', }); - return this.faker.random.arrayElement(this.faker.definitions.color.human); + return this.faker.color.human(); } /** From ec6147d6583f80fb97aa3626f31ecc65fb768687 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 15:27:23 -0600 Subject: [PATCH 20/72] feat: create percentage generator in commerce and use it in color module --- src/color.ts | 19 ++++--------------- src/commerce.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/color.ts b/src/color.ts index 71a57a490ec..206d6193852 100644 --- a/src/color.ts +++ b/src/color.ts @@ -118,7 +118,7 @@ export class Color { */ rgbaNumeric(): number[] { const rgba: number[] = this.faker.color.rgbNumeric(); - rgba.push(this.getPercentage()); + rgba.push(this.faker.commerce.percentage(0.01)); return rgba; } @@ -129,7 +129,7 @@ export class Color { * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] */ cmyk(): number[] { - return [0, 0, 0, 0].map(() => this.getPercentage()); + return [0, 0, 0, 0].map(() => this.faker.commerce.percentage(0.01)); } /** @@ -141,7 +141,7 @@ export class Color { hsl(): number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; for (let i = 0; i < 2; i++) { - hsl.push(this.getPercentage()); + hsl.push(this.faker.commerce.percentage(0.01)); } return hsl; } @@ -154,18 +154,7 @@ export class Color { */ hsla(): number[] { const hsla: number[] = this.faker.color.hsl(); - hsla.push(this.getPercentage()); + hsla.push(this.faker.commerce.percentage(0.01)); return hsla; } - - /** - * Return a percentage value in decimal format betwene 0 and 1 - * with percision of two decimal place. - * - * @example - * this.getPercentage() // 0.36 - */ - private getPercentage(): number { - return this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }); - } } diff --git a/src/commerce.ts b/src/commerce.ts index 96ca6ab70ea..eb1b88e88ec 100644 --- a/src/commerce.ts +++ b/src/commerce.ts @@ -45,6 +45,24 @@ export class Commerce { ); } + /** + * Return a percentage value in decimal format between 0 and 1 + * with percision of two decimal place. + * + * @param percision Precision of the generated number. Defaults to `0.01`. + * + * @example + * this.getPercentage(0.1) // 0.4 + * this.getPercentage(0.01) // 0.36 + */ + percentage(percision: number): number { + return this.faker.datatype.float({ + min: 0, + max: 1, + precision: percision || 0.01, + }); + } + /** * Generates a random descriptive product name. * From 38cb1ec0e25e453fbfd0e862da9487cbac2f2019 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 19:08:33 -0600 Subject: [PATCH 21/72] feat: add hwb, lab, and lch generation and tests --- src/color.ts | 41 +++++++++++++++++++++++++++++++++++++++++ test/color.spec.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/color.ts b/src/color.ts index 206d6193852..ca2303258de 100644 --- a/src/color.ts +++ b/src/color.ts @@ -157,4 +157,45 @@ export class Color { hsla.push(this.faker.commerce.percentage(0.01)); return hsla; } + + /** + * Returns a HWB color. + * + * @example + * faker.color.hwb() // [201, 0.21, 0.31] + */ + hwb(): number[] { + return this.hsl(); + } + + /** + * Returns a LAB (CIELAB) color. + * + * @example + * faker.color.lab() // [0.8, -80, 100] + */ + lab(): number[] { + const lab = [this.faker.commerce.percentage(0.01)]; + for (let i = 0; i < 2; i++) { + lab.push(this.faker.datatype.number({ min: -100, max: 100 })); + } + return lab; + } + + /** + * Returns a LCH color. Even though upper bound of + * chroma in LCH color space is theoretically unbounded, + * it is bounded to 230 as anything above will not + * make a noticable difference in the browser. + * + * @example + * faker.color.lch() // [0.8, 230, 50] + */ + lch(): number[] { + const lch = [this.faker.commerce.percentage(0.01)]; + for (let i = 0; i < 2; i++) { + lch.push(this.faker.datatype.number({ min: 0, max: 230 })); + } + return lch; + } } diff --git a/test/color.spec.ts b/test/color.spec.ts index 8120f6bd74f..73c18eaea1c 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -200,6 +200,45 @@ describe('color', () => { }); }); }); + + describe(`hwb()`, () => { + it('should return a random hwb color in decimal format', () => { + const color = faker.color.hsl(); + expect(color).length(3); + expect(color[0]).toBeGreaterThanOrEqual(0); + expect(color[0]).toBeLessThanOrEqual(360); + color.slice(1).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(1); + }); + }); + }); + + describe(`lab()`, () => { + it('should return a random lab color in decimal format', () => { + const color = faker.color.lab(); + expect(color).length(3); + expect(color[0]).toBeGreaterThanOrEqual(0); + expect(color[0]).toBeLessThanOrEqual(1); + color.slice(1).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(-100); + expect(value).toBeLessThanOrEqual(100); + }); + }); + }); + + describe(`lch()`, () => { + it('should return a random lch color in decimal format', () => { + const color = faker.color.lch(); + expect(color).length(3); + expect(color[0]).toBeGreaterThanOrEqual(0); + expect(color[0]).toBeLessThanOrEqual(1); + color.slice(1).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(230); + }); + }); + }); } }); }); From ac581577a56ecb576ae73df0dbacbc8376d920bd Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 19:20:29 -0600 Subject: [PATCH 22/72] feat: add display p3 generation and test --- src/color.ts | 10 ++++++++++ test/color.spec.ts | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/color.ts b/src/color.ts index ca2303258de..61d6fcdf3eb 100644 --- a/src/color.ts +++ b/src/color.ts @@ -198,4 +198,14 @@ export class Color { } return lch; } + + /** + * Return a display-p3 color. + * + * @example + * faker.color.displayP3() // [0.93, 1, 0.82] + */ + displayP3(): number[] { + return [0, 0, 0].map(() => this.faker.commerce.percentage(0.01)); + } } diff --git a/test/color.spec.ts b/test/color.spec.ts index 73c18eaea1c..eafaa89d148 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -239,6 +239,17 @@ describe('color', () => { }); }); }); + + describe(`displayP3()`, () => { + it('should return a random display-p3 color in decimal format', () => { + const color = faker.color.displayP3(); + expect(color).length(3); + color.forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(1); + }); + }); + }); } }); }); From 4f5c9cb1fb6b00d77af61b07f8363a9c54f5bf4a Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 21:16:08 -0600 Subject: [PATCH 23/72] feat: add formating and options to rgb generation --- src/color.ts | 162 +++++++++++++++++++++++++++++---------------- test/color.spec.ts | 100 ++++++++++++---------------- 2 files changed, 148 insertions(+), 114 deletions(-) diff --git a/src/color.ts b/src/color.ts index 61d6fcdf3eb..bf0de06f563 100644 --- a/src/color.ts +++ b/src/color.ts @@ -1,5 +1,16 @@ import type { Faker } from '.'; +type ColorSpace = + | 'rgb' + | 'rgba' + | 'hsl' + | 'hsla' + | 'hwb' + | 'cmyk' + | 'lab' + | 'lch' + | 'display-p3'; + /** * Formats the hex format of a generated color string according * to options specified by user. @@ -9,7 +20,7 @@ import type { Faker } from '.'; * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. * @param options.case Letter case of the generated hex color. Defaults to `mixed`. */ -function applyHexFormatting( +function applyHexFormat( hexColor: string, options?: { prefix?: string; @@ -28,6 +39,68 @@ function applyHexFormatting( return hexColor; } +/** + * Converts an array of numbers into binary string format. + * + * @param values Array of values to be converted. + */ +function toBinary(values: number[]): string { + const binary: string[] = values.map((value: number) => { + const isFloat: boolean = Number(value) === value && value % 1 !== 0; + if (isFloat) { + const buffer: ArrayBuffer = new ArrayBuffer(4); + new DataView(buffer).setFloat32(0, value); + const bytes = new Uint8Array(buffer); + return toBinary(Array.from(bytes)).split(' ').join(''); + } + return (value >>> 0).toString(2).padStart(8, '0'); + }); + return binary.join(' '); +} + +/** + * Converts an array of numbers into CSS accepted format. + * + * @param values Array of values to be converted. + * @param colorSpace Color space to format CSS string for. + */ +function toCSS(values: number[], colorSpace: ColorSpace): string { + let css: string; + switch (colorSpace) { + case 'rgb': + css = `rgb(${values[0]}, ${values[1]}, ${values[2]})`; + break; + } + return css; +} + +/** + * Converts an array of color values to the specified color format. + * + * @param values Array of color values to be converted. + * @param format Format of generated RGB color. + * @param colorSpace Color space to format CSS string for. Defaults to `rgb`. + */ +function toColorFormat( + values: number[], + format: 'decimal' | 'css' | 'binary', + colorSpace?: ColorSpace +): string | number[] { + colorSpace = colorSpace || 'rgb'; + if (format === 'decimal') return values; + + let result: string | number[]; + switch (format) { + case 'css': + result = toCSS(values, colorSpace); + break; + case 'binary': + result = toBinary(values); + break; + } + return result; +} + /** * Module to generate colors. */ @@ -53,73 +126,46 @@ export class Color { } /** - * Returns a RGB color in hex format. + * Returns a RGB color. * * @param options options object. - * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. - * @param options.case Letter case of the generated hex color. Defaults to `mixed`. + * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. + * @param options.case Letter case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. + * @param options.format Format of generated RGB color. Defaults to `hex`. + * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example - * faker.color.rgbHex() // '0xffffFF' - * faker.color.rgbHex({ prefix: '#' }) // '#ffffFF' - * faker.color.rgbHex({ case: 'upper' }) // '0xFFFFFF' - * faker.color.rgbHex({ case: 'lower' }) // '0xffffff' - * faker.color.rgbHex({ prefix: '#', case: 'lower' }) // '#ffffff' + * faker.color.rgb() // '0xffffFF' + * faker.color.rgb({ prefix: '#' }) // '#ffffFF' + * faker.color.rgb({ case: 'upper' }) // '0xFFFFFF' + * faker.color.rgb({ case: 'lower' }) // '0xffffff' + * faker.color.rgb({ prefix: '#', case: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'decimal' }) // '[255, 255, 255]' + * faker.color.rgb({ format: 'css' }) // 'rgb(255, 0, 0)' + * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' + * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // '[255, 255, 255, 0.4]' */ - rgbHex(options?: { + rgb(options?: { prefix?: string; case?: 'upper' | 'lower' | 'mixed'; - }): string { - let color = this.faker.datatype.hexadecimal(6); - if (options) color = applyHexFormatting(color, options); - return color; - } + format?: 'hex' | 'decimal' | 'css' | 'binary'; + includeAlpha?: boolean; + }): string | number[] { + let color: string | number[]; + if (!options?.format) options = { ...options, format: 'hex' }; + if (options?.format === 'hex') { + color = this.faker.datatype.hexadecimal(options?.includeAlpha ? 8 : 6); + color = applyHexFormat(color, options); + return color; + } - /** - * Returns a RGB color in decimal format. - * - * @example - * faker.color.rgbNumeric() // '[255, 255, 255]' - */ - rgbNumeric(): number[] { - return [0, 0, 0].map(() => + color = [0, 0, 0].map(() => this.faker.datatype.number({ min: 0, max: 255 }) ); - } - - /** - * Return a RGBA color in hex format. - * - * @param options options object. - * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. - * @param options.case Letter case of the generated hex color. Defaults to `mixed`. - * - * @example - * faker.color.rgbaHex() // '0xffffFF00' - * faker.color.rgbaHex({ prefix: '#' }) // '#ffffFF00' - * faker.color.rgbaHex({ case: 'upper' }) // '0xFFFFFF00' - * faker.color.rgbaHex({ case: 'lower' }) // '0xffffff00' - * faker.color.rgbaHex({ prefix: '#', case: 'lower' }) // '#ffffff00' - */ - rgbaHex(options?: { - prefix?: string; - case?: 'upper' | 'lower' | 'mixed'; - }): string { - let color = this.faker.datatype.hexadecimal(8); - if (options) color = applyHexFormatting(color, options); - return color; - } - - /** - * Returns a RGBA color in decimal format. - * - * @example - * faker.color.rgbaNumeric() // '[255, 255, 255, 0.54]' - */ - rgbaNumeric(): number[] { - const rgba: number[] = this.faker.color.rgbNumeric(); - rgba.push(this.faker.commerce.percentage(0.01)); - return rgba; + if (options?.includeAlpha) { + color.push(this.faker.commerce.percentage(0.01)); + } + return toColorFormat(color, options.format, 'rgb'); } /** diff --git a/test/color.spec.ts b/test/color.spec.ts index eafaa89d148..4830f9c8d9f 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -6,10 +6,7 @@ const seededRuns = [ seed: 42, expectations: { human: 'grey', - rgbHex: '0x8BE4AB', - rgbaHex: '0x8BE4ABdd', - rgbNumeric: [95, 203, 243], - rgbaNumeric: [95, 203, 243, 0.18], + rgb: '0x8BE4AB', hsl: [135, 0.8, 0.96], hsla: [135, 0.8, 0.96, 0.18], }, @@ -18,10 +15,7 @@ const seededRuns = [ seed: 1337, expectations: { human: 'black', - rgbHex: '0x5c346b', - rgbaHex: '0x5c346ba0', - rgbNumeric: [67, 143, 40], - rgbaNumeric: [67, 143, 40, 0.21], + rgb: '0x5c346b', hsl: [94, 0.56, 0.16], hsla: [94, 0.56, 0.16, 0.21], }, @@ -30,10 +24,7 @@ const seededRuns = [ seed: 1211, expectations: { human: 'azure', - rgbHex: '0xEaDB42', - rgbaHex: '0xEaDB42F0', - rgbNumeric: [237, 117, 228], - rgbaNumeric: [237, 117, 228, 0.78], + rgb: '0xEaDB42', hsl: [335, 0.46, 0.9], hsla: [335, 0.46, 0.9, 0.78], }, @@ -42,15 +33,7 @@ const seededRuns = [ const NON_SEEDED_BASED_RUN = 5; -const functionNames = [ - 'human', - 'rgbHex', - 'rgbaHex', - 'rgbNumeric', - 'rgbaNumeric', - 'hsl', - 'hsla', -]; +const functionNames = ['human', 'rgb', 'hsl', 'hsla']; describe('color', () => { afterEach(() => { @@ -84,83 +67,88 @@ describe('color', () => { }); }); - describe(`rgbHex()`, () => { + describe(`rgb()`, () => { it('should return a random rgb hex color', () => { - const color = faker.color.rgbHex(); + const color = faker.color.rgb(); expect(color).match(/^(0x[a-fA-F0-9]{6})$/); }); }); - describe(`rgbHex({ prefix: '#' })`, () => { + describe(`rgb({ prefix: '#' })`, () => { it('should return a random rgb hex color with # prefix', () => { - const color = faker.color.rgbHex({ prefix: '#' }); + const color = faker.color.rgb({ prefix: '#' }); expect(color).match(/^(#[a-fA-F0-9]{6})$/); }); }); describe(`rgbHex({ prefix: '#', case: 'lower' })`, () => { it('should return a random rgb hex color with # prefix and lower case only', () => { - const color = faker.color.rgbHex({ prefix: '#', case: 'lower' }); + const color = faker.color.rgb({ prefix: '#', case: 'lower' }); expect(color).match(/^(#[a-f0-9]{6})$/); }); }); - describe(`rgbHex({ prefix: '#', case: 'upper' })`, () => { + describe(`rgb({ prefix: '#', case: 'upper' })`, () => { it('should return a random rgb hex color with # prefix and upper case only', () => { - const color = faker.color.rgbHex({ prefix: '#', case: 'upper' }); + const color = faker.color.rgb({ prefix: '#', case: 'upper' }); expect(color).match(/^(#[A-F0-9]{6})$/); }); }); - describe(`rgbNumeric()`, () => { + describe(`rgb({ format: 'decimal' })`, () => { it('should return a random rgb color in decimal format', () => { - const color = faker.color.rgbNumeric(); + const color = faker.color.rgb({ format: 'decimal' }); expect(color).length(3); - color.forEach((value: number) => { + (color as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(255); }); }); }); - describe(`rgbaHex()`, () => { - it('should return a random rgba hex color', () => { - const color = faker.color.rgbaHex(); - expect(color).match(/^(0x[a-fA-F0-9]{8})$/); + describe(`rgb({ format: 'css' })`, () => { + it('should return a random rgb color in css format', () => { + const color = faker.color.rgb({ format: 'css' }); + expect(color).match(/^(rgb\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}\))$/); }); }); - describe(`rgbaHex({ prefix: '#' })`, () => { - it('should return a random rgba hex color with # prefix', () => { - const color = faker.color.rgbaHex({ prefix: '#' }); - expect(color).match(/^(#[a-fA-F0-9]{8})$/); + describe(`rgb({ format: 'binary' })`, () => { + it('should return a random rgb color in binary format', () => { + const color = faker.color.rgb({ format: 'binary' }); + expect(color).match(/^([01]{8} [01]{8} [01]{8})$/); }); }); - describe(`rgbaHex({ prefix: '#', case: 'lower' })`, () => { - it('should return a random rgb hex color with # prefix and lower case only', () => { - const color = faker.color.rgbaHex({ prefix: '#', case: 'lower' }); - expect(color).match(/^(#[a-f0-9]{8})$/); + describe(`rgb({ includeAlpha: true })`, () => { + it('should return a random rgb color in hex format with alpha value', () => { + const color = faker.color.rgb({ includeAlpha: true }); + expect(color).match(/^(0x[a-fA-F0-9]{8})$/); }); }); - describe(`rgbaHex({ prefix: '#', case: 'upper' })`, () => { - it('should return a random rgba hex color with # prefix and upper case only', () => { - const color = faker.color.rgbaHex({ prefix: '#', case: 'upper' }); - expect(color).match(/^(#[A-F0-9]{8})$/); + describe(`rgb({ format: 'decimal', includeAlpha: true })`, () => { + it('should return a random rgb color in hex format with alpha value', () => { + const color = faker.color.rgb({ + format: 'decimal', + includeAlpha: true, + }); + expect(color[color.length - 1]).toBeGreaterThanOrEqual(0); + expect(color[color.length - 1]).toBeLessThanOrEqual(1); + (color.slice(0, 4) as number[]).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(255); + }); }); }); - describe(`rgbaNumeric()`, () => { - it('should return a random rgba color in decimal format', () => { - const color = faker.color.rgbaNumeric(); - expect(color).length(4); - color.slice(0, 3).forEach((value: number) => { - expect(value).toBeGreaterThanOrEqual(0); - expect(value).toBeLessThanOrEqual(255); + describe(`rgb({ format: 'binary', includeAlpha: true })`, () => { + it('should return a random rgb color in binary format with alpha value', () => { + const color = faker.color.rgb({ + format: 'binary', + includeAlpha: true, }); - expect(color[color.length - 1]).toBeGreaterThanOrEqual(0); - expect(color[color.length - 1]).toBeLessThanOrEqual(1); + expect(color).match(/^([01]{8} [01]{8} [01]{8} [01]{32})$/); }); }); From ed68ec7da7c244553266f492dde889bc96e3c483 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 21:57:23 -0600 Subject: [PATCH 24/72] feat: add formatting options to cmyk generation --- src/color.ts | 25 ++++++++++++++++++++++--- test/color.spec.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/color.ts b/src/color.ts index bf0de06f563..b913eede28b 100644 --- a/src/color.ts +++ b/src/color.ts @@ -70,6 +70,13 @@ function toCSS(values: number[], colorSpace: ColorSpace): string { case 'rgb': css = `rgb(${values[0]}, ${values[1]}, ${values[2]})`; break; + case 'rgba': + css = `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`; + break; + case 'cmyk': + values = values.map((value: number) => Math.round(value * 100)); + css = `cmyk(${values[0]}%, ${values[1]}%, ${values[2]}%, ${values[3]}%)`; + break; } return css; } @@ -152,6 +159,7 @@ export class Color { includeAlpha?: boolean; }): string | number[] { let color: string | number[]; + let colorSpace: ColorSpace = 'rgb'; if (!options?.format) options = { ...options, format: 'hex' }; if (options?.format === 'hex') { color = this.faker.datatype.hexadecimal(options?.includeAlpha ? 8 : 6); @@ -164,18 +172,29 @@ export class Color { ); if (options?.includeAlpha) { color.push(this.faker.commerce.percentage(0.01)); + colorSpace = 'rgba'; } - return toColorFormat(color, options.format, 'rgb'); + return toColorFormat(color, options.format, colorSpace); } /** * Returns a CMYK color. * + * @param options options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * * @example * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] + * faker.color.cmyk({ format: 'decimal' }) // [0.31, 0.52, 0.32, 0.43] + * faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%) + * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 */ - cmyk(): number[] { - return [0, 0, 0, 0].map(() => this.faker.commerce.percentage(0.01)); + cmyk(options?: { format?: 'decimal' | 'css' | 'binary' }): string | number[] { + const color: string | number[] = [0, 0, 0, 0].map(() => + this.faker.commerce.percentage(0.01) + ); + + return toColorFormat(color, options?.format || 'decimal', 'cmyk'); } /** diff --git a/test/color.spec.ts b/test/color.spec.ts index 4830f9c8d9f..4b827c6cb0d 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -148,7 +148,16 @@ describe('color', () => { format: 'binary', includeAlpha: true, }); - expect(color).match(/^([01]{8} [01]{8} [01]{8} [01]{32})$/); + expect(color).match(/^([01]{8} [01]{8} [01]{8} [01]{8,32})$/); + }); + }); + + describe(`rgb({ format: 'css', includeAlpha: true })`, () => { + it('should return a random rgb color in css format with alpha value', () => { + const color = faker.color.rgb({ format: 'css', includeAlpha: true }); + expect(color).match( + /^(rgba\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, \d*\.?\d*\))$/ + ); }); }); @@ -156,13 +165,42 @@ describe('color', () => { it('should return a random cmyk color', () => { const color = faker.color.cmyk(); expect(color).length(4); - color.forEach((value: number) => { + (color as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); }); }); + describe(`cmyk({ format: 'decimal' })`, () => { + it('should return a random cmyk color in decimal format', () => { + const color = faker.color.cmyk({ format: 'decimal' }); + expect(color).length(4); + (color as number[]).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(1); + }); + }); + }); + + describe(`cmyk({ format: 'css' })`, () => { + it('should return a random cmyk color in css format', () => { + const color = faker.color.cmyk({ format: 'css' }); + expect(color).match( + /^(cmyk\([0-9]{1,3}%, [0-9]{1,3}%, [0-9]{1,3}%, [0-9]{1,3}%\))$/ + ); + }); + }); + + describe(`cmyk({ format: 'binary' })`, () => { + it('should return a random cmyk color in binary format', () => { + const color = faker.color.cmyk({ format: 'binary' }); + expect(color).match( + /^([01]{8,32} [01]{8,32} [01]{8,32} [01]{8,32})$/ + ); + }); + }); + describe(`hsl()`, () => { it('should return a random hsl color in decimal format', () => { const color = faker.color.hsl(); From f66fd4c1f628ccb07ef772e89712786997f3db45 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 23:13:36 -0600 Subject: [PATCH 25/72] feat: add formatting options to hsl generator --- src/color.ts | 47 +++++++++++++++++++++++++++----------------- test/color.spec.ts | 49 +++++++++++++++++++++++++++++++++------------- 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/color.ts b/src/color.ts index b913eede28b..cd308aece88 100644 --- a/src/color.ts +++ b/src/color.ts @@ -66,6 +66,7 @@ function toBinary(values: number[]): string { */ function toCSS(values: number[], colorSpace: ColorSpace): string { let css: string; + const percentages = values.map((value: number) => Math.round(value * 100)); switch (colorSpace) { case 'rgb': css = `rgb(${values[0]}, ${values[1]}, ${values[2]})`; @@ -74,8 +75,13 @@ function toCSS(values: number[], colorSpace: ColorSpace): string { css = `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`; break; case 'cmyk': - values = values.map((value: number) => Math.round(value * 100)); - css = `cmyk(${values[0]}%, ${values[1]}%, ${values[2]}%, ${values[3]}%)`; + css = `cmyk(${percentages[0]}%, ${percentages[1]}%, ${percentages[2]}%, ${percentages[3]}%)`; + break; + case 'hsl': + css = `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}%)`; + break; + case 'hsla': + css = `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}% / ${percentages[3]})`; break; } return css; @@ -200,27 +206,32 @@ export class Color { /** * Returns a HSL color. * + * @param options options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. + * * @example * faker.color.hsl() // [201, 0.23, 0.32] + * faker.color.hsl({ format: 'decimal' }) // [300, 0.21, 0.52] + * faker.color.hsl({ format: 'decimal', includeAlpha: true }) // [300, 0.21, 0.52, 0.28] + * faker.color.hsl({ format: 'css' }) // hsl(0deg, 100%, 80%) + * faker.color.hsl({ format: 'css', includeAlpha: true }) // hsl(0deg 100% 50% / 0.5) + * faker.color.hsl({ format: 'binary' }) // (8-32 bits) x 3 + * faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4 */ - hsl(): number[] { + hsl(options?: { + format?: 'decimal' | 'css' | 'binary'; + includeAlpha?: boolean; + }): string | number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; - for (let i = 0; i < 2; i++) { + for (let i = 0; i < (options?.includeAlpha ? 3 : 2); i++) { hsl.push(this.faker.commerce.percentage(0.01)); } - return hsl; - } - - /** - * Returns a HSLA color. - * - * @example - * faker.color.hsla() // [201, 0.21, 0.31, 0.11] - */ - hsla(): number[] { - const hsla: number[] = this.faker.color.hsl(); - hsla.push(this.faker.commerce.percentage(0.01)); - return hsla; + return toColorFormat( + hsl, + options?.format || 'decimal', + options?.includeAlpha ? 'hsla' : 'hsl' + ); } /** @@ -229,7 +240,7 @@ export class Color { * @example * faker.color.hwb() // [201, 0.21, 0.31] */ - hwb(): number[] { + hwb(): string | number[] { return this.hsl(); } diff --git a/test/color.spec.ts b/test/color.spec.ts index 4b827c6cb0d..00dc1513548 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -8,7 +8,6 @@ const seededRuns = [ human: 'grey', rgb: '0x8BE4AB', hsl: [135, 0.8, 0.96], - hsla: [135, 0.8, 0.96, 0.18], }, }, { @@ -17,7 +16,6 @@ const seededRuns = [ human: 'black', rgb: '0x5c346b', hsl: [94, 0.56, 0.16], - hsla: [94, 0.56, 0.16, 0.21], }, }, { @@ -26,14 +24,13 @@ const seededRuns = [ human: 'azure', rgb: '0xEaDB42', hsl: [335, 0.46, 0.9], - hsla: [335, 0.46, 0.9, 0.78], }, }, ]; const NON_SEEDED_BASED_RUN = 5; -const functionNames = ['human', 'rgb', 'hsl', 'hsla']; +const functionNames = ['human', 'rgb', 'hsl']; describe('color', () => { afterEach(() => { @@ -207,23 +204,47 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(360); - color.slice(1).forEach((value: number) => { + (color.slice(1) as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); }); }); - describe(`hsla()`, () => { - it('should return a random hsla color in decimal format', () => { - const color = faker.color.hsla(); - expect(color).length(4); - expect(color[0]).toBeGreaterThanOrEqual(0); - expect(color[0]).toBeLessThanOrEqual(360); - color.slice(1).forEach((value: number) => { - expect(value).toBeGreaterThanOrEqual(0); - expect(value).toBeLessThanOrEqual(1); + describe(`hsl({ format: 'css' })`, () => { + it('should return a random hsl color in css format', () => { + const color = faker.color.hsl({ format: 'css' }); + expect(color).match( + /^(hsl\([0-9]{1,3}deg [0-9]{1,3}% [0-9]{1,3}%\))$/ + ); + }); + }); + + describe(`hsl({ format: 'css', includeAlpha: true })`, () => { + it('should return a random hsl color in css format with an alpha value', () => { + const color = faker.color.hsl({ format: 'css', includeAlpha: true }); + expect(color).match( + /^(hsl\([0-9]{1,3}deg [0-9]{1,3}% [0-9]{1,3}% \/ \d*\.?\d*\))$/ + ); + }); + }); + + describe(`hsl({ format: 'binary' })`, () => { + it('should return a random hsl color in binary format', () => { + const color = faker.color.hsl({ format: 'binary' }); + expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + }); + }); + + describe(`hsl({ format: 'binary', includeAlpha: true })`, () => { + it('should return a random hsl color in binary format with an alpha value', () => { + const color = faker.color.hsl({ + format: 'binary', + includeAlpha: true, }); + expect(color).match( + /^([01]{8,32} [01]{8,32} [01]{8,32} [01]{8,32})$/ + ); }); }); From 32e10564c2f3f2bc701bd91210457d86c6fc58b7 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Fri, 8 Apr 2022 23:21:42 -0600 Subject: [PATCH 26/72] feat: add formatting options to hwb generator --- src/color.ts | 21 +++++++++++++++++---- test/color.spec.ts | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/color.ts b/src/color.ts index cd308aece88..24284ab9cd1 100644 --- a/src/color.ts +++ b/src/color.ts @@ -83,6 +83,9 @@ function toCSS(values: number[], colorSpace: ColorSpace): string { case 'hsla': css = `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}% / ${percentages[3]})`; break; + case 'hwb': + css = `hwb(${values[0]} ${percentages[1]}% ${percentages[2]}%)`; + break; } return css; } @@ -187,7 +190,7 @@ export class Color { * Returns a CMYK color. * * @param options options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated CMYK color. Defaults to `decimal`. * * @example * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] @@ -207,7 +210,7 @@ export class Color { * Returns a HSL color. * * @param options options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated HSL color. Defaults to `decimal`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example @@ -237,11 +240,21 @@ export class Color { /** * Returns a HWB color. * + * @param options options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * * @example * faker.color.hwb() // [201, 0.21, 0.31] + * faker.color.hwb({ format: 'decimal' }) // [201, 0.21, 0.31] + * faker.color.hwb({ format: 'css' }) // hwb(194 0% 0%) + * faker.color.hwb({ format: 'binary' }) // (8-32 bits x 3) */ - hwb(): string | number[] { - return this.hsl(); + hwb(options?: { format?: 'decimal' | 'css' | 'binary' }): string | number[] { + const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; + for (let i = 0; i < 2; i++) { + hsl.push(this.faker.commerce.percentage(0.01)); + } + return toColorFormat(hsl, options?.format || 'decimal', 'hwb'); } /** diff --git a/test/color.spec.ts b/test/color.spec.ts index 00dc1513548..bed82e8fc61 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -8,6 +8,7 @@ const seededRuns = [ human: 'grey', rgb: '0x8BE4AB', hsl: [135, 0.8, 0.96], + hwb: [135, 0.8, 0.96], }, }, { @@ -16,6 +17,7 @@ const seededRuns = [ human: 'black', rgb: '0x5c346b', hsl: [94, 0.56, 0.16], + hwb: [94, 0.56, 0.16], }, }, { @@ -24,13 +26,14 @@ const seededRuns = [ human: 'azure', rgb: '0xEaDB42', hsl: [335, 0.46, 0.9], + hwb: [335, 0.46, 0.9], }, }, ]; const NON_SEEDED_BASED_RUN = 5; -const functionNames = ['human', 'rgb', 'hsl']; +const functionNames = ['human', 'rgb', 'hsl', 'hwb']; describe('color', () => { afterEach(() => { @@ -250,17 +253,44 @@ describe('color', () => { describe(`hwb()`, () => { it('should return a random hwb color in decimal format', () => { - const color = faker.color.hsl(); + const color = faker.color.hwb(); expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(360); - color.slice(1).forEach((value: number) => { + (color.slice(1) as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); }); }); + describe(`hwb({ format: 'decimal' })`, () => { + it('should return a random hwb color in decimal format', () => { + const color = faker.color.hwb(); + expect(color).length(3); + expect(color[0]).toBeGreaterThanOrEqual(0); + expect(color[0]).toBeLessThanOrEqual(360); + (color.slice(1) as number[]).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(1); + }); + }); + }); + + describe(`hwb({ format: 'css' })`, () => { + it('should return a random hwb color in css format', () => { + const color = faker.color.hwb({ format: 'css' }); + expect(color).match(/^(hwb\([0-9]{1,3} [0-9]{1,3}% [0-9]{1,3}%\))$/); + }); + }); + + describe(`hwb({ format: 'binary' })`, () => { + it('should return a random hwb color in binary format', () => { + const color = faker.color.hwb({ format: 'binary' }); + expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + }); + }); + describe(`lab()`, () => { it('should return a random lab color in decimal format', () => { const color = faker.color.lab(); From 4a71385369130ac75c3c99698587f2bdd9dc95e4 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 9 Apr 2022 12:57:12 -0600 Subject: [PATCH 27/72] feat: add formatting options for lab, lch, and display-p3 generators --- src/color.ts | 58 +++++++++++++++++++++++------ test/color.spec.ts | 91 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 134 insertions(+), 15 deletions(-) diff --git a/src/color.ts b/src/color.ts index 24284ab9cd1..d594256219d 100644 --- a/src/color.ts +++ b/src/color.ts @@ -86,6 +86,15 @@ function toCSS(values: number[], colorSpace: ColorSpace): string { case 'hwb': css = `hwb(${values[0]} ${percentages[1]}% ${percentages[2]}%)`; break; + case 'lab': + css = `lab(${percentages[0]}% ${values[1]} ${values[2]})`; + break; + case 'lch': + css = `lch(${percentages[0]}% ${values[1]} ${values[2]})`; + break; + case 'display-p3': + css = `color(display-p3 ${values[0]} ${values[1]} ${values[2]})`; + break; } return css; } @@ -260,15 +269,23 @@ export class Color { /** * Returns a LAB (CIELAB) color. * + * @param options options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * * @example - * faker.color.lab() // [0.8, -80, 100] + * faker.color.lab() // [0.832133, -80.3245, 100.1234] + * faker.color.lab({ format: 'decimal' }) // [0.856773, -80.2345, 100.2341] + * faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664) + * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) */ - lab(): number[] { - const lab = [this.faker.commerce.percentage(0.01)]; + lab(options?: { format?: 'decimal' | 'css' | 'binary' }): string | number[] { + const lab = [this.faker.commerce.percentage(0.000001)]; for (let i = 0; i < 2; i++) { - lab.push(this.faker.datatype.number({ min: -100, max: 100 })); + lab.push( + this.faker.datatype.float({ min: -100, max: 100, precision: 0.0001 }) + ); } - return lab; + return toColorFormat(lab, options?.format || 'decimal', 'lab'); } /** @@ -277,24 +294,41 @@ export class Color { * it is bounded to 230 as anything above will not * make a noticable difference in the browser. * + * @param options options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * * @example - * faker.color.lch() // [0.8, 230, 50] + * faker.color.lch() // [0.522345, 72.2, 56.2] + * faker.color.lch{ format: 'decimal' }) // [0.522345, 72.2, 56.2] + * faker.color.lch{ format: 'css' }) // lch(52.2345% 72.2 56.2) + * faker.color.lch{ format: 'binary' }) // (8-32 bits x 3) */ - lch(): number[] { - const lch = [this.faker.commerce.percentage(0.01)]; + lch(options?: { format?: 'decimal' | 'css' | 'binary' }): string | number[] { + const lch = [this.faker.commerce.percentage(0.000001)]; for (let i = 0; i < 2; i++) { - lch.push(this.faker.datatype.number({ min: 0, max: 230 })); + lch.push( + this.faker.datatype.number({ min: 0, max: 230, precision: 0.1 }) + ); } - return lch; + return toColorFormat(lch, options?.format || 'decimal', 'lch'); } /** * Return a display-p3 color. * + * @param options options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * * @example * faker.color.displayP3() // [0.93, 1, 0.82] + * faker.color.displayP3({ format: 'decimal' }) // [0.12, 0.21, 0.31] + * faker.color.displayP3({ format: 'css' }) // color(display-p3 0.12 1 0.23) + * faker.color.displayP3({ format: 'binary' }) // (8-32 bits x 3) */ - displayP3(): number[] { - return [0, 0, 0].map(() => this.faker.commerce.percentage(0.01)); + displayP3(options?: { + format?: 'decimal' | 'css' | 'binary'; + }): string | number[] { + const p3 = [0, 0, 0].map(() => this.faker.commerce.percentage(0.01)); + return toColorFormat(p3, options?.format || 'decimal', 'display-p3'); } } diff --git a/test/color.spec.ts b/test/color.spec.ts index bed82e8fc61..8adcce42fa8 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -297,36 +297,121 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(1); - color.slice(1).forEach((value: number) => { + (color.slice(1) as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(-100); expect(value).toBeLessThanOrEqual(100); }); }); }); + describe(`lab({ format: 'decimal' })`, () => { + it('should return a random lab color in decimal format', () => { + const color = faker.color.lab({ format: 'decimal' }); + expect(color).length(3); + expect(color[0]).toBeGreaterThanOrEqual(0); + expect(color[0]).toBeLessThanOrEqual(1); + (color.slice(1) as number[]).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(-100); + expect(value).toBeLessThanOrEqual(100); + }); + }); + }); + + describe(`lab({ format: 'css' })`, () => { + it('should return a random lab color in css format', () => { + const color = faker.color.lab({ format: 'css' }); + expect(color).match( + /^(lab\((\d*\.?\d*|[0-9]{1,3})% -?\d*\.?\d* -?\d*\.?\d*\))$/ + ); + }); + }); + + describe(`lab({ format: 'binary' })`, () => { + it('should return a random lab color in binary format', () => { + const color = faker.color.lab({ format: 'binary' }); + expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + }); + }); + describe(`lch()`, () => { it('should return a random lch color in decimal format', () => { const color = faker.color.lch(); expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(1); - color.slice(1).forEach((value: number) => { + (color.slice(1) as number[]).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(230); + }); + }); + }); + + describe(`lch({ format: 'decimal' })`, () => { + it('should return a random lch color in decimal format', () => { + const color = faker.color.lch({ format: 'decimal' }); + expect(color).length(3); + expect(color[0]).toBeGreaterThanOrEqual(0); + expect(color[0]).toBeLessThanOrEqual(1); + (color.slice(1) as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(230); }); }); }); + describe(`lch({ format: 'css' })`, () => { + it('should return a random lch color in css format', () => { + const color = faker.color.lch({ format: 'css' }); + expect(color).match( + /^(lch\((\d*\.?\d*|[0-9]{1,3})% \d*\.?\d* \d*\.?\d*\))$/ + ); + }); + }); + + describe(`lch({ format: 'binary' })`, () => { + it('should return a random lch color in binary format', () => { + const color = faker.color.lch({ format: 'binary' }); + expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + }); + }); + describe(`displayP3()`, () => { it('should return a random display-p3 color in decimal format', () => { const color = faker.color.displayP3(); expect(color).length(3); - color.forEach((value: number) => { + (color as number[]).forEach((value: number) => { + expect(value).toBeGreaterThanOrEqual(0); + expect(value).toBeLessThanOrEqual(1); + }); + }); + }); + + describe(`displayP3({ format: 'decimal' })`, () => { + it('should return a random displayP3 color in decimal format', () => { + const color = faker.color.displayP3({ format: 'decimal' }); + expect(color).length(3); + (color as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); }); }); + + describe(`displayP3({ format: 'css' })`, () => { + it('should return a random displayP3 color in css format', () => { + const color = faker.color.displayP3({ format: 'css' }); + expect(color).match( + /^color\(display-p3 \d*\.?\d* \d*\.?\d* \d*\.?\d*\)$/ + ); + }); + }); + + describe(`displayP3({ format: 'binary' })`, () => { + it('should return a random displayP3 color in binary format', () => { + const color = faker.color.displayP3({ format: 'binary' }); + expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); + }); + }); } }); }); From 9e26b3b384a61cade64df3da945787c3c87d9169 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 9 Apr 2022 13:13:07 -0600 Subject: [PATCH 28/72] feat: add seeded tests for all functions --- test/color.spec.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/color.spec.ts b/test/color.spec.ts index 8adcce42fa8..04f0e1d9ef7 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -9,6 +9,10 @@ const seededRuns = [ rgb: '0x8BE4AB', hsl: [135, 0.8, 0.96], hwb: [135, 0.8, 0.96], + cmyk: [0.37, 0.8, 0.96, 0.18], + lab: [0.37454, 59.3086, 90.1429], + lch: [0.37454, 183.2, 218.7], + displayP3: [0.37, 0.8, 0.96], }, }, { @@ -18,6 +22,10 @@ const seededRuns = [ rgb: '0x5c346b', hsl: [94, 0.56, 0.16], hwb: [94, 0.56, 0.16], + cmyk: [0.26, 0.56, 0.16, 0.21], + lab: [0.262024, 12.106, -68.2632], + lch: [0.262024, 128.9, 36.5], + displayP3: [0.26, 0.56, 0.16], }, }, { @@ -27,13 +35,26 @@ const seededRuns = [ rgb: '0xEaDB42', hsl: [335, 0.46, 0.9], hwb: [335, 0.46, 0.9], + cmyk: [0.93, 0.46, 0.9, 0.78], + lab: [0.928521, -8.197, 78.6944], + lch: [0.928521, 105.6, 205.5], + displayP3: [0.93, 0.46, 0.9], }, }, ]; const NON_SEEDED_BASED_RUN = 5; -const functionNames = ['human', 'rgb', 'hsl', 'hwb']; +const functionNames = [ + 'human', + 'rgb', + 'hsl', + 'hwb', + 'cmyk', + 'lab', + 'lch', + 'displayP3', +]; describe('color', () => { afterEach(() => { From c67643150c11593126e9b2be7442d4c0bbf41c9c Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 9 Apr 2022 13:36:39 -0600 Subject: [PATCH 29/72] feat: add random color gamut name generator and test --- src/color.ts | 14 +++++++++- src/definitions/color.ts | 6 +++- src/locales/en/color/color_gamut.ts | 43 +++++++++++++++++++++++++++++ src/locales/en/color/index.ts | 2 ++ test/color.spec.ts | 11 ++++++++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/locales/en/color/color_gamut.ts diff --git a/src/color.ts b/src/color.ts index d594256219d..1808e94030d 100644 --- a/src/color.ts +++ b/src/color.ts @@ -141,7 +141,7 @@ export class Color { } /** - * Returns a human readable color name. + * Returns a random human readable color name. * * @example * faker.color.human() // 'red' @@ -150,6 +150,18 @@ export class Color { return this.faker.random.arrayElement(this.faker.definitions.color.human); } + /** + * Returns a random color gamut name. + * + * @example + * faker.color.colorGamut() // 'sRGB' + */ + colorGamut(): string { + return this.faker.random.arrayElement( + this.faker.definitions.color.colorGamut + ); + } + /** * Returns a RGB color. * diff --git a/src/definitions/color.ts b/src/definitions/color.ts index ad56d21dab8..05536d1b84e 100644 --- a/src/definitions/color.ts +++ b/src/definitions/color.ts @@ -8,9 +8,13 @@ export interface ColorDefinitions { * Human readable color names */ human: string[]; + /** + * Color gamut names. + */ + colorGamut: string[]; } /** * Internal: A list of all keys for the ColorDefinitions. */ -export const COLOR = allOf()('human'); +export const COLOR = allOf()('human', 'colorGamut'); diff --git a/src/locales/en/color/color_gamut.ts b/src/locales/en/color/color_gamut.ts new file mode 100644 index 00000000000..2fe91419aed --- /dev/null +++ b/src/locales/en/color/color_gamut.ts @@ -0,0 +1,43 @@ +export default [ + 'CIE 1931 XYZ', + 'CIEUVW', + 'Uniform Color Spaces (UCSs)', + 'CIELUV', + 'CIELAB', + 'HSLuv', + 'sRGB', + 'Adobe RGB', + 'Adobe Wide Gamut RGB', + 'Rec. 2100', + 'ProPhoto RGB Color Space', + 'scRGB', + 'DCI-P3', + 'Display-P3', + 'Rec. 601', + 'Rec. 709', + 'Academy Color Encoding System (ACES)', + 'Rec. 2020', + 'YPbPr', + 'YDbDr', + 'YIQ', + 'xvYCC', + 'sYCC', + 'HSV', + 'HSL', + 'HWB', + 'RGBA', + 'HSLA', + 'LCh', + 'CMY', + 'CMYK', + 'Munsell Color System', + 'Natural Color System (NSC)', + 'Pantone Matching System (PMS)', + 'RAL', + 'Federal Standard 595C', + 'British Standard Colour (BS)', + 'HKS', + 'LMS', + 'RG', + 'RGK', +]; diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index 79cc39f02f3..f94bb761183 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -3,10 +3,12 @@ * Run 'pnpm run generate:locales' to update. */ import type { ColorDefinitions } from '../../..'; +import colorGamut from './color_gamut'; import human from './human'; const color: ColorDefinitions = { human, + colorGamut, }; export default color; diff --git a/test/color.spec.ts b/test/color.spec.ts index 04f0e1d9ef7..0c6bb15cd51 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -6,6 +6,7 @@ const seededRuns = [ seed: 42, expectations: { human: 'grey', + colorGamut: 'Rec. 709', rgb: '0x8BE4AB', hsl: [135, 0.8, 0.96], hwb: [135, 0.8, 0.96], @@ -19,6 +20,7 @@ const seededRuns = [ seed: 1337, expectations: { human: 'black', + colorGamut: 'ProPhoto RGB Color Space', rgb: '0x5c346b', hsl: [94, 0.56, 0.16], hwb: [94, 0.56, 0.16], @@ -32,6 +34,7 @@ const seededRuns = [ seed: 1211, expectations: { human: 'azure', + colorGamut: 'LMS', rgb: '0xEaDB42', hsl: [335, 0.46, 0.9], hwb: [335, 0.46, 0.9], @@ -47,6 +50,7 @@ const NON_SEEDED_BASED_RUN = 5; const functionNames = [ 'human', + 'colorGamut', 'rgb', 'hsl', 'hwb', @@ -88,6 +92,13 @@ describe('color', () => { }); }); + describe(`colorGamut()`, () => { + it('should return random color gamut from color gamut array', () => { + const gamut = faker.color.colorGamut(); + expect(faker.definitions.color.colorGamut).toContain(gamut); + }); + }); + describe(`rgb()`, () => { it('should return a random rgb hex color', () => { const color = faker.color.rgb(); From 643d25eaa1c743379d8b955454b4a6acf0bf6227 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 9 Apr 2022 13:40:31 -0600 Subject: [PATCH 30/72] fix: return css strings from switch cases and add default rgb case --- src/color.ts | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/color.ts b/src/color.ts index 1808e94030d..4df64e4e2e6 100644 --- a/src/color.ts +++ b/src/color.ts @@ -62,41 +62,33 @@ function toBinary(values: number[]): string { * Converts an array of numbers into CSS accepted format. * * @param values Array of values to be converted. - * @param colorSpace Color space to format CSS string for. + * @param colorSpace Color space to format CSS string for. If invalid color space + * is provided, RGB CSS will be returned. */ function toCSS(values: number[], colorSpace: ColorSpace): string { - let css: string; const percentages = values.map((value: number) => Math.round(value * 100)); switch (colorSpace) { case 'rgb': - css = `rgb(${values[0]}, ${values[1]}, ${values[2]})`; - break; + return `rgb(${values[0]}, ${values[1]}, ${values[2]})`; case 'rgba': - css = `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`; - break; + return `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`; case 'cmyk': - css = `cmyk(${percentages[0]}%, ${percentages[1]}%, ${percentages[2]}%, ${percentages[3]}%)`; - break; + return `cmyk(${percentages[0]}%, ${percentages[1]}%, ${percentages[2]}%, ${percentages[3]}%)`; case 'hsl': - css = `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}%)`; - break; + return `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}%)`; case 'hsla': - css = `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}% / ${percentages[3]})`; - break; + return `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}% / ${percentages[3]})`; case 'hwb': - css = `hwb(${values[0]} ${percentages[1]}% ${percentages[2]}%)`; - break; + return `hwb(${values[0]} ${percentages[1]}% ${percentages[2]}%)`; case 'lab': - css = `lab(${percentages[0]}% ${values[1]} ${values[2]})`; - break; + return `lab(${percentages[0]}% ${values[1]} ${values[2]})`; case 'lch': - css = `lch(${percentages[0]}% ${values[1]} ${values[2]})`; - break; + return `lch(${percentages[0]}% ${values[1]} ${values[2]})`; case 'display-p3': - css = `color(display-p3 ${values[0]} ${values[1]} ${values[2]})`; - break; + return `color(display-p3 ${values[0]} ${values[1]} ${values[2]})`; + default: + return toCSS(values, 'rgb'); } - return css; } /** From 579f01505fc576cc78f48a5355d8ec125b426791 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 9 Apr 2022 13:41:45 -0600 Subject: [PATCH 31/72] fix: remove string quotes around array return type of rgb() example --- src/color.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/color.ts b/src/color.ts index 4df64e4e2e6..0a2ba533f59 100644 --- a/src/color.ts +++ b/src/color.ts @@ -169,10 +169,10 @@ export class Color { * faker.color.rgb({ case: 'upper' }) // '0xFFFFFF' * faker.color.rgb({ case: 'lower' }) // '0xffffff' * faker.color.rgb({ prefix: '#', case: 'lower' }) // '#ffffff' - * faker.color.rgb({ format: 'decimal' }) // '[255, 255, 255]' + * faker.color.rgb({ format: 'decimal' }) // [255, 255, 255] * faker.color.rgb({ format: 'css' }) // 'rgb(255, 0, 0)' * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' - * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // '[255, 255, 255, 0.4]' + * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [255, 255, 255, 0.4] */ rgb(options?: { prefix?: string; From 317ec8624da9164426b87a8c5656bab449d65e39 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 9 Apr 2022 13:46:18 -0600 Subject: [PATCH 32/72] fix: change color definition object to be partial --- src/locales/en/color/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index f94bb761183..934d64c6062 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -6,7 +6,7 @@ import type { ColorDefinitions } from '../../..'; import colorGamut from './color_gamut'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, colorGamut, }; From c91b5c258b6e6bc226b3d7d45e82195b8f78cd44 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 9 Apr 2022 13:54:05 -0600 Subject: [PATCH 33/72] fix: change color definition object for all locales to be partial --- src/locales/ar/color/index.ts | 2 +- src/locales/az/color/index.ts | 2 +- src/locales/el/color/index.ts | 2 +- src/locales/es/color/index.ts | 2 +- src/locales/es_MX/color/index.ts | 2 +- src/locales/fa/color/index.ts | 2 +- src/locales/he/color/index.ts | 2 +- src/locales/hy/color/index.ts | 2 +- src/locales/lv/color/index.ts | 2 +- src/locales/nl/color/index.ts | 2 +- src/locales/pt_BR/color/index.ts | 2 +- src/locales/pt_PT/color/index.ts | 2 +- src/locales/ru/color/index.ts | 2 +- src/locales/sv/color/index.ts | 2 +- src/locales/tr/color/index.ts | 2 +- src/locales/ur/color/index.ts | 2 +- 16 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/locales/ar/color/index.ts b/src/locales/ar/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/ar/color/index.ts +++ b/src/locales/ar/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/az/color/index.ts b/src/locales/az/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/az/color/index.ts +++ b/src/locales/az/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/el/color/index.ts b/src/locales/el/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/el/color/index.ts +++ b/src/locales/el/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/es/color/index.ts b/src/locales/es/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/es/color/index.ts +++ b/src/locales/es/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/es_MX/color/index.ts b/src/locales/es_MX/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/es_MX/color/index.ts +++ b/src/locales/es_MX/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/fa/color/index.ts b/src/locales/fa/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/fa/color/index.ts +++ b/src/locales/fa/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/he/color/index.ts b/src/locales/he/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/he/color/index.ts +++ b/src/locales/he/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/hy/color/index.ts b/src/locales/hy/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/hy/color/index.ts +++ b/src/locales/hy/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/lv/color/index.ts b/src/locales/lv/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/lv/color/index.ts +++ b/src/locales/lv/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/nl/color/index.ts b/src/locales/nl/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/nl/color/index.ts +++ b/src/locales/nl/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/pt_BR/color/index.ts b/src/locales/pt_BR/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/pt_BR/color/index.ts +++ b/src/locales/pt_BR/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/pt_PT/color/index.ts b/src/locales/pt_PT/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/pt_PT/color/index.ts +++ b/src/locales/pt_PT/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/ru/color/index.ts b/src/locales/ru/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/ru/color/index.ts +++ b/src/locales/ru/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/sv/color/index.ts b/src/locales/sv/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/sv/color/index.ts +++ b/src/locales/sv/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/tr/color/index.ts b/src/locales/tr/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/tr/color/index.ts +++ b/src/locales/tr/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; diff --git a/src/locales/ur/color/index.ts b/src/locales/ur/color/index.ts index 79cc39f02f3..f55555e9b39 100644 --- a/src/locales/ur/color/index.ts +++ b/src/locales/ur/color/index.ts @@ -5,7 +5,7 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: ColorDefinitions = { +const color: Partial = { human, }; From ba84e5b8ab7fb2b885c638eab2ac89ae5b0230e8 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 9 Apr 2022 13:55:44 -0600 Subject: [PATCH 34/72] fix: change colorSpace fallback to rgb for toColorFormat function to be in signature --- src/color.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/color.ts b/src/color.ts index 0a2ba533f59..a663bd9410f 100644 --- a/src/color.ts +++ b/src/color.ts @@ -101,9 +101,8 @@ function toCSS(values: number[], colorSpace: ColorSpace): string { function toColorFormat( values: number[], format: 'decimal' | 'css' | 'binary', - colorSpace?: ColorSpace + colorSpace: ColorSpace = 'rgb' ): string | number[] { - colorSpace = colorSpace || 'rgb'; if (format === 'decimal') return values; let result: string | number[]; From 0272c06cb17fc7ef2a60387f7e554d7a94b28dd7 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sun, 10 Apr 2022 12:54:07 -0600 Subject: [PATCH 35/72] feat: create the seperation between color gamut and color space --- src/color.ts | 43 ++++++++++--------- src/definitions/color.ts | 10 +++-- src/locales/en/color/gamut.ts | 11 +++++ src/locales/en/color/index.ts | 6 ++- .../en/color/{color_gamut.ts => space.ts} | 0 test/color.spec.ts | 25 ++++++++--- 6 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 src/locales/en/color/gamut.ts rename src/locales/en/color/{color_gamut.ts => space.ts} (100%) diff --git a/src/color.ts b/src/color.ts index a663bd9410f..a80088bcc20 100644 --- a/src/color.ts +++ b/src/color.ts @@ -1,15 +1,7 @@ import type { Faker } from '.'; +import type gamut from './locales/en/color/gamut'; -type ColorSpace = - | 'rgb' - | 'rgba' - | 'hsl' - | 'hsla' - | 'hwb' - | 'cmyk' - | 'lab' - | 'lch' - | 'display-p3'; +type Gamut = typeof gamut[number]; /** * Formats the hex format of a generated color string according @@ -62,12 +54,12 @@ function toBinary(values: number[]): string { * Converts an array of numbers into CSS accepted format. * * @param values Array of values to be converted. - * @param colorSpace Color space to format CSS string for. If invalid color space + * @param gamut Color space to format CSS string for. If invalid color space * is provided, RGB CSS will be returned. */ -function toCSS(values: number[], colorSpace: ColorSpace): string { +function toCSS(values: number[], gamut: Gamut): string { const percentages = values.map((value: number) => Math.round(value * 100)); - switch (colorSpace) { + switch (gamut) { case 'rgb': return `rgb(${values[0]}, ${values[1]}, ${values[2]})`; case 'rgba': @@ -101,7 +93,7 @@ function toCSS(values: number[], colorSpace: ColorSpace): string { function toColorFormat( values: number[], format: 'decimal' | 'css' | 'binary', - colorSpace: ColorSpace = 'rgb' + colorSpace: Gamut = 'rgb' ): string | number[] { if (format === 'decimal') return values; @@ -142,15 +134,24 @@ export class Color { } /** - * Returns a random color gamut name. + * Returns a random color space name. * * @example - * faker.color.colorGamut() // 'sRGB' + * faker.color.space() // 'sRGB' */ - colorGamut(): string { - return this.faker.random.arrayElement( - this.faker.definitions.color.colorGamut - ); + space(): string { + return this.faker.random.arrayElement(this.faker.definitions.color.space); + } + + /** + * Return a random color gamut name supported by CSS media feature. + * Source: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/color-gamut. + * + * @example + * faker.color.gamut() // 'display-p3' + */ + gamut(): string { + return this.faker.random.arrayElement(this.faker.definitions.color.gamut); } /** @@ -180,7 +181,7 @@ export class Color { includeAlpha?: boolean; }): string | number[] { let color: string | number[]; - let colorSpace: ColorSpace = 'rgb'; + let colorSpace: Gamut = 'rgb'; if (!options?.format) options = { ...options, format: 'hex' }; if (options?.format === 'hex') { color = this.faker.datatype.hexadecimal(options?.includeAlpha ? 8 : 6); diff --git a/src/definitions/color.ts b/src/definitions/color.ts index 05536d1b84e..5872101ffca 100644 --- a/src/definitions/color.ts +++ b/src/definitions/color.ts @@ -9,12 +9,16 @@ export interface ColorDefinitions { */ human: string[]; /** - * Color gamut names. + * Color space names. */ - colorGamut: string[]; + space: string[]; + /** + * Color gamut names supported by CSS. + */ + gamut: string[]; } /** * Internal: A list of all keys for the ColorDefinitions. */ -export const COLOR = allOf()('human', 'colorGamut'); +export const COLOR = allOf()('human', 'space', 'gamut'); diff --git a/src/locales/en/color/gamut.ts b/src/locales/en/color/gamut.ts new file mode 100644 index 00000000000..b653d2bdb49 --- /dev/null +++ b/src/locales/en/color/gamut.ts @@ -0,0 +1,11 @@ +export default [ + 'rgb', + 'rgba', + 'hsl', + 'hsla', + 'hwb', + 'cmyk', + 'lab', + 'lch', + 'display-p3', +] as const; diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index 934d64c6062..e867e1cfd4a 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -3,12 +3,14 @@ * Run 'pnpm run generate:locales' to update. */ import type { ColorDefinitions } from '../../..'; -import colorGamut from './color_gamut'; +import gamut from './gamut'; import human from './human'; +import space from './space'; const color: Partial = { human, - colorGamut, + space, + gamut, }; export default color; diff --git a/src/locales/en/color/color_gamut.ts b/src/locales/en/color/space.ts similarity index 100% rename from src/locales/en/color/color_gamut.ts rename to src/locales/en/color/space.ts diff --git a/test/color.spec.ts b/test/color.spec.ts index 0c6bb15cd51..50e338c0df1 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -6,7 +6,8 @@ const seededRuns = [ seed: 42, expectations: { human: 'grey', - colorGamut: 'Rec. 709', + space: 'Rec. 709', + gamut: 'hsla', rgb: '0x8BE4AB', hsl: [135, 0.8, 0.96], hwb: [135, 0.8, 0.96], @@ -20,7 +21,8 @@ const seededRuns = [ seed: 1337, expectations: { human: 'black', - colorGamut: 'ProPhoto RGB Color Space', + space: 'ProPhoto RGB Color Space', + gamut: 'hsl', rgb: '0x5c346b', hsl: [94, 0.56, 0.16], hwb: [94, 0.56, 0.16], @@ -34,7 +36,8 @@ const seededRuns = [ seed: 1211, expectations: { human: 'azure', - colorGamut: 'LMS', + space: 'LMS', + gamut: 'display-p3', rgb: '0xEaDB42', hsl: [335, 0.46, 0.9], hwb: [335, 0.46, 0.9], @@ -50,7 +53,8 @@ const NON_SEEDED_BASED_RUN = 5; const functionNames = [ 'human', - 'colorGamut', + 'space', + 'gamut', 'rgb', 'hsl', 'hwb', @@ -92,10 +96,17 @@ describe('color', () => { }); }); - describe(`colorGamut()`, () => { + describe(`space()`, () => { + it('should return random color space from color space array', () => { + const space = faker.color.space(); + expect(faker.definitions.color.space).toContain(space); + }); + }); + + describe(`gamut()`, () => { it('should return random color gamut from color gamut array', () => { - const gamut = faker.color.colorGamut(); - expect(faker.definitions.color.colorGamut).toContain(gamut); + const gamut = faker.color.gamut(); + expect(faker.definitions.color.gamut).toContain(gamut); }); }); From 1f58b0d375febe93388ad87ba3a6a054f5cd86b9 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sun, 10 Apr 2022 12:58:44 -0600 Subject: [PATCH 36/72] fix: make gamut readonly in color definition to resolve assignment error --- src/definitions/color.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/definitions/color.ts b/src/definitions/color.ts index 5872101ffca..c8c6c732d86 100644 --- a/src/definitions/color.ts +++ b/src/definitions/color.ts @@ -15,7 +15,7 @@ export interface ColorDefinitions { /** * Color gamut names supported by CSS. */ - gamut: string[]; + gamut: readonly string[]; } /** From 977be947ba0f097b49416b5f36e0624247fdb424 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sun, 10 Apr 2022 17:25:54 -0600 Subject: [PATCH 37/72] feat: seperate CSS functions, color spaces, and create their generators --- src/color.ts | 88 +++++++++++++------ src/definitions/color.ts | 13 ++- .../en/color/{gamut.ts => css_functions.ts} | 2 +- src/locales/en/color/css_spaces.ts | 8 ++ src/locales/en/color/index.ts | 10 ++- test/color.spec.ts | 64 ++++++++------ 6 files changed, 125 insertions(+), 60 deletions(-) rename src/locales/en/color/{gamut.ts => css_functions.ts} (86%) create mode 100644 src/locales/en/color/css_spaces.ts diff --git a/src/color.ts b/src/color.ts index a80088bcc20..52b9b7ddda0 100644 --- a/src/color.ts +++ b/src/color.ts @@ -1,7 +1,9 @@ import type { Faker } from '.'; -import type gamut from './locales/en/color/gamut'; +import type cssFunctions from './locales/en/color/css_functions'; +import type cssSpaces from './locales/en/color/css_spaces'; -type Gamut = typeof gamut[number]; +type CSSFunction = typeof cssFunctions[number]; +type CSSSpace = typeof cssSpaces[number]; /** * Formats the hex format of a generated color string according @@ -54,12 +56,16 @@ function toBinary(values: number[]): string { * Converts an array of numbers into CSS accepted format. * * @param values Array of values to be converted. - * @param gamut Color space to format CSS string for. If invalid color space - * is provided, RGB CSS will be returned. + * @param cssFunction CSS function to be generated for the color. Defaults to `rgb`. + * @param space Color space to format CSS color function with. Defaults to `srgb`. */ -function toCSS(values: number[], gamut: Gamut): string { +function toCSS( + values: number[], + cssFunction: CSSFunction = 'rgb', + space: CSSSpace = 'sRGB' +): string { const percentages = values.map((value: number) => Math.round(value * 100)); - switch (gamut) { + switch (cssFunction) { case 'rgb': return `rgb(${values[0]}, ${values[1]}, ${values[2]})`; case 'rgba': @@ -76,8 +82,8 @@ function toCSS(values: number[], gamut: Gamut): string { return `lab(${percentages[0]}% ${values[1]} ${values[2]})`; case 'lch': return `lch(${percentages[0]}% ${values[1]} ${values[2]})`; - case 'display-p3': - return `color(display-p3 ${values[0]} ${values[1]} ${values[2]})`; + case 'color': + return `color(${space} ${values[0]} ${values[1]} ${values[2]})`; default: return toCSS(values, 'rgb'); } @@ -88,19 +94,21 @@ function toCSS(values: number[], gamut: Gamut): string { * * @param values Array of color values to be converted. * @param format Format of generated RGB color. - * @param colorSpace Color space to format CSS string for. Defaults to `rgb`. + * @param cssFunction CSS function to be generated for the color. Defaults to `rgb`. + * @param space Color space to format CSS color function with. Defaults to `srgb`. */ function toColorFormat( values: number[], format: 'decimal' | 'css' | 'binary', - colorSpace: Gamut = 'rgb' + cssFunction: CSSFunction = 'rgb', + space: CSSSpace = 'sRGB' ): string | number[] { if (format === 'decimal') return values; let result: string | number[]; switch (format) { case 'css': - result = toCSS(values, colorSpace); + result = toCSS(values, cssFunction, space); break; case 'binary': result = toBinary(values); @@ -134,7 +142,8 @@ export class Color { } /** - * Returns a random color space name. + * Returns a random color space name from the worldwide accepted color spaces. + * Source: https://en.wikipedia.org/wiki/List_of_color_spaces_and_their_uses * * @example * faker.color.space() // 'sRGB' @@ -144,14 +153,27 @@ export class Color { } /** - * Return a random color gamut name supported by CSS media feature. - * Source: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/color-gamut. + * Returns a random css supported color function name. + * + * @example + * faker.color.cssSupportedFunction() // 'rgb' + */ + cssSupportedFunction(): string { + return this.faker.random.arrayElement( + this.faker.definitions.color.cssFunctions + ); + } + + /** + * Returns a random css supported color space name. * * @example - * faker.color.gamut() // 'display-p3' + * faker.color.cssSupportedSpace() // 'display-p3' */ - gamut(): string { - return this.faker.random.arrayElement(this.faker.definitions.color.gamut); + cssSupportedSpace(): string { + return this.faker.random.arrayElement( + this.faker.definitions.color.cssSpaces + ); } /** @@ -181,7 +203,7 @@ export class Color { includeAlpha?: boolean; }): string | number[] { let color: string | number[]; - let colorSpace: Gamut = 'rgb'; + let cssFunction: CSSFunction = 'rgb'; if (!options?.format) options = { ...options, format: 'hex' }; if (options?.format === 'hex') { color = this.faker.datatype.hexadecimal(options?.includeAlpha ? 8 : 6); @@ -194,9 +216,9 @@ export class Color { ); if (options?.includeAlpha) { color.push(this.faker.commerce.percentage(0.01)); - colorSpace = 'rgba'; + cssFunction = 'rgba'; } - return toColorFormat(color, options.format, colorSpace); + return toColorFormat(color, options.format, cssFunction); } /** @@ -318,21 +340,31 @@ export class Color { } /** - * Return a display-p3 color. + * Returns a random color based on CSS color space specified. * * @param options options object. * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.space Color space to generate the color for. Defaults to `sRGB`; * * @example - * faker.color.displayP3() // [0.93, 1, 0.82] - * faker.color.displayP3({ format: 'decimal' }) // [0.12, 0.21, 0.31] - * faker.color.displayP3({ format: 'css' }) // color(display-p3 0.12 1 0.23) - * faker.color.displayP3({ format: 'binary' }) // (8-32 bits x 3) + * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] + * faker.color.colorByCSSColorSpace({ format: 'decimal' }) // [0.12, 0.21, 0.31] + * faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // color(display-p3 0.12 1 0.23) + * faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3) */ - displayP3(options?: { + colorByCSSColorSpace(options?: { format?: 'decimal' | 'css' | 'binary'; + space?: CSSSpace; }): string | number[] { - const p3 = [0, 0, 0].map(() => this.faker.commerce.percentage(0.01)); - return toColorFormat(p3, options?.format || 'decimal', 'display-p3'); + if (options?.format === 'css' && !options?.space) { + options = { ...options, space: 'sRGB' }; + } + const color = [0, 0, 0].map(() => this.faker.commerce.percentage(0.0001)); + return toColorFormat( + color, + options?.format || 'decimal', + 'color', + options?.space + ); } } diff --git a/src/definitions/color.ts b/src/definitions/color.ts index c8c6c732d86..2e20968d7ef 100644 --- a/src/definitions/color.ts +++ b/src/definitions/color.ts @@ -15,10 +15,19 @@ export interface ColorDefinitions { /** * Color gamut names supported by CSS. */ - gamut: readonly string[]; + cssSpaces: readonly string[]; + /** + * Functions supported by CSS to produce color. + */ + cssFunctions: readonly string[]; } /** * Internal: A list of all keys for the ColorDefinitions. */ -export const COLOR = allOf()('human', 'space', 'gamut'); +export const COLOR = allOf()( + 'human', + 'space', + 'cssSpaces', + 'cssFunctions' +); diff --git a/src/locales/en/color/gamut.ts b/src/locales/en/color/css_functions.ts similarity index 86% rename from src/locales/en/color/gamut.ts rename to src/locales/en/color/css_functions.ts index b653d2bdb49..cd59d5785ab 100644 --- a/src/locales/en/color/gamut.ts +++ b/src/locales/en/color/css_functions.ts @@ -7,5 +7,5 @@ export default [ 'cmyk', 'lab', 'lch', - 'display-p3', + 'color', ] as const; diff --git a/src/locales/en/color/css_spaces.ts b/src/locales/en/color/css_spaces.ts new file mode 100644 index 00000000000..305823be151 --- /dev/null +++ b/src/locales/en/color/css_spaces.ts @@ -0,0 +1,8 @@ +export default [ + 'sRGB', + 'display-p3', + 'rec2020', + 'a98-rgb', + 'prophoto-rgb', + 'rec2020', +] as const; diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index e867e1cfd4a..c689199971c 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -3,14 +3,16 @@ * Run 'pnpm run generate:locales' to update. */ import type { ColorDefinitions } from '../../..'; -import gamut from './gamut'; +import cssFunctions from './css_functions'; +import cssSpaces from './css_spaces'; import human from './human'; import space from './space'; -const color: Partial = { +const color = { + cssFunctions, + cssSpaces, human, space, - gamut, -}; +} as Partial; export default color; diff --git a/test/color.spec.ts b/test/color.spec.ts index 50e338c0df1..a61d214d347 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -7,14 +7,15 @@ const seededRuns = [ expectations: { human: 'grey', space: 'Rec. 709', - gamut: 'hsla', + cssSupportedFunction: 'hsla', + cssSupportedSpace: 'rec2020', rgb: '0x8BE4AB', hsl: [135, 0.8, 0.96], hwb: [135, 0.8, 0.96], cmyk: [0.37, 0.8, 0.96, 0.18], lab: [0.37454, 59.3086, 90.1429], lch: [0.37454, 183.2, 218.7], - displayP3: [0.37, 0.8, 0.96], + colorByCSSColorSpace: [0.3745, 0.7966, 0.9508], }, }, { @@ -22,14 +23,15 @@ const seededRuns = [ expectations: { human: 'black', space: 'ProPhoto RGB Color Space', - gamut: 'hsl', + cssSupportedFunction: 'hsl', + cssSupportedSpace: 'display-p3', rgb: '0x5c346b', hsl: [94, 0.56, 0.16], hwb: [94, 0.56, 0.16], cmyk: [0.26, 0.56, 0.16, 0.21], lab: [0.262024, 12.106, -68.2632], lch: [0.262024, 128.9, 36.5], - displayP3: [0.26, 0.56, 0.16], + colorByCSSColorSpace: [0.262, 0.5605, 0.1586], }, }, { @@ -37,14 +39,15 @@ const seededRuns = [ expectations: { human: 'azure', space: 'LMS', - gamut: 'display-p3', + cssSupportedFunction: 'color', + cssSupportedSpace: 'rec2020', rgb: '0xEaDB42', hsl: [335, 0.46, 0.9], hwb: [335, 0.46, 0.9], cmyk: [0.93, 0.46, 0.9, 0.78], lab: [0.928521, -8.197, 78.6944], lch: [0.928521, 105.6, 205.5], - displayP3: [0.93, 0.46, 0.9], + colorByCSSColorSpace: [0.9286, 0.459, 0.8935], }, }, ]; @@ -54,14 +57,15 @@ const NON_SEEDED_BASED_RUN = 5; const functionNames = [ 'human', 'space', - 'gamut', + 'cssSupportedFunction', + 'cssSupportedSpace', 'rgb', 'hsl', 'hwb', 'cmyk', 'lab', 'lch', - 'displayP3', + 'colorByCSSColorSpace', ]; describe('color', () => { @@ -103,10 +107,17 @@ describe('color', () => { }); }); - describe(`gamut()`, () => { - it('should return random color gamut from color gamut array', () => { - const gamut = faker.color.gamut(); - expect(faker.definitions.color.gamut).toContain(gamut); + describe(`cssSupportedFunction()`, () => { + it('should return random css supported color function from css functions array', () => { + const func = faker.color.cssSupportedFunction(); + expect(faker.definitions.color.cssFunctions).toContain(func); + }); + }); + + describe(`cssSupportedSpace()`, () => { + it('should return random css supported color space from css spaces array', () => { + const space = faker.color.cssSupportedSpace(); + expect(faker.definitions.color.cssSpaces).toContain(space); }); }); @@ -418,9 +429,9 @@ describe('color', () => { }); }); - describe(`displayP3()`, () => { - it('should return a random display-p3 color in decimal format', () => { - const color = faker.color.displayP3(); + describe(`colorByCSSColorSpace()`, () => { + it('should return a random color for a CSS color space in decimal format', () => { + const color = faker.color.colorByCSSColorSpace(); expect(color).length(3); (color as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); @@ -429,9 +440,9 @@ describe('color', () => { }); }); - describe(`displayP3({ format: 'decimal' })`, () => { - it('should return a random displayP3 color in decimal format', () => { - const color = faker.color.displayP3({ format: 'decimal' }); + describe(`colorByCSSColorSpace({ format: 'decimal' })`, () => { + it('should return a random color for a CSS color space in decimal format', () => { + const color = faker.color.colorByCSSColorSpace({ format: 'decimal' }); expect(color).length(3); (color as number[]).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); @@ -440,18 +451,21 @@ describe('color', () => { }); }); - describe(`displayP3({ format: 'css' })`, () => { - it('should return a random displayP3 color in css format', () => { - const color = faker.color.displayP3({ format: 'css' }); + describe(`colorByCSSColorSpace({ format: 'css' })`, () => { + it('should return a random color for a CSS color space in css format', () => { + const color = faker.color.colorByCSSColorSpace({ + format: 'css', + space: 'prophoto-rgb', + }); expect(color).match( - /^color\(display-p3 \d*\.?\d* \d*\.?\d* \d*\.?\d*\)$/ + /^color\(prophoto-rgb \d*\.?\d* \d*\.?\d* \d*\.?\d*\)$/ ); }); }); - describe(`displayP3({ format: 'binary' })`, () => { - it('should return a random displayP3 color in binary format', () => { - const color = faker.color.displayP3({ format: 'binary' }); + describe(`colorByCSSColorSpace({ format: 'binary' })`, () => { + it('should return a random color for a CSS color space in binary format', () => { + const color = faker.color.colorByCSSColorSpace({ format: 'binary' }); expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/); }); }); From 35a8424d31b545140013a97ab87c1ed0741e10c1 Mon Sep 17 00:00:00 2001 From: Harsohail Brar <47438886+harsohailB@users.noreply.github.com> Date: Mon, 11 Apr 2022 11:18:49 -0600 Subject: [PATCH 38/72] fix: update options object doc casing Co-authored-by: Shinigami --- src/color.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/color.ts b/src/color.ts index 52b9b7ddda0..adc521c2cb9 100644 --- a/src/color.ts +++ b/src/color.ts @@ -10,7 +10,7 @@ type CSSSpace = typeof cssSpaces[number]; * to options specified by user. * * @param hexColor Hex color string to be formated. - * @param options options object. + * @param options Options object. * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. * @param options.case Letter case of the generated hex color. Defaults to `mixed`. */ From 3fa7156618ac848eb4e374ad010653ef220d71e5 Mon Sep 17 00:00:00 2001 From: Harsohail Brar <47438886+harsohailB@users.noreply.github.com> Date: Mon, 11 Apr 2022 11:19:24 -0600 Subject: [PATCH 39/72] chore: change inline if statement to be on new line Co-authored-by: Shinigami --- src/color.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/color.ts b/src/color.ts index adc521c2cb9..9346b241475 100644 --- a/src/color.ts +++ b/src/color.ts @@ -21,7 +21,9 @@ function applyHexFormat( case?: 'upper' | 'lower' | 'mixed'; } ): string { - if (options?.prefix) hexColor = hexColor.replace('0x', options.prefix); + if (options?.prefix) { + hexColor = hexColor.replace('0x', options.prefix); + } switch (options?.case) { case 'upper': hexColor = hexColor.toUpperCase(); From f0e031c07a2dfa31eea72103bf219c6e772da20b Mon Sep 17 00:00:00 2001 From: Harsohail Brar <47438886+harsohailB@users.noreply.github.com> Date: Mon, 11 Apr 2022 11:19:50 -0600 Subject: [PATCH 40/72] fix: spelling mistake in docs for "formatted" Co-authored-by: Shinigami --- src/color.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/color.ts b/src/color.ts index 9346b241475..666395f10c7 100644 --- a/src/color.ts +++ b/src/color.ts @@ -9,7 +9,7 @@ type CSSSpace = typeof cssSpaces[number]; * Formats the hex format of a generated color string according * to options specified by user. * - * @param hexColor Hex color string to be formated. + * @param hexColor Hex color string to be formatted. * @param options Options object. * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. * @param options.case Letter case of the generated hex color. Defaults to `mixed`. From eef1d6dce5c639d216df0b4301310f75f5ca59d7 Mon Sep 17 00:00:00 2001 From: Harsohail Brar <47438886+harsohailB@users.noreply.github.com> Date: Mon, 11 Apr 2022 11:20:21 -0600 Subject: [PATCH 41/72] fix: add type to map function on values Co-authored-by: Shinigami --- src/color.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/color.ts b/src/color.ts index 666395f10c7..debb14d0807 100644 --- a/src/color.ts +++ b/src/color.ts @@ -41,7 +41,7 @@ function applyHexFormat( * @param values Array of values to be converted. */ function toBinary(values: number[]): string { - const binary: string[] = values.map((value: number) => { + const binary: string[] = values.map((value) => { const isFloat: boolean = Number(value) === value && value % 1 !== 0; if (isFloat) { const buffer: ArrayBuffer = new ArrayBuffer(4); From b1b87aba71c277e790496b52b755bfb152116b50 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Mon, 11 Apr 2022 11:27:14 -0600 Subject: [PATCH 42/72] fix: small grammatical, typos, and type inferences --- src/color.ts | 36 ++++++++++++++++++------------------ src/commerce.ts | 6 +++--- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/color.ts b/src/color.ts index debb14d0807..0e517c267b0 100644 --- a/src/color.ts +++ b/src/color.ts @@ -14,7 +14,7 @@ type CSSSpace = typeof cssSpaces[number]; * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. * @param options.case Letter case of the generated hex color. Defaults to `mixed`. */ -function applyHexFormat( +function formatHexColor( hexColor: string, options?: { prefix?: string; @@ -42,9 +42,9 @@ function applyHexFormat( */ function toBinary(values: number[]): string { const binary: string[] = values.map((value) => { - const isFloat: boolean = Number(value) === value && value % 1 !== 0; + const isFloat = value === value && value % 1 !== 0; if (isFloat) { - const buffer: ArrayBuffer = new ArrayBuffer(4); + const buffer = new ArrayBuffer(4); new DataView(buffer).setFloat32(0, value); const bytes = new Uint8Array(buffer); return toBinary(Array.from(bytes)).split(' ').join(''); @@ -59,14 +59,14 @@ function toBinary(values: number[]): string { * * @param values Array of values to be converted. * @param cssFunction CSS function to be generated for the color. Defaults to `rgb`. - * @param space Color space to format CSS color function with. Defaults to `srgb`. + * @param space Color space to format CSS color function with. Defaults to `sRGB`. */ function toCSS( values: number[], cssFunction: CSSFunction = 'rgb', space: CSSSpace = 'sRGB' ): string { - const percentages = values.map((value: number) => Math.round(value * 100)); + const percentages = values.map((value) => Math.round(value * 100)); switch (cssFunction) { case 'rgb': return `rgb(${values[0]}, ${values[1]}, ${values[2]})`; @@ -97,7 +97,7 @@ function toCSS( * @param values Array of color values to be converted. * @param format Format of generated RGB color. * @param cssFunction CSS function to be generated for the color. Defaults to `rgb`. - * @param space Color space to format CSS color function with. Defaults to `srgb`. + * @param space Color space to format CSS color function with. Defaults to `sRGB`. */ function toColorFormat( values: number[], @@ -179,9 +179,9 @@ export class Color { } /** - * Returns a RGB color. + * Returns an RGB color. * - * @param options options object. + * @param options Options object. * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. * @param options.case Letter case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. * @param options.format Format of generated RGB color. Defaults to `hex`. @@ -209,7 +209,7 @@ export class Color { if (!options?.format) options = { ...options, format: 'hex' }; if (options?.format === 'hex') { color = this.faker.datatype.hexadecimal(options?.includeAlpha ? 8 : 6); - color = applyHexFormat(color, options); + color = formatHexColor(color, options); return color; } @@ -226,7 +226,7 @@ export class Color { /** * Returns a CMYK color. * - * @param options options object. + * @param options Options object. * @param options.format Format of generated CMYK color. Defaults to `decimal`. * * @example @@ -244,9 +244,9 @@ export class Color { } /** - * Returns a HSL color. + * Returns an HSL color. * - * @param options options object. + * @param options Options object. * @param options.format Format of generated HSL color. Defaults to `decimal`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * @@ -275,9 +275,9 @@ export class Color { } /** - * Returns a HWB color. + * Returns an HWB color. * - * @param options options object. + * @param options Options object. * @param options.format Format of generated RGB color. Defaults to `decimal`. * * @example @@ -297,7 +297,7 @@ export class Color { /** * Returns a LAB (CIELAB) color. * - * @param options options object. + * @param options Options object. * @param options.format Format of generated RGB color. Defaults to `decimal`. * * @example @@ -317,12 +317,12 @@ export class Color { } /** - * Returns a LCH color. Even though upper bound of + * Returns an LCH color. Even though upper bound of * chroma in LCH color space is theoretically unbounded, * it is bounded to 230 as anything above will not * make a noticable difference in the browser. * - * @param options options object. + * @param options Options object. * @param options.format Format of generated RGB color. Defaults to `decimal`. * * @example @@ -344,7 +344,7 @@ export class Color { /** * Returns a random color based on CSS color space specified. * - * @param options options object. + * @param options Options object. * @param options.format Format of generated RGB color. Defaults to `decimal`. * @param options.space Color space to generate the color for. Defaults to `sRGB`; * diff --git a/src/commerce.ts b/src/commerce.ts index 0d8399b9e84..be2ac0d9f5b 100644 --- a/src/commerce.ts +++ b/src/commerce.ts @@ -49,17 +49,17 @@ export class Commerce { * Return a percentage value in decimal format between 0 and 1 * with percision of two decimal place. * - * @param percision Precision of the generated number. Defaults to `0.01`. + * @param precision Precision of the generated number. Defaults to `0.01`. * * @example * this.getPercentage(0.1) // 0.4 * this.getPercentage(0.01) // 0.36 */ - percentage(percision: number): number { + percentage(precision: number): number { return this.faker.datatype.float({ min: 0, max: 1, - precision: percision || 0.01, + precision: precision || 0.01, }); } From b4b475a1dd1a67221076c23414f765303707f5a1 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Mon, 11 Apr 2022 12:35:24 -0600 Subject: [PATCH 43/72] fix: only calculate percentages in toCSS when needed --- src/color.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/color.ts b/src/color.ts index 0e517c267b0..e2b2a1e157c 100644 --- a/src/color.ts +++ b/src/color.ts @@ -66,26 +66,34 @@ function toCSS( cssFunction: CSSFunction = 'rgb', space: CSSSpace = 'sRGB' ): string { - const percentages = values.map((value) => Math.round(value * 100)); + const percentage = (value: number) => Math.round(value * 100); switch (cssFunction) { case 'rgb': return `rgb(${values[0]}, ${values[1]}, ${values[2]})`; case 'rgba': return `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`; + case 'color': + return `color(${space} ${values[0]} ${values[1]} ${values[2]})`; case 'cmyk': - return `cmyk(${percentages[0]}%, ${percentages[1]}%, ${percentages[2]}%, ${percentages[3]}%)`; + return `cmyk(${percentage(values[0])}%, ${percentage( + values[1] + )}%, ${percentage(values[2])}%, ${percentage(values[3])}%)`; case 'hsl': - return `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}%)`; + return `hsl(${values[0]}deg ${percentage(values[1])}% ${percentage( + values[2] + )}%)`; case 'hsla': - return `hsl(${values[0]}deg ${percentages[1]}% ${percentages[2]}% / ${percentages[3]})`; + return `hsl(${values[0]}deg ${percentage(values[1])}% ${percentage( + values[2] + )}% / ${percentage(values[3])})`; case 'hwb': - return `hwb(${values[0]} ${percentages[1]}% ${percentages[2]}%)`; + return `hwb(${values[0]} ${percentage(values[1])}% ${percentage( + values[2] + )}%)`; case 'lab': - return `lab(${percentages[0]}% ${values[1]} ${values[2]})`; + return `lab(${percentage(values[0])}% ${values[1]} ${values[2]})`; case 'lch': - return `lch(${percentages[0]}% ${values[1]} ${values[2]})`; - case 'color': - return `color(${space} ${values[0]} ${values[1]} ${values[2]})`; + return `lch(${percentage(values[0])}% ${values[1]} ${values[2]})`; default: return toCSS(values, 'rgb'); } From 73a98a456439e0e0dcc3ed7d4b0075db1989970e Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Mon, 11 Apr 2022 12:38:10 -0600 Subject: [PATCH 44/72] feat: extract color format to be reused --- src/color.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/color.ts b/src/color.ts index e2b2a1e157c..59de9e10f69 100644 --- a/src/color.ts +++ b/src/color.ts @@ -4,6 +4,7 @@ import type cssSpaces from './locales/en/color/css_spaces'; type CSSFunction = typeof cssFunctions[number]; type CSSSpace = typeof cssSpaces[number]; +type ColorFormat = 'decimal' | 'css' | 'binary'; /** * Formats the hex format of a generated color string according @@ -109,7 +110,7 @@ function toCSS( */ function toColorFormat( values: number[], - format: 'decimal' | 'css' | 'binary', + format: ColorFormat, cssFunction: CSSFunction = 'rgb', space: CSSSpace = 'sRGB' ): string | number[] { @@ -209,7 +210,7 @@ export class Color { rgb(options?: { prefix?: string; case?: 'upper' | 'lower' | 'mixed'; - format?: 'hex' | 'decimal' | 'css' | 'binary'; + format?: 'hex' | ColorFormat; includeAlpha?: boolean; }): string | number[] { let color: string | number[]; @@ -243,7 +244,7 @@ export class Color { * faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%) * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 */ - cmyk(options?: { format?: 'decimal' | 'css' | 'binary' }): string | number[] { + cmyk(options?: { format?: ColorFormat }): string | number[] { const color: string | number[] = [0, 0, 0, 0].map(() => this.faker.commerce.percentage(0.01) ); @@ -268,7 +269,7 @@ export class Color { * faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4 */ hsl(options?: { - format?: 'decimal' | 'css' | 'binary'; + format?: ColorFormat; includeAlpha?: boolean; }): string | number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; @@ -294,7 +295,7 @@ export class Color { * faker.color.hwb({ format: 'css' }) // hwb(194 0% 0%) * faker.color.hwb({ format: 'binary' }) // (8-32 bits x 3) */ - hwb(options?: { format?: 'decimal' | 'css' | 'binary' }): string | number[] { + hwb(options?: { format?: ColorFormat }): string | number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; for (let i = 0; i < 2; i++) { hsl.push(this.faker.commerce.percentage(0.01)); @@ -314,7 +315,7 @@ export class Color { * faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664) * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) */ - lab(options?: { format?: 'decimal' | 'css' | 'binary' }): string | number[] { + lab(options?: { format?: ColorFormat }): string | number[] { const lab = [this.faker.commerce.percentage(0.000001)]; for (let i = 0; i < 2; i++) { lab.push( @@ -339,7 +340,7 @@ export class Color { * faker.color.lch{ format: 'css' }) // lch(52.2345% 72.2 56.2) * faker.color.lch{ format: 'binary' }) // (8-32 bits x 3) */ - lch(options?: { format?: 'decimal' | 'css' | 'binary' }): string | number[] { + lch(options?: { format?: ColorFormat }): string | number[] { const lch = [this.faker.commerce.percentage(0.000001)]; for (let i = 0; i < 2; i++) { lch.push( @@ -363,7 +364,7 @@ export class Color { * faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3) */ colorByCSSColorSpace(options?: { - format?: 'decimal' | 'css' | 'binary'; + format?: ColorFormat; space?: CSSSpace; }): string | number[] { if (options?.format === 'css' && !options?.space) { From d1e8905aceeda19438371187cbd3637b8b04c1d7 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Mon, 11 Apr 2022 12:40:50 -0600 Subject: [PATCH 45/72] feat: improve readability of rgb generator --- src/color.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/color.ts b/src/color.ts index 59de9e10f69..f336b22d4be 100644 --- a/src/color.ts +++ b/src/color.ts @@ -213,23 +213,22 @@ export class Color { format?: 'hex' | ColorFormat; includeAlpha?: boolean; }): string | number[] { + const { format = 'hex', includeAlpha = false } = options || {}; let color: string | number[]; let cssFunction: CSSFunction = 'rgb'; - if (!options?.format) options = { ...options, format: 'hex' }; - if (options?.format === 'hex') { - color = this.faker.datatype.hexadecimal(options?.includeAlpha ? 8 : 6); + if (format === 'hex') { + color = this.faker.datatype.hexadecimal(includeAlpha ? 8 : 6); color = formatHexColor(color, options); return color; } - color = [0, 0, 0].map(() => this.faker.datatype.number({ min: 0, max: 255 }) ); - if (options?.includeAlpha) { + if (includeAlpha) { color.push(this.faker.commerce.percentage(0.01)); cssFunction = 'rgba'; } - return toColorFormat(color, options.format, cssFunction); + return toColorFormat(color, format, cssFunction); } /** From d0eafbbe111fa0d4a1018d61e383eed9183e88cd Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Mon, 11 Apr 2022 12:43:07 -0600 Subject: [PATCH 46/72] feat: use js Array to construct arrays --- src/color.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/color.ts b/src/color.ts index f336b22d4be..3bf764ad67f 100644 --- a/src/color.ts +++ b/src/color.ts @@ -221,7 +221,7 @@ export class Color { color = formatHexColor(color, options); return color; } - color = [0, 0, 0].map(() => + color = Array.from({ length: 3 }).map(() => this.faker.datatype.number({ min: 0, max: 255 }) ); if (includeAlpha) { @@ -244,7 +244,7 @@ export class Color { * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 */ cmyk(options?: { format?: ColorFormat }): string | number[] { - const color: string | number[] = [0, 0, 0, 0].map(() => + const color: string | number[] = Array.from({ length: 4 }).map(() => this.faker.commerce.percentage(0.01) ); @@ -369,7 +369,9 @@ export class Color { if (options?.format === 'css' && !options?.space) { options = { ...options, space: 'sRGB' }; } - const color = [0, 0, 0].map(() => this.faker.commerce.percentage(0.0001)); + const color = Array.from({ length: 3 }).map(() => + this.faker.commerce.percentage(0.0001) + ); return toColorFormat( color, options?.format || 'decimal', From 75674ddd9780585ea8f435623ec931d1555a41ed Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Mon, 11 Apr 2022 12:46:56 -0600 Subject: [PATCH 47/72] fix: remove percentage generator from commerce and generate percentages inline in color module --- src/color.ts | 20 +++++++++++++------- src/commerce.ts | 18 ------------------ 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/color.ts b/src/color.ts index 3bf764ad67f..ca482cd9967 100644 --- a/src/color.ts +++ b/src/color.ts @@ -225,7 +225,9 @@ export class Color { this.faker.datatype.number({ min: 0, max: 255 }) ); if (includeAlpha) { - color.push(this.faker.commerce.percentage(0.01)); + color.push( + this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }) + ); cssFunction = 'rgba'; } return toColorFormat(color, format, cssFunction); @@ -245,7 +247,7 @@ export class Color { */ cmyk(options?: { format?: ColorFormat }): string | number[] { const color: string | number[] = Array.from({ length: 4 }).map(() => - this.faker.commerce.percentage(0.01) + this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }) ); return toColorFormat(color, options?.format || 'decimal', 'cmyk'); @@ -273,7 +275,7 @@ export class Color { }): string | number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; for (let i = 0; i < (options?.includeAlpha ? 3 : 2); i++) { - hsl.push(this.faker.commerce.percentage(0.01)); + hsl.push(this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 })); } return toColorFormat( hsl, @@ -297,7 +299,7 @@ export class Color { hwb(options?: { format?: ColorFormat }): string | number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; for (let i = 0; i < 2; i++) { - hsl.push(this.faker.commerce.percentage(0.01)); + hsl.push(this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 })); } return toColorFormat(hsl, options?.format || 'decimal', 'hwb'); } @@ -315,7 +317,9 @@ export class Color { * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) */ lab(options?: { format?: ColorFormat }): string | number[] { - const lab = [this.faker.commerce.percentage(0.000001)]; + const lab = [ + this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }), + ]; for (let i = 0; i < 2; i++) { lab.push( this.faker.datatype.float({ min: -100, max: 100, precision: 0.0001 }) @@ -340,7 +344,9 @@ export class Color { * faker.color.lch{ format: 'binary' }) // (8-32 bits x 3) */ lch(options?: { format?: ColorFormat }): string | number[] { - const lch = [this.faker.commerce.percentage(0.000001)]; + const lch = [ + this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }), + ]; for (let i = 0; i < 2; i++) { lch.push( this.faker.datatype.number({ min: 0, max: 230, precision: 0.1 }) @@ -370,7 +376,7 @@ export class Color { options = { ...options, space: 'sRGB' }; } const color = Array.from({ length: 3 }).map(() => - this.faker.commerce.percentage(0.0001) + this.faker.datatype.float({ min: 0, max: 1, precision: 0.0001 }) ); return toColorFormat( color, diff --git a/src/commerce.ts b/src/commerce.ts index be2ac0d9f5b..b951d7f8c69 100644 --- a/src/commerce.ts +++ b/src/commerce.ts @@ -45,24 +45,6 @@ export class Commerce { ); } - /** - * Return a percentage value in decimal format between 0 and 1 - * with percision of two decimal place. - * - * @param precision Precision of the generated number. Defaults to `0.01`. - * - * @example - * this.getPercentage(0.1) // 0.4 - * this.getPercentage(0.01) // 0.36 - */ - percentage(precision: number): number { - return this.faker.datatype.float({ - min: 0, - max: 1, - precision: precision || 0.01, - }); - } - /** * Generates a random descriptive product name. * From e8c65f49daf627873a0db3c429785c3dbf874562 Mon Sep 17 00:00:00 2001 From: Harsohail Brar <47438886+harsohailB@users.noreply.github.com> Date: Sat, 16 Apr 2022 17:34:51 -0600 Subject: [PATCH 48/72] fix: update docs noticeable typo Co-authored-by: Shinigami --- src/color.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/color.ts b/src/color.ts index ca482cd9967..f9a79ef3742 100644 --- a/src/color.ts +++ b/src/color.ts @@ -332,7 +332,7 @@ export class Color { * Returns an LCH color. Even though upper bound of * chroma in LCH color space is theoretically unbounded, * it is bounded to 230 as anything above will not - * make a noticable difference in the browser. + * make a noticeable difference in the browser. * * @param options Options object. * @param options.format Format of generated RGB color. Defaults to `decimal`. From fcfea4082f4f1e8dd9e07e62b9c5ec626e4aee1a Mon Sep 17 00:00:00 2001 From: Harsohail Brar <47438886+harsohailB@users.noreply.github.com> Date: Sat, 16 Apr 2022 17:39:46 -0600 Subject: [PATCH 49/72] fix: simplify toColorFormat switch statement Co-authored-by: Shinigami --- src/color.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/color.ts b/src/color.ts index f9a79ef3742..69e9a052632 100644 --- a/src/color.ts +++ b/src/color.ts @@ -114,18 +114,14 @@ function toColorFormat( cssFunction: CSSFunction = 'rgb', space: CSSSpace = 'sRGB' ): string | number[] { - if (format === 'decimal') return values; - - let result: string | number[]; switch (format) { case 'css': - result = toCSS(values, cssFunction, space); - break; + return toCSS(values, cssFunction, space); case 'binary': - result = toBinary(values); - break; + return toBinary(values); + default: + return values; } - return result; } /** From 80cc9cb974e527d65e039b23a6941c2de6a8268f Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 16 Apr 2022 17:46:26 -0600 Subject: [PATCH 50/72] fix: condition for checking if number is a float --- src/color.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/color.ts b/src/color.ts index 69e9a052632..ecba59db5fe 100644 --- a/src/color.ts +++ b/src/color.ts @@ -43,7 +43,7 @@ function formatHexColor( */ function toBinary(values: number[]): string { const binary: string[] = values.map((value) => { - const isFloat = value === value && value % 1 !== 0; + const isFloat = Number(value) === value && value % 1 !== 0; if (isFloat) { const buffer = new ArrayBuffer(4); new DataView(buffer).setFloat32(0, value); @@ -69,8 +69,6 @@ function toCSS( ): string { const percentage = (value: number) => Math.round(value * 100); switch (cssFunction) { - case 'rgb': - return `rgb(${values[0]}, ${values[1]}, ${values[2]})`; case 'rgba': return `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`; case 'color': @@ -95,8 +93,9 @@ function toCSS( return `lab(${percentage(values[0])}% ${values[1]} ${values[2]})`; case 'lch': return `lch(${percentage(values[0])}% ${values[1]} ${values[2]})`; + case 'rgb': default: - return toCSS(values, 'rgb'); + return `rgb(${values[0]}, ${values[1]}, ${values[2]})`; } } From 07314ac985234b8b7a8640c913d088c9af3dd8f3 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 16 Apr 2022 17:48:29 -0600 Subject: [PATCH 51/72] feat: add doc for hex format option in rgb function --- src/color.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/color.ts b/src/color.ts index ecba59db5fe..2e9209c3628 100644 --- a/src/color.ts +++ b/src/color.ts @@ -197,6 +197,7 @@ export class Color { * faker.color.rgb({ case: 'upper' }) // '0xFFFFFF' * faker.color.rgb({ case: 'lower' }) // '0xffffff' * faker.color.rgb({ prefix: '#', case: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'hex', case: 'lower' }) // '#ffffff' * faker.color.rgb({ format: 'decimal' }) // [255, 255, 255] * faker.color.rgb({ format: 'css' }) // 'rgb(255, 0, 0)' * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' From 8a9b9d6842271af84568ea50e7f11cd82b818d4d Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 16 Apr 2022 18:11:27 -0600 Subject: [PATCH 52/72] fix: move css functions and spaces out of locales into color.ts --- src/color.ts | 37 +++++++++++++++++++++------ src/definitions/color.ts | 15 +---------- src/locales/en/color/css_functions.ts | 11 -------- src/locales/en/color/css_spaces.ts | 8 ------ src/locales/en/color/index.ts | 4 --- test/color.spec.ts | 5 ++-- 6 files changed, 33 insertions(+), 47 deletions(-) delete mode 100644 src/locales/en/color/css_functions.ts delete mode 100644 src/locales/en/color/css_spaces.ts diff --git a/src/color.ts b/src/color.ts index 2e9209c3628..2f40898d181 100644 --- a/src/color.ts +++ b/src/color.ts @@ -1,6 +1,31 @@ import type { Faker } from '.'; -import type cssFunctions from './locales/en/color/css_functions'; -import type cssSpaces from './locales/en/color/css_spaces'; + +/** + * Color space names supported by CSS. + */ +export const cssSpaces = [ + 'sRGB', + 'display-p3', + 'rec2020', + 'a98-rgb', + 'prophoto-rgb', + 'rec2020', +]; + +/** + * Functions supported by CSS to produce color. + */ +export const cssFunctions = [ + 'rgb', + 'rgba', + 'hsl', + 'hsla', + 'hwb', + 'cmyk', + 'lab', + 'lch', + 'color', +]; type CSSFunction = typeof cssFunctions[number]; type CSSSpace = typeof cssSpaces[number]; @@ -165,9 +190,7 @@ export class Color { * faker.color.cssSupportedFunction() // 'rgb' */ cssSupportedFunction(): string { - return this.faker.random.arrayElement( - this.faker.definitions.color.cssFunctions - ); + return this.faker.random.arrayElement(cssFunctions); } /** @@ -177,9 +200,7 @@ export class Color { * faker.color.cssSupportedSpace() // 'display-p3' */ cssSupportedSpace(): string { - return this.faker.random.arrayElement( - this.faker.definitions.color.cssSpaces - ); + return this.faker.random.arrayElement(cssSpaces); } /** diff --git a/src/definitions/color.ts b/src/definitions/color.ts index 2e20968d7ef..2496600e857 100644 --- a/src/definitions/color.ts +++ b/src/definitions/color.ts @@ -12,22 +12,9 @@ export interface ColorDefinitions { * Color space names. */ space: string[]; - /** - * Color gamut names supported by CSS. - */ - cssSpaces: readonly string[]; - /** - * Functions supported by CSS to produce color. - */ - cssFunctions: readonly string[]; } /** * Internal: A list of all keys for the ColorDefinitions. */ -export const COLOR = allOf()( - 'human', - 'space', - 'cssSpaces', - 'cssFunctions' -); +export const COLOR = allOf()('human', 'space'); diff --git a/src/locales/en/color/css_functions.ts b/src/locales/en/color/css_functions.ts deleted file mode 100644 index cd59d5785ab..00000000000 --- a/src/locales/en/color/css_functions.ts +++ /dev/null @@ -1,11 +0,0 @@ -export default [ - 'rgb', - 'rgba', - 'hsl', - 'hsla', - 'hwb', - 'cmyk', - 'lab', - 'lch', - 'color', -] as const; diff --git a/src/locales/en/color/css_spaces.ts b/src/locales/en/color/css_spaces.ts deleted file mode 100644 index 305823be151..00000000000 --- a/src/locales/en/color/css_spaces.ts +++ /dev/null @@ -1,8 +0,0 @@ -export default [ - 'sRGB', - 'display-p3', - 'rec2020', - 'a98-rgb', - 'prophoto-rgb', - 'rec2020', -] as const; diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index c689199971c..b3a628721ec 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -3,14 +3,10 @@ * Run 'pnpm run generate:locales' to update. */ import type { ColorDefinitions } from '../../..'; -import cssFunctions from './css_functions'; -import cssSpaces from './css_spaces'; import human from './human'; import space from './space'; const color = { - cssFunctions, - cssSpaces, human, space, } as Partial; diff --git a/test/color.spec.ts b/test/color.spec.ts index a61d214d347..9244a60b955 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -1,5 +1,6 @@ import { afterEach, describe, expect, it } from 'vitest'; import { faker } from '../src'; +import { cssFunctions, cssSpaces } from '../src/color'; const seededRuns = [ { @@ -110,14 +111,14 @@ describe('color', () => { describe(`cssSupportedFunction()`, () => { it('should return random css supported color function from css functions array', () => { const func = faker.color.cssSupportedFunction(); - expect(faker.definitions.color.cssFunctions).toContain(func); + expect(cssFunctions).toContain(func); }); }); describe(`cssSupportedSpace()`, () => { it('should return random css supported color space from css spaces array', () => { const space = faker.color.cssSupportedSpace(); - expect(faker.definitions.color.cssSpaces).toContain(space); + expect(cssSpaces).toContain(space); }); }); From 8c6dad4c70a1baa5136255aeca66679a55a4fdf8 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 16 Apr 2022 19:07:23 -0600 Subject: [PATCH 53/72] feat: change rgb() default values to (prefix:#, case:lower) --- src/color.ts | 34 ++++++++++++++++++++-------------- test/color.spec.ts | 28 ++++++++++++++-------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/color.ts b/src/color.ts index 2f40898d181..fa1a94fcb98 100644 --- a/src/color.ts +++ b/src/color.ts @@ -38,19 +38,16 @@ type ColorFormat = 'decimal' | 'css' | 'binary'; * @param hexColor Hex color string to be formatted. * @param options Options object. * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. - * @param options.case Letter case of the generated hex color. Defaults to `mixed`. + * @param options.typeCase Letter type case of the generated hex color. Defaults to `mixed`. */ function formatHexColor( hexColor: string, options?: { prefix?: string; - case?: 'upper' | 'lower' | 'mixed'; + typeCase?: 'upper' | 'lower' | 'mixed'; } ): string { - if (options?.prefix) { - hexColor = hexColor.replace('0x', options.prefix); - } - switch (options?.case) { + switch (options?.typeCase) { case 'upper': hexColor = hexColor.toUpperCase(); break; @@ -58,6 +55,9 @@ function formatHexColor( hexColor = hexColor.toLowerCase(); break; } + if (options?.prefix) { + hexColor = options.prefix + hexColor; + } return hexColor; } @@ -208,17 +208,17 @@ export class Color { * * @param options Options object. * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. - * @param options.case Letter case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. + * @param options.typeCase Letter type case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. * @param options.format Format of generated RGB color. Defaults to `hex`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example * faker.color.rgb() // '0xffffFF' * faker.color.rgb({ prefix: '#' }) // '#ffffFF' - * faker.color.rgb({ case: 'upper' }) // '0xFFFFFF' - * faker.color.rgb({ case: 'lower' }) // '0xffffff' - * faker.color.rgb({ prefix: '#', case: 'lower' }) // '#ffffff' - * faker.color.rgb({ format: 'hex', case: 'lower' }) // '#ffffff' + * faker.color.rgb({ typeCase: 'upper' }) // '0xFFFFFF' + * faker.color.rgb({ typeCase: 'lower' }) // '0xffffff' + * faker.color.rgb({ prefix: '#', typeCase: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'hex', typeCase: 'lower' }) // '#ffffff' * faker.color.rgb({ format: 'decimal' }) // [255, 255, 255] * faker.color.rgb({ format: 'css' }) // 'rgb(255, 0, 0)' * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' @@ -226,15 +226,21 @@ export class Color { */ rgb(options?: { prefix?: string; - case?: 'upper' | 'lower' | 'mixed'; + typeCase?: 'upper' | 'lower' | 'mixed'; format?: 'hex' | ColorFormat; includeAlpha?: boolean; }): string | number[] { - const { format = 'hex', includeAlpha = false } = options || {}; + const { + format = 'hex', + includeAlpha = false, + prefix = '#', + typeCase = 'lower', + } = options || {}; + options = { format, includeAlpha, prefix, typeCase }; let color: string | number[]; let cssFunction: CSSFunction = 'rgb'; if (format === 'hex') { - color = this.faker.datatype.hexadecimal(includeAlpha ? 8 : 6); + color = this.faker.datatype.hexadecimal(includeAlpha ? 8 : 6).slice(2); color = formatHexColor(color, options); return color; } diff --git a/test/color.spec.ts b/test/color.spec.ts index 9244a60b955..7b7d8c8e2ae 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -10,7 +10,7 @@ const seededRuns = [ space: 'Rec. 709', cssSupportedFunction: 'hsla', cssSupportedSpace: 'rec2020', - rgb: '0x8BE4AB', + rgb: '#8be4ab', hsl: [135, 0.8, 0.96], hwb: [135, 0.8, 0.96], cmyk: [0.37, 0.8, 0.96, 0.18], @@ -26,7 +26,7 @@ const seededRuns = [ space: 'ProPhoto RGB Color Space', cssSupportedFunction: 'hsl', cssSupportedSpace: 'display-p3', - rgb: '0x5c346b', + rgb: '#5c346b', hsl: [94, 0.56, 0.16], hwb: [94, 0.56, 0.16], cmyk: [0.26, 0.56, 0.16, 0.21], @@ -42,7 +42,7 @@ const seededRuns = [ space: 'LMS', cssSupportedFunction: 'color', cssSupportedSpace: 'rec2020', - rgb: '0xEaDB42', + rgb: '#eadb42', hsl: [335, 0.46, 0.9], hwb: [335, 0.46, 0.9], cmyk: [0.93, 0.46, 0.9, 0.78], @@ -125,28 +125,28 @@ describe('color', () => { describe(`rgb()`, () => { it('should return a random rgb hex color', () => { const color = faker.color.rgb(); - expect(color).match(/^(0x[a-fA-F0-9]{6})$/); + expect(color).match(/^(#[a-f0-9]{6})$/); }); }); - describe(`rgb({ prefix: '#' })`, () => { + describe(`rgb({ prefix: '0x' })`, () => { it('should return a random rgb hex color with # prefix', () => { - const color = faker.color.rgb({ prefix: '#' }); - expect(color).match(/^(#[a-fA-F0-9]{6})$/); + const color = faker.color.rgb({ prefix: '0x' }); + expect(color).match(/^(0x[a-f0-9]{6})$/); }); }); - describe(`rgbHex({ prefix: '#', case: 'lower' })`, () => { + describe(`rgbHex({ prefix: '0x', case: 'lower' })`, () => { it('should return a random rgb hex color with # prefix and lower case only', () => { - const color = faker.color.rgb({ prefix: '#', case: 'lower' }); - expect(color).match(/^(#[a-f0-9]{6})$/); + const color = faker.color.rgb({ prefix: '0x', typeCase: 'lower' }); + expect(color).match(/^(0x[a-f0-9]{6})$/); }); }); - describe(`rgb({ prefix: '#', case: 'upper' })`, () => { + describe(`rgb({ prefix: '0x', case: 'upper' })`, () => { it('should return a random rgb hex color with # prefix and upper case only', () => { - const color = faker.color.rgb({ prefix: '#', case: 'upper' }); - expect(color).match(/^(#[A-F0-9]{6})$/); + const color = faker.color.rgb({ prefix: '0x', typeCase: 'upper' }); + expect(color).match(/^(0x[A-F0-9]{6})$/); }); }); @@ -178,7 +178,7 @@ describe('color', () => { describe(`rgb({ includeAlpha: true })`, () => { it('should return a random rgb color in hex format with alpha value', () => { const color = faker.color.rgb({ includeAlpha: true }); - expect(color).match(/^(0x[a-fA-F0-9]{8})$/); + expect(color).match(/^(#[a-fA-F0-9]{8})$/); }); }); From 11fdcc87847eb404e3a0c6cbc464cc8bd8f0a822 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 16 Apr 2022 19:27:03 -0600 Subject: [PATCH 54/72] feat: add function overloading to generators that can return string or number[] --- src/color.ts | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/color.ts b/src/color.ts index fa1a94fcb98..78f3ae991f7 100644 --- a/src/color.ts +++ b/src/color.ts @@ -29,7 +29,10 @@ export const cssFunctions = [ type CSSFunction = typeof cssFunctions[number]; type CSSSpace = typeof cssSpaces[number]; -type ColorFormat = 'decimal' | 'css' | 'binary'; +type StringColorFormat = 'css' | 'binary'; +type NumberColorFormat = 'decimal'; +type ColorFormat = StringColorFormat | NumberColorFormat; +type TypeCase = 'lower' | 'upper' | 'mixed'; /** * Formats the hex format of a generated color string according @@ -44,7 +47,7 @@ function formatHexColor( hexColor: string, options?: { prefix?: string; - typeCase?: 'upper' | 'lower' | 'mixed'; + typeCase?: TypeCase; } ): string { switch (options?.typeCase) { @@ -226,7 +229,19 @@ export class Color { */ rgb(options?: { prefix?: string; - typeCase?: 'upper' | 'lower' | 'mixed'; + typeCase?: TypeCase; + format?: 'hex' | StringColorFormat; + includeAlpha?: boolean; + }): string; + rgb(options?: { + prefix?: string; + typeCase?: TypeCase; + format?: NumberColorFormat; + includeAlpha?: boolean; + }): number[]; + rgb(options?: { + prefix?: string; + typeCase?: TypeCase; format?: 'hex' | ColorFormat; includeAlpha?: boolean; }): string | number[] { @@ -268,11 +283,12 @@ export class Color { * faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%) * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 */ + cmyk(options?: { format?: StringColorFormat }): string; + cmyk(options?: { format?: NumberColorFormat }): number[]; cmyk(options?: { format?: ColorFormat }): string | number[] { const color: string | number[] = Array.from({ length: 4 }).map(() => this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }) ); - return toColorFormat(color, options?.format || 'decimal', 'cmyk'); } @@ -292,6 +308,11 @@ export class Color { * faker.color.hsl({ format: 'binary' }) // (8-32 bits) x 3 * faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4 */ + hsl(options?: { format?: StringColorFormat; includeAlpha?: boolean }): string; + hsl(options?: { + format?: NumberColorFormat; + includeAlpha?: boolean; + }): number[]; hsl(options?: { format?: ColorFormat; includeAlpha?: boolean; @@ -319,6 +340,8 @@ export class Color { * faker.color.hwb({ format: 'css' }) // hwb(194 0% 0%) * faker.color.hwb({ format: 'binary' }) // (8-32 bits x 3) */ + hwb(options?: { format?: StringColorFormat }): string; + hwb(options?: { format?: NumberColorFormat }): number[]; hwb(options?: { format?: ColorFormat }): string | number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; for (let i = 0; i < 2; i++) { @@ -339,6 +362,8 @@ export class Color { * faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664) * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) */ + lab(options?: { format?: StringColorFormat }): string; + lab(options?: { format?: NumberColorFormat }): number[]; lab(options?: { format?: ColorFormat }): string | number[] { const lab = [ this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }), @@ -366,6 +391,8 @@ export class Color { * faker.color.lch{ format: 'css' }) // lch(52.2345% 72.2 56.2) * faker.color.lch{ format: 'binary' }) // (8-32 bits x 3) */ + lch(options?: { format?: StringColorFormat }): string; + lch(options?: { format?: NumberColorFormat }): number[]; lch(options?: { format?: ColorFormat }): string | number[] { const lch = [ this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }), @@ -391,6 +418,14 @@ export class Color { * faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // color(display-p3 0.12 1 0.23) * faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3) */ + colorByCSSColorSpace(options?: { + format?: StringColorFormat; + space?: CSSSpace; + }): string; + colorByCSSColorSpace(options?: { + format?: NumberColorFormat; + space?: CSSSpace; + }): number[]; colorByCSSColorSpace(options?: { format?: ColorFormat; space?: CSSSpace; From b7d871d048430c88920932339f5d96c082624838 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sat, 16 Apr 2022 22:28:40 -0600 Subject: [PATCH 55/72] feat: add overloaded return types for when no options are passed --- src/color.ts | 7 +++++++ test/color.spec.ts | 26 +++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/color.ts b/src/color.ts index 78f3ae991f7..7cc0f1cad0a 100644 --- a/src/color.ts +++ b/src/color.ts @@ -227,6 +227,7 @@ export class Color { * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [255, 255, 255, 0.4] */ + rgb(): string; rgb(options?: { prefix?: string; typeCase?: TypeCase; @@ -283,6 +284,7 @@ export class Color { * faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%) * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 */ + cmyk(): number[]; cmyk(options?: { format?: StringColorFormat }): string; cmyk(options?: { format?: NumberColorFormat }): number[]; cmyk(options?: { format?: ColorFormat }): string | number[] { @@ -308,6 +310,7 @@ export class Color { * faker.color.hsl({ format: 'binary' }) // (8-32 bits) x 3 * faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4 */ + hsl(): number[]; hsl(options?: { format?: StringColorFormat; includeAlpha?: boolean }): string; hsl(options?: { format?: NumberColorFormat; @@ -340,6 +343,7 @@ export class Color { * faker.color.hwb({ format: 'css' }) // hwb(194 0% 0%) * faker.color.hwb({ format: 'binary' }) // (8-32 bits x 3) */ + hwb(): number[]; hwb(options?: { format?: StringColorFormat }): string; hwb(options?: { format?: NumberColorFormat }): number[]; hwb(options?: { format?: ColorFormat }): string | number[] { @@ -362,6 +366,7 @@ export class Color { * faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664) * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) */ + lab(): number[]; lab(options?: { format?: StringColorFormat }): string; lab(options?: { format?: NumberColorFormat }): number[]; lab(options?: { format?: ColorFormat }): string | number[] { @@ -391,6 +396,7 @@ export class Color { * faker.color.lch{ format: 'css' }) // lch(52.2345% 72.2 56.2) * faker.color.lch{ format: 'binary' }) // (8-32 bits x 3) */ + lch(): number[]; lch(options?: { format?: StringColorFormat }): string; lch(options?: { format?: NumberColorFormat }): number[]; lch(options?: { format?: ColorFormat }): string | number[] { @@ -418,6 +424,7 @@ export class Color { * faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // color(display-p3 0.12 1 0.23) * faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3) */ + colorByCSSColorSpace(): number[]; colorByCSSColorSpace(options?: { format?: StringColorFormat; space?: CSSSpace; diff --git a/test/color.spec.ts b/test/color.spec.ts index 7b7d8c8e2ae..1113b6b62ea 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -154,7 +154,7 @@ describe('color', () => { it('should return a random rgb color in decimal format', () => { const color = faker.color.rgb({ format: 'decimal' }); expect(color).length(3); - (color as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(255); }); @@ -190,7 +190,7 @@ describe('color', () => { }); expect(color[color.length - 1]).toBeGreaterThanOrEqual(0); expect(color[color.length - 1]).toBeLessThanOrEqual(1); - (color.slice(0, 4) as number[]).forEach((value: number) => { + color.slice(0, 4).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(255); }); @@ -220,7 +220,7 @@ describe('color', () => { it('should return a random cmyk color', () => { const color = faker.color.cmyk(); expect(color).length(4); - (color as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); @@ -231,7 +231,7 @@ describe('color', () => { it('should return a random cmyk color in decimal format', () => { const color = faker.color.cmyk({ format: 'decimal' }); expect(color).length(4); - (color as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); @@ -262,7 +262,7 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(360); - (color.slice(1) as number[]).forEach((value: number) => { + color.slice(1).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); @@ -312,7 +312,7 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(360); - (color.slice(1) as number[]).forEach((value: number) => { + color.slice(1).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); @@ -325,7 +325,7 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(360); - (color.slice(1) as number[]).forEach((value: number) => { + color.slice(1).forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); @@ -352,7 +352,7 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(1); - (color.slice(1) as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(-100); expect(value).toBeLessThanOrEqual(100); }); @@ -365,7 +365,7 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(1); - (color.slice(1) as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(-100); expect(value).toBeLessThanOrEqual(100); }); @@ -394,7 +394,7 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(1); - (color.slice(1) as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(230); }); @@ -407,7 +407,7 @@ describe('color', () => { expect(color).length(3); expect(color[0]).toBeGreaterThanOrEqual(0); expect(color[0]).toBeLessThanOrEqual(1); - (color.slice(1) as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(230); }); @@ -434,7 +434,7 @@ describe('color', () => { it('should return a random color for a CSS color space in decimal format', () => { const color = faker.color.colorByCSSColorSpace(); expect(color).length(3); - (color as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); @@ -445,7 +445,7 @@ describe('color', () => { it('should return a random color for a CSS color space in decimal format', () => { const color = faker.color.colorByCSSColorSpace({ format: 'decimal' }); expect(color).length(3); - (color as number[]).forEach((value: number) => { + color.forEach((value: number) => { expect(value).toBeGreaterThanOrEqual(0); expect(value).toBeLessThanOrEqual(1); }); From 8e8f04f6c4546a40de99f604710e6bfd8ed88e5d Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sun, 17 Apr 2022 11:08:27 -0600 Subject: [PATCH 56/72] fix: remove first condition in isFloat check and make css functions and spaces constant case --- src/color.ts | 18 +++++++++--------- test/color.spec.ts | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/color.ts b/src/color.ts index 7cc0f1cad0a..efc61ae4515 100644 --- a/src/color.ts +++ b/src/color.ts @@ -3,19 +3,19 @@ import type { Faker } from '.'; /** * Color space names supported by CSS. */ -export const cssSpaces = [ +export const CSS_SPACES = [ 'sRGB', 'display-p3', 'rec2020', 'a98-rgb', 'prophoto-rgb', 'rec2020', -]; +] as const; /** * Functions supported by CSS to produce color. */ -export const cssFunctions = [ +export const CSS_FUNCTIONS = [ 'rgb', 'rgba', 'hsl', @@ -25,10 +25,10 @@ export const cssFunctions = [ 'lab', 'lch', 'color', -]; +] as const; -type CSSFunction = typeof cssFunctions[number]; -type CSSSpace = typeof cssSpaces[number]; +type CSSFunction = typeof CSS_FUNCTIONS[number]; +type CSSSpace = typeof CSS_SPACES[number]; type StringColorFormat = 'css' | 'binary'; type NumberColorFormat = 'decimal'; type ColorFormat = StringColorFormat | NumberColorFormat; @@ -71,7 +71,7 @@ function formatHexColor( */ function toBinary(values: number[]): string { const binary: string[] = values.map((value) => { - const isFloat = Number(value) === value && value % 1 !== 0; + const isFloat = value % 1 !== 0; if (isFloat) { const buffer = new ArrayBuffer(4); new DataView(buffer).setFloat32(0, value); @@ -193,7 +193,7 @@ export class Color { * faker.color.cssSupportedFunction() // 'rgb' */ cssSupportedFunction(): string { - return this.faker.random.arrayElement(cssFunctions); + return this.faker.random.arrayElement(CSS_FUNCTIONS); } /** @@ -203,7 +203,7 @@ export class Color { * faker.color.cssSupportedSpace() // 'display-p3' */ cssSupportedSpace(): string { - return this.faker.random.arrayElement(cssSpaces); + return this.faker.random.arrayElement(CSS_SPACES); } /** diff --git a/test/color.spec.ts b/test/color.spec.ts index 1113b6b62ea..7f8593edf03 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -1,6 +1,6 @@ import { afterEach, describe, expect, it } from 'vitest'; import { faker } from '../src'; -import { cssFunctions, cssSpaces } from '../src/color'; +import { CSS_FUNCTIONS, CSS_SPACES } from '../src/color'; const seededRuns = [ { @@ -111,14 +111,14 @@ describe('color', () => { describe(`cssSupportedFunction()`, () => { it('should return random css supported color function from css functions array', () => { const func = faker.color.cssSupportedFunction(); - expect(cssFunctions).toContain(func); + expect(CSS_FUNCTIONS).toContain(func); }); }); describe(`cssSupportedSpace()`, () => { it('should return random css supported color space from css spaces array', () => { const space = faker.color.cssSupportedSpace(); - expect(cssSpaces).toContain(space); + expect(CSS_SPACES).toContain(space); }); }); From 3e7da9ed68b35c840a51fa0e070b79b49993e66e Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Sun, 17 Apr 2022 18:45:44 -0600 Subject: [PATCH 57/72] chore: change type case option to be named casing --- src/color.ts | 28 ++++++++++++++-------------- test/color.spec.ts | 4 ++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/color.ts b/src/color.ts index efc61ae4515..84b03b2a505 100644 --- a/src/color.ts +++ b/src/color.ts @@ -32,7 +32,7 @@ type CSSSpace = typeof CSS_SPACES[number]; type StringColorFormat = 'css' | 'binary'; type NumberColorFormat = 'decimal'; type ColorFormat = StringColorFormat | NumberColorFormat; -type TypeCase = 'lower' | 'upper' | 'mixed'; +type Casing = 'lower' | 'upper' | 'mixed'; /** * Formats the hex format of a generated color string according @@ -41,16 +41,16 @@ type TypeCase = 'lower' | 'upper' | 'mixed'; * @param hexColor Hex color string to be formatted. * @param options Options object. * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. - * @param options.typeCase Letter type case of the generated hex color. Defaults to `mixed`. + * @param options.casing Letter type case of the generated hex color. Defaults to `mixed`. */ function formatHexColor( hexColor: string, options?: { prefix?: string; - typeCase?: TypeCase; + casing?: Casing; } ): string { - switch (options?.typeCase) { + switch (options?.casing) { case 'upper': hexColor = hexColor.toUpperCase(); break; @@ -211,17 +211,17 @@ export class Color { * * @param options Options object. * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. - * @param options.typeCase Letter type case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. + * @param options.casing Letter type case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. * @param options.format Format of generated RGB color. Defaults to `hex`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example * faker.color.rgb() // '0xffffFF' * faker.color.rgb({ prefix: '#' }) // '#ffffFF' - * faker.color.rgb({ typeCase: 'upper' }) // '0xFFFFFF' - * faker.color.rgb({ typeCase: 'lower' }) // '0xffffff' - * faker.color.rgb({ prefix: '#', typeCase: 'lower' }) // '#ffffff' - * faker.color.rgb({ format: 'hex', typeCase: 'lower' }) // '#ffffff' + * faker.color.rgb({ casing: 'upper' }) // '0xFFFFFF' + * faker.color.rgb({ casing: 'lower' }) // '0xffffff' + * faker.color.rgb({ prefix: '#', casing: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'hex', casing: 'lower' }) // '#ffffff' * faker.color.rgb({ format: 'decimal' }) // [255, 255, 255] * faker.color.rgb({ format: 'css' }) // 'rgb(255, 0, 0)' * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' @@ -230,19 +230,19 @@ export class Color { rgb(): string; rgb(options?: { prefix?: string; - typeCase?: TypeCase; + casing?: Casing; format?: 'hex' | StringColorFormat; includeAlpha?: boolean; }): string; rgb(options?: { prefix?: string; - typeCase?: TypeCase; + casing?: Casing; format?: NumberColorFormat; includeAlpha?: boolean; }): number[]; rgb(options?: { prefix?: string; - typeCase?: TypeCase; + casing?: Casing; format?: 'hex' | ColorFormat; includeAlpha?: boolean; }): string | number[] { @@ -250,9 +250,9 @@ export class Color { format = 'hex', includeAlpha = false, prefix = '#', - typeCase = 'lower', + casing = 'lower', } = options || {}; - options = { format, includeAlpha, prefix, typeCase }; + options = { format, includeAlpha, prefix, casing }; let color: string | number[]; let cssFunction: CSSFunction = 'rgb'; if (format === 'hex') { diff --git a/test/color.spec.ts b/test/color.spec.ts index 7f8593edf03..c8470562629 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -138,14 +138,14 @@ describe('color', () => { describe(`rgbHex({ prefix: '0x', case: 'lower' })`, () => { it('should return a random rgb hex color with # prefix and lower case only', () => { - const color = faker.color.rgb({ prefix: '0x', typeCase: 'lower' }); + const color = faker.color.rgb({ prefix: '0x', casing: 'lower' }); expect(color).match(/^(0x[a-f0-9]{6})$/); }); }); describe(`rgb({ prefix: '0x', case: 'upper' })`, () => { it('should return a random rgb hex color with # prefix and upper case only', () => { - const color = faker.color.rgb({ prefix: '0x', typeCase: 'upper' }); + const color = faker.color.rgb({ prefix: '0x', casing: 'upper' }); expect(color).match(/^(0x[A-F0-9]{6})$/); }); }); From 9d5cd2c69552755db009b6a093556f962bb4a85f Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 18 Apr 2022 09:21:11 +0200 Subject: [PATCH 58/72] chore: run generate scripts --- docs/.vitepress/api-pages.ts | 1 + src/locales/en/color/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/.vitepress/api-pages.ts b/docs/.vitepress/api-pages.ts index 88f6c48edfc..1e11214682b 100644 --- a/docs/.vitepress/api-pages.ts +++ b/docs/.vitepress/api-pages.ts @@ -3,6 +3,7 @@ export const apiPages = [ { text: 'Address', link: '/api/address.html' }, { text: 'Animal', link: '/api/animal.html' }, + { text: 'Color', link: '/api/color.html' }, { text: 'Commerce', link: '/api/commerce.html' }, { text: 'Company', link: '/api/company.html' }, { text: 'Database', link: '/api/database.html' }, diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index b3a628721ec..2f84c198438 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -6,9 +6,9 @@ import type { ColorDefinitions } from '../../..'; import human from './human'; import space from './space'; -const color = { +const color: ColorDefinitions = { human, space, -} as Partial; +}; export default color; From dfcaa8fd7a732aef7232ea5458aec195edb05b81 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Mon, 9 May 2022 13:52:00 -0600 Subject: [PATCH 59/72] fix: color module import into color spec file --- test/color.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/color.spec.ts b/test/color.spec.ts index c8470562629..f7af52402f0 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -1,6 +1,6 @@ import { afterEach, describe, expect, it } from 'vitest'; import { faker } from '../src'; -import { CSS_FUNCTIONS, CSS_SPACES } from '../src/color'; +import { CSS_FUNCTIONS, CSS_SPACES } from '../src/modules/color'; const seededRuns = [ { From 1ee36fd170cc29f0899dc2715a4594249b373107 Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Mon, 9 May 2022 14:18:56 -0600 Subject: [PATCH 60/72] fix: update seed call in color spec file --- src/modules/commerce/index.ts | 2 +- test/color.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/commerce/index.ts b/src/modules/commerce/index.ts index bb34baec576..d17c473bed6 100644 --- a/src/modules/commerce/index.ts +++ b/src/modules/commerce/index.ts @@ -15,7 +15,7 @@ export class Commerce { } } - /*pnpm run build* + /** * Returns a human readable color name. * * @example diff --git a/test/color.spec.ts b/test/color.spec.ts index f7af52402f0..9d438291783 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -91,7 +91,7 @@ describe('color', () => { faker.seed(Math.ceil(Math.random() * 1_000_000_000)); describe(`random seeded tests for seed ${JSON.stringify( - faker.seedValue + faker.seed() )}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { describe(`human()`, () => { From d261bdba5edc8e5221ec8cb900099e0dae64a5bd Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Wed, 18 May 2022 11:45:21 -0600 Subject: [PATCH 61/72] fix: move function comments to last signature to be picked up by docs --- src/modules/color/index.ts | 88 +++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 8a6805f3c1a..05513b145b6 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -206,6 +206,19 @@ export class Color { return this.faker.helpers.arrayElement(CSS_SPACES); } + rgb(): string; + rgb(options?: { + prefix?: string; + casing?: Casing; + format?: 'hex' | StringColorFormat; + includeAlpha?: boolean; + }): string; + rgb(options?: { + prefix?: string; + casing?: Casing; + format?: NumberColorFormat; + includeAlpha?: boolean; + }): number[]; /** * Returns an RGB color. * @@ -227,19 +240,6 @@ export class Color { * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [255, 255, 255, 0.4] */ - rgb(): string; - rgb(options?: { - prefix?: string; - casing?: Casing; - format?: 'hex' | StringColorFormat; - includeAlpha?: boolean; - }): string; - rgb(options?: { - prefix?: string; - casing?: Casing; - format?: NumberColorFormat; - includeAlpha?: boolean; - }): number[]; rgb(options?: { prefix?: string; casing?: Casing; @@ -272,11 +272,14 @@ export class Color { return toColorFormat(color, format, cssFunction); } + cmyk(): number[]; + cmyk(options?: { format?: StringColorFormat }): string; + cmyk(options?: { format?: NumberColorFormat }): number[]; /** * Returns a CMYK color. * * @param options Options object. - * @param options.format Format of generated CMYK color. Defaults to `decimal`. + * @param options.format Format of generated CMYK color. Defaults to 'decimal'. * * @example * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] @@ -284,9 +287,6 @@ export class Color { * faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%) * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 */ - cmyk(): number[]; - cmyk(options?: { format?: StringColorFormat }): string; - cmyk(options?: { format?: NumberColorFormat }): number[]; cmyk(options?: { format?: ColorFormat }): string | number[] { const color: string | number[] = Array.from({ length: 4 }).map(() => this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }) @@ -294,6 +294,12 @@ export class Color { return toColorFormat(color, options?.format || 'decimal', 'cmyk'); } + hsl(): number[]; + hsl(options?: { format?: StringColorFormat; includeAlpha?: boolean }): string; + hsl(options?: { + format?: NumberColorFormat; + includeAlpha?: boolean; + }): number[]; /** * Returns an HSL color. * @@ -310,12 +316,6 @@ export class Color { * faker.color.hsl({ format: 'binary' }) // (8-32 bits) x 3 * faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4 */ - hsl(): number[]; - hsl(options?: { format?: StringColorFormat; includeAlpha?: boolean }): string; - hsl(options?: { - format?: NumberColorFormat; - includeAlpha?: boolean; - }): number[]; hsl(options?: { format?: ColorFormat; includeAlpha?: boolean; @@ -331,6 +331,9 @@ export class Color { ); } + hwb(): number[]; + hwb(options?: { format?: StringColorFormat }): string; + hwb(options?: { format?: NumberColorFormat }): number[]; /** * Returns an HWB color. * @@ -343,9 +346,6 @@ export class Color { * faker.color.hwb({ format: 'css' }) // hwb(194 0% 0%) * faker.color.hwb({ format: 'binary' }) // (8-32 bits x 3) */ - hwb(): number[]; - hwb(options?: { format?: StringColorFormat }): string; - hwb(options?: { format?: NumberColorFormat }): number[]; hwb(options?: { format?: ColorFormat }): string | number[] { const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })]; for (let i = 0; i < 2; i++) { @@ -354,6 +354,9 @@ export class Color { return toColorFormat(hsl, options?.format || 'decimal', 'hwb'); } + lab(): number[]; + lab(options?: { format?: StringColorFormat }): string; + lab(options?: { format?: NumberColorFormat }): number[]; /** * Returns a LAB (CIELAB) color. * @@ -366,9 +369,6 @@ export class Color { * faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664) * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) */ - lab(): number[]; - lab(options?: { format?: StringColorFormat }): string; - lab(options?: { format?: NumberColorFormat }): number[]; lab(options?: { format?: ColorFormat }): string | number[] { const lab = [ this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }), @@ -381,6 +381,9 @@ export class Color { return toColorFormat(lab, options?.format || 'decimal', 'lab'); } + lch(): number[]; + lch(options?: { format?: StringColorFormat }): string; + lch(options?: { format?: NumberColorFormat }): number[]; /** * Returns an LCH color. Even though upper bound of * chroma in LCH color space is theoretically unbounded, @@ -392,13 +395,10 @@ export class Color { * * @example * faker.color.lch() // [0.522345, 72.2, 56.2] - * faker.color.lch{ format: 'decimal' }) // [0.522345, 72.2, 56.2] - * faker.color.lch{ format: 'css' }) // lch(52.2345% 72.2 56.2) - * faker.color.lch{ format: 'binary' }) // (8-32 bits x 3) + * faker.color.lch({ format: 'decimal' }) // [0.522345, 72.2, 56.2] + * faker.color.lch({ format: 'css' }) // lch(52.2345% 72.2 56.2) + * faker.color.lch({ format: 'binary' }) // (8-32 bits x 3) */ - lch(): number[]; - lch(options?: { format?: StringColorFormat }): string; - lch(options?: { format?: NumberColorFormat }): number[]; lch(options?: { format?: ColorFormat }): string | number[] { const lch = [ this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }), @@ -411,6 +411,15 @@ export class Color { return toColorFormat(lch, options?.format || 'decimal', 'lch'); } + colorByCSSColorSpace(): number[]; + colorByCSSColorSpace(options?: { + format?: StringColorFormat; + space?: CSSSpace; + }): string; + colorByCSSColorSpace(options?: { + format?: NumberColorFormat; + space?: CSSSpace; + }): number[]; /** * Returns a random color based on CSS color space specified. * @@ -424,15 +433,6 @@ export class Color { * faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // color(display-p3 0.12 1 0.23) * faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3) */ - colorByCSSColorSpace(): number[]; - colorByCSSColorSpace(options?: { - format?: StringColorFormat; - space?: CSSSpace; - }): string; - colorByCSSColorSpace(options?: { - format?: NumberColorFormat; - space?: CSSSpace; - }): number[]; colorByCSSColorSpace(options?: { format?: ColorFormat; space?: CSSSpace; From 2cc1f63e49e560efb8cf52d87172351b27c7a65d Mon Sep 17 00:00:00 2001 From: Harsohail Brar Date: Wed, 18 May 2022 11:47:05 -0600 Subject: [PATCH 62/72] chore: run generate:locales --- src/locales/ar/color/index.ts | 3 +-- src/locales/ar/commerce/index.ts | 2 +- src/locales/az/color/index.ts | 3 +-- src/locales/az/commerce/index.ts | 2 +- src/locales/el/color/index.ts | 3 +-- src/locales/el/commerce/index.ts | 2 +- src/locales/en/color/index.ts | 3 +-- src/locales/es/color/index.ts | 3 +-- src/locales/es/commerce/index.ts | 2 +- src/locales/es_MX/color/index.ts | 3 +-- src/locales/es_MX/commerce/index.ts | 2 +- src/locales/fa/color/index.ts | 3 +-- src/locales/fa/commerce/index.ts | 2 +- src/locales/he/color/index.ts | 3 +-- src/locales/hy/color/index.ts | 3 +-- src/locales/lv/color/index.ts | 3 +-- src/locales/lv/commerce/index.ts | 2 +- src/locales/nl/color/index.ts | 3 +-- src/locales/nl/commerce/index.ts | 2 +- src/locales/pt_BR/color/index.ts | 3 +-- src/locales/pt_BR/commerce/index.ts | 2 +- src/locales/pt_PT/color/index.ts | 3 +-- src/locales/pt_PT/commerce/index.ts | 2 +- src/locales/ro/address/index.ts | 2 +- src/locales/ru/color/index.ts | 3 +-- src/locales/ru/commerce/index.ts | 2 +- src/locales/sv/color/index.ts | 3 +-- src/locales/sv/commerce/index.ts | 2 +- src/locales/tr/color/index.ts | 3 +-- src/locales/ur/color/index.ts | 3 +-- src/locales/ur/commerce/index.ts | 2 +- 31 files changed, 31 insertions(+), 48 deletions(-) diff --git a/src/locales/ar/color/index.ts b/src/locales/ar/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/ar/color/index.ts +++ b/src/locales/ar/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/ar/commerce/index.ts b/src/locales/ar/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/ar/commerce/index.ts +++ b/src/locales/ar/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/az/color/index.ts b/src/locales/az/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/az/color/index.ts +++ b/src/locales/az/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/az/commerce/index.ts b/src/locales/az/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/az/commerce/index.ts +++ b/src/locales/az/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/el/color/index.ts b/src/locales/el/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/el/color/index.ts +++ b/src/locales/el/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/el/commerce/index.ts b/src/locales/el/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/el/commerce/index.ts +++ b/src/locales/el/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index 2f84c198438..9d61d69c861 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -2,11 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; import space from './space'; -const color: ColorDefinitions = { +const color = { human, space, }; diff --git a/src/locales/es/color/index.ts b/src/locales/es/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/es/color/index.ts +++ b/src/locales/es/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/es/commerce/index.ts b/src/locales/es/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/es/commerce/index.ts +++ b/src/locales/es/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/es_MX/color/index.ts b/src/locales/es_MX/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/es_MX/color/index.ts +++ b/src/locales/es_MX/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/es_MX/commerce/index.ts b/src/locales/es_MX/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/es_MX/commerce/index.ts +++ b/src/locales/es_MX/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/fa/color/index.ts b/src/locales/fa/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/fa/color/index.ts +++ b/src/locales/fa/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/fa/commerce/index.ts b/src/locales/fa/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/fa/commerce/index.ts +++ b/src/locales/fa/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/he/color/index.ts b/src/locales/he/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/he/color/index.ts +++ b/src/locales/he/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/hy/color/index.ts b/src/locales/hy/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/hy/color/index.ts +++ b/src/locales/hy/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/lv/color/index.ts b/src/locales/lv/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/lv/color/index.ts +++ b/src/locales/lv/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/lv/commerce/index.ts b/src/locales/lv/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/lv/commerce/index.ts +++ b/src/locales/lv/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/nl/color/index.ts b/src/locales/nl/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/nl/color/index.ts +++ b/src/locales/nl/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/nl/commerce/index.ts b/src/locales/nl/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/nl/commerce/index.ts +++ b/src/locales/nl/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/pt_BR/color/index.ts b/src/locales/pt_BR/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/pt_BR/color/index.ts +++ b/src/locales/pt_BR/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/pt_BR/commerce/index.ts b/src/locales/pt_BR/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/pt_BR/commerce/index.ts +++ b/src/locales/pt_BR/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/pt_PT/color/index.ts b/src/locales/pt_PT/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/pt_PT/color/index.ts +++ b/src/locales/pt_PT/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/pt_PT/commerce/index.ts b/src/locales/pt_PT/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/pt_PT/commerce/index.ts +++ b/src/locales/pt_PT/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/ro/address/index.ts b/src/locales/ro/address/index.ts index c45df4de8bc..2ff1dcfd176 100644 --- a/src/locales/ro/address/index.ts +++ b/src/locales/ro/address/index.ts @@ -29,6 +29,6 @@ const address: AddressDefinitions = { street_name, street_suffix, streets, -} as Partial; +}; export default address; diff --git a/src/locales/ru/color/index.ts b/src/locales/ru/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/ru/color/index.ts +++ b/src/locales/ru/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/ru/commerce/index.ts b/src/locales/ru/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/ru/commerce/index.ts +++ b/src/locales/ru/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/sv/color/index.ts b/src/locales/sv/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/sv/color/index.ts +++ b/src/locales/sv/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/sv/commerce/index.ts b/src/locales/sv/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/sv/commerce/index.ts +++ b/src/locales/sv/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; diff --git a/src/locales/tr/color/index.ts b/src/locales/tr/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/tr/color/index.ts +++ b/src/locales/tr/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/ur/color/index.ts b/src/locales/ur/color/index.ts index f55555e9b39..23403f6936b 100644 --- a/src/locales/ur/color/index.ts +++ b/src/locales/ur/color/index.ts @@ -2,10 +2,9 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ -import type { ColorDefinitions } from '../../..'; import human from './human'; -const color: Partial = { +const color = { human, }; diff --git a/src/locales/ur/commerce/index.ts b/src/locales/ur/commerce/index.ts index 1c8b94caae5..7e5f4475212 100644 --- a/src/locales/ur/commerce/index.ts +++ b/src/locales/ur/commerce/index.ts @@ -6,7 +6,7 @@ import type { CommerceDefinitions } from '../../..'; import department from './department'; import product_name from './product_name'; -const commerce: Partial = { +const commerce: CommerceDefinitions = { department, product_name, }; From ae17a7be662218b8e4b04a3d1cbd2f94343fcdfa Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 19 May 2022 19:17:10 +0200 Subject: [PATCH 63/72] chore: update docs --- src/modules/color/index.ts | 233 ++++++++++++++++++++++++++++++++++++- 1 file changed, 227 insertions(+), 6 deletions(-) diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 05513b145b6..f2c4b2fd92d 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -27,12 +27,12 @@ export const CSS_FUNCTIONS = [ 'color', ] as const; -type CSSFunction = typeof CSS_FUNCTIONS[number]; -type CSSSpace = typeof CSS_SPACES[number]; -type StringColorFormat = 'css' | 'binary'; -type NumberColorFormat = 'decimal'; -type ColorFormat = StringColorFormat | NumberColorFormat; -type Casing = 'lower' | 'upper' | 'mixed'; +export type CSSFunction = typeof CSS_FUNCTIONS[number]; +export type CSSSpace = typeof CSS_SPACES[number]; +export type StringColorFormat = 'css' | 'binary'; +export type NumberColorFormat = 'decimal'; +export type ColorFormat = StringColorFormat | NumberColorFormat; +export type Casing = 'lower' | 'upper' | 'mixed'; /** * Formats the hex format of a generated color string according @@ -206,13 +206,56 @@ export class Color { return this.faker.helpers.arrayElement(CSS_SPACES); } + /** + * Returns an RGB color. + * + * @example + * faker.color.rgb() // '0xffffFF' + */ rgb(): string; + /** + * Returns an RGB color. + * + * @param options Options object. + * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. + * @param options.casing Letter type case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. + * @param options.format Format of generated RGB color. Defaults to `hex`. + * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. + * + * @example + * faker.color.rgb() // '0xffffFF' + * faker.color.rgb({ prefix: '#' }) // '#ffffFF' + * faker.color.rgb({ casing: 'upper' }) // '0xFFFFFF' + * faker.color.rgb({ casing: 'lower' }) // '0xffffff' + * faker.color.rgb({ prefix: '#', casing: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'hex', casing: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'css' }) // 'rgb(255, 0, 0)' + * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' + */ rgb(options?: { prefix?: string; casing?: Casing; format?: 'hex' | StringColorFormat; includeAlpha?: boolean; }): string; + /** + * Returns an RGB color. + * + * @param options Options object. + * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. + * @param options.casing Letter type case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. + * @param options.format Format of generated RGB color. Defaults to `hex`. + * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. + * + * @example + * faker.color.rgb() // '0xffffFF' + * faker.color.rgb({ prefix: '#' }) // '#ffffFF' + * faker.color.rgb({ casing: 'upper' }) // '0xFFFFFF' + * faker.color.rgb({ casing: 'lower' }) // '0xffffff' + * faker.color.rgb({ prefix: '#', casing: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'decimal' }) // [255, 255, 255] + * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [255, 255, 255, 0.4] + */ rgb(options?: { prefix?: string; casing?: Casing; @@ -272,8 +315,35 @@ export class Color { return toColorFormat(color, format, cssFunction); } + /** + * Returns a CMYK color. + * + * @example + * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] + */ cmyk(): number[]; + /** + * Returns a CMYK color. + * + * @param options Options object. + * @param options.format Format of generated CMYK color. Defaults to 'decimal'. + * + * @example + * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] + * faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%) + * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 + */ cmyk(options?: { format?: StringColorFormat }): string; + /** + * Returns a CMYK color. + * + * @param options Options object. + * @param options.format Format of generated CMYK color. Defaults to 'decimal'. + * + * @example + * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] + * faker.color.cmyk({ format: 'decimal' }) // [0.31, 0.52, 0.32, 0.43] + */ cmyk(options?: { format?: NumberColorFormat }): number[]; /** * Returns a CMYK color. @@ -294,8 +364,40 @@ export class Color { return toColorFormat(color, options?.format || 'decimal', 'cmyk'); } + /** + * Returns an HSL color. + * + * @example + * faker.color.hsl() // [201, 0.23, 0.32] + */ hsl(): number[]; + /** + * Returns an HSL color. + * + * @param options Options object. + * @param options.format Format of generated HSL color. Defaults to `decimal`. + * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. + * + * @example + * faker.color.hsl() // [201, 0.23, 0.32] + * faker.color.hsl({ format: 'css' }) // hsl(0deg, 100%, 80%) + * faker.color.hsl({ format: 'css', includeAlpha: true }) // hsl(0deg 100% 50% / 0.5) + * faker.color.hsl({ format: 'binary' }) // (8-32 bits) x 3 + * faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4 + */ hsl(options?: { format?: StringColorFormat; includeAlpha?: boolean }): string; + /** + * Returns an HSL color. + * + * @param options Options object. + * @param options.format Format of generated HSL color. Defaults to `decimal`. + * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. + * + * @example + * faker.color.hsl() // [201, 0.23, 0.32] + * faker.color.hsl({ format: 'decimal' }) // [300, 0.21, 0.52] + * faker.color.hsl({ format: 'decimal', includeAlpha: true }) // [300, 0.21, 0.52, 0.28] + */ hsl(options?: { format?: NumberColorFormat; includeAlpha?: boolean; @@ -331,8 +433,35 @@ export class Color { ); } + /** + * Returns an HWB color. + * + * @example + * faker.color.hwb() // [201, 0.21, 0.31] + */ hwb(): number[]; + /** + * Returns an HWB color. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.hwb() // [201, 0.21, 0.31] + * faker.color.hwb({ format: 'css' }) // hwb(194 0% 0%) + * faker.color.hwb({ format: 'binary' }) // (8-32 bits x 3) + */ hwb(options?: { format?: StringColorFormat }): string; + /** + * Returns an HWB color. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.hwb() // [201, 0.21, 0.31] + * faker.color.hwb({ format: 'decimal' }) // [201, 0.21, 0.31] + */ hwb(options?: { format?: NumberColorFormat }): number[]; /** * Returns an HWB color. @@ -354,8 +483,35 @@ export class Color { return toColorFormat(hsl, options?.format || 'decimal', 'hwb'); } + /** + * Returns a LAB (CIELAB) color. + * + * @example + * faker.color.lab() // [0.832133, -80.3245, 100.1234] + */ lab(): number[]; + /** + * Returns a LAB (CIELAB) color. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.lab() // [0.832133, -80.3245, 100.1234] + * faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664) + * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) + */ lab(options?: { format?: StringColorFormat }): string; + /** + * Returns a LAB (CIELAB) color. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.lab() // [0.832133, -80.3245, 100.1234] + * faker.color.lab({ format: 'decimal' }) // [0.856773, -80.2345, 100.2341] + */ lab(options?: { format?: NumberColorFormat }): number[]; /** * Returns a LAB (CIELAB) color. @@ -381,8 +537,44 @@ export class Color { return toColorFormat(lab, options?.format || 'decimal', 'lab'); } + /** + * Returns an LCH color. Even though upper bound of + * chroma in LCH color space is theoretically unbounded, + * it is bounded to 230 as anything above will not + * make a noticeable difference in the browser. + * + * @example + * faker.color.lch() // [0.522345, 72.2, 56.2] + */ lch(): number[]; + /** + * Returns an LCH color. Even though upper bound of + * chroma in LCH color space is theoretically unbounded, + * it is bounded to 230 as anything above will not + * make a noticeable difference in the browser. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.lch() // [0.522345, 72.2, 56.2] + * faker.color.lch({ format: 'css' }) // lch(52.2345% 72.2 56.2) + * faker.color.lch({ format: 'binary' }) // (8-32 bits x 3) + */ lch(options?: { format?: StringColorFormat }): string; + /** + * Returns an LCH color. Even though upper bound of + * chroma in LCH color space is theoretically unbounded, + * it is bounded to 230 as anything above will not + * make a noticeable difference in the browser. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.lch() // [0.522345, 72.2, 56.2] + * faker.color.lch({ format: 'decimal' }) // [0.522345, 72.2, 56.2] + */ lch(options?: { format?: NumberColorFormat }): number[]; /** * Returns an LCH color. Even though upper bound of @@ -411,11 +603,40 @@ export class Color { return toColorFormat(lch, options?.format || 'decimal', 'lch'); } + /** + * Returns a random color based on CSS color space specified. + * + * @example + * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] + */ colorByCSSColorSpace(): number[]; + /** + * Returns a random color based on CSS color space specified. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.space Color space to generate the color for. Defaults to `sRGB`; + * + * @example + * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] + * faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // color(display-p3 0.12 1 0.23) + * faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3) + */ colorByCSSColorSpace(options?: { format?: StringColorFormat; space?: CSSSpace; }): string; + /** + * Returns a random color based on CSS color space specified. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.space Color space to generate the color for. Defaults to `sRGB`; + * + * @example + * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] + * faker.color.colorByCSSColorSpace({ format: 'decimal' }) // [0.12, 0.21, 0.31] + */ colorByCSSColorSpace(options?: { format?: NumberColorFormat; space?: CSSSpace; From 07a3d1e768b2615336bc85673869d26c89e3adc9 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 19 May 2022 19:21:00 +0200 Subject: [PATCH 64/72] chore: fix docs --- src/modules/color/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index f2c4b2fd92d..13a9cf0c28e 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -217,8 +217,8 @@ export class Color { * Returns an RGB color. * * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. - * @param options.casing Letter type case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. + * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `0x`. + * @param options.casing Letter type case of the generated hex color. Only applied when 'hex' format is used. Defaults to `mixed`. * @param options.format Format of generated RGB color. Defaults to `hex`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * @@ -242,8 +242,8 @@ export class Color { * Returns an RGB color. * * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. - * @param options.casing Letter type case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. + * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `0x`. + * @param options.casing Letter type case of the generated hex color. Only applied when 'hex' format is used. Defaults to `mixed`. * @param options.format Format of generated RGB color. Defaults to `hex`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * @@ -266,8 +266,8 @@ export class Color { * Returns an RGB color. * * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. Only applied when 'hex' format is used. - * @param options.casing Letter type case of the generated hex color. Defaults to `mixed`. Only applied when 'hex' format is used. + * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `0x`. + * @param options.casing Letter type case of the generated hex color. Only applied when 'hex' format is used. Defaults to `mixed`. * @param options.format Format of generated RGB color. Defaults to `hex`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * From 1701a1abbed4a2819ed20920cd5dd4b2ab1e0f46 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 19 May 2022 19:25:56 +0200 Subject: [PATCH 65/72] chore: replace internal usage --- src/modules/random/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index e3e36ec4168..89cd44fd3b0 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -42,7 +42,7 @@ export class Random { this.faker.commerce.productAdjective, this.faker.commerce.productMaterial, this.faker.commerce.product, - this.faker.commerce.color, + this.faker.color.human, this.faker.company.catchPhraseAdjective, this.faker.company.catchPhraseDescriptor, From da9da5691be15721961677a586ca7f0d63330bb7 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 19 May 2022 19:28:24 +0200 Subject: [PATCH 66/72] chore: fix generate:locales script --- scripts/generateLocales.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/generateLocales.ts b/scripts/generateLocales.ts index 42a290d39ea..9fd3256b14d 100644 --- a/scripts/generateLocales.ts +++ b/scripts/generateLocales.ts @@ -47,6 +47,7 @@ type DefinitionsType = { const definitionsTypes: DefinitionsType = { address: 'AddressDefinitions', animal: 'AnimalDefinitions', + color: 'ColorDefinitions', commerce: 'CommerceDefinitions', company: 'CompanyDefinitions', database: 'DatabaseDefinitions', From 3a919d39a456b33dd8c65a6e4376b19a957e3abf Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 19 May 2022 19:29:40 +0200 Subject: [PATCH 67/72] chore: generate locales --- src/locales/ar/color/index.ts | 3 ++- src/locales/az/color/index.ts | 3 ++- src/locales/el/color/index.ts | 3 ++- src/locales/en/color/index.ts | 3 ++- src/locales/es/color/index.ts | 3 ++- src/locales/es_MX/color/index.ts | 3 ++- src/locales/fa/color/index.ts | 3 ++- src/locales/he/color/index.ts | 3 ++- src/locales/hy/color/index.ts | 3 ++- src/locales/lv/color/index.ts | 3 ++- src/locales/nl/color/index.ts | 3 ++- src/locales/pt_BR/color/index.ts | 3 ++- src/locales/pt_PT/color/index.ts | 3 ++- src/locales/ru/color/index.ts | 3 ++- src/locales/sv/color/index.ts | 3 ++- src/locales/tr/color/index.ts | 3 ++- src/locales/ur/color/index.ts | 3 ++- 17 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/locales/ar/color/index.ts b/src/locales/ar/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/ar/color/index.ts +++ b/src/locales/ar/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/az/color/index.ts b/src/locales/az/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/az/color/index.ts +++ b/src/locales/az/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/el/color/index.ts b/src/locales/el/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/el/color/index.ts +++ b/src/locales/el/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/en/color/index.ts b/src/locales/en/color/index.ts index 9d61d69c861..2f84c198438 100644 --- a/src/locales/en/color/index.ts +++ b/src/locales/en/color/index.ts @@ -2,10 +2,11 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; import space from './space'; -const color = { +const color: ColorDefinitions = { human, space, }; diff --git a/src/locales/es/color/index.ts b/src/locales/es/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/es/color/index.ts +++ b/src/locales/es/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/es_MX/color/index.ts b/src/locales/es_MX/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/es_MX/color/index.ts +++ b/src/locales/es_MX/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/fa/color/index.ts b/src/locales/fa/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/fa/color/index.ts +++ b/src/locales/fa/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/he/color/index.ts b/src/locales/he/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/he/color/index.ts +++ b/src/locales/he/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/hy/color/index.ts b/src/locales/hy/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/hy/color/index.ts +++ b/src/locales/hy/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/lv/color/index.ts b/src/locales/lv/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/lv/color/index.ts +++ b/src/locales/lv/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/nl/color/index.ts b/src/locales/nl/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/nl/color/index.ts +++ b/src/locales/nl/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/pt_BR/color/index.ts b/src/locales/pt_BR/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/pt_BR/color/index.ts +++ b/src/locales/pt_BR/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/pt_PT/color/index.ts b/src/locales/pt_PT/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/pt_PT/color/index.ts +++ b/src/locales/pt_PT/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/ru/color/index.ts b/src/locales/ru/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/ru/color/index.ts +++ b/src/locales/ru/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/sv/color/index.ts b/src/locales/sv/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/sv/color/index.ts +++ b/src/locales/sv/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/tr/color/index.ts b/src/locales/tr/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/tr/color/index.ts +++ b/src/locales/tr/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; diff --git a/src/locales/ur/color/index.ts b/src/locales/ur/color/index.ts index 23403f6936b..79cc39f02f3 100644 --- a/src/locales/ur/color/index.ts +++ b/src/locales/ur/color/index.ts @@ -2,9 +2,10 @@ * This file is automatically generated. * Run 'pnpm run generate:locales' to update. */ +import type { ColorDefinitions } from '../../..'; import human from './human'; -const color = { +const color: ColorDefinitions = { human, }; From 76b7ced4da4190a684f68f7f104bcd05c7a878db Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 19 May 2022 19:34:19 +0200 Subject: [PATCH 68/72] chore: reexport color types --- src/index.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/index.ts b/src/index.ts index 060fa05a09e..77cb1742716 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,6 +28,15 @@ export type { } from './definitions'; export { FakerError } from './errors/faker-error'; export type { FakerOptions, UsableLocale, UsedLocales } from './faker'; +export type { + Casing, + Color, + ColorFormat, + CSSFunction, + CSSSpace, + NumberColorFormat, + StringColorFormat, +} from './modules/color'; export { Gender } from './modules/name'; export type { GenderType } from './modules/name'; export { Faker }; From 88e355367d6967035be57104da585796eb4f3cd0 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 19 May 2022 19:34:58 +0200 Subject: [PATCH 69/72] chore: not the module --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 77cb1742716..d437fd9d5e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,7 +30,6 @@ export { FakerError } from './errors/faker-error'; export type { FakerOptions, UsableLocale, UsedLocales } from './faker'; export type { Casing, - Color, ColorFormat, CSSFunction, CSSSpace, From 7d312b3f5d2ce6f35c65779c0e33733c11dc1502 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 21 May 2022 20:12:52 +0200 Subject: [PATCH 70/72] chore: add additional signaturs for docs --- src/modules/color/index.ts | 119 +++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 13a9cf0c28e..5466159deb1 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -262,6 +262,33 @@ export class Color { format?: NumberColorFormat; includeAlpha?: boolean; }): number[]; + /** + * Returns an RGB color. + * + * @param options Options object. + * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `0x`. + * @param options.casing Letter type case of the generated hex color. Only applied when 'hex' format is used. Defaults to `mixed`. + * @param options.format Format of generated RGB color. Defaults to `hex`. + * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. + * + * @example + * faker.color.rgb() // '0xffffFF' + * faker.color.rgb({ prefix: '#' }) // '#ffffFF' + * faker.color.rgb({ casing: 'upper' }) // '0xFFFFFF' + * faker.color.rgb({ casing: 'lower' }) // '0xffffff' + * faker.color.rgb({ prefix: '#', casing: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'hex', casing: 'lower' }) // '#ffffff' + * faker.color.rgb({ format: 'decimal' }) // [255, 255, 255] + * faker.color.rgb({ format: 'css' }) // 'rgb(255, 0, 0)' + * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' + * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [255, 255, 255, 0.4] + */ + rgb(options?: { + prefix?: string; + casing?: Casing; + format?: 'hex' | ColorFormat; + includeAlpha?: boolean; + }): string | number[]; /** * Returns an RGB color. * @@ -345,6 +372,19 @@ export class Color { * faker.color.cmyk({ format: 'decimal' }) // [0.31, 0.52, 0.32, 0.43] */ cmyk(options?: { format?: NumberColorFormat }): number[]; + /** + * Returns a CMYK color. + * + * @param options Options object. + * @param options.format Format of generated CMYK color. Defaults to 'decimal'. + * + * @example + * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] + * faker.color.cmyk({ format: 'decimal' }) // [0.31, 0.52, 0.32, 0.43] + * faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%) + * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 + */ + cmyk(options?: { format?: ColorFormat }): string | number[]; /** * Returns a CMYK color. * @@ -402,6 +442,26 @@ export class Color { format?: NumberColorFormat; includeAlpha?: boolean; }): number[]; + /** + * Returns an HSL color. + * + * @param options Options object. + * @param options.format Format of generated HSL color. Defaults to `decimal`. + * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. + * + * @example + * faker.color.hsl() // [201, 0.23, 0.32] + * faker.color.hsl({ format: 'decimal' }) // [300, 0.21, 0.52] + * faker.color.hsl({ format: 'decimal', includeAlpha: true }) // [300, 0.21, 0.52, 0.28] + * faker.color.hsl({ format: 'css' }) // hsl(0deg, 100%, 80%) + * faker.color.hsl({ format: 'css', includeAlpha: true }) // hsl(0deg 100% 50% / 0.5) + * faker.color.hsl({ format: 'binary' }) // (8-32 bits) x 3 + * faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4 + */ + hsl(options?: { + format?: ColorFormat; + includeAlpha?: boolean; + }): string | number[]; /** * Returns an HSL color. * @@ -463,6 +523,19 @@ export class Color { * faker.color.hwb({ format: 'decimal' }) // [201, 0.21, 0.31] */ hwb(options?: { format?: NumberColorFormat }): number[]; + /** + * Returns an HWB color. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.hwb() // [201, 0.21, 0.31] + * faker.color.hwb({ format: 'decimal' }) // [201, 0.21, 0.31] + * faker.color.hwb({ format: 'css' }) // hwb(194 0% 0%) + * faker.color.hwb({ format: 'binary' }) // (8-32 bits x 3) + */ + hwb(options?: { format?: ColorFormat }): string | number[]; /** * Returns an HWB color. * @@ -513,6 +586,19 @@ export class Color { * faker.color.lab({ format: 'decimal' }) // [0.856773, -80.2345, 100.2341] */ lab(options?: { format?: NumberColorFormat }): number[]; + /** + * Returns a LAB (CIELAB) color. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.lab() // [0.832133, -80.3245, 100.1234] + * faker.color.lab({ format: 'decimal' }) // [0.856773, -80.2345, 100.2341] + * faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664) + * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) + */ + lab(options?: { format?: ColorFormat }): string | number[]; /** * Returns a LAB (CIELAB) color. * @@ -576,6 +662,22 @@ export class Color { * faker.color.lch({ format: 'decimal' }) // [0.522345, 72.2, 56.2] */ lch(options?: { format?: NumberColorFormat }): number[]; + /** + * Returns an LCH color. Even though upper bound of + * chroma in LCH color space is theoretically unbounded, + * it is bounded to 230 as anything above will not + * make a noticeable difference in the browser. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * + * @example + * faker.color.lch() // [0.522345, 72.2, 56.2] + * faker.color.lch({ format: 'decimal' }) // [0.522345, 72.2, 56.2] + * faker.color.lch({ format: 'css' }) // lch(52.2345% 72.2 56.2) + * faker.color.lch({ format: 'binary' }) // (8-32 bits x 3) + */ + lch(options?: { format?: ColorFormat }): string | number[]; /** * Returns an LCH color. Even though upper bound of * chroma in LCH color space is theoretically unbounded, @@ -641,6 +743,23 @@ export class Color { format?: NumberColorFormat; space?: CSSSpace; }): number[]; + /** + * Returns a random color based on CSS color space specified. + * + * @param options Options object. + * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.space Color space to generate the color for. Defaults to `sRGB`; + * + * @example + * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] + * faker.color.colorByCSSColorSpace({ format: 'decimal' }) // [0.12, 0.21, 0.31] + * faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // color(display-p3 0.12 1 0.23) + * faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3) + */ + colorByCSSColorSpace(options?: { + format?: ColorFormat; + space?: CSSSpace; + }): string | number[]; /** * Returns a random color based on CSS color space specified. * From 9a4e06e0e3ac5a1fc3b93479be6d42fec4097b8e Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 21 May 2022 23:09:49 +0200 Subject: [PATCH 71/72] chore: apply suggestions --- src/modules/color/index.ts | 90 +++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 5466159deb1..c0aeb7a8cbe 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -40,8 +40,8 @@ export type Casing = 'lower' | 'upper' | 'mixed'; * * @param hexColor Hex color string to be formatted. * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Defaults to `0x`. - * @param options.casing Letter type case of the generated hex color. Defaults to `mixed`. + * @param options.prefix Prefix of the generated hex color. Defaults to `'0x'`. + * @param options.casing Letter type case of the generated hex color. Defaults to `'mixed'`. */ function formatHexColor( hexColor: string, @@ -87,8 +87,8 @@ function toBinary(values: number[]): string { * Converts an array of numbers into CSS accepted format. * * @param values Array of values to be converted. - * @param cssFunction CSS function to be generated for the color. Defaults to `rgb`. - * @param space Color space to format CSS color function with. Defaults to `sRGB`. + * @param cssFunction CSS function to be generated for the color. Defaults to `'rgb'`. + * @param space Color space to format CSS color function with. Defaults to `'sRGB'`. */ function toCSS( values: number[], @@ -132,8 +132,8 @@ function toCSS( * * @param values Array of color values to be converted. * @param format Format of generated RGB color. - * @param cssFunction CSS function to be generated for the color. Defaults to `rgb`. - * @param space Color space to format CSS color function with. Defaults to `sRGB`. + * @param cssFunction CSS function to be generated for the color. Defaults to `'rgb'`. + * @param space Color space to format CSS color function with. Defaults to `'sRGB'`. */ function toColorFormat( values: number[], @@ -217,8 +217,8 @@ export class Color { * Returns an RGB color. * * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `0x`. - * @param options.casing Letter type case of the generated hex color. Only applied when 'hex' format is used. Defaults to `mixed`. + * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `'0x'`. + * @param options.casing Letter type case of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'mixed'`. * @param options.format Format of generated RGB color. Defaults to `hex`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * @@ -242,9 +242,9 @@ export class Color { * Returns an RGB color. * * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `0x`. - * @param options.casing Letter type case of the generated hex color. Only applied when 'hex' format is used. Defaults to `mixed`. - * @param options.format Format of generated RGB color. Defaults to `hex`. + * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `'0x'`. + * @param options.casing Letter type case of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'mixed'`. + * @param options.format Format of generated RGB color. Defaults to `'hex'`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example @@ -266,9 +266,9 @@ export class Color { * Returns an RGB color. * * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `0x`. - * @param options.casing Letter type case of the generated hex color. Only applied when 'hex' format is used. Defaults to `mixed`. - * @param options.format Format of generated RGB color. Defaults to `hex`. + * @param options.prefix Prefix of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'0x'`. + * @param options.casing Letter type case of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'mixed'`. + * @param options.format Format of generated RGB color. Defaults to `'hex'`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example @@ -293,9 +293,9 @@ export class Color { * Returns an RGB color. * * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `0x`. - * @param options.casing Letter type case of the generated hex color. Only applied when 'hex' format is used. Defaults to `mixed`. - * @param options.format Format of generated RGB color. Defaults to `hex`. + * @param options.prefix Prefix of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'0x'`. + * @param options.casing Letter type case of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'mixed'`. + * @param options.format Format of generated RGB color. Defaults to `'hex'`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example @@ -353,7 +353,7 @@ export class Color { * Returns a CMYK color. * * @param options Options object. - * @param options.format Format of generated CMYK color. Defaults to 'decimal'. + * @param options.format Format of generated CMYK color. Defaults to `'decimal'`. * * @example * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] @@ -365,7 +365,7 @@ export class Color { * Returns a CMYK color. * * @param options Options object. - * @param options.format Format of generated CMYK color. Defaults to 'decimal'. + * @param options.format Format of generated CMYK color. Defaults to `'decimal'`. * * @example * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] @@ -376,7 +376,7 @@ export class Color { * Returns a CMYK color. * * @param options Options object. - * @param options.format Format of generated CMYK color. Defaults to 'decimal'. + * @param options.format Format of generated CMYK color. Defaults to `'decimal'`. * * @example * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] @@ -389,7 +389,7 @@ export class Color { * Returns a CMYK color. * * @param options Options object. - * @param options.format Format of generated CMYK color. Defaults to 'decimal'. + * @param options.format Format of generated CMYK color. Defaults to `'decimal'`. * * @example * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] @@ -415,7 +415,7 @@ export class Color { * Returns an HSL color. * * @param options Options object. - * @param options.format Format of generated HSL color. Defaults to `decimal`. + * @param options.format Format of generated HSL color. Defaults to `'decimal'`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example @@ -430,7 +430,7 @@ export class Color { * Returns an HSL color. * * @param options Options object. - * @param options.format Format of generated HSL color. Defaults to `decimal`. + * @param options.format Format of generated HSL color. Defaults to `'decimal'`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example @@ -446,7 +446,7 @@ export class Color { * Returns an HSL color. * * @param options Options object. - * @param options.format Format of generated HSL color. Defaults to `decimal`. + * @param options.format Format of generated HSL color. Defaults to `'decimal'`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example @@ -466,7 +466,7 @@ export class Color { * Returns an HSL color. * * @param options Options object. - * @param options.format Format of generated HSL color. Defaults to `decimal`. + * @param options.format Format of generated HSL color. Defaults to `'decimal'`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example @@ -504,7 +504,7 @@ export class Color { * Returns an HWB color. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.hwb() // [201, 0.21, 0.31] @@ -516,7 +516,7 @@ export class Color { * Returns an HWB color. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.hwb() // [201, 0.21, 0.31] @@ -527,7 +527,7 @@ export class Color { * Returns an HWB color. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.hwb() // [201, 0.21, 0.31] @@ -540,7 +540,7 @@ export class Color { * Returns an HWB color. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.hwb() // [201, 0.21, 0.31] @@ -567,7 +567,7 @@ export class Color { * Returns a LAB (CIELAB) color. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.lab() // [0.832133, -80.3245, 100.1234] @@ -579,7 +579,7 @@ export class Color { * Returns a LAB (CIELAB) color. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.lab() // [0.832133, -80.3245, 100.1234] @@ -590,7 +590,7 @@ export class Color { * Returns a LAB (CIELAB) color. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.lab() // [0.832133, -80.3245, 100.1234] @@ -603,7 +603,7 @@ export class Color { * Returns a LAB (CIELAB) color. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.lab() // [0.832133, -80.3245, 100.1234] @@ -640,7 +640,7 @@ export class Color { * make a noticeable difference in the browser. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.lch() // [0.522345, 72.2, 56.2] @@ -655,7 +655,7 @@ export class Color { * make a noticeable difference in the browser. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.lch() // [0.522345, 72.2, 56.2] @@ -669,7 +669,7 @@ export class Color { * make a noticeable difference in the browser. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.lch() // [0.522345, 72.2, 56.2] @@ -685,7 +685,7 @@ export class Color { * make a noticeable difference in the browser. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. * * @example * faker.color.lch() // [0.522345, 72.2, 56.2] @@ -716,8 +716,8 @@ export class Color { * Returns a random color based on CSS color space specified. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. - * @param options.space Color space to generate the color for. Defaults to `sRGB`; + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. + * @param options.space Color space to generate the color for. Defaults to `'sRGB'`. * * @example * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] @@ -732,8 +732,8 @@ export class Color { * Returns a random color based on CSS color space specified. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. - * @param options.space Color space to generate the color for. Defaults to `sRGB`; + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. + * @param options.space Color space to generate the color for. Defaults to `'sRGB'`. * * @example * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] @@ -747,8 +747,8 @@ export class Color { * Returns a random color based on CSS color space specified. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. - * @param options.space Color space to generate the color for. Defaults to `sRGB`; + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. + * @param options.space Color space to generate the color for. Defaults to `'sRGB'`. * * @example * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] @@ -764,8 +764,8 @@ export class Color { * Returns a random color based on CSS color space specified. * * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `decimal`. - * @param options.space Color space to generate the color for. Defaults to `sRGB`; + * @param options.format Format of generated RGB color. Defaults to `'decimal'`. + * @param options.space Color space to generate the color for. Defaults to `'sRGB'`. * * @example * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] From b172a943e441c7006a9e2846ec46ffbae8d4b663 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 22 May 2022 12:58:59 +0200 Subject: [PATCH 72/72] chore: apply jsdoc suggestions --- src/modules/color/index.ts | 97 -------------------------------------- 1 file changed, 97 deletions(-) diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index c0aeb7a8cbe..8b5fdba277c 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -242,23 +242,15 @@ export class Color { * Returns an RGB color. * * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Only applied when 'hex' format is used. Defaults to `'0x'`. - * @param options.casing Letter type case of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'mixed'`. * @param options.format Format of generated RGB color. Defaults to `'hex'`. * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. * * @example * faker.color.rgb() // '0xffffFF' - * faker.color.rgb({ prefix: '#' }) // '#ffffFF' - * faker.color.rgb({ casing: 'upper' }) // '0xFFFFFF' - * faker.color.rgb({ casing: 'lower' }) // '0xffffff' - * faker.color.rgb({ prefix: '#', casing: 'lower' }) // '#ffffff' * faker.color.rgb({ format: 'decimal' }) // [255, 255, 255] * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [255, 255, 255, 0.4] */ rgb(options?: { - prefix?: string; - casing?: Casing; format?: NumberColorFormat; includeAlpha?: boolean; }): number[]; @@ -289,27 +281,6 @@ export class Color { format?: 'hex' | ColorFormat; includeAlpha?: boolean; }): string | number[]; - /** - * Returns an RGB color. - * - * @param options Options object. - * @param options.prefix Prefix of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'0x'`. - * @param options.casing Letter type case of the generated hex color. Only applied when `'hex'` format is used. Defaults to `'mixed'`. - * @param options.format Format of generated RGB color. Defaults to `'hex'`. - * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. - * - * @example - * faker.color.rgb() // '0xffffFF' - * faker.color.rgb({ prefix: '#' }) // '#ffffFF' - * faker.color.rgb({ casing: 'upper' }) // '0xFFFFFF' - * faker.color.rgb({ casing: 'lower' }) // '0xffffff' - * faker.color.rgb({ prefix: '#', casing: 'lower' }) // '#ffffff' - * faker.color.rgb({ format: 'hex', casing: 'lower' }) // '#ffffff' - * faker.color.rgb({ format: 'decimal' }) // [255, 255, 255] - * faker.color.rgb({ format: 'css' }) // 'rgb(255, 0, 0)' - * faker.color.rgb({ format: 'binary' }) // '10000000 00000000 11111111' - * faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [255, 255, 255, 0.4] - */ rgb(options?: { prefix?: string; casing?: Casing; @@ -385,18 +356,6 @@ export class Color { * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 */ cmyk(options?: { format?: ColorFormat }): string | number[]; - /** - * Returns a CMYK color. - * - * @param options Options object. - * @param options.format Format of generated CMYK color. Defaults to `'decimal'`. - * - * @example - * faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43] - * faker.color.cmyk({ format: 'decimal' }) // [0.31, 0.52, 0.32, 0.43] - * faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%) - * faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4 - */ cmyk(options?: { format?: ColorFormat }): string | number[] { const color: string | number[] = Array.from({ length: 4 }).map(() => this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }) @@ -462,22 +421,6 @@ export class Color { format?: ColorFormat; includeAlpha?: boolean; }): string | number[]; - /** - * Returns an HSL color. - * - * @param options Options object. - * @param options.format Format of generated HSL color. Defaults to `'decimal'`. - * @param options.includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. - * - * @example - * faker.color.hsl() // [201, 0.23, 0.32] - * faker.color.hsl({ format: 'decimal' }) // [300, 0.21, 0.52] - * faker.color.hsl({ format: 'decimal', includeAlpha: true }) // [300, 0.21, 0.52, 0.28] - * faker.color.hsl({ format: 'css' }) // hsl(0deg, 100%, 80%) - * faker.color.hsl({ format: 'css', includeAlpha: true }) // hsl(0deg 100% 50% / 0.5) - * faker.color.hsl({ format: 'binary' }) // (8-32 bits) x 3 - * faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4 - */ hsl(options?: { format?: ColorFormat; includeAlpha?: boolean; @@ -599,18 +542,6 @@ export class Color { * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) */ lab(options?: { format?: ColorFormat }): string | number[]; - /** - * Returns a LAB (CIELAB) color. - * - * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `'decimal'`. - * - * @example - * faker.color.lab() // [0.832133, -80.3245, 100.1234] - * faker.color.lab({ format: 'decimal' }) // [0.856773, -80.2345, 100.2341] - * faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664) - * faker.color.lab({ format: 'binary' }) // (8-32 bits x 3) - */ lab(options?: { format?: ColorFormat }): string | number[] { const lab = [ this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }), @@ -678,21 +609,6 @@ export class Color { * faker.color.lch({ format: 'binary' }) // (8-32 bits x 3) */ lch(options?: { format?: ColorFormat }): string | number[]; - /** - * Returns an LCH color. Even though upper bound of - * chroma in LCH color space is theoretically unbounded, - * it is bounded to 230 as anything above will not - * make a noticeable difference in the browser. - * - * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `'decimal'`. - * - * @example - * faker.color.lch() // [0.522345, 72.2, 56.2] - * faker.color.lch({ format: 'decimal' }) // [0.522345, 72.2, 56.2] - * faker.color.lch({ format: 'css' }) // lch(52.2345% 72.2 56.2) - * faker.color.lch({ format: 'binary' }) // (8-32 bits x 3) - */ lch(options?: { format?: ColorFormat }): string | number[] { const lch = [ this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }), @@ -760,19 +676,6 @@ export class Color { format?: ColorFormat; space?: CSSSpace; }): string | number[]; - /** - * Returns a random color based on CSS color space specified. - * - * @param options Options object. - * @param options.format Format of generated RGB color. Defaults to `'decimal'`. - * @param options.space Color space to generate the color for. Defaults to `'sRGB'`. - * - * @example - * faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82] - * faker.color.colorByCSSColorSpace({ format: 'decimal' }) // [0.12, 0.21, 0.31] - * faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // color(display-p3 0.12 1 0.23) - * faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3) - */ colorByCSSColorSpace(options?: { format?: ColorFormat; space?: CSSSpace;