npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@urltools/modify-url

v2.2.0

Published

modify url based on serializable config

Downloads

18

Readme

modify-url

motivation

i need a tool which modifies the url however i want without writing any code (except for the tool) based on just a serializable config which is so intuitive that you don't need to read the docs

usage

import modifyUrl from '@urltools/modify-url'

// serializable config is used to modify the url
const config = {
  protocol: 'https',
  host: 'google.com',
  searchParams: {
    type: 'pick',
    value: ['q']
  }
}

const oldUrl = 'http://bing.com/search?q=galaxy&tracking=somethingyoudontwant'

// create processUrl once based on serializable config
const processUrl = modifyUrl(config)

// processUrl can be used in pipe (functional style) or as a url transformation function
const newUrl = processUrl(oldUrl)

expect(newUrl).toBe('https://google.com/search?q=galaxy')

config

WHATWG terminology is used for different parts of the url

json schema of config

import modifyUrl from '@urltools/modify-url'

// this assertion is represented ->
const oldUrl = 'http://google.com/'
const config = { protocol: 'https' }
const newUrl = 'https://google.com/'
expect(
  modifyUrl(config)(oldUrl)
).toBe(newUrl)

// -> like this for brevity
'http://google.com/'
{ protocol: 'https' }
'https://google.com/'

/******************************/
// config keys

// protocol
'http://google.com'
{ protocol: 'https' }
'https://google.com/'

// username
'http://google.com'
{ username: 'akash' }
'http://[email protected]'

// password
'http://google.com'
{ password: 'secret123' }
'http://:[email protected]'

// host
'http://bing.com/some/path'
{ host: 'google.com:3000' }
'http://google.com:3000/some/path'

// pathname - replace
'http://google.com/some/path'
{ pathname: { type: 'replace', value: '/some/other/path' } }
'http://google.com/some/other/path'

// pathname - append
'http://google.com/some/path'
{ pathname: { type: 'append', value: '../foo' } }
'http://google.com/some/foo'

// searchParams - pick
'http://google.com/search?q=galaxy&foo=bar&track=someid'
{ searchParams: { type: 'pick', value: ['foo', 'track'] } }
'http://google.com/search?foo=bar&track=someid'

// searchParams - omit
'http://google.com/search?q=galaxy&foo=bar&track=someid'
{ searchParams: { type: 'omit', value: ['foo', 'track'] } }
'http://google.com/search?q=galaxy'

// searchParams - replace
'http://google.com/search?q=galaxy&foo=bar'
{ searchParams: { type: 'replace', value: { name: 'akash' } } }
'http://google.com/search?name=akash'

// searchParams - append
'http://google.com/search?q=galaxy&foo=bar'
{ searchParams: { type: 'append', value: { name: 'akash' } } }
'http://google.com/search?q=galaxy&foo=bar&name=akash'

// hash
'http://google.com/some/path'
{ hash: 'section2' }
'http://google.com/some/path#section2'

/******************************/
// examples

// most of the scenarios can be covered by a single config
// as multiple parts of the url are independent

// e.g. config with one part of the url
const config1 = { protocol: 'https' }

// e.g. config with multiple parts of the url
const config2 = { protocol: 'https', host: 'google.com' }


// complex scenario might need multiple configs

// e.g. searchParams pick and append
const config3 = { searchParams: { type: 'pick', value: ['q'] } }
const config4 = { searchParams: { type: 'append', value: { name: 'akash' } } }

const originalUrl = 'http://google.com/search?q=galaxy&undesirablefoo=bar&pryingtrackid=baz'
const expectedUrl = 'http://google.com/search?q=galaxy&name=akash'

const tempUrl = modifyUrl(config3)(originalUrl)
const finalUrl = modifyUrl(config4)(tempUrl)

expect(finalUrl).toBe(expectedUrl)

// if you are using a functional library like ramda,
const f = require('ramda').pipe(
  modifyUrl(config3),
  modifyUrl(config4)
)

expect(
  f(originalUrl)
).toBe(expectedUrl)

config validation

JSON schema is provided to validate dynamic configs (e.g. from user input). Please use a JSON schema validator like AJV

const schema = require('@urltools/utils/lib/schema/modifyUrl.json')

config

WHATWG terminology is used for different parts of the url

json schema of config