-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* 返回 base64 解码后的字符串。 | ||
* | ||
* @param str 要解码的字符串 | ||
* @returns 解码后的字符串 | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem#Solution_1_%E2%80%93_escaping_the_string_before_encoding_it | ||
*/ | ||
export default function base64Decode(str: string): string { | ||
return decodeURIComponent( | ||
atob(str) | ||
.split('') | ||
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)) // tslint:disable-line | ||
.join('') | ||
) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* 返回 base64 编码后的字符串。 | ||
* | ||
* @param str 要编码的字符串 | ||
* @returns 编码后的字符串 | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem#Solution_1_%E2%80%93_escaping_the_string_before_encoding_it | ||
*/ | ||
export default function base64Encode(str: string | number): string { | ||
return btoa( | ||
encodeURIComponent(str as string) | ||
.replace( | ||
/%([0-9A-F]{2})/g, | ||
function toSolidBytes(match, p1) { | ||
return String.fromCharCode(`0x${p1}` as any) | ||
} | ||
) | ||
) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import base64Decode from './base64Decode' | ||
import repeat from './repeat' | ||
|
||
/** | ||
* 返回 base64url 解码后的字符串。 | ||
* | ||
* @param str 要解码的字符串 | ||
* @returns 解码后的字符串 | ||
* @see http://www.ietf.org/rfc/rfc4648.txt | ||
*/ | ||
export default function base64UrlDecode(str: string): string { | ||
const remainder = str.length % 4 | ||
if (str !== '' && remainder > 0) { | ||
str += repeat('=', 4 - remainder) | ||
} | ||
return base64Decode(str.replace(/-/g, '+').replace(/_/g, '/')) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import base64Encode from './base64Encode' | ||
|
||
/** | ||
* 返回 base64url 编码后的字符串。 | ||
* | ||
* @param str 要编码的字符串 | ||
* @returns 编码后的字符串 | ||
* @see http://www.ietf.org/rfc/rfc4648.txt | ||
*/ | ||
export default function base64UrlEncode(str: string | number): string { | ||
return base64Encode(str) | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=+$/, '') | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* 重复 N 次给定字符串。 | ||
* | ||
* @param str 要重复的字符串 | ||
* @param [n=1] 重复的次数 | ||
* @returns 结果字符串 | ||
*/ | ||
export default function repeat(str: string | number, n: number = 1) { | ||
n = Math.round(n <= 0 ? 1 : n) | ||
let result = '' | ||
while (n--) { | ||
result += str | ||
} | ||
return result | ||
} |
This file contains 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