From 924ff64ed7c98b5634970a08c3f4c9fbe9430678 Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sat, 14 Oct 2023 23:46:24 +0200 Subject: [PATCH 01/11] added toCamelCase method --- README.md | 1 + src/index.js | 2 ++ src/lib/toCamelCase.js | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 src/lib/toCamelCase.js diff --git a/README.md b/README.md index e00b7cfac..4919d3cde 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,7 @@ Sanitizer | Description **stripLow(input [, keep_new_lines])** | remove characters with a numerical value < 32 and 127, mostly control characters. If `keep_new_lines` is `true`, newline characters are preserved (`\n` and `\r`, hex `0xA` and `0xD`). Unicode-safe in JavaScript. **toBoolean(input [, strict])** | convert the input string to a boolean. Everything except for `'0'`, `'false'` and `''` returns `true`. In strict mode only `'1'` and `'true'` return `true`. **toDate(input)** | convert the input string to a date, or `null` if the input is not a date. +**toCamelCase(input)** | convert the input string to camelCase, or `null` if the input is not a string. String will be separated based on spaces, hyphens, or underscores. **toFloat(input)** | convert the input string to a float, or `NaN` if the input is not a float. **toInt(input [, radix])** | convert the input string to an integer, or `NaN` if the input is not an integer. **trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input. diff --git a/src/index.js b/src/index.js index bef4cfff4..d71289b47 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import toDate from './lib/toDate'; import toFloat from './lib/toFloat'; import toInt from './lib/toInt'; import toBoolean from './lib/toBoolean'; +import toCamelCase from './lib/toCamelCase'; import equals from './lib/equals'; import contains from './lib/contains'; import matches from './lib/matches'; @@ -228,6 +229,7 @@ const validator = { isWhitelisted, normalizeEmail, toString, + toCamelCase, isSlug, isStrongPassword, isTaxID, diff --git a/src/lib/toCamelCase.js b/src/lib/toCamelCase.js new file mode 100644 index 000000000..32a3ce1cd --- /dev/null +++ b/src/lib/toCamelCase.js @@ -0,0 +1,18 @@ +export default function toCamelCase(inputString) { + + // If the input string is empty, return an empty string + if (typeof inputString !== 'string' || inputString.length === 0) { + return null; + } + + // Split the input string into words using spaces, hyphens, or underscores as separators + const words = inputString.split(/[\s\-_]+/); + + // Capitalize the first letter of each word except the first one + for (let i = 1; i < words.length; i++) { + words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1); + } + + // Join the words back together to form the camelCase string + return words.join(''); +} From 2702cfe66eb39af63ad2890241570f28967365ef Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 14:17:16 +0200 Subject: [PATCH 02/11] formatted the code --- src/lib/toCamelCase.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib/toCamelCase.js b/src/lib/toCamelCase.js index 32a3ce1cd..c1823ca2d 100644 --- a/src/lib/toCamelCase.js +++ b/src/lib/toCamelCase.js @@ -1,7 +1,6 @@ export default function toCamelCase(inputString) { - // If the input string is empty, return an empty string - if (typeof inputString !== 'string' || inputString.length === 0) { + if (typeof inputString !== "string" || inputString.length === 0) { return null; } @@ -14,5 +13,5 @@ export default function toCamelCase(inputString) { } // Join the words back together to form the camelCase string - return words.join(''); + return words.join(""); } From cd4923c69041d4aa1d717bf68eb6f5bd240ceebc Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 14:20:16 +0200 Subject: [PATCH 03/11] used signle quotes for string --- src/lib/toCamelCase.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/toCamelCase.js b/src/lib/toCamelCase.js index c1823ca2d..9ee84c192 100644 --- a/src/lib/toCamelCase.js +++ b/src/lib/toCamelCase.js @@ -1,6 +1,6 @@ export default function toCamelCase(inputString) { // If the input string is empty, return an empty string - if (typeof inputString !== "string" || inputString.length === 0) { + if (typeof inputString !== 'string' || inputString.length === 0) { return null; } @@ -13,5 +13,5 @@ export default function toCamelCase(inputString) { } // Join the words back together to form the camelCase string - return words.join(""); + return words.join(''); } From 0109c446503a828c8d91337ce17b661d92550c0c Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 14:41:50 +0200 Subject: [PATCH 04/11] used asserrString --- src/lib/toCamelCase.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib/toCamelCase.js b/src/lib/toCamelCase.js index 9ee84c192..cdfc08498 100644 --- a/src/lib/toCamelCase.js +++ b/src/lib/toCamelCase.js @@ -1,8 +1,7 @@ +import assertString from "./util/assertString"; + export default function toCamelCase(inputString) { - // If the input string is empty, return an empty string - if (typeof inputString !== 'string' || inputString.length === 0) { - return null; - } + assertString(inputString); // Split the input string into words using spaces, hyphens, or underscores as separators const words = inputString.split(/[\s\-_]+/); From f6cc3a4ecccfe23b0d4cc90c37ebca8714180d7d Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 14:45:59 +0200 Subject: [PATCH 05/11] optimized code --- src/lib/toCamelCase.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/lib/toCamelCase.js b/src/lib/toCamelCase.js index cdfc08498..2538a6991 100644 --- a/src/lib/toCamelCase.js +++ b/src/lib/toCamelCase.js @@ -3,14 +3,11 @@ import assertString from "./util/assertString"; export default function toCamelCase(inputString) { assertString(inputString); - // Split the input string into words using spaces, hyphens, or underscores as separators const words = inputString.split(/[\s\-_]+/); - // Capitalize the first letter of each word except the first one - for (let i = 1; i < words.length; i++) { - words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1); - } + words.map((word) => { + return word.charAt(0).toUpperCase() + word.slice(1); + }); - // Join the words back together to form the camelCase string - return words.join(''); + return words.join(""); } From 309e5bcf9c03fc17159f39219df16270797ca8cd Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 14:48:34 +0200 Subject: [PATCH 06/11] used signle quotes for string --- src/lib/toCamelCase.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/toCamelCase.js b/src/lib/toCamelCase.js index 2538a6991..8f5434538 100644 --- a/src/lib/toCamelCase.js +++ b/src/lib/toCamelCase.js @@ -1,4 +1,4 @@ -import assertString from "./util/assertString"; +import assertString from './util/assertString'; export default function toCamelCase(inputString) { assertString(inputString); @@ -9,5 +9,5 @@ export default function toCamelCase(inputString) { return word.charAt(0).toUpperCase() + word.slice(1); }); - return words.join(""); + return words.join(''); } From 3decef8723aacd03f5e69c8a90a2bf8726363555 Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 14:50:43 +0200 Subject: [PATCH 07/11] remove block from map --- src/lib/toCamelCase.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/toCamelCase.js b/src/lib/toCamelCase.js index 8f5434538..3f891a35a 100644 --- a/src/lib/toCamelCase.js +++ b/src/lib/toCamelCase.js @@ -5,9 +5,7 @@ export default function toCamelCase(inputString) { const words = inputString.split(/[\s\-_]+/); - words.map((word) => { - return word.charAt(0).toUpperCase() + word.slice(1); - }); + words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)); return words.join(''); } From 08067a0b142a460bf768956fc1a45fb15b1fb0dc Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 14:53:06 +0200 Subject: [PATCH 08/11] replaced map with loop --- src/lib/toCamelCase.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/toCamelCase.js b/src/lib/toCamelCase.js index 3f891a35a..46e1e1fa7 100644 --- a/src/lib/toCamelCase.js +++ b/src/lib/toCamelCase.js @@ -5,7 +5,9 @@ export default function toCamelCase(inputString) { const words = inputString.split(/[\s\-_]+/); - words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)); + for (let i = 1; i < words.length; i++) { + words[i] = words[i].substr(0, 1).toUpperCase() + words[i].substr(1); + } return words.join(''); } From 63dda57a5e2eda5496608203c84a353892e6ed92 Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 15:01:13 +0200 Subject: [PATCH 09/11] added test cases --- test/exports.test.js | 1 + test/sanitizers.test.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/test/exports.test.js b/test/exports.test.js index 0bff532ab..fc490a232 100644 --- a/test/exports.test.js +++ b/test/exports.test.js @@ -16,6 +16,7 @@ describe('Exports', () => { it('should export sanitizers', () => { assert.strictEqual(typeof validator.toBoolean, 'function'); assert.strictEqual(typeof validator.toFloat, 'function'); + assert.strictEqual(typeof validator.toCamelCase, 'function'); }); it('should export the version number', () => { diff --git a/test/sanitizers.test.js b/test/sanitizers.test.js index ecb0e128f..413a96926 100644 --- a/test/sanitizers.test.js +++ b/test/sanitizers.test.js @@ -153,6 +153,20 @@ describe('Sanitizers', () => { }); }); + it('should convert strings to camel case', () => { + test({ + sanitizer: 'toCamelCase', + expect: { + 'camle case': 'camleCase', + 'camel-case': 'camelCase', + 'camel_case': 'camelCase', + 'camel case': 'camelCase', + 123: null, + '': '', + }, + }); + }); + it('should escape HTML', () => { test({ sanitizer: 'escape', From cc85925734f468072f1055373be1a4ad546a3839 Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 15:05:13 +0200 Subject: [PATCH 10/11] added test cases --- test/sanitizers.test.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/sanitizers.test.js b/test/sanitizers.test.js index 413a96926..8ed9fd24d 100644 --- a/test/sanitizers.test.js +++ b/test/sanitizers.test.js @@ -158,9 +158,6 @@ describe('Sanitizers', () => { sanitizer: 'toCamelCase', expect: { 'camle case': 'camleCase', - 'camel-case': 'camelCase', - 'camel_case': 'camelCase', - 'camel case': 'camelCase', 123: null, '': '', }, From 00f981c0da20bb33cde9c47d041eb4e805fccdcd Mon Sep 17 00:00:00 2001 From: Muhammad Haris Date: Sun, 15 Oct 2023 15:06:56 +0200 Subject: [PATCH 11/11] added test cases --- test/sanitizers.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/sanitizers.test.js b/test/sanitizers.test.js index 8ed9fd24d..49ce49333 100644 --- a/test/sanitizers.test.js +++ b/test/sanitizers.test.js @@ -158,7 +158,6 @@ describe('Sanitizers', () => { sanitizer: 'toCamelCase', expect: { 'camle case': 'camleCase', - 123: null, '': '', }, });