Skip to content

Commit d216c2e

Browse files
authored
removeUrlParams (#31)
* feat: limitStringToTarget’ * docs: 文档 * fix: tslint * feat: removeUrlParams * fix: index导入
1 parent 5a65cf7 commit d216c2e

File tree

9 files changed

+100
-1
lines changed

9 files changed

+100
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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)

docs/functions/getUrlQuery.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ group:
1414
import React from 'react';
1515
import { getUrlQuery } from '@mlz/doraemon';
1616

17+
// 默认取location.href
18+
getUrlQuery();
1719
export default (props) => {
1820
const query = getUrlQuery('https://juicecube.github.io/doraemon?name=doraemon');
1921
return (

docs/functions/removeUrlParams.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
```

src/getUrlQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as _ from 'lodash';
33
import { 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)) {

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import deleteInvalidValue from './deleteInvalidValue';
88
import digitalToCash from './digitalToCash';
99
import copyTextToClipboard from './copyTextToClipboard';
1010
import limitStringToTarget from './limitStringToTarget';
11+
import removeUrlParams from './removeUrlParams';
1112

1213
export {
1314
getUrlQuery,
@@ -20,6 +21,7 @@ export {
2021
digitalToCash,
2122
copyTextToClipboard,
2223
limitStringToTarget,
24+
removeUrlParams,
2325
};
2426

2527
export const doraemaon = {
@@ -33,6 +35,7 @@ export const doraemaon = {
3335
digitalToCash,
3436
copyTextToClipboard,
3537
limitStringToTarget,
38+
removeUrlParams,
3639
};
3740

3841
export default doraemaon;

src/removeUrlParams.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;

test/getUrlQuery.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { getUrlQuery } from '../src/getUrlQuery';
2+
import { domain } from '../src/constants';
3+
import { LocationSetter } from './utils/location-setter';
24

35
describe('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
});

test/removeUrlParams.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
});

test/utils/location-setter.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)