データと保存
種別ごとに数える
normalizetype
携帯、固定電話、フリーダイヤル、不正の件数を数えます。
試す
fixed_line2invalid1ip_phone1mobile2toll_free2unified_number1
しくみ
type ごとに数えると、連絡先の内訳が分かります。たとえば、SMS を受け取れる顧客の数です。不正な記録は別に数えるので、整える必要のある量も分かります。
各言語で
import { normalize } from "@kurashina-design/japan-phone";
const numbers = [
"090-1234-5678", "080-1000-0000", "03-5253-8111", "092-513-5612", "050-3818-1250",
"0120-123-456", "0800-123-4567", "0570-123-456", "03-1234-5678",
];
const counts = {};
for (const input of numbers) {
const type = normalize(input)?.type ?? "invalid";
counts[type] = (counts[type] ?? 0) + 1;
}
for (const type of Object.keys(counts).sort()) console.log(type, counts[type]);
from japan_phone import normalize
numbers = [
"090-1234-5678", "080-1000-0000", "03-5253-8111", "092-513-5612", "050-3818-1250",
"0120-123-456", "0800-123-4567", "0570-123-456", "03-1234-5678",
]
counts = {}
for value in numbers:
number = normalize(value)
kind = number.type if number else "invalid"
counts[kind] = counts.get(kind, 0) + 1
for kind in sorted(counts):
print(kind, counts[kind])
import jp.kurashina.japanphone.JapanPhoneNumber;
import jp.kurashina.japanphone.JapanPhone;
import java.util.Map;
import java.util.TreeMap;
String[] numbers = {
"090-1234-5678", "080-1000-0000", "03-5253-8111", "092-513-5612", "050-3818-1250",
"0120-123-456", "0800-123-4567", "0570-123-456", "03-1234-5678",
};
Map<String, Integer> counts = new TreeMap<>();
for (String input : numbers) {
String type = JapanPhone.normalize(input).map(number -> number.getType().getValue()).orElse("invalid");
counts.put(type, counts.getOrDefault(type, 0) + 1);
}
for (Map.Entry<String, Integer> count : counts.entrySet()) {
System.out.println(count.getKey() + " " + count.getValue());
}
出力:
fixed_line 2
invalid 1
ip_phone 1
mobile 2
toll_free 2
unified_number 1