【Node.js】テスト用に安全に使用できる電話番号生成

こんにちは、フリーランスエンジニアの太田雅昭です。

安全に使用できる電話番号

世の中には、ドラマなどで安全に使用できる電話番号といったものが提供されています。例えばアメリカの555が検索ですぐにヒットしますが、イギリスでもそうした電話番号が提供されています。

アメリカの555

こちらは割と有名な話のようです。アメリカの555-0100から555-0199までがドラマなどで安全に使用できる番号として提供されています。

Node.jsで自動生成するには以下のようになります。

function generate555SafePhone() {
    let phone = '+1-555-0';
    const rand = random(100, 199);
    return phone + rand;
}

function random(min: number, max: number) {
    const result = min + Math.random() * (max - min);
    return Math.floor(result);
}

イギリスの安全な電話番号

イギリスでもそうした安全に使用できる電話番号が提供されています。

Telephone numbers for use in TV and radio drama programmes

Ofcom continues to receive requests for Geographic Numbers to be used for drama purposes. In light of this, Ofcom has set out below a range of Geographic Telephone Numbers recommended for drama purposes (1000 numbers within each Geographic Area Code).

Node.jsで書くと以下のようになります。なお最新情報などご自身で十分検証を行った上でご使用ください。

// [area code, min, max][]
const UK_CODES = [
    ['0113', 4960000, 4960999],
    ['0114', 4960000, 4960999],
    ['0115', 4960000, 4960999],
    ['0116', 4960000, 4960999],
    ['0117', 4960000, 4960999],
    ['0118', 4960000, 4960999],
    ['0121', 4960000, 4960999],
    ['0131', 4960000, 4960999],
    ['0141', 4960000, 4960999],
    ['0151', 4960000, 4960999],
    ['0161', 4960000, 4960999],
    ['020', 79460000, 79460999],
    ['0191', 4980000, 4980999],
    ['028', 96496000, 96496999],
    ['029', 20180000, 20180999],
    ['01632', 960000, 960999],
    ['07700', 900000, 900999],
    ['08081', 570000, 570999],
    ['0909', 8790000, 8790999],
    ['03069', 990000, 990999],
] as const;


export function generateUkSafePhone(){
    const [area, min, max] = UK_CODES[random(0, UK_CODES.length)];
    const number = random(min, max);
    return `+44-${area}-${number}`;

}

function random(min: number, max: number) {
    const result = min + Math.random() * (max - min);
    return Math.floor(result);
}

小話

最近、ゴールデンカムイの漫画を読み終わりました。面白かったです。最後の方はかなり画力も高くなっていましたね。ストーリーも練られていて、ゆっくり読み進めていきました。満足です。