Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Sanitizer | Description
**toDate(input)** | convert the input string to a date, or `null` if the input is not a date.
**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.
**toString(input)** | convert the input to a string. `null`, `undefined` and `NaN` are converted to an empty string, and objects are converted using their own `toString` method.
**trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input.
**unescape(input)** | replace HTML encoded entities with `<`, `>`, `&`, `'`, `"`, `` ` ``, `\` and `/`.
**whitelist(input, chars)** | remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `whitelist(input, '\\[\\]')`.
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ import blacklist from './lib/blacklist';
import isWhitelisted from './lib/isWhitelisted';

import normalizeEmail from './lib/normalizeEmail';
import toString from './lib/util/toString';

import isSlug from './lib/isSlug';
import isLicensePlate from './lib/isLicensePlate';
Expand Down
8 changes: 8 additions & 0 deletions test/exports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ describe('Exports', () => {
assert.strictEqual(typeof validator.toFloat, 'function');
});

it('should export toString and not inherit Object.prototype.toString', () => {
assert.notStrictEqual(validator.toString, Object.prototype.toString);
assert.strictEqual(validator.toString('test'), 'test');
assert.strictEqual(validator.toString(123), '123');
assert.strictEqual(validator.toString(null), '');
assert.strictEqual(validator.toString(undefined), '');
});

it('should export the version number', () => {
/* eslint-disable global-require */
assert.strictEqual(
Expand Down
12 changes: 12 additions & 0 deletions test/sanitizers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ describe('Sanitizers', () => {
});
});

it('should convert inputs to strings', () => {
test({
sanitizer: 'toString',
expect: {
foo: 'foo',
'': '',
123: '123',
' bar ': ' bar ',
},
});
});

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