import { parse , stringifyUrl , stringify } from 'query-string' ; param { string } s return { object } export const parseQuery = ( s = '' ) : { [ key ] : any ; } => { return parse ( s || window . location . search ) ; } ; param { object } query param { string } href return { string } export const generateUrl = ( query , href ? ) : string => { const parsed = parseQuery ( ) ; Object . assign ( parsed , query ) ; return stringifyUrl ( { url : href ? href : location . href , query : parsed } ) ; } ; param { any } history param { string } pathname param { object } query export const pushUrl = ( history , pathname , query ? ) => { const currentQuery = parseQuery ( ) ; history . push ( `${ pathname }?${ stringify ( Object . assign ( currentQuery , query ) ) }` ) ; } ; param { string } name return { string | null } export const getParams = ( name ) : string | null => { const parsed = parseQuery ( ) ; const res = parsed [ name ] ; if ( res ) { return res === 'undefined' || res === 'null' || res === 'false' ? null : decodeURIComponent ( res ) ; } return null ; } ; param { string } name param { any } value export const setParams = ( function ( window ) { return function ( name , value ) { const _originHref = window . location . origin + window . location . pathname ; if ( ! name ) { return ; } let query = { } ; if ( typeof name === 'string' ) { query = { [ name ] : value , } ; } if ( Object . prototype . toString . call ( name ) === '[object Object]' ) { query = name ; } const parsed = parseQuery ( ) ; const historyStr = `?${ stringify ( Object . assign ( { } , parsed , query ) ) }` ; if ( window . history . replaceState ) { const url = _originHref + historyStr ; window . history . replaceState ( null , '' , url ) ; } else { window . location . hash = historyStr ; } } ; } ) ( window ) ; param { string } name export const removeParams = ( function ( window ) { return function ( name ) { const _originHref = window . location . origin + window . location . pathname ; if ( ! name ) { return ; } let removeList = [ ] ; const nameType = Object . prototype . toString . call ( name ) ; if ( nameType === '[object String]' ) { removeList . push ( name ) ; } else if ( nameType === '[object Array]' ) { removeList = name ; } else if ( nameType === '[object Object]' ) { removeList = Object . keys ( name ) ; } else { return ; } const parsed = parseQuery ( ) ; Object . keys ( parsed ) . forEach ( ( val ) => { if ( removeList . includes ( val ) ) { delete parsed [ val ] ; } } ) ; const historyStr = `?${ stringify ( Object . assign ( { } , parsed ) ) }` ; if ( window . history . replaceState ) { const url = _originHref + historyStr ; window . history . replaceState ( null , '' , url ) ; } else { window . location . hash = historyStr ; } } ; } ) ( window ) ;