JSFiddle - React, Tailwind, and code Playground
JavaScript
const string_count = (str => {
const countCharacters = (_ => {
const regexLookup = ['A', 'z']
.reduce((gathered, item) => {
if (gathered === false) {
return item;
} else if (gathered === 'A') {
const range = {' ': new RegExp(' ', 'g')};
for (let s = gathered.charCodeAt(0), e = item.charCodeAt(0); s < e; ++s) {
try {
range[String.fromCharCode(s)] = new RegExp(String.fromCharCode(s), 'g');
} catch(e) {
range[String.fromCharCode(s)] = RegExp('\\' + String.fromCharCode(s), 'g');
}
}
return range;
}
}, false);
return (str, char) => str.match(regexLookup[char]).length;
})();
return str => str
.split('')
.reduce((gathered, char) => {
gathered[char] = countCharacters(str, char);
return gathered;
}, {});
})();
console.log(string_count('Hello World'));
console.log(string_count('ABCabcABCabcxyz'));