Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: prefer string templates over string concatenation #732

Merged
merged 24 commits into from
Apr 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions src/internet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,35 +278,44 @@ export class Internet {
}

/**
* Generates a random css hex color code.
* Calculates aesthetically pleasing color from a given base.
*
* @param baseRed255 The optional base red. Used for aesthetically pleasing color palettes. Supports values between `0` and `255`. Defaults to `0`.
* @param baseGreen255 The optional base green. Used for aesthetically pleasing color palettes. Supports values between `0` and `255`. Defaults to `0`.
* @param baseBlue255 The optional base blue. Used for aesthetically pleasing color palettes. Supports values between `0` and `255`. Defaults to `0`.
* Based on:
* http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette
*
* @param base Base color value in range form 0 to 255
* @private
*/
private colorFromBase(base: number): string {
pkuczynski marked this conversation as resolved.
Show resolved Hide resolved
return Math.floor((this.faker.datatype.number(256) + base) / 2)
.toString(16)
.padStart(2, '0');
}

/**
* Generates a random css hex color code in aesthetically pleasing color palette.
*
* Based on
* http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette
*
* @param redBase The optional base red in range between `0` and `255`. Defaults to `0`.
* @param greenBase The optional base green in range between `0` and `255`. Defaults to `0`.
* @param blueBase The optional base blue in range between `0` and `255`. Defaults to `0`.
*
* @example
* faker.internet.color() // '#30686e'
* faker.internet.color(100, 100, 100) // '#4e5f8b'
*/
color(
baseRed255: number = 0,
baseGreen255: number = 0,
baseBlue255: number = 0
redBase: number = 0,
greenBase: number = 0,
blueBase: number = 0
): string {
// based on awesome response : http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette
const red = Math.floor((this.faker.datatype.number(256) + baseRed255) / 2);
const green = Math.floor(
(this.faker.datatype.number(256) + baseGreen255) / 2
);
const blue = Math.floor(
(this.faker.datatype.number(256) + baseBlue255) / 2
);
const redStr = red.toString(16);
const greenStr = green.toString(16);
const blueStr = blue.toString(16);
return `#${redStr.length === 1 ? '0' : ''}${redStr}${
greenStr.length === 1 ? '0' : ''
}${greenStr}${blueStr.length === 1 ? '0' : ''}${blueStr}`;
const red = this.colorFromBase(redBase);
const green = this.colorFromBase(greenBase);
const blue = this.colorFromBase(blueBase);

return `#${red}${green}${blue}`;
}

/**
Expand Down