Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add a random string generator for javascript #55

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

Merged
Mathys-Gasnier merged 3 commits into quicksnip-dev:main from kruimol:main
Dec 31, 2024
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
Prev Previous commit
Merge branch 'main' into main
  • Loading branch information
Mathys-Gasnier authored Dec 31, 2024
commit b31a78114a3b2b53b0ec74cd76cb263cfb33c6ce
95 changes: 56 additions & 39 deletions public/data/javascript.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -767,45 +767,62 @@
"tags": ["javascript", "utility", "throttle", "performance"],
"author": "dostonnabotov"
},
{
"title": "Get Contrast Color",
"description": "Returns either black or white text color based on the brightness of the provided hex color.",
"code": [
"const getContrastColor = (hexColor) => {",
" // Expand short hex color to full format",
" if (hexColor.length === 4) {",
" hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;",
" }",
" const r = parseInt(hexColor.slice(1, 3), 16);",
" const g = parseInt(hexColor.slice(3, 5), 16);",
" const b = parseInt(hexColor.slice(5, 7), 16);",
" const brightness = (r * 299 + g * 587 + b * 114) / 1000;",
" return brightness >= 128 ? \"#000000\" : \"#FFFFFF\";",
"};",
"",
"// Usage:",
"console.log(getContrastColor('#fff')); // Output: #000000 (black)",
"console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)",
"console.log(getContrastColor('#ff6347')); // Output: #000000 (black)",
"console.log(getContrastColor('#f4f')); // Output: #000000 (black)"
],
"tags": ["color", "hex", "contrast", "brightness", "utility"],
"author": "yaya12085"
},
{
"title": "Random string",
"description": "Generates a random string of characters of a certain length",
"code": [
"function makeid(length, characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {",
" return Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * characters.length))).join('');",
"}",
"",
"console.log(makeid(5), \"1234\" /* (optional) */);"
],
"tags": ["javascript", "function", "random"],
"author": "kruimol"
}

{
"title": "Get Contrast Color",
"description": "Returns either black or white text color based on the brightness of the provided hex color.",
"code": [
"const getContrastColor = (hexColor) => {",
" // Expand short hex color to full format",
" if (hexColor.length === 4) {",
" hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;",
" }",
" const r = parseInt(hexColor.slice(1, 3), 16);",
" const g = parseInt(hexColor.slice(3, 5), 16);",
" const b = parseInt(hexColor.slice(5, 7), 16);",
" const brightness = (r * 299 + g * 587 + b * 114) / 1000;",
" return brightness >= 128 ? \"#000000\" : \"#FFFFFF\";",
"};",
"",
"// Usage:",
"console.log(getContrastColor('#fff')); // Output: #000000 (black)",
"console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)",
"console.log(getContrastColor('#ff6347')); // Output: #000000 (black)",
"console.log(getContrastColor('#f4f')); // Output: #000000 (black)"
],
"tags": ["javascript", "color", "hex", "contrast", "brightness", "utility"],
"author": "yaya12085"
},
{
"title": "Sleep Function",
"description": "Waits for a specified amount of milliseconds before resolving.",
"code": [
"const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));",
"",
"// Usage:",
"async function main() {",
" console.log('Hello');",
" await sleep(2000); // Waits for 2 seconds",
" console.log('World!');",
"}",
"",
"main();"
],
"tags": ["javascript", "sleep", "delay", "utility", "promises"],
"author": "0xHouss"
},
{
"title": "Random string",
"description": "Generates a random string of characters of a certain length",
"code": [
"function makeid(length, characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {",
" return Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * characters.length))).join('');",
"}",
"",
"console.log(makeid(5), \"1234\" /* (optional) */);"
],
"tags": ["javascript", "function", "random"],
"author": "kruimol"
}
]
},
{
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /