I wrote myself a function to turn a string into an abbreviation and it's currently fairly long and it's case-sensitive.
I need a way shorten it so it works 100% of the time. Currently it screws up if one of the splitting words has a capital, if a word ends in a splitting word.
My splitting words are basically the words I'm removing (as most companies and such don't include them). They include:
Also, the way I'm removing them is using split and join (str.split('and ').join('')
) which to me doesn't seem like the easiest way.
Other than these issues, it works fine. Could anyone help me shrink the function and fix the issues? Thanks.
Function:
String.prototype.toAbbrev = function () {
var s = [];
var a = this.split('and ').join('').split('of ').join('').split('the').join('').split('for ').join('').split('to ').join('').split(' ');
for (var i = 1; i < a.length + 1; i++) {
s.push(a[i - 1].charAt(0).toUpperCase());
}
return s.join('.');
}
Outputs on Tested Companies
The National Aeronautics and Space Administration -> N.A.S.A The National Roads and Motorists' Association -> N.R.M.A Royal Society for the Prevention of Cruelty to Animals -> R.S.P.C.A
The arrow function is known for its ability to provide short definitions. Using the recipes presented above, you can shorten arrow functions by removing the params parentheses, curly braces or return keyword.
It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.
What It Is. This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be used similarly to the way you would use function expressions. In other words, you can often use them in place of expressions like function (foo) {...} .
Arrow function is one of the features introduced in the ES6 version of JavaScript. It allows you to create functions in a cleaner way compared to regular functions. For example, This function // function expression let x = function(x, y) { return x * y; }
I think an approach like this might work better:
var toAbbrev = function(str){
return str.replace(/\b(?:and|of|the|for|to)(?: |$)/gi,''). // remove all occurances of ignored words
split(' '). // split into words by spaces
map(function(x){
return x.charAt(0).toUpperCase(); // change each word into its first letter capitalized
}).
join('.'); // join with periods
};
and here's a breakdown of the regular expression:
/
\b // word boundary
(?:and|of|the|for|to) // non-capturing group. matches and/of/the/for/to
(?: |$) // non-capturing group. matches space or end of string
/gi // flags: g = global (match all), i = case-insensitive
And here's an alternative method that has a less complicated regular expression:
var toAbbrev = function(str){
return str.split(' '). // split into words
filter(function(x){
return !/^(?:and|of|the|for|to)$/i.test(x); // filter out excluded words
}).
map(function(x){
return x.charAt(0).toUpperCase(); // convert to first letter, captialized
}).
join('.'); // join with periods
};
And regex breakdown:
/
^ // start of string
(?:and|of|the|for|to) // non-capturing group. matches and/of/the/for/to
$ // end of string
/i // flags: i = case-insensitive
An even shorter one:
str.replace(/(and|of|the|for|to)( |$)/gi, "").replace(/(.).+?(\s|$)/g, "$1.");
To make sure it is capitalized you can do .toUpperCase
at the end.
(.) //selects the first character
.+ //matches the rest of the characters
? //? indicates a lazy match
(\s|$) //match a space or the end
$1. //means "the first selected match plus a dot"
Let's make it into one Regex!
str.replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
"Royal Society for the Prevention of Cruelty to Animals"
.replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
//R.S.P.C.A
"Josie and the Pussycats"
.replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
//J.P.
This should, in theory, cover all legit names. For names with preposition(s) at the end, you can technically do this:
.replace(/((and|of|the|for|to) )*(.).+?(\s|$)((and|of|the|for|to) ?)*/ig, "$3.")
But this is clearly longer than the one with two replace
s and this defeats its purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With