From 599e984cb7cdd8cd8e760dfbd1fddf1026e89be5 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 30 Dec 2024 22:30:33 +0530 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20Add=20utility=20functions=20and?= =?UTF-8?q?=20snippets=20to=20javascript.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/consolidated/all_snippets.json | 120 +++++++++++++++++++++++++ public/data/javascript.json | 122 +++++++++++++++++++++++++- 2 files changed, 241 insertions(+), 1 deletion(-) diff --git a/public/consolidated/all_snippets.json b/public/consolidated/all_snippets.json index 4748bead..dc6915d5 100644 --- a/public/consolidated/all_snippets.json +++ b/public/consolidated/all_snippets.json @@ -371,6 +371,99 @@ "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" + } + ] + }, + { + "language": "javascript", + "categoryName": "Object Manipulation", + "snippets": [ + { + "title": "Filter Object", + "description": "Filters out entries in an object where the value is falsy.", + "code": [ + "export const filterObject = (object = {}) =>", + " Object.fromEntries(", + " Object.entries(object)", + " .map(([key, value]) => value && [key, value])", + " .filter((item) => item),", + " );", + "", + "// Usage:", + "const obj = { a: 1, b: null, c: undefined, d: 4 };", + "console.log(filterObject(obj)); // Output: { a: 1, d: 4 }" + ], + "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" } ] }, @@ -659,6 +752,33 @@ } ] }, + { + "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" + } + ] + }, { "language": "python", "categoryName": "String Manipulation", diff --git a/public/data/javascript.json b/public/data/javascript.json index 9fb3dc05..973d77ce 100644 --- a/public/data/javascript.json +++ b/public/data/javascript.json @@ -80,6 +80,98 @@ ], "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": "Filters out entries in an object where the value is falsy.", + "code": [ + "export const filterObject = (object = {}) =>", + " Object.fromEntries(", + " Object.entries(object)", + " .map(([key, value]) => value && [key, value])", + " .filter((item) => item),", + " );", + "", + "// Usage:", + "const obj = { a: 1, b: null, c: undefined, d: 4 };", + "console.log(filterObject(obj)); // Output: { a: 1, d: 4 }" + ], + "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" } ] }, @@ -313,5 +405,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" + } + ] +} + ] From 8935d03ee2815cef93da81e07a2a076e94faf287 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 30 Dec 2024 22:49:55 +0530 Subject: [PATCH 2/3] =?UTF-8?q?=E2=8F=AA=20Revert=20changes=20in=20all=5Fs?= =?UTF-8?q?nippets.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/consolidated/all_snippets.json | 120 -------------------------- 1 file changed, 120 deletions(-) diff --git a/public/consolidated/all_snippets.json b/public/consolidated/all_snippets.json index dc6915d5..4748bead 100644 --- a/public/consolidated/all_snippets.json +++ b/public/consolidated/all_snippets.json @@ -371,99 +371,6 @@ "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" - } - ] - }, - { - "language": "javascript", - "categoryName": "Object Manipulation", - "snippets": [ - { - "title": "Filter Object", - "description": "Filters out entries in an object where the value is falsy.", - "code": [ - "export const filterObject = (object = {}) =>", - " Object.fromEntries(", - " Object.entries(object)", - " .map(([key, value]) => value && [key, value])", - " .filter((item) => item),", - " );", - "", - "// Usage:", - "const obj = { a: 1, b: null, c: undefined, d: 4 };", - "console.log(filterObject(obj)); // Output: { a: 1, d: 4 }" - ], - "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" } ] }, @@ -752,33 +659,6 @@ } ] }, - { - "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" - } - ] - }, { "language": "python", "categoryName": "String Manipulation", From 01c2c83eabe54419e58325f6a8d5db93b887092f Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 30 Dec 2024 23:10:56 +0530 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fix=20spelling=20mista?= =?UTF-8?q?ke=20in=20description=20and=20update=20filterObject=20function?= =?UTF-8?q?=20to=20handle=20null=20and=20undefined=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/data/javascript.json | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/public/data/javascript.json b/public/data/javascript.json index 973d77ce..f0dd6748 100644 --- a/public/data/javascript.json +++ b/public/data/javascript.json @@ -121,18 +121,26 @@ "snippets": [ { "title": "Filter Object", - "description": "Filters out entries in an object where the value is falsy.", + "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)", - " .map(([key, value]) => value && [key, value])", - " .filter((item) => item),", + " .filter(([key, value]) => value !== null && value !== undefined && value !== '' && (typeof value !== 'object' || Object.keys(value).length > 0))", " );", "", "// Usage:", - "const obj = { a: 1, b: null, c: undefined, d: 4 };", - "console.log(filterObject(obj)); // Output: { a: 1, d: 4 }" + "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"