From 15ebc01f93849c8638bbca3881156d92d1078c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 31 Aug 2018 14:12:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96base64=E7=9A=84=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E5=92=8C=E8=A7=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 3 ++- index.js | 13 +++++-------- package.json | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Readme.md b/Readme.md index 10862ee..872a335 100644 --- a/Readme.md +++ b/Readme.md @@ -117,9 +117,10 @@ crypto.base64encode('hello world') //aGVsbG8gd29ybGQ= -### base64decode(str[, urlFriendly]) +### base64decode(str[, urlFriendly][, outEncode]) - str `` - urlFriendly `` 可选 +- outEncode `` 可选,默认ascii, 如果之前的编码的字符串带有中文等字符,请设置为utf8等 > 与之对应的,便是这个base解码了。同样`urlFriendly`是指要解码的字符串之前是否采用了URL友好处理,默认否。 diff --git a/index.js b/index.js index 1ca1f94..0c25d8f 100644 --- a/index.js +++ b/index.js @@ -188,13 +188,11 @@ module.exports = { } let encode = str.toString('base64') if (urlFriendly) { - encode = encode - .replace(/[+\/]/g, m => { - return m === '+' ? '-' : '_' - }) + return encode + .replace(/\+/g, '-') + .replace(/\//g, '_') .replace(/=/g, '') } - return encode }, @@ -207,9 +205,8 @@ module.exports = { base64decode(str, urlFriendly, encoding) { if (urlFriendly) { str = str - .replace(/[-_]/g, m => { - return m === '-' ? '+' : '/' - }) + .replace(/-/g, '+') + .replace(/_/g, '/') .replace(/[^A-Za-z0-9\+\/]/g, '') } diff --git a/package.json b/package.json index 4385e16..b82830d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto.js", - "version": "1.1.3", + "version": "1.1.4", "description": "原生crypto加密模块的二次封装,简化常用加密函数的使用", "keywords": [ "md5",