-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from jjcantu/new-snippets-js
[Snippets] - add a few useful JS snippets
- Loading branch information
Showing
5 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
snippets/javascript/color-manipulation/rgb-to-hex-color.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: RGB to Hex Color | ||
description: Converts RGB color values to hexadecimal color code. | ||
author: jjcantu | ||
tags: color,conversion | ||
--- | ||
|
||
```js | ||
function rgbToHex(r, g, b) { | ||
const toHex = (n) => { | ||
const hex = n.toString(16); | ||
return hex.length === 1 ? '0' + hex : hex; | ||
}; | ||
|
||
return '#' + toHex(r) + toHex(g) + toHex(b); | ||
} | ||
|
||
// Usage: | ||
console.log(rgbToHex(255, 128, 0)); // Output: "#ff8000" | ||
console.log(rgbToHex(0, 255, 0)); // Output: "#00ff00" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Format File Size | ||
description: Converts bytes into human-readable file size format. | ||
author: jjcantu | ||
tags: format,size | ||
--- | ||
|
||
```js | ||
function formatFileSize(bytes) { | ||
if (bytes === 0) return '0 Bytes'; | ||
|
||
const k = 1024; | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
} | ||
|
||
// Usage: | ||
console.log(formatFileSize(1234)); // Output: "1.21 KB" | ||
console.log(formatFileSize(1234567)); // Output: "1.18 MB" | ||
``` |
27 changes: 27 additions & 0 deletions
27
snippets/javascript/object-manipulation/deep-clone-object.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: Deep Clone Object | ||
description: Creates a deep copy of an object or array without reference. | ||
author: jjcantu | ||
tags: object,clone | ||
--- | ||
|
||
```js | ||
function deepClone(obj) { | ||
if (obj === null || typeof obj !== 'object') return obj; | ||
|
||
const clone = Array.isArray(obj) ? [] : {}; | ||
|
||
for (let key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
clone[key] = deepClone(obj[key]); | ||
} | ||
} | ||
|
||
return clone; | ||
} | ||
|
||
// Usage: | ||
const original = { a: 1, b: { c: 2 }, d: [1, 2, 3] }; | ||
const cloned = deepClone(original); | ||
console.log(cloned); // Output: 'original' but cloned | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
title: Generate UUID | ||
description: Generates a UUID (v4) string. | ||
author: jjcantu | ||
tags: uuid, generate, string | ||
--- | ||
|
||
```js | ||
function generateUUID() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
const r = Math.random() * 16 | 0; | ||
const v = c === 'x' ? r : (r & 0x3 | 0x8); | ||
return v.toString(16); | ||
}); | ||
} | ||
|
||
// Usage: | ||
console.log(generateUUID()); // Output: "a1b2c3d4-e5f6-4g7h-8i9j-k0l1m2n3o4p5" | ||
``` |