2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00

feat: export encode function from buildURL helper (#6897)

* feat: export encode function from buildURL helper

* chore: added tests

---------

Co-authored-by: Jason Saayman <jasonsaayman@gmail.com>
This commit is contained in:
Kingo64
2026-04-27 01:16:18 +10:00
committed by GitHub
parent 2f89ce60ac
commit a88f748cc9
2 changed files with 38 additions and 2 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ import AxiosURLSearchParams from '../helpers/AxiosURLSearchParams.js';
*
* @returns {string} The encoded value.
*/
function encode(val) {
export function encode(val) {
return encodeURIComponent(val)
.replace(/%3A/gi, ':')
.replace(/%24/g, '$')
+37 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from 'vitest';
import buildURL from '../../../lib/helpers/buildURL.js';
import buildURL, { encode } from '../../../lib/helpers/buildURL.js';
describe('helpers::buildURL', () => {
it('should support null params', () => {
@@ -124,3 +124,39 @@ describe('helpers::buildURL', () => {
expect(buildURL('/foo', params, customSerializer)).toEqual('/foo?rendered');
});
});
describe('helpers::encode', () => {
it('should be exported as a named export', () => {
expect(typeof encode).toBe('function');
});
it('should leave plain ASCII unchanged', () => {
expect(encode('foo')).toEqual('foo');
});
it('should preserve `:` rather than percent-encoding it', () => {
expect(encode(':')).toEqual(':');
});
it('should preserve `$` rather than percent-encoding it', () => {
expect(encode('$')).toEqual('$');
});
it('should preserve `,` rather than percent-encoding it', () => {
expect(encode(',')).toEqual(',');
});
it('should encode space as `+` (form-style) rather than `%20`', () => {
expect(encode(' ')).toEqual('+');
});
it('should still percent-encode characters outside the preserved set', () => {
expect(encode('a/b')).toEqual('a%2Fb');
expect(encode('a&b')).toEqual('a%26b');
expect(encode('a=b')).toEqual('a%3Db');
});
it('should apply all substitutions together', () => {
expect(encode('a:b$c,d e')).toEqual('a:b$c,d+e');
});
});