You have to create an abbreviation function. It will take string and return a string while taking the first and last char of word and replace the remaining with the number of chars in it.
For example:
internationalization => i18n
Localization => L10n
Deepak-Vishwa => D4k-V4a
I Love India => I L2e I3a
const abbrev = (text = "") => { return text.replace(/\w{4,}/g, s => { l = s.length; return `${s[0]}${l - 2}${s[l - 1]}`; }); };
const abbrevWord = (text = "") => {if (text.length < 3) return text;const first = text.charAt(0);const last = text.slice(-1);const remLen = text.length - 2;return `${first}${remLen}${last}`;};const simpleChars = /\w+/g;- const abbrev = (text = "") => {
return text.replace(simpleChars, (matched) => abbrevWord(matched));- return text.replace(/\w{4,}/g, s => {
- l = s.length;
- return `${s[0]}${l - 2}${s[l - 1]}`;
- });
- };
const Test = require("@codewars/test-compat"); describe("Solution", function() { it("should abbreviate single words", function() { Test.assertEquals(abbrev("internationalization"), "i18n"); Test.assertEquals(abbrev("Localization"), "L10n"); }); it("should abbreviate multiple words", function() { Test.assertEquals(abbrev("I Love India"), "I L2e I3a"); }); it("should abbreviate hyphenated words", function() { Test.assertEquals(abbrev("Deepak-Vishwa"), "D4k-V4a"); }); it("should not abbreviate single letters", function() { Test.assertEquals(abbrev("A"), "A"); }); it("should not abbreviate 2 letter words", function() { Test.assertEquals(abbrev("If"), "If"); }); it("should not abbreviate 3 letter words", function() { Test.assertEquals(abbrev("The"), "The"); }); it("should only abbreviate mutiple words of 4 letters or more", function() { Test.assertEquals(abbrev("The quick brown fox jumps over the lazy dog"), "The q3k b3n fox j3s o2r the l2y dog"); }); });
- const Test = require("@codewars/test-compat");
- describe("Solution", function() {
it("should test for something", function() {- it("should abbreviate single words", function() {
- Test.assertEquals(abbrev("internationalization"), "i18n");
- Test.assertEquals(abbrev("Localization"), "L10n");
Test.assertEquals(abbrev("Deepak-Vishwa"), "D4k-V4a");Test.assertEquals(abbrev("I Love India"), "I L2e I3a");- });
- it("should abbreviate multiple words", function() {
- Test.assertEquals(abbrev("I Love India"), "I L2e I3a");
- });
- it("should abbreviate hyphenated words", function() {
- Test.assertEquals(abbrev("Deepak-Vishwa"), "D4k-V4a");
- });
- it("should not abbreviate single letters", function() {
- Test.assertEquals(abbrev("A"), "A");
- });
- it("should not abbreviate 2 letter words", function() {
- Test.assertEquals(abbrev("If"), "If");
- });
- it("should not abbreviate 3 letter words", function() {
- Test.assertEquals(abbrev("The"), "The");
- });
- it("should only abbreviate mutiple words of 4 letters or more", function() {
- Test.assertEquals(abbrev("The quick brown fox jumps over the lazy dog"), "The q3k b3n fox j3s o2r the l2y dog");
- });
- });