Created
January 16, 2017 22:51
-
-
Save aegyed91/72208ca6d933f4f3256b7129dc175aae to your computer and use it in GitHub Desktop.
QueryEncoder no php flag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { QueryEncoder, URLSearchParams } from '@angular/http'; | |
import { AppConfig } from '../../app/app.config'; | |
/** | |
* PHP's urlencode replaces `+` with ` ` while encodeURI in js does not touch `+`. | |
* | |
* @link http://php.net/manual/en/function.urlencode.php | |
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI | |
* @link https://angular.io/docs/ts/latest/api/http/index/URLSearchParams-class.html | |
*/ | |
export class Qu3ryEncoder extends QueryEncoder { | |
constructor(private config?: {php: boolean;}) { | |
super(); | |
} | |
encodeKey(k: string): string { | |
return super.encodeKey(k); | |
} | |
encodeValue(v: string): string { | |
return this.config.php ? this.standardEncoding(v) : super.encodeValue(v); | |
} | |
// https://github.com/angular/angular/blob/8f5dd1f11e6ca1888fdbd3231c06d6df00aba5cc/modules/%40angular/http/src/url_search_params.ts#L33-L44 | |
private standardEncoding(v: string): string { | |
return encodeURIComponent(v) | |
.replace(/%40/gi, '@') | |
.replace(/%3A/gi, ':') | |
.replace(/%24/gi, '$') | |
.replace(/%2C/gi, ',') | |
.replace(/%3B/gi, ';') | |
// .replace(/%2B/gi, '+') | |
.replace(/%3D/gi, '=') | |
.replace(/%3F/gi, '?') | |
.replace(/%2F/gi, '/'); | |
} | |
} | |
/** | |
* Example usage: let params = new URLS3archParams(); | |
* | |
* @source: https://github.com/angular/angular/blob/8f5dd1f11e6ca1888fdbd3231c06d6df00aba5cc/modules/%40angular/http/src/url_search_params.ts#L81 | |
*/ | |
export class URLS3archParams extends URLSearchParams { | |
constructor(rawParams: string = '', queryEncoder: QueryEncoder = new Qu3ryEncoder({php: AppConfig.respectPHP_urlencode})) { | |
super(rawParams, queryEncoder); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment