Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

initials

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

initials - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

2

bower.json
{
"name": "initials",
"version": "0.1.0",
"version": "0.2.0",
"authors": [

@@ -5,0 +5,0 @@ "Gregor Martynus <gregor@martynus.net>"

@@ -25,2 +25,4 @@ /* global define */

// PUBLIC API METHODS
//

@@ -36,2 +38,22 @@ // initials alows to be used with either a string or an array of strings

//
// finds initials in a name and adds them right ot them
//
function addInitialsTo (nameOrNames, length) {
if (! nameOrNames) return '';
if (typeof nameOrNames === 'string') return addInitialsToSingleName(nameOrNames, length);
return addInitialsToMultipleNames(nameOrNames, length);
}
//
// extract name, initials, email
//
function parse (nameOrNames, options) {
if (! nameOrNames) return {};
if (typeof nameOrNames === 'string') return parseSingleName(nameOrNames, options);
return parseMultipleNames(nameOrNames, options);
}
// HELPER METHODS
//
// Find initials in a single given name string

@@ -60,3 +82,3 @@ //

if (name.length > length) {
return name.match(findFirstLettersPattern);
return name.match(findFirstLettersPattern).join('');
} else {

@@ -93,2 +115,150 @@ return name;

//
function initialsForMultipleNames (names, length) {
length = length || 2;
var optionsForNames = [];
var optionsCountForNames;
var map = {};
var duplicatesMap = {};
var initialsForNamesMap = {};
var options;
var initials;
// get all possible initials for all names for given length
names.forEach(function(name) {
if (initialsForNamesMap[name]) return;
initials = findPreferredInitials(name);
if (initials) {
map[initials] = 1;
initialsForNamesMap[name] = [initials];
return;
}
// return all possible initials for given length
options = getPossibleInitialsForName(name).filter( function(initials) {
if (initials.length !== length) return false;
if (map[initials]) duplicatesMap[initials] = 1;
map[initials] = 1;
return true;
});
initialsForNamesMap[name] = options;
});
// remove duplicates
for (var name in initialsForNamesMap) {
options = initialsForNamesMap[name];
optionsForNames.push(options);
for (var j = 0; j < options.length; j++) {
if (duplicatesMap[options[j]]) {
options.splice(j, 1);
}
}
}
// make sure we still have options for every name
optionsCountForNames = optionsForNames.map( function(options) { return options.length; });
if (Math.min.apply(null, optionsCountForNames) === 0) {
return initialsForMultipleNames(names, length + 1);
}
// if we do, return the first option for each
return names.map( function(name) { return initialsForNamesMap[name][0]; });
}
//
//
//
function addInitialsToSingleName (name, length) {
var parts = parseSingleName(name, length);
return format(parts);
}
//
//
//
function addInitialsToMultipleNames (names, length) {
return parseMultipleNames(names, length).map( format );
}
//
//
//
function parseSingleName (name, options) {
var initials;
var email;
var matches;
var parts = {};
// normalize options
if (! options) options = {};
if (typeof options === 'number') options = {length: options};
// are initials part of the name?
initials = findPreferredInitials(name);
if (initials) {
// if yes, remove it from name
name = name.replace(initials, '');
}
// use preferred initials if passed
if (options.initials) initials = options.initials;
// if no initials found yet, extract initials from name
if (!initials) initials = initialsForSingleName(name, options.length);
// is there an email in the name?
matches = name.match(findEmailPattern);
if (matches != null) email = matches.pop();
if (email) {
// if yes, remove it from name
name = name.replace(email, '');
}
// clean up the remainings
name = name.replace(findAllNonCharactersPattern, ' ').trim();
// do only return what's present
if (name) parts.name = name;
if (initials) parts.initials = initials;
if (email) parts.email = email;
return parts;
}
//
//
//
function parseMultipleNames (names) {
var initialsArray = initialsForMultipleNames(names, length);
return names.map(function(name, i) {
return parseSingleName(name, {
initials: initialsArray[i]
});
});
}
//
//
//
function format (parts) {
// neither name nor email: return initials
if (! parts.name && ! parts.email) return parts.initials;
// no email: return name with initials
if (! parts.email) return parts.name + ' (' + parts.initials + ')';
// no name: return email with initials
if (! parts.name) return parts.email + ' (' + parts.initials + ')';
// return name with initials & name
return parts.name + ' (' + parts.initials + ') <' + parts.email + '>';
}
//
//
//
function cleanupName (name) {

@@ -207,65 +377,2 @@ // in case the name is an email address, remove the @xx.yy part

//
// inspired by http://www.perlmonks.org/bare/?abspart=1;displaytype=displaycode;node_id=336125;part=6
//
function initialsForMultipleNames (names, length) {
length = length || 2;
var optionsForNames = [];
var optionsCountForNames;
var map = {};
var duplicatesMap = {};
var initialsForNamesMap = {};
var options;
var initials;
var namesLengths;
// make sure we still have options for every name
namesLengths = names.map( function(name) { return name.length; });
// get all possible initials for all names for given length
names.forEach(function(name) {
if (initialsForNamesMap[name]) return;
initials = findPreferredInitials(name);
if (initials) {
map[initials] = 1;
initialsForNamesMap[name] = [initials];
return;
}
// return all possible initials for given length
options = getPossibleInitialsForName(name).filter( function(initials) {
if (initials.length !== length) return false;
if (map[initials]) duplicatesMap[initials] = 1;
map[initials] = 1;
return true;
});
initialsForNamesMap[name] = options;
});
// remove duplicates
for (var name in initialsForNamesMap) {
options = initialsForNamesMap[name];
optionsForNames.push(options);
for (var j = 0; j < options.length; j++) {
if (duplicatesMap[options[j]]) {
options.splice(j, 1);
}
}
}
// make sure we still have options for every name
optionsCountForNames = optionsForNames.map( function(options) { return options.length; });
if (Math.min.apply(null, optionsCountForNames) === 0) {
return initialsForMultipleNames(names, length + 1);
}
// if we do, return the first option for each
return names.map( function(name) { return initialsForNamesMap[name][0]; });
}
// based on http://web.archive.org/web/20120918093154/http://lehelk.com/2011/05/06/script-to-remove-diacritics/

@@ -364,3 +471,9 @@ var diacriticsMap = [

}
// extend public API
initials.addTo = addInitialsTo;
initials.parse = parse;
initials.find = initials;
return initials;
});
{
"name": "initials",
"version": "0.1.0",
"version": "0.2.0",
"description": "initials for names",

@@ -5,0 +5,0 @@ "main": "initials.js",

@@ -1,3 +0,3 @@

Initials. Because JD is shorter than JD
=======================================
Initials. Because JD is shorter than John Doe
=============================================

@@ -4,0 +4,0 @@ > a JavaScript function to get initials from names

@@ -34,2 +34,36 @@ /* global initials, test, equal, deepEqual */

deepEqual( initials(['joe@example.com']), ['jo'], 'domains are ignored when a name is an email' );
deepEqual( initials(['joe@example.com']), ['jo'], 'domains are ignored when a name is an email' );
// https://github.com/gr2m/initials.js/issues/1
// deepEqual( initials(['j']), ['j'], 'j ☛ j');
});
test( 'initials.addTo( name )', function() {
'use strict';
equal( initials.addTo('John Doe'), 'John Doe (JD)', 'John Doe ☛ John Doe (JD)' );
equal( initials.addTo('(JJ) Jack Johnson'), 'Jack Johnson (JJ)', 'Jack Johnson ☛ Jack Johnson (JJ)' );
equal( initials.addTo('JD'), 'JD', 'JD ☛ JD' );
equal( initials.addTo('John Doe (JoDo) joe@example.com'), 'John Doe (JoDo) <joe@example.com>', 'John Doe (JoDo) joe@example.com ☛ John Doe (JoDo) <joe@example.com>' );
equal( initials.addTo('joe@example.com'), 'joe@example.com (jo)', 'joe@example.com ☛ joe@example.com (jo)' );
});
test( 'initials.addTo( namesArray )', function() {
'use strict';
deepEqual( initials.addTo(['John Doe', 'Robert Roe', 'Larry Loe']), ['John Doe (JD)', 'Robert Roe (RR)', 'Larry Loe (LL)'], 'John Doe, Robert Roe, Larry Loe ☛ John Doe (JD), Robert Roe (RR), Larry Loe (LL)' );
deepEqual( initials.addTo(['John Doe', 'Jane Dane']), ['John Doe (JDo)', 'Jane Dane (JDa)'], 'John Doe, Jane Dane ☛ John Doe (JDo), Jane Dane (JDa)' );
});
test( 'initials.parse( name )', function() {
'use strict';
deepEqual( initials.parse('John Doe'), {name: 'John Doe', initials: 'JD'}, 'John Doe ☛ name: John Doe, initials: JD' );
deepEqual( initials.parse('JD'), {initials: 'JD'}, 'JD ☛ initials: JD' );
deepEqual( initials.parse('joe@example.com'), {email: 'joe@example.com', initials: 'jo'}, 'joe@example.com ☛ email: joe@example.com, initials: jo' );
});
test( 'initials.parse( namesArray )', function() {
'use strict';
deepEqual( initials.parse(['John Doe', 'Robert Roe', 'Larry Loe']), [{name: 'John Doe', initials: 'JD'},{name: 'Robert Roe', initials: 'RR'},{name: 'Larry Loe', initials: 'LL'}], 'John Doe, Robert Roe, Larry Loe ☛ name: John Doe, initials: JD; name: Robert Roe, initials: RR; name: Larry Loe, initials: LL' );
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc