UNPKG

hd-utils

Version:

A handy utils for modern JS developers

17 lines (16 loc) 669 B
import isString from '../validation/isString'; import isURL from '../validation/isURL'; import addHttpToURL from './addHttpToURL'; /** * @description will return the url params of the passed Url * @example getUrlPathParams("www.example.com/1/2", {filterEmpty:true}) // ["1", "2"] * @example getUrlPathParams("www.example.com/1/2", {filterEmpty:false}) // ["", "1", "2"] */ export default function getUrlPathParams(url, options) { if (!(isString(url) && isURL(url))) return []; const paramList = new URL(addHttpToURL(url)).pathname.split('/'); return options?.filterEmpty ? paramList.filter((s) => s.trim().length) : paramList; }