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 utility functions and snippets to javascript.json #36

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
Changes from all commits
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
130 changes: 129 additions & 1 deletion public/data/javascript.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,106 @@
],
"tags": ["javascript", "string", "reverse", "utility"],
"author": "dostonnabotov"
},
{
"title": "Truncate Text",
"description": "Truncates the text to a maximum length and appends '...' if the text exceeds the maximum length.",
"code": [
"const truncateText = (text = '', maxLength = 50) => {",
" return `${text.slice(0, maxLength)}${text.length >= maxLength ? '...' : ''}`;",
"};",
"",
"// Usage:",
"const title = \"Hello, World! This is a Test.\";",
"console.log(truncateText(title)); // Output: 'Hello, World! This is a Test.'",
"console.log(truncateText(title, 10)); // Output: 'Hello, Wor...'"
],
"tags": ["javascript", "string", "truncate", "utility", "text"],
"author": "realvishalrana"
},
{
"title": "Data with Prefix",
"description": "Adds a prefix and postfix to data, with a fallback value.",
"code": [
"const dataWithPrefix = (data, fallback = '-', prefix = '', postfix = '') => {",
" return data ? `${prefix}${data}${postfix}` : fallback;",
"};",
"",
"// Usage:",
"console.log(dataWithPrefix('123', '-', '(', ')')); // Output: '(123)'",
"console.log(dataWithPrefix('', '-', '(', ')')); // Output: '-'",
"console.log(dataWithPrefix('Hello', 'N/A', 'Mr. ', '')); // Output: 'Mr. Hello'",
"console.log(dataWithPrefix(null, 'N/A', 'Mr. ', '')); // Output: 'N/A'"
],
"tags": ["javascript", "data", "utility"],
"author": "realvishalrana"
}
]
},
{
"categoryName": "Object Manipulation",
"snippets": [
{
"title": "Filter Object",
"description": "Filter out entries in an object where the value is falsy, including empty strings, empty objects, null, and undefined.",
"code": [
"export const filterObject = (object = {}) =>",
" Object.fromEntries(",
" Object.entries(object)",
" .filter(([key, value]) => value !== null && value !== undefined && value !== '' && (typeof value !== 'object' || Object.keys(value).length > 0))",
" );",
"",
"// Usage:",
"const obj1 = { a: 1, b: null, c: undefined, d: 4, e: '', f: {} };",
"console.log(filterObject(obj1)); // Output: { a: 1, d: 4 }",
"",
"const obj2 = { x: 0, y: false, z: 'Hello', w: [] };",
"console.log(filterObject(obj2)); // Output: { z: 'Hello' }",
"",
"const obj3 = { name: 'John', age: null, address: { city: 'New York' }, phone: '' };",
"console.log(filterObject(obj3)); // Output: { name: 'John', address: { city: 'New York' } }",
"",
"const obj4 = { a: 0, b: '', c: false, d: {}, e: 'Valid' };",
"console.log(filterObject(obj4)); // Output: { e: 'Valid' }"
],
"tags": ["javascript", "object", "filter", "utility"],
"author": "realvishalrana"
},
{
"title": "Get Nested Value",
"description": "Retrieves the value at a given path in a nested object.",
"code": [
"const getNestedValue = (obj, path) => {",
" const keys = path.split('.');",
" return keys.reduce((currentObject, key) => {",
" return currentObject && typeof currentObject === 'object' ? currentObject[key] : undefined;",
" }, obj);",
"};",
"",
"// Usage:",
"const obj = { a: { b: { c: 42 } } };",
"console.log(getNestedValue(obj, 'a.b.c')); // Output: 42"
],
"tags": ["javascript", "object", "nested", "utility"],
"author": "realvishalrana"
},
{
"title": "Unique By Key",
"description": "Filters an array of objects to only include unique objects by a specified key.",
"code": [
"const uniqueByKey = (key, arr) =>",
" arr.filter((obj, index, self) => index === self.findIndex((t) => t?.[key] === obj?.[key]));",
"",
"// Usage:",
"const arr = [",
" { id: 1, name: 'John' },",
" { id: 2, name: 'Jane' },",
" { id: 1, name: 'John' }",
"];",
"console.log(uniqueByKey('id', arr)); // Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]"
],
"tags": ["javascript", "array", "unique", "utility"],
"author": "realvishalrana"
}
]
},
Expand Down Expand Up @@ -313,5 +413,33 @@
"author": "dostonnabotov"
}
]
}
},
{
"categoryName": "Number Formatting",
"snippets": [
{
"title": "Number Formatter",
"description": "Formats a number with suffixes (K, M, B, etc.).",
"code": [
"const nFormatter = (num) => {",
" if (!num) return;",
" num = parseFloat(num.toString().replace(/[^0-9.]/g, ''));",
" const suffixes = ['', 'K', 'M', 'B', 'T', 'P', 'E'];",
" let index = 0;",
" while (num >= 1000 && index < suffixes.length - 1) {",
" num /= 1000;",
" index++;",
" }",
" return num.toFixed(2).replace(/\\.0+$|(\\.[0-9]*[1-9])0+$/, '1ドル') + suffixes[index];",
"};",
"",
"// Usage:",
"console.log(nFormatter(1234567)); // Output: '1.23M'"
],
"tags": ["javascript", "number", "format", "utility"],
"author": "realvishalrana"
}
]
}

]

AltStyle によって変換されたページ (->オリジナル) /