Punycode
Deprecated since: v7.0.0
[Stable: 0 - Deprecated]
Stable: 0 Stability: 0 - 非推奨
ソースコード: lib/punycode.js
Node.jsにバンドルされているpunycodeモジュールのバージョンは非推奨となっています。 将来のNode.jsのメジャーバージョンで、このモジュールは削除されます。 現在punycode
モジュールに依存しているユーザーは、ユーザーランドで提供されるPunycode.jsモジュールを使用するように切り替える必要があります。 punycodeベースのURLエンコーディングについては、url.domainToASCII
または、より一般的には、WHATWG URL APIを参照してください。
punycode
モジュールは、Punycode.jsモジュールのバンドルされたバージョンです。 これは以下を使用してアクセスできます。
const punycode = require('node:punycode');
Punycodeは、RFC 3492で定義された文字エンコーディングスキームであり、主に国際化ドメイン名での使用を目的としています。 URLのホスト名はASCII文字のみに制限されているため、非ASCII文字を含むドメイン名はPunycodeスキームを使用してASCIIに変換する必要があります。 たとえば、英語の単語 'example'
に翻訳される日本語の文字は '例'
です。 国際化ドメイン名 '例.com'
( 'example.com'
と同等)は、PunycodeによってASCII文字列 'xn--fsq.com'
として表されます。
punycode
モジュールは、Punycode標準の簡単な実装を提供します。
punycode
モジュールは Node.js で使用されているサードパーティの依存関係であり、開発者の便宜のために利用できるようにされています。 モジュールの修正またはその他の変更は、Punycode.js プロジェクトに指示する必要があります。
punycode.decode(string)
追加: v0.5.1
string
<string>
punycode.decode()
メソッドは、ASCII 文字のみの Punycode 文字列を、同等の Unicode コードポイントの文字列に変換します。
punycode.decode('maana-pta'); // 'mañana'
punycode.decode('--dqo34k'); // '☃-⌘'
punycode.encode(string)
Added in: v0.5.1
string
<string>
punycode.encode()
メソッドは、Unicode コードポイントの文字列を ASCII のみの文字の Punycode 文字列に変換します。
punycode.encode('mañana'); // 'maana-pta'
punycode.encode('☃-⌘'); // '--dqo34k'
punycode.toASCII(domain)
Added in: v0.6.1
domain
<string>
punycode.toASCII()
メソッドは、国際化ドメイン名を表す Unicode 文字列を Punycode に変換します。 ドメイン名の非 ASCII 部分のみが変換されます。 すでに ASCII 文字のみを含む文字列に対して punycode.toASCII()
を呼び出しても、効果はありません。
// ドメイン名をエンコードする
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
punycode.toASCII('example.com'); // 'example.com'
punycode.toUnicode(domain)
Added in: v0.6.1
domain
<string>
punycode.toUnicode()
メソッドは、Punycode でエンコードされた文字を含むドメイン名を表す文字列を Unicode に変換します。 ドメイン名の Punycode でエンコードされた部分のみが変換されます。
// ドメイン名をデコードする
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
punycode.toUnicode('example.com'); // 'example.com'
punycode.ucs2
Added in: v0.7.0
punycode.ucs2.decode(string)
Added in: v0.7.0
string
<string>
punycode.ucs2.decode()
メソッドは、文字列内の各 Unicode シンボルの数値コードポイント値を含む配列を返します。
punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
// U+1D306 の中央のテトラグラムのサロゲートペア:
punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
punycode.ucs2.encode(codePoints)
追加: v0.7.0
codePoints
<integer[]>
punycode.ucs2.encode()
メソッドは、数値コードポイント値の配列に基づいて文字列を返します。
punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
punycode.version
追加: v0.6.1
現在の Punycode.js のバージョン番号を識別する文字列を返します。