File tree Expand file tree Collapse file tree 9 files changed +100
-1
lines changed
Expand file tree Collapse file tree 9 files changed +100
-1
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,13 @@ npm i @mlz/doraemon@latest
5151## TODO
5252[ TODOS] ( https://github.com/juicecube/doraemon/projects/3 ) ;
5353
54+ ## 开发
55+ ### 常用命令
56+ ``` bash
57+ # 单个文件测试
58+ jest test/limitStringToTarget.test.ts
59+ ```
60+
5461## 须知
5562
5663- 如有问题或提需求请提[ issue] ( https://github.com/juicecube/doraemon/issues ) 。
Original file line number Diff line number Diff line change 1414import React from ' react' ;
1515import { getUrlQuery } from ' @mlz/doraemon' ;
1616
17+ // 默认取location.href
18+ getUrlQuery ();
1719export default (props ) => {
1820 const query = getUrlQuery (' https://juicecube.github.io/doraemon?name=doraemon' );
1921 return (
Original file line number Diff line number Diff line change 1+ ---
2+ group :
3+ title : 方法
4+ toc : menu
5+ ---
6+
7+ # removeUrlParams
8+ 删除指定url上的指定参数。
9+ ***
10+
11+ ``` javascript
12+ // https://da.ithen.cn?name=zane
13+ removeUrlParams (' https://da.ithen.cn?name=zane&age=18' , ' age);
14+ // https://da.ithen.cn?name=zane
15+ removeUrlParams(' https: // da.ithen.cn?name=zane&age=18', ['age']);
16+ ` ` `
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import * as _ from 'lodash';
33import { IObject } from './global' ;
44
55/** 获取url参数 */
6- export const getUrlQuery = ( url :string ) => {
6+ export const getUrlQuery = ( url :string = location . href ) => {
77 const result :IObject = { } ;
88 // 不是string
99 if ( ! _ . isString ( url ) ) {
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import deleteInvalidValue from './deleteInvalidValue';
88import digitalToCash from './digitalToCash' ;
99import copyTextToClipboard from './copyTextToClipboard' ;
1010import limitStringToTarget from './limitStringToTarget' ;
11+ import removeUrlParams from './removeUrlParams' ;
1112
1213export {
1314 getUrlQuery ,
@@ -20,6 +21,7 @@ export {
2021 digitalToCash ,
2122 copyTextToClipboard ,
2223 limitStringToTarget ,
24+ removeUrlParams ,
2325} ;
2426
2527export const doraemaon = {
@@ -33,6 +35,7 @@ export const doraemaon = {
3335 digitalToCash,
3436 copyTextToClipboard,
3537 limitStringToTarget,
38+ removeUrlParams,
3639} ;
3740
3841export default doraemaon ;
Original file line number Diff line number Diff line change 1+ import addQueryToUrl from "./addQueryToUrl" ;
2+ import getUrlQuery from "./getUrlQuery" ;
3+
4+ /** 移除url上的指定参数 */
5+ export function removeUrlParams ( url :string , keys :string | string [ ] ) {
6+ if ( ! url || ! keys ) {
7+ return url ;
8+ }
9+ let _keys = keys ;
10+ if ( typeof ( keys ) === 'string' ) {
11+ _keys = [ keys ] ;
12+ }
13+ const urlParts = url . split ( '?' ) ;
14+ const params = getUrlQuery ( url ) ;
15+ if ( urlParts . length > 1 ) {
16+ // 删除指定参数
17+ for ( const key of _keys ) {
18+ delete params [ key ] ;
19+ }
20+ }
21+ return addQueryToUrl ( params , urlParts [ 0 ] ) ;
22+ }
23+
24+ export default removeUrlParams ;
Original file line number Diff line number Diff line change 11import { getUrlQuery } from '../src/getUrlQuery' ;
2+ import { domain } from '../src/constants' ;
3+ import { LocationSetter } from './utils/location-setter' ;
24
35describe ( 'getUrlQuery' , ( ) => {
6+ const locationSetter = new LocationSetter ( ) ;
7+ it ( 'default location.href' , ( ) => {
8+ locationSetter . set ( { href : `${ domain } ?a=1` } ) ;
9+ expect ( getUrlQuery ( ) ) . toEqual ( { a : '1' } ) ;
10+ } ) ;
411 it ( 'null' , ( ) => {
512 expect ( getUrlQuery ( null ) ) . toEqual ( { } ) ;
613 } ) ;
Original file line number Diff line number Diff line change 1+ import { domain } from './../src/constants' ;
2+ import removeUrlParams from '../src/removeUrlParams' ;
3+
4+ describe ( 'removeUrlParams' , ( ) => {
5+ const url = `${ domain } ?a=123&b=456&c=789` ;
6+ it ( 'validate params' , ( ) => {
7+ expect ( removeUrlParams ( null , null ) ) . toBeNull ( ) ;
8+ expect ( removeUrlParams ( null , 'a' ) ) . toBeNull ( ) ;
9+ expect ( removeUrlParams ( url , null ) ) . toBe ( url ) ;
10+ } ) ;
11+ it ( 'test keys & url' , ( ) => {
12+ expect ( removeUrlParams ( domain , 'a' ) ) . toBe ( domain ) ;
13+ expect ( removeUrlParams ( domain , [ 'a' ] ) ) . toBe ( domain ) ;
14+ expect ( removeUrlParams ( url , 'a' ) ) . toBe ( `${ domain } ?b=456&c=789` ) ;
15+ expect ( removeUrlParams ( url , [ 'a' , 'b' ] ) ) . toBe ( `${ domain } ?c=789` ) ;
16+ expect ( removeUrlParams ( url , [ ] ) ) . toBe ( url ) ;
17+ } ) ;
18+ } ) ;
Original file line number Diff line number Diff line change 1+ import { IObject } from "../../src/global" ;
2+
3+ /** 设置location的类 */
4+ export class LocationSetter {
5+ constructor ( ) {
6+ this . _defineLocation ( ) ;
7+ }
8+ private _defineLocation = ( value :IObject = { } ) => {
9+ Object . defineProperty ( global , 'location' , {
10+ value,
11+ writable : true ,
12+ } ) ;
13+ }
14+ /** 设置location */
15+ set = ( location :any ) => {
16+ global . location = location ;
17+ }
18+ /** 重置为初始值 */
19+ reset = ( ) => {
20+ this . set ( undefined ) ;
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments