Id { static String asId(String text) { String[] parts = text.split("[-\\s]"); List<String> sanitized = new ArrayList<>(); for (String part : parts) { if (part != null) { String s = part.replaceAll("\\s+", ""); s = s.replaceAll("[^a-zA-Z0-9-_]", ""); s = s.replace('_', '-'); if (s.length() != 0) { sanitized.add(s); } } } if (sanitized.isEmpty()) { return null; } else { return sanitized.stream() .filter(s -> s != null && s.trim().length() != 0) .map(String::toLowerCase) .collect(joining("-")); } } private  Id(); static String unique(); static String unique(String id, String... additionalIds); static String build(String id, String... additionalIds);  }
@Test void asId() { assertEquals("lorem-ipsum", Id.asId("lorem-ipsum")); assertEquals("lorem-ipsum", Id.asId("Lorem Ipsum")); assertEquals("l0rem-ip5um", Id.asId("l0rem-ip5um")); assertEquals("lorem---ipsum", Id.asId("lorem §±!@#$%^&*()=_+[]{};'\\:\"|,./<>?`~ ipsum")); }