diff --git a/packages/url/README.md b/packages/url/README.md index ae5b6bf945fe5f..86159b22c20343 100644 --- a/packages/url/README.md +++ b/packages/url/README.md @@ -28,8 +28,8 @@ const newURL = addQueryArgs( 'https://google.com', { q: 'test' } ); // https://g _Parameters_ -- _url_ `[string]`: URL to which arguments should be appended. If omitted, only the resulting querystring is returned. -- _args_ `[Object]`: Query arguments to apply to URL. +- _url_ `string`: URL to which arguments should be appended. If omitted, only the resulting querystring is returned. +- _args_ `Record< string, unknown >`: Query arguments to apply to URL. _Returns_ @@ -58,7 +58,7 @@ const queryString = buildQueryString( { _Parameters_ -- _data_ `Record`: Data to encode. +- _data_ `Record< string, unknown >`: Data to encode. _Returns_ @@ -99,7 +99,7 @@ const imageUrl = filterURLForDisplay( _Parameters_ - _url_ `string`: Original URL. -- _maxLength_ `number|null`: URL length. +- _maxLength_ `number | null`: URL length. _Returns_ @@ -122,7 +122,7 @@ _Parameters_ _Returns_ -- `string|void`: The authority part of the URL. +- `string | void`: The authority part of the URL. ### getFilename @@ -141,7 +141,7 @@ _Parameters_ _Returns_ -- `string|void`: The filename part of the URL. +- `string | void`: The filename part of the URL. ### getFragment @@ -164,7 +164,7 @@ _Parameters_ _Returns_ -- `string|void`: The fragment part of the URL. +- `string | void`: The fragment part of the URL. ### getPath @@ -183,7 +183,7 @@ _Parameters_ _Returns_ -- `string|void`: The path part of the URL. +- `string | void`: The path part of the URL. ### getPathAndQueryString @@ -225,7 +225,7 @@ _Parameters_ _Returns_ -- `string|void`: The protocol part of the URL. +- `string | void`: The protocol part of the URL. ### getQueryArg @@ -244,7 +244,7 @@ _Parameters_ _Returns_ -- `QueryArgParsed|void`: Query arg value. +- `QueryArgParsed | undefined`: Query arg value. ### getQueryArgs @@ -283,7 +283,7 @@ _Parameters_ _Returns_ -- `string|void`: The query string part of the URL. +- `string | void`: The query string part of the URL. ### hasQueryArg @@ -525,7 +525,7 @@ const newUrl = removeQueryArgs( _Parameters_ - _url_ `string`: URL. -- _args_ `...string`: Query Args. +- _args_ `string[]`: Query Args. _Returns_ diff --git a/packages/url/src/add-query-args.js b/packages/url/src/add-query-args.ts similarity index 78% rename from packages/url/src/add-query-args.js rename to packages/url/src/add-query-args.ts index 556a3fb706a447..1f4c05de3bbdd6 100644 --- a/packages/url/src/add-query-args.js +++ b/packages/url/src/add-query-args.ts @@ -10,18 +10,21 @@ import { getFragment } from './get-fragment'; * includes query arguments, the arguments are merged with (and take precedent * over) the existing set. * - * @param {string} [url=''] URL to which arguments should be appended. If omitted, - * only the resulting querystring is returned. - * @param {Object} [args] Query arguments to apply to URL. + * @param url URL to which arguments should be appended. If omitted, + * only the resulting querystring is returned. + * @param args Query arguments to apply to URL. * * @example * ```js * const newURL = addQueryArgs( 'https://google.com', { q: 'test' } ); // https://google.com/?q=test * ``` * - * @return {string} URL with arguments applied. + * @return URL with arguments applied. */ -export function addQueryArgs( url = '', args ) { +export function addQueryArgs( + url: string = '', + args?: Record< string, unknown > +): string { // If no arguments are to be appended, return original URL. if ( ! args || ! Object.keys( args ).length ) { return url; diff --git a/packages/url/src/build-query-string.js b/packages/url/src/build-query-string.ts similarity index 82% rename from packages/url/src/build-query-string.js rename to packages/url/src/build-query-string.ts index 5f8b6a68079a5b..5315d77bae03ad 100644 --- a/packages/url/src/build-query-string.js +++ b/packages/url/src/build-query-string.ts @@ -18,14 +18,14 @@ * // "simple=is%20ok&arrays%5B0%5D=are&arrays%5B1%5D=fine&arrays%5B2%5D=too&objects%5BevenNested%5D%5Bok%5D=yes" * ``` * - * @param {Record} data Data to encode. + * @param data Data to encode. * - * @return {string} Query string. + * @return Query string. */ -export function buildQueryString( data ) { +export function buildQueryString( data: Record< string, unknown > ): string { let string = ''; - const stack = Object.entries( data ); + const stack: Array< [ string, unknown ] > = Object.entries( data ); let pair; while ( ( pair = stack.shift() ) ) { @@ -39,7 +39,7 @@ export function buildQueryString( data ) { // Push array or object values onto the stack as composed of their // original key and nested index or key, retaining order by a // combination of Array#reverse and Array#unshift onto the stack. - const valuePairs = Object.entries( value ).reverse(); + const valuePairs = Object.entries( value as object ).reverse(); for ( const [ member, memberValue ] of valuePairs ) { stack.unshift( [ `${ key }[${ member }]`, memberValue ] ); } @@ -50,7 +50,8 @@ export function buildQueryString( data ) { } string += - '&' + [ key, value ].map( encodeURIComponent ).join( '=' ); + '&' + + [ key, String( value ) ].map( encodeURIComponent ).join( '=' ); } } diff --git a/packages/url/src/clean-for-slug.js b/packages/url/src/clean-for-slug.ts similarity index 90% rename from packages/url/src/clean-for-slug.js rename to packages/url/src/clean-for-slug.ts index e288f5949537ad..a6dd495ff61f4f 100644 --- a/packages/url/src/clean-for-slug.js +++ b/packages/url/src/clean-for-slug.ts @@ -15,11 +15,11 @@ import removeAccents from 'remove-accents'; * except hyphens. Converts remaining string to lowercase. It does not account * for octets, HTML entities, or other encoded characters. * - * @param {string} string Title or slug to be processed. + * @param string Title or slug to be processed. * - * @return {string} Processed string. + * @return Processed string. */ -export function cleanForSlug( string ) { +export function cleanForSlug( string: string ): string { if ( ! string ) { return ''; } diff --git a/packages/url/src/filter-url-for-display.js b/packages/url/src/filter-url-for-display.ts similarity index 88% rename from packages/url/src/filter-url-for-display.js rename to packages/url/src/filter-url-for-display.ts index 436070b667a3e6..848aac53efed03 100644 --- a/packages/url/src/filter-url-for-display.js +++ b/packages/url/src/filter-url-for-display.ts @@ -1,8 +1,8 @@ /** * Returns a URL for display. * - * @param {string} url Original URL. - * @param {number|null} maxLength URL length. + * @param url Original URL. + * @param maxLength URL length. * * @example * ```js @@ -10,9 +10,12 @@ * const imageUrl = filterURLForDisplay( 'https://www.wordpress.org/wp-content/uploads/img.png', 20 ); // …ent/uploads/img.png * ``` * - * @return {string} Displayed URL. + * @return Displayed URL. */ -export function filterURLForDisplay( url, maxLength = null ) { +export function filterURLForDisplay( + url: string, + maxLength: number | null = null +): string { if ( ! url ) { return ''; } diff --git a/packages/url/src/get-authority.js b/packages/url/src/get-authority.ts similarity index 75% rename from packages/url/src/get-authority.js rename to packages/url/src/get-authority.ts index f29afd5411c11d..ae11cedef0566d 100644 --- a/packages/url/src/get-authority.js +++ b/packages/url/src/get-authority.ts @@ -1,7 +1,7 @@ /** * Returns the authority part of the URL. * - * @param {string} url The full URL. + * @param url The full URL. * * @example * ```js @@ -9,9 +9,9 @@ * const authority2 = getAuthority( 'https://localhost:8080/test/' ); // 'localhost:8080' * ``` * - * @return {string|void} The authority part of the URL. + * @return The authority part of the URL. */ -export function getAuthority( url ) { +export function getAuthority( url: string ): string | void { const matches = /^[^\/\s:]+:(?:\/\/)?\/?([^\/\s#?]+)[\/#?]{0,1}\S*$/.exec( url ); diff --git a/packages/url/src/get-filename.js b/packages/url/src/get-filename.ts similarity index 78% rename from packages/url/src/get-filename.js rename to packages/url/src/get-filename.ts index d744a8da6bccfe..d5550618734370 100644 --- a/packages/url/src/get-filename.js +++ b/packages/url/src/get-filename.ts @@ -1,7 +1,7 @@ /** * Returns the filename part of the URL. * - * @param {string} url The full URL. + * @param url The full URL. * * @example * ```js @@ -9,9 +9,9 @@ * const filename2 = getFilename( '/this/is/a/test.png' ); // 'test.png' * ``` * - * @return {string|void} The filename part of the URL. + * @return The filename part of the URL. */ -export function getFilename( url ) { +export function getFilename( url: string ): string | void { let filename; if ( ! url ) { diff --git a/packages/url/src/get-fragment.js b/packages/url/src/get-fragment.ts similarity index 75% rename from packages/url/src/get-fragment.js rename to packages/url/src/get-fragment.ts index 4f15ef0ce06af8..d6a48a0bfc7cda 100644 --- a/packages/url/src/get-fragment.js +++ b/packages/url/src/get-fragment.ts @@ -1,7 +1,7 @@ /** * Returns the fragment part of the URL. * - * @param {string} url The full URL + * @param url The full URL * * @example * ```js @@ -9,9 +9,9 @@ * const fragment2 = getFragment( 'https://wordpress.org#another-fragment?query=true' ); // '#another-fragment' * ``` * - * @return {string|void} The fragment part of the URL. + * @return The fragment part of the URL. */ -export function getFragment( url ) { +export function getFragment( url: string ): string | void { const matches = /^\S+?(#[^\s\?]*)/.exec( url ); if ( matches ) { return matches[ 1 ]; diff --git a/packages/url/src/get-path-and-query-string.js b/packages/url/src/get-path-and-query-string.ts similarity index 80% rename from packages/url/src/get-path-and-query-string.js rename to packages/url/src/get-path-and-query-string.ts index f858846b07802c..64869cab6272e9 100644 --- a/packages/url/src/get-path-and-query-string.js +++ b/packages/url/src/get-path-and-query-string.ts @@ -6,7 +6,7 @@ import { getPath, getQueryString } from '.'; /** * Returns the path part and query string part of the URL. * - * @param {string} url The full URL. + * @param url The full URL. * * @example * ```js @@ -14,9 +14,9 @@ import { getPath, getQueryString } from '.'; * const pathAndQueryString2 = getPathAndQueryString( 'https://wordpress.org/help/faq/' ); // '/help/faq' * ``` * - * @return {string} The path part and query string part of the URL. + * @return The path part and query string part of the URL. */ -export function getPathAndQueryString( url ) { +export function getPathAndQueryString( url: string ): string { const path = getPath( url ); const queryString = getQueryString( url ); let value = '/'; diff --git a/packages/url/src/get-path.js b/packages/url/src/get-path.ts similarity index 76% rename from packages/url/src/get-path.js rename to packages/url/src/get-path.ts index e9c4c717f6b92b..3a3b192d179c7c 100644 --- a/packages/url/src/get-path.js +++ b/packages/url/src/get-path.ts @@ -1,7 +1,7 @@ /** * Returns the path part of the URL. * - * @param {string} url The full URL. + * @param url The full URL. * * @example * ```js @@ -9,9 +9,9 @@ * const path2 = getPath( 'https://wordpress.org/help/faq/' ); // 'help/faq' * ``` * - * @return {string|void} The path part of the URL. + * @return The path part of the URL. */ -export function getPath( url ) { +export function getPath( url: string ): string | void { const matches = /^[^\/\s:]+:(?:\/\/)?[^\/\s#?]+[\/]([^\s#?]+)[#?]{0,1}\S*$/.exec( url ); if ( matches ) { diff --git a/packages/url/src/get-protocol.js b/packages/url/src/get-protocol.ts similarity index 70% rename from packages/url/src/get-protocol.js rename to packages/url/src/get-protocol.ts index 07b375c10f841b..f0360580c47f0e 100644 --- a/packages/url/src/get-protocol.js +++ b/packages/url/src/get-protocol.ts @@ -1,7 +1,7 @@ /** * Returns the protocol part of the URL. * - * @param {string} url The full URL. + * @param url The full URL. * * @example * ```js @@ -9,9 +9,9 @@ * const protocol2 = getProtocol( 'https://wordpress.org' ); // 'https:' * ``` * - * @return {string|void} The protocol part of the URL. + * @return The protocol part of the URL. */ -export function getProtocol( url ) { +export function getProtocol( url: string ): string | void { const matches = /^([^\s:]+:)/.exec( url ); if ( matches ) { return matches[ 1 ]; diff --git a/packages/url/src/get-query-arg.js b/packages/url/src/get-query-arg.js deleted file mode 100644 index 05f554cd8f67d4..00000000000000 --- a/packages/url/src/get-query-arg.js +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Internal dependencies - */ -import { getQueryArgs } from './get-query-args'; - -/** - * @typedef {{[key: string]: QueryArgParsed}} QueryArgObject - */ - -/** - * @typedef {string|string[]|QueryArgObject} QueryArgParsed - */ - -/** - * Returns a single query argument of the url - * - * @param {string} url URL. - * @param {string} arg Query arg name. - * - * @example - * ```js - * const foo = getQueryArg( 'https://wordpress.org?foo=bar&bar=baz', 'foo' ); // bar - * ``` - * - * @return {QueryArgParsed|void} Query arg value. - */ -export function getQueryArg( url, arg ) { - return getQueryArgs( url )[ arg ]; -} diff --git a/packages/url/src/get-query-arg.ts b/packages/url/src/get-query-arg.ts new file mode 100644 index 00000000000000..7bc1872a5fe429 --- /dev/null +++ b/packages/url/src/get-query-arg.ts @@ -0,0 +1,30 @@ +/** + * Internal dependencies + */ +import { getQueryArgs } from './get-query-args'; + +export interface QueryArgObject { + [ key: string ]: QueryArgParsed; +} + +export type QueryArgParsed = string | string[] | QueryArgObject; + +/** + * Returns a single query argument of the url + * + * @param url URL. + * @param arg Query arg name. + * + * @example + * ```js + * const foo = getQueryArg( 'https://wordpress.org?foo=bar&bar=baz', 'foo' ); // bar + * ``` + * + * @return Query arg value. + */ +export function getQueryArg( + url: string, + arg: string +): QueryArgParsed | undefined { + return getQueryArgs( url )[ arg ]; +} diff --git a/packages/url/src/get-query-args.js b/packages/url/src/get-query-args.ts similarity index 84% rename from packages/url/src/get-query-args.js rename to packages/url/src/get-query-args.ts index 45626f1d8a1e94..71fe6c06c0bb45 100644 --- a/packages/url/src/get-query-args.js +++ b/packages/url/src/get-query-args.ts @@ -3,22 +3,19 @@ */ import { safeDecodeURIComponent } from './safe-decode-uri-component'; import { getQueryString } from './get-query-string'; +import type { QueryArgParsed } from './get-query-arg'; -/** @typedef {import('./get-query-arg').QueryArgParsed} QueryArgParsed */ - -/** - * @typedef {Record} QueryArgs - */ +type QueryArgs = Record< string, QueryArgParsed >; /** * Sets a value in object deeply by a given array of path segments. Mutates the * object reference. * - * @param {Record} object Object in which to assign. - * @param {string[]} path Path segment at which to set value. - * @param {*} value Value to set. + * @param object Object in which to assign. + * @param path Path segment at which to set value. + * @param value Value to set. */ -function setPath( object, path, value ) { +function setPath( object: Record< string, any >, path: string[], value: any ) { const length = path.length; const lastIndex = length - 1; for ( let i = 0; i < length; i++ ) { @@ -61,7 +58,7 @@ function setPath( object, path, value ) { * Returns an object of query arguments of the given URL. If the given URL is * invalid or has no querystring, an empty object is returned. * - * @param {string} url URL. + * @param url URL. * * @example * ```js @@ -69,9 +66,9 @@ function setPath( object, path, value ) { * // { "foo": "bar", "bar": "baz" } * ``` * - * @return {QueryArgs} Query args object. + * @return Query args object. */ -export function getQueryArgs( url ) { +export function getQueryArgs( url: string ): QueryArgs { return ( ( getQueryString( url ) || '' ) // Normalize space encoding, accounting for PHP URL encoding diff --git a/packages/url/src/get-query-string.js b/packages/url/src/get-query-string.ts similarity index 72% rename from packages/url/src/get-query-string.js rename to packages/url/src/get-query-string.ts index 8624ce5c6c8b9d..91f9947a61e664 100644 --- a/packages/url/src/get-query-string.js +++ b/packages/url/src/get-query-string.ts @@ -1,16 +1,16 @@ /** * Returns the query string part of the URL. * - * @param {string} url The full URL. + * @param url The full URL. * * @example * ```js * const queryString = getQueryString( 'http://localhost:8080/this/is/a/test?query=true#fragment' ); // 'query=true' * ``` * - * @return {string|void} The query string part of the URL. + * @return The query string part of the URL. */ -export function getQueryString( url ) { +export function getQueryString( url: string ): string | void { let query; try { query = new URL( url, 'http://example.com' ).search.substring( 1 ); diff --git a/packages/url/src/has-query-arg.js b/packages/url/src/has-query-arg.ts similarity index 64% rename from packages/url/src/has-query-arg.js rename to packages/url/src/has-query-arg.ts index a940338dd16655..41cf83cc552a86 100644 --- a/packages/url/src/has-query-arg.js +++ b/packages/url/src/has-query-arg.ts @@ -6,16 +6,16 @@ import { getQueryArg } from './get-query-arg'; /** * Determines whether the URL contains a given query arg. * - * @param {string} url URL. - * @param {string} arg Query arg name. + * @param url URL. + * @param arg Query arg name. * * @example * ```js * const hasBar = hasQueryArg( 'https://wordpress.org?foo=bar&bar=baz', 'bar' ); // true * ``` * - * @return {boolean} Whether or not the URL contains the query arg. + * @return Whether or not the URL contains the query arg. */ -export function hasQueryArg( url, arg ) { +export function hasQueryArg( url: string, arg: string ): boolean { return getQueryArg( url, arg ) !== undefined; } diff --git a/packages/url/src/index.js b/packages/url/src/index.ts similarity index 100% rename from packages/url/src/index.js rename to packages/url/src/index.ts diff --git a/packages/url/src/is-email.js b/packages/url/src/is-email.ts similarity index 66% rename from packages/url/src/is-email.js rename to packages/url/src/is-email.ts index 815c4cdbd5fece..1e4ff1e1d1ae01 100644 --- a/packages/url/src/is-email.js +++ b/packages/url/src/is-email.ts @@ -4,15 +4,15 @@ const EMAIL_REGEXP = /** * Determines whether the given string looks like an email. * - * @param {string} email The string to scrutinise. + * @param email The string to scrutinise. * * @example * ```js * const isEmail = isEmail( 'hello@wordpress.org' ); // true * ``` * - * @return {boolean} Whether or not it looks like an email. + * @return Whether or not it looks like an email. */ -export function isEmail( email ) { +export function isEmail( email: string ): boolean { return EMAIL_REGEXP.test( email ); } diff --git a/packages/url/src/is-phone-number.js b/packages/url/src/is-phone-number.ts similarity index 68% rename from packages/url/src/is-phone-number.js rename to packages/url/src/is-phone-number.ts index ed7aad1a3540ea..d482b1ad7e25b1 100644 --- a/packages/url/src/is-phone-number.js +++ b/packages/url/src/is-phone-number.ts @@ -3,16 +3,16 @@ const PHONE_REGEXP = /^(tel:)?(\+)?\d{6,15}$/; /** * Determines whether the given string looks like a phone number. * - * @param {string} phoneNumber The string to scrutinize. + * @param phoneNumber The string to scrutinize. * * @example * ```js * const isPhoneNumber = isPhoneNumber('+1 (555) 123-4567'); // true * ``` * - * @return {boolean} Whether or not it looks like a phone number. + * @return Whether or not it looks like a phone number. */ -export function isPhoneNumber( phoneNumber ) { +export function isPhoneNumber( phoneNumber: string ): boolean { // Remove any separator from phone number. phoneNumber = phoneNumber.replace( /[-.() ]/g, '' ); return PHONE_REGEXP.test( phoneNumber ); diff --git a/packages/url/src/is-url.js b/packages/url/src/is-url.ts similarity index 77% rename from packages/url/src/is-url.js rename to packages/url/src/is-url.ts index 1fda847947ed46..6cb8202aa0b7d3 100644 --- a/packages/url/src/is-url.js +++ b/packages/url/src/is-url.ts @@ -1,7 +1,7 @@ /** * Determines whether the given string looks like a URL. * - * @param {string} url The string to scrutinise. + * @param url The string to scrutinise. * * @example * ```js @@ -11,9 +11,9 @@ * @see https://url.spec.whatwg.org/ * @see https://url.spec.whatwg.org/#valid-url-string * - * @return {boolean} Whether or not it looks like a URL. + * @return Whether or not it looks like a URL. */ -export function isURL( url ) { +export function isURL( url: string ): boolean { // A URL can be considered value if the `URL` constructor is able to parse // it. The constructor throws an error for an invalid URL. try { diff --git a/packages/url/src/is-valid-authority.js b/packages/url/src/is-valid-authority.ts similarity index 63% rename from packages/url/src/is-valid-authority.js rename to packages/url/src/is-valid-authority.ts index 4734259b34b19c..2e93840d0e530d 100644 --- a/packages/url/src/is-valid-authority.js +++ b/packages/url/src/is-valid-authority.ts @@ -1,7 +1,7 @@ /** * Checks for invalid characters within the provided authority. * - * @param {string} authority A string containing the URL authority. + * @param authority A string containing the URL authority. * * @example * ```js @@ -9,9 +9,9 @@ * const isNotValid = isValidAuthority( 'wordpress#org' ); // false * ``` * - * @return {boolean} True if the argument contains a valid authority. + * @return True if the argument contains a valid authority. */ -export function isValidAuthority( authority ) { +export function isValidAuthority( authority: string ): boolean { if ( ! authority ) { return false; } diff --git a/packages/url/src/is-valid-fragment.js b/packages/url/src/is-valid-fragment.ts similarity index 67% rename from packages/url/src/is-valid-fragment.js rename to packages/url/src/is-valid-fragment.ts index ce19c0f859e5a7..4a6579e0c40754 100644 --- a/packages/url/src/is-valid-fragment.js +++ b/packages/url/src/is-valid-fragment.ts @@ -1,7 +1,7 @@ /** * Checks for invalid characters within the provided fragment. * - * @param {string} fragment The url fragment. + * @param fragment The url fragment. * * @example * ```js @@ -9,9 +9,9 @@ * const isNotValid = isValidFragment( '#invalid-#fragment' ); // false * ``` * - * @return {boolean} True if the argument contains a valid fragment. + * @return True if the argument contains a valid fragment. */ -export function isValidFragment( fragment ) { +export function isValidFragment( fragment: string ): boolean { if ( ! fragment ) { return false; } diff --git a/packages/url/src/is-valid-path.js b/packages/url/src/is-valid-path.ts similarity index 68% rename from packages/url/src/is-valid-path.js rename to packages/url/src/is-valid-path.ts index 7ab5d9d95eeca7..b0f58354e7b94c 100644 --- a/packages/url/src/is-valid-path.js +++ b/packages/url/src/is-valid-path.ts @@ -1,7 +1,7 @@ /** * Checks for invalid characters within the provided path. * - * @param {string} path The URL path. + * @param path The URL path. * * @example * ```js @@ -9,9 +9,9 @@ * const isNotValid = isValidPath( '/invalid?test/path/' ); // false * ``` * - * @return {boolean} True if the argument contains a valid path + * @return True if the argument contains a valid path */ -export function isValidPath( path ) { +export function isValidPath( path: string ): boolean { if ( ! path ) { return false; } diff --git a/packages/url/src/is-valid-protocol.js b/packages/url/src/is-valid-protocol.ts similarity index 62% rename from packages/url/src/is-valid-protocol.js rename to packages/url/src/is-valid-protocol.ts index 689768114318c4..fa8a05271a739a 100644 --- a/packages/url/src/is-valid-protocol.js +++ b/packages/url/src/is-valid-protocol.ts @@ -1,7 +1,7 @@ /** * Tests if a url protocol is valid. * - * @param {string} protocol The url protocol. + * @param protocol The url protocol. * * @example * ```js @@ -9,9 +9,9 @@ * const isNotValid = isValidProtocol( 'https :' ); // false * ``` * - * @return {boolean} True if the argument is a valid protocol (e.g. http:, tel:). + * @return True if the argument is a valid protocol (e.g. http:, tel:). */ -export function isValidProtocol( protocol ) { +export function isValidProtocol( protocol: string ): boolean { if ( ! protocol ) { return false; } diff --git a/packages/url/src/is-valid-query-string.js b/packages/url/src/is-valid-query-string.ts similarity index 67% rename from packages/url/src/is-valid-query-string.js rename to packages/url/src/is-valid-query-string.ts index 039cede44de959..16139f52b626f5 100644 --- a/packages/url/src/is-valid-query-string.js +++ b/packages/url/src/is-valid-query-string.ts @@ -1,7 +1,7 @@ /** * Checks for invalid characters within the provided query string. * - * @param {string} queryString The query string. + * @param queryString The query string. * * @example * ```js @@ -9,9 +9,9 @@ * const isNotValid = isValidQueryString( 'query=true?another=false' ); // false * ``` * - * @return {boolean} True if the argument contains a valid query string. + * @return True if the argument contains a valid query string. */ -export function isValidQueryString( queryString ) { +export function isValidQueryString( queryString: string ): boolean { if ( ! queryString ) { return false; } diff --git a/packages/url/src/normalize-path.js b/packages/url/src/normalize-path.ts similarity index 89% rename from packages/url/src/normalize-path.js rename to packages/url/src/normalize-path.ts index eb1cafed083657..93d7cca54f9158 100644 --- a/packages/url/src/normalize-path.js +++ b/packages/url/src/normalize-path.ts @@ -3,11 +3,11 @@ * will be treated as identical, regardless of order they appear in the original * text. * - * @param {string} path Original path. + * @param path Original path. * - * @return {string} Normalized path. + * @return Normalized path. */ -export function normalizePath( path ) { +export function normalizePath( path: string ): string { const split = path.split( '?' ); const query = split[ 1 ]; const base = split[ 0 ]; diff --git a/packages/url/src/prepend-http.js b/packages/url/src/prepend-http.ts similarity index 81% rename from packages/url/src/prepend-http.js rename to packages/url/src/prepend-http.ts index e76488c51a4266..47ebc6e536f65d 100644 --- a/packages/url/src/prepend-http.js +++ b/packages/url/src/prepend-http.ts @@ -8,16 +8,16 @@ const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i; /** * Prepends "http://" to a url, if it looks like something that is meant to be a TLD. * - * @param {string} url The URL to test. + * @param url The URL to test. * * @example * ```js * const actualURL = prependHTTP( 'wordpress.org' ); // http://wordpress.org * ``` * - * @return {string} The updated URL. + * @return The updated URL. */ -export function prependHTTP( url ) { +export function prependHTTP( url: string ): string { if ( ! url ) { return url; } diff --git a/packages/url/src/prepend-https.js b/packages/url/src/prepend-https.ts similarity index 83% rename from packages/url/src/prepend-https.js rename to packages/url/src/prepend-https.ts index 237bf3e18cff70..8cc062ed000eb2 100644 --- a/packages/url/src/prepend-https.js +++ b/packages/url/src/prepend-https.ts @@ -8,16 +8,16 @@ import { prependHTTP } from './prepend-http'; * * Note: this will not replace "http://" with "https://". * - * @param {string} url The URL to test. + * @param url The URL to test. * * @example * ```js * const actualURL = prependHTTPS( 'wordpress.org' ); // https://wordpress.org * ``` * - * @return {string} The updated URL. + * @return The updated URL. */ -export function prependHTTPS( url ) { +export function prependHTTPS( url: string ): string { if ( ! url ) { return url; } diff --git a/packages/url/src/remove-query-args.js b/packages/url/src/remove-query-args.ts similarity index 84% rename from packages/url/src/remove-query-args.js rename to packages/url/src/remove-query-args.ts index 5d31313b08a74e..7695d2527979f1 100644 --- a/packages/url/src/remove-query-args.js +++ b/packages/url/src/remove-query-args.ts @@ -7,17 +7,17 @@ import { buildQueryString } from './build-query-string'; /** * Removes arguments from the query string of the url * - * @param {string} url URL. - * @param {...string} args Query Args. + * @param url URL. + * @param args Query Args. * * @example * ```js * const newUrl = removeQueryArgs( 'https://wordpress.org?foo=bar&bar=baz&baz=foobar', 'foo', 'bar' ); // https://wordpress.org?baz=foobar * ``` * - * @return {string} Updated URL. + * @return Updated URL. */ -export function removeQueryArgs( url, ...args ) { +export function removeQueryArgs( url: string, ...args: string[] ): string { const fragment = url.replace( /^[^#]*/, '' ); url = url.replace( /#.*/, '' ); diff --git a/packages/url/src/safe-decode-uri-component.js b/packages/url/src/safe-decode-uri-component.ts similarity index 61% rename from packages/url/src/safe-decode-uri-component.js rename to packages/url/src/safe-decode-uri-component.ts index 2bf2047803fba3..236637bf768470 100644 --- a/packages/url/src/safe-decode-uri-component.js +++ b/packages/url/src/safe-decode-uri-component.ts @@ -2,11 +2,11 @@ * Safely decodes a URI component with `decodeURIComponent`. Returns the URI component unmodified if * `decodeURIComponent` throws an error. * - * @param {string} uriComponent URI component to decode. + * @param uriComponent URI component to decode. * - * @return {string} Decoded URI component if possible. + * @return Decoded URI component if possible. */ -export function safeDecodeURIComponent( uriComponent ) { +export function safeDecodeURIComponent( uriComponent: string ): string { try { return decodeURIComponent( uriComponent ); } catch ( uriComponentError ) { diff --git a/packages/url/src/safe-decode-uri.js b/packages/url/src/safe-decode-uri.ts similarity index 72% rename from packages/url/src/safe-decode-uri.js rename to packages/url/src/safe-decode-uri.ts index f5f6379e19164a..ab0830597fd0df 100644 --- a/packages/url/src/safe-decode-uri.js +++ b/packages/url/src/safe-decode-uri.ts @@ -2,16 +2,16 @@ * Safely decodes a URI with `decodeURI`. Returns the URI unmodified if * `decodeURI` throws an error. * - * @param {string} uri URI to decode. + * @param uri URI to decode. * * @example * ```js * const badUri = safeDecodeURI( '%z' ); // does not throw an Error, simply returns '%z' * ``` * - * @return {string} Decoded URI if possible. + * @return Decoded URI if possible. */ -export function safeDecodeURI( uri ) { +export function safeDecodeURI( uri: string ): string { try { return decodeURI( uri ); } catch ( uriError ) { diff --git a/packages/url/src/test/index.js b/packages/url/src/test/index.js index 0be7c7542b71c8..9243471c8c079c 100644 --- a/packages/url/src/test/index.js +++ b/packages/url/src/test/index.js @@ -494,12 +494,12 @@ describe( 'getPathAndQueryString', () => { beforeAll( jest.resetModules ); afterAll( jest.resetModules ); it( 'combines the results of `getPath` and `getQueryString`', () => { - jest.doMock( '../get-path.js', () => ( { + jest.doMock( '../get-path', () => ( { getPath( { path } = {} ) { return path; }, } ) ); - jest.doMock( '../get-query-string.js', () => ( { + jest.doMock( '../get-query-string', () => ( { getQueryString( { queryString } = {} ) { return queryString; },