Skip to content
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

added toCamelCase method #2324

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -228,6 +229,7 @@ const validator = {
isWhitelisted,
normalizeEmail,
toString,
toCamelCase,
isSlug,
isStrongPassword,
isTaxID,
Expand Down
13 changes: 13 additions & 0 deletions src/lib/toCamelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assertString from './util/assertString';

export default function toCamelCase(inputString) {
assertString(inputString);

const words = inputString.split(/[\s\-_]+/);

for (let i = 1; i < words.length; i++) {
words[i] = words[i].substr(0, 1).toUpperCase() + words[i].substr(1);
}

return words.join('');
}
1 change: 1 addition & 0 deletions test/exports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
10 changes: 10 additions & 0 deletions test/sanitizers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ describe('Sanitizers', () => {
});
});

it('should convert strings to camel case', () => {
test({
sanitizer: 'toCamelCase',
expect: {
'camle case': 'camleCase',
'': '',
},
});
});

it('should escape HTML', () => {
test({
sanitizer: 'escape',
Expand Down
Loading