From a88f748cc957eeb45a982236190d89cf07a8a230 Mon Sep 17 00:00:00 2001 From: Kingo64 Date: Mon, 27 Apr 2026 01:16:18 +1000 Subject: [PATCH] feat: export encode function from buildURL helper (#6897) * feat: export encode function from buildURL helper * chore: added tests --------- Co-authored-by: Jason Saayman --- lib/helpers/buildURL.js | 2 +- tests/unit/helpers/buildURL.test.js | 38 ++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/helpers/buildURL.js b/lib/helpers/buildURL.js index d6745da5..b3e230e5 100644 --- a/lib/helpers/buildURL.js +++ b/lib/helpers/buildURL.js @@ -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, '$') diff --git a/tests/unit/helpers/buildURL.test.js b/tests/unit/helpers/buildURL.test.js index 1ed78540..6f114c06 100644 --- a/tests/unit/helpers/buildURL.test.js +++ b/tests/unit/helpers/buildURL.test.js @@ -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'); + }); +});