Node decodeURIComponent 是幂等的吗?如果没有,是否有幂等的选择?

Is Node decodeURIComponent idempotent? If not, is there an idempotent alternative?

Node decodeURIComponent 是幂等的吗?

是否...

decodeURIComponent(x) === decodeURIComponent(decodeURIComponent(x))

所有 x?

如果不是,是否有幂等的替代方案?我自己也在考虑这是否可行。

没有

> encodeURIComponent('%')
'%25'
> encodeURIComponent(encodeURIComponent('%'))
'%2525'

> decodeURIComponent('%2525')
'%25'
> decodeURIComponent(decodeURIComponent('%2525'))
'%'

> decodeURIComponent('%2525') === decodeURIComponent(decodeURIComponent('%2525'))
false

没有。如果字符串解码为一个新的序列,它本身可以解释为编码的 URI 组件,那么它可以再次解码为不同的字符串:

const x = '%2521';
console.log(decodeURIComponent(x), decodeURIComponent(decodeURIComponent(x)));

%2521%21!

任何给定的字符串要么以特定格式编码,要么是纯文本。您无法猜测 它应该是什么。 %21 可以是纯文本字符串“%21”,也可以是表示“!”的 URL 编码字符串。您需要 知道 它应该是什么并相应地解释它。这通常适用于任何和所有文本编码格式。