Data and storage
Find duplicates written differently
normalize
Group entries that are the same number in different notations.
Try it
03-5253-81114090-1234-56782
How it works
03-5253-8111, 03-5253-8111, (03)5253-8111, and +81-3-5253-8111 are one number. Group by the normalized value to find duplicate customers or contacts.
Entries that are not valid keep their original text as the key, so they stay visible for a person to check.
In each language
import { normalize } from "@kurashina-design/japan-phone";
// A customer list where the same numbers were typed in different ways.
const entries = ["03-5253-8111", "03-5253-8111", "(03)5253-8111", "+81-3-5253-8111", "090-1234-5678", "09012345678"];
const counts = new Map();
for (const entry of entries) {
const key = normalize(entry)?.national ?? entry;
counts.set(key, (counts.get(key) ?? 0) + 1);
}
for (const [number, count] of counts) console.log(`${number}: ${count}`);
from japan_phone import normalize
# A customer list where the same numbers were typed in different ways.
entries = ["03-5253-8111", "03-5253-8111", "(03)5253-8111", "+81-3-5253-8111", "090-1234-5678", "09012345678"]
counts = {}
for entry in entries:
number = normalize(entry)
key = number.national if number else entry
counts[key] = counts.get(key, 0) + 1
for number, count in counts.items():
print(f"{number}: {count}")
import jp.kurashina.japanphone.JapanPhoneNumber;
import jp.kurashina.japanphone.JapanPhone;
import java.util.LinkedHashMap;
import java.util.Map;
// A customer list where the same numbers were typed in different ways.
String[] entries = {"03-5253-8111", "03-5253-8111", "(03)5253-8111", "+81-3-5253-8111", "090-1234-5678", "09012345678"};
Map<String, Integer> counts = new LinkedHashMap<>();
for (String entry : entries) {
String key = JapanPhone.normalize(entry).map(JapanPhoneNumber::getNational).orElse(entry);
counts.put(key, counts.getOrDefault(key, 0) + 1);
}
for (Map.Entry<String, Integer> count : counts.entrySet()) {
System.out.println(count.getKey() + ": " + count.getValue());
}
Output:
03-5253-8111: 4
090-1234-5678: 2