Skip to content

Commit cdb7daf

Browse files
authored
fix(index): export the real toString implementation (#2839)
1 parent d4e02ee commit cdb7daf

4 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Sanitizer | Description
191191
**toDate(input)** | convert the input string to a date, or `null` if the input is not a date.
192192
**toFloat(input)** | convert the input string to a float, or `NaN` if the input is not a float.
193193
**toInt(input [, radix])** | convert the input string to an integer, or `NaN` if the input is not an integer.
194+
**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.
194195
**trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input.
195196
**unescape(input)** | replace HTML encoded entities with `<`, `>`, `&`, `'`, `"`, `` ` ``, `\` and `/`.
196197
**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, '\\[\\]')`.

‎src/index.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import blacklist from './lib/blacklist';
123123
import isWhitelisted from './lib/isWhitelisted';
124124

125125
import normalizeEmail from './lib/normalizeEmail';
126+
import toString from './lib/util/toString';
126127

127128
import isSlug from './lib/isSlug';
128129
import isLicensePlate from './lib/isLicensePlate';

‎test/exports.test.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ describe('Exports', () => {
2424
assert.strictEqual(typeof validator.toFloat, 'function');
2525
});
2626

27+
it('should export toString and not inherit Object.prototype.toString', () => {
28+
assert.notStrictEqual(validator.toString, Object.prototype.toString);
29+
assert.strictEqual(validator.toString('test'), 'test');
30+
assert.strictEqual(validator.toString(123), '123');
31+
assert.strictEqual(validator.toString(null), '');
32+
assert.strictEqual(validator.toString(undefined), '');
33+
});
34+
2735
it('should export the version number', () => {
2836
/* eslint-disable global-require */
2937
assert.strictEqual(

‎test/sanitizers.test.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ describe('Sanitizers', () => {
153153
});
154154
});
155155

156+
it('should convert inputs to strings', () => {
157+
test({
158+
sanitizer: 'toString',
159+
expect: {
160+
foo: 'foo',
161+
'': '',
162+
123: '123',
163+
' bar ': ' bar ',
164+
},
165+
});
166+
});
167+
156168
it('should escape HTML', () => {
157169
test({
158170
sanitizer: 'escape',

0 commit comments

Comments
 (0)