Comparing version 1.0.3 to 1.0.4
342
initials.js
@@ -5,16 +5,17 @@ /* global define */ | ||
(function (root, factory) { | ||
/* istanbul ignore next */ | ||
if (typeof define === 'function' && define.amd) { | ||
define([], function () { | ||
root.initials = factory(); | ||
return root.initials; | ||
}); | ||
root.initials = factory() | ||
return root.initials | ||
}) | ||
} else if (typeof exports === 'object') { | ||
module.exports = factory(); | ||
module.exports = factory() | ||
} else { | ||
root.initials = factory(); | ||
root.initials = factory() | ||
} | ||
})(this, function(){ // jshint ignore:line | ||
})(this, function () { | ||
// defaults | ||
var defaultLength = 2; | ||
var defaultLength = 2 | ||
@@ -24,9 +25,9 @@ // there is no support for look-behinds in JS, and the \b selector | ||
// "non letters", that we use later to build our regex. | ||
var nonLetters = ' -\\/:-@\\[-`\\{-\\~'; | ||
var nonLetters = ' -\\/:-@\\[-`\\{-\\~' | ||
// regex patterns | ||
var uppercaseLettersOnlyPattern = /^[A-Z]+$/; | ||
var initialsInNamePattern = /\(([^\)]+)\)/; | ||
var nameIsEmailPattern = /^[^\s]+@[^\s]+$/; | ||
var findDomainInEmailPattern = /@[^\s]+/; | ||
var findEmailPattern = /[\w\._-]+@[\w\.-]+[\w]/g; | ||
var uppercaseLettersOnlyPattern = /^[A-Z]+$/ | ||
var initialsInNamePattern = /\(([^\)]+)\)/ | ||
var nameIsEmailPattern = /^[^\s]+@[^\s]+$/ | ||
var findDomainInEmailPattern = /@[^\s]+/ | ||
var findEmailPattern = /[\w\._-]+@[\w\.-]+[\w]/g | ||
@@ -36,4 +37,4 @@ // match everything that is a "non letter" (see above) | ||
// Expl: "Jörg Jäger-Franke" => ["J", " J", "-F"] | ||
var findFirstLettersOfWordsPattern = new RegExp('(^|[' + nonLetters + '])[^' + nonLetters + ']', 'g'); | ||
var findAllNonCharactersPattern = new RegExp('['+nonLetters+']', 'g'); | ||
var findFirstLettersOfWordsPattern = new RegExp('(^|[' + nonLetters + '])[^' + nonLetters + ']', 'g') | ||
var findAllNonCharactersPattern = new RegExp('[' + nonLetters + ']', 'g') | ||
@@ -45,6 +46,6 @@ // PUBLIC API METHODS | ||
// | ||
function initials(nameOrNames, options) { | ||
if (! nameOrNames) return ''; | ||
if (typeof nameOrNames === 'string') return initialsForSingleName(nameOrNames, normalize(options)); | ||
return initialsForMultipleNames(nameOrNames, normalize(options)); | ||
function initials (nameOrNames, options) { | ||
if (!nameOrNames) return '' | ||
if (typeof nameOrNames === 'string') return initialsForSingleName(nameOrNames, normalize(options)) | ||
return initialsForMultipleNames(nameOrNames, normalize(options)) | ||
} | ||
@@ -56,5 +57,5 @@ | ||
function addInitialsTo (nameOrNames, options) { | ||
if (! nameOrNames) return ''; | ||
if (typeof nameOrNames === 'string') return addInitialsToSingleName(nameOrNames, normalize(options)); | ||
return addInitialsToMultipleNames(nameOrNames, normalize(options)); | ||
if (!nameOrNames) return '' | ||
if (typeof nameOrNames === 'string') return addInitialsToSingleName(nameOrNames, normalize(options)) | ||
return addInitialsToMultipleNames(nameOrNames, normalize(options)) | ||
} | ||
@@ -66,5 +67,5 @@ | ||
function parse (nameOrNames, options) { | ||
if (! nameOrNames) return {}; | ||
if (typeof nameOrNames === 'string') return parseSingleName(nameOrNames, normalize(options)); | ||
return parseMultipleNames(nameOrNames, normalize(options)); | ||
if (!nameOrNames) return {} | ||
if (typeof nameOrNames === 'string') return parseSingleName(nameOrNames, normalize(options)) | ||
return parseMultipleNames(nameOrNames, normalize(options)) | ||
} | ||
@@ -77,13 +78,13 @@ | ||
// | ||
function initialsForSingleName(name, options) { | ||
var matches; | ||
var result; | ||
var initials; | ||
var length = options.length || 2; | ||
function initialsForSingleName (name, options) { | ||
var matches | ||
var result | ||
var initials | ||
var length = options.length || 2 | ||
initials = findPreferredInitials(name, options); | ||
if (initials) return initials; | ||
initials = findPreferredInitials(name, options) | ||
if (initials) return initials | ||
name = cleanupName(name); | ||
if (! name) return ''; | ||
name = cleanupName(name) | ||
if (!name) return '' | ||
@@ -96,17 +97,17 @@ // there is no support for look-behinds in JS, and the \b selector | ||
matches = name.match(findFirstLettersOfWordsPattern).map(function (match) { | ||
return match[match.length - 1]; | ||
}); | ||
return match[match.length - 1] | ||
}) | ||
if (matches.length < 2) { | ||
if (name.length > length) { | ||
return name.substr(0, length); | ||
return name.substr(0, length) | ||
} else { | ||
return name; | ||
return name | ||
} | ||
} else { | ||
result = matches.join(''); | ||
result = matches.join('') | ||
} | ||
if (result.length >= length) { | ||
return result; | ||
return result | ||
} | ||
@@ -119,12 +120,12 @@ | ||
// First, we calculate all remaining options that we have | ||
var possibleInitials = getPossibleInitialsForName(name); | ||
var option; | ||
var possibleInitials = getPossibleInitialsForName(name) | ||
var option | ||
// then we return the first option that has the required length | ||
while (option = possibleInitials.shift()) { // jshint ignore:line | ||
if (option.length >= length) return option; | ||
} | ||
for (var i = 0; i < possibleInitials.length; i++) { | ||
if (possibleInitials[i].length >= length) return possibleInitials[i] | ||
}; | ||
// if that didn't work, we return the last possible option | ||
return option; | ||
return option | ||
} | ||
@@ -136,52 +137,52 @@ | ||
function initialsForMultipleNames (names, options) { | ||
var optionsForNames = []; | ||
var optionsCountForNames; | ||
var map = {}; | ||
var duplicatesMap = {}; | ||
var initialsForNamesMap = {}; | ||
var initials; | ||
var possibleInitials; | ||
var length = options.length || 2; | ||
var optionsForNames = [] | ||
var optionsCountForNames | ||
var map = {} | ||
var duplicatesMap = {} | ||
var initialsForNamesMap = {} | ||
var initials | ||
var possibleInitials | ||
var length = options.length || 2 | ||
// get all possible initials for all names for given length | ||
names.forEach(function(name) { | ||
names.forEach(function (name) { | ||
// normalize | ||
if (! name) name = ''; | ||
if (!name) name = '' | ||
// known name? Gets same initials, stop here | ||
if (initialsForNamesMap[name]) return; | ||
if (initialsForNamesMap[name]) return | ||
// too short to extract initials from? Use name as initials. | ||
if (name.length < length) { | ||
initialsForNamesMap[name] = [name]; | ||
return; | ||
initialsForNamesMap[name] = [name] | ||
return | ||
} | ||
// preferred initials like (JD)? Use these | ||
initials = findPreferredInitials(name, options); | ||
initials = findPreferredInitials(name, options) | ||
if (initials) { | ||
map[initials] = 1; | ||
initialsForNamesMap[name] = [initials]; | ||
return; | ||
map[initials] = 1 | ||
initialsForNamesMap[name] = [initials] | ||
return | ||
} | ||
// return all possible initials for given length | ||
possibleInitials = getPossibleInitialsForName(name).filter( function(initials) { | ||
if (initials.length !== length) return false; | ||
if (map[initials]) duplicatesMap[initials] = 1; | ||
map[initials] = 1; | ||
return true; | ||
}); | ||
possibleInitials = getPossibleInitialsForName(name).filter(function (initials) { | ||
if (initials.length !== length) return false | ||
if (map[initials]) duplicatesMap[initials] = 1 | ||
map[initials] = 1 | ||
return true | ||
}) | ||
initialsForNamesMap[name] = possibleInitials; | ||
}); | ||
initialsForNamesMap[name] = possibleInitials | ||
}) | ||
// remove duplicates | ||
for (var name in initialsForNamesMap) { | ||
possibleInitials = initialsForNamesMap[name]; | ||
optionsForNames.push(possibleInitials); | ||
possibleInitials = initialsForNamesMap[name] | ||
optionsForNames.push(possibleInitials) | ||
for (var i = 0; i < possibleInitials.length; i++) { | ||
if (duplicatesMap[possibleInitials[i]]) { | ||
possibleInitials.splice(i, 1); | ||
possibleInitials.splice(i, 1) | ||
} | ||
@@ -192,14 +193,14 @@ } | ||
// make sure we still have options for every name | ||
optionsCountForNames = optionsForNames.map( function(options) { return options.length; }); | ||
optionsCountForNames = optionsForNames.map(function (options) { return options.length }) | ||
// if names were empty, optionsCountForNames is empty. In that case stop here | ||
if (optionsCountForNames.length === 0) return names; | ||
if (optionsCountForNames.length === 0) return names | ||
if (Math.min.apply(null, optionsCountForNames) === 0) { | ||
options.length++; | ||
return initialsForMultipleNames(names, options); | ||
options.length++ | ||
return initialsForMultipleNames(names, options) | ||
} | ||
// if we do, return the first option for each | ||
return names.map( function(name) { return initialsForNamesMap[name][0]; }); | ||
return names.map(function (name) { return initialsForNamesMap[name][0] }) | ||
} | ||
@@ -211,4 +212,4 @@ | ||
function addInitialsToSingleName (name, options) { | ||
var parts = parseSingleName(name, options); | ||
return format(parts); | ||
var parts = parseSingleName(name, options) | ||
return format(parts) | ||
} | ||
@@ -220,3 +221,3 @@ | ||
function addInitialsToMultipleNames (names, options) { | ||
return parseMultipleNames(names, options).map( format ); | ||
return parseMultipleNames(names, options).map(format) | ||
} | ||
@@ -228,40 +229,53 @@ | ||
function parseSingleName (name, options) { | ||
var initials; | ||
var email; | ||
var matches; | ||
var parts = {}; | ||
var initials | ||
var email | ||
var matches | ||
var parts = {} | ||
if (! name) return {}; | ||
if (!name) return {} | ||
// are initials part of the name? | ||
initials = findPreferredInitials(name, options); | ||
initials = findPreferredInitials(name, options) | ||
if (initials) { | ||
// if yes, remove it from name | ||
name = name.replace(uppercaseLettersOnlyPattern, ''); | ||
name = name.replace(initialsInNamePattern, ''); | ||
name = name.replace(uppercaseLettersOnlyPattern, '') | ||
name = name.replace(initialsInNamePattern, '') | ||
} | ||
// use preferred initials if passed | ||
if (options.initials) initials = options.initials; | ||
if (options.initials) initials = options.initials | ||
// if no initials found yet, extract initials from name | ||
if (!initials) initials = initialsForSingleName(name, options); | ||
if (!initials) initials = initialsForSingleName(name, options) | ||
// is there an email in the name? | ||
matches = name.match(findEmailPattern); | ||
if (matches != null) email = matches.pop(); | ||
matches = name.match(findEmailPattern) | ||
if (matches != null) email = matches.pop() | ||
if (email) { | ||
// if yes, remove it from name | ||
name = name.replace(email, ''); | ||
name = name.replace(email, '') | ||
// if the email and the name are the same, initials can not be rendered | ||
// the initials method uses email for rendering just when the name is false | ||
// see https://github.com/gr2m/initials/issues/7 for more details | ||
if (name.trim() === '<' + email + '>') { | ||
// set the name to undefined | ||
name = '' | ||
// fire up the initials again with the email | ||
if (!initials) { | ||
initials = initialsForSingleName(email, options) | ||
} | ||
} | ||
} | ||
// clean up the rest | ||
name = name.replace(findAllNonCharactersPattern, ' ').trim(); | ||
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; | ||
if (name) parts.name = name | ||
if (initials) parts.initials = initials | ||
if (email) parts.email = email | ||
return parts; | ||
return parts | ||
} | ||
@@ -273,8 +287,8 @@ | ||
function parseMultipleNames (names, options) { | ||
var initialsArray = initialsForMultipleNames(names, options); | ||
var initialsArray = initialsForMultipleNames(names, options) | ||
return names.map(function(name, i) { | ||
options.existing[name] = initialsArray[i]; | ||
return parseSingleName(name, options); | ||
}); | ||
return names.map(function (name, i) { | ||
options.existing[name] = initialsArray[i] | ||
return parseSingleName(name, options) | ||
}) | ||
} | ||
@@ -288,12 +302,12 @@ | ||
// neither name nor email: return initials | ||
if (! parts.name && ! parts.email) return parts.initials; | ||
if (!parts.name && !parts.email) return parts.initials | ||
// no email: return name with initials | ||
if (! parts.email) return parts.name + ' (' + parts.initials + ')'; | ||
if (!parts.email) return parts.name + ' (' + parts.initials + ')' | ||
// no name: return email with initials | ||
if (! parts.name) return parts.email + ' (' + parts.initials + ')'; | ||
if (!parts.name) return parts.email + ' (' + parts.initials + ')' | ||
// return name with initials & name | ||
return parts.name + ' (' + parts.initials + ') <' + parts.email + '>'; | ||
return parts.name + ' (' + parts.initials + ') <' + parts.email + '>' | ||
} | ||
@@ -308,14 +322,13 @@ | ||
if (nameIsEmailPattern.test(name)) { | ||
name = name.replace(findDomainInEmailPattern, ''); | ||
name = name.replace(findDomainInEmailPattern, '') | ||
} else { | ||
name = name.replace(findEmailPattern, ''); | ||
name = name.replace(findEmailPattern, '') | ||
} | ||
// replace all non characters with ' ' & trim | ||
name = name.replace(findAllNonCharactersPattern, ' ').trim(); | ||
name = name.replace(findAllNonCharactersPattern, ' ').trim() | ||
return name; | ||
return name | ||
} | ||
// | ||
@@ -325,18 +338,18 @@ // | ||
function findPreferredInitials (name, options) { | ||
var matches; | ||
var matches | ||
// if preferred initials passed for current name | ||
if (options.existing[name]) return options.existing[name]; | ||
if (options.existing[name]) return options.existing[name] | ||
// if the name contains only upcase letters, let's take it as the initials as well | ||
if (uppercaseLettersOnlyPattern.test(name)) { | ||
return name; | ||
return name | ||
} | ||
// are the initials part of the given name, e.g. »Eddie Murphie (em)«? | ||
matches = name.match(initialsInNamePattern); | ||
matches = name.match(initialsInNamePattern) | ||
// if yes, return them | ||
if (matches != null) { | ||
return matches.pop(); | ||
return matches.pop() | ||
} | ||
@@ -353,13 +366,12 @@ } | ||
// | ||
var cache = {}; | ||
var cache = {} | ||
function getPossibleInitialsForName (name) { | ||
var parts; | ||
var partsPossibilities; | ||
var options = []; | ||
var currentParts; | ||
var parts | ||
var partsPossibilities | ||
var options = [] | ||
name = cleanupName(name); | ||
name = cleanupName(name) | ||
if (cache[name]) { | ||
return cache[name].slice(0); // return copy | ||
return cache[name].slice(0) // return copy | ||
} | ||
@@ -369,21 +381,20 @@ | ||
// 'John Doe' => ['Doe', 'John'] | ||
parts = name.split(' '); | ||
currentParts = parts; | ||
parts = name.split(' ') | ||
// map parts to all its possible initials | ||
// 'John' => ['J', 'Jo', 'Joh', 'John'] | ||
partsPossibilities = parts.map(getPossibleInitialsForWord); | ||
partsPossibilities = parts.map(getPossibleInitialsForWord) | ||
options = combineAll(partsPossibilities); | ||
options = combineAll(partsPossibilities) | ||
// sort options, shortest first | ||
options = options.sort(function(a, b) { | ||
return a.length - b.length || options.indexOf(a) - options.indexOf(b); | ||
}); | ||
options = options.sort(function (a, b) { | ||
return a.length - b.length || options.indexOf(a) - options.indexOf(b) | ||
}) | ||
// cache for future | ||
cache[name] = options; | ||
cache[name] = options | ||
// return options; | ||
return options.slice(0); | ||
// return options | ||
return options.slice(0) | ||
} | ||
@@ -394,19 +405,18 @@ | ||
// | ||
function combineAll(array) { | ||
var current = array.shift(); | ||
var temp; | ||
var results; | ||
if(array.length > 0) { | ||
results = []; | ||
temp = combineAll(array); | ||
current.forEach(function(value1) { | ||
temp.forEach(function(value2) { | ||
results.push(value1 + value2); | ||
}); | ||
}); | ||
return results; | ||
function combineAll (array) { | ||
var current = array.shift() | ||
var temp | ||
var results | ||
if (array.length > 0) { | ||
results = [] | ||
temp = combineAll(array) | ||
current.forEach(function (value1) { | ||
temp.forEach(function (value2) { | ||
results.push(value1 + value2) | ||
}) | ||
}) | ||
return results | ||
} else { | ||
return current | ||
} | ||
else { | ||
return current; | ||
} | ||
} | ||
@@ -418,8 +428,8 @@ | ||
function getPossibleInitialsForWord (word) { | ||
var options = []; | ||
var options = [] | ||
while (word.length) { | ||
options.unshift(word); | ||
word = word.substr(0, word.length - 1); | ||
options.unshift(word) | ||
word = word.substr(0, word.length - 1) | ||
} | ||
return options; | ||
return options | ||
} | ||
@@ -433,17 +443,17 @@ | ||
function normalize (options) { | ||
if (! options) options = {length: defaultLength}; | ||
if (typeof options === 'number') options = {length: options}; | ||
if (!options) options = {length: defaultLength} | ||
if (typeof options === 'number') options = {length: options} | ||
options.length = Math.max(options.length || 0, defaultLength); | ||
options.existing = options.existing || {}; | ||
options.length = Math.max(options.length || 0, defaultLength) | ||
options.existing = options.existing || {} | ||
return options; | ||
return options | ||
} | ||
// extend public API | ||
initials.addTo = addInitialsTo; | ||
initials.parse = parse; | ||
initials.find = initials; | ||
initials.addTo = addInitialsTo | ||
initials.parse = parse | ||
initials.find = initials | ||
return initials; | ||
}); | ||
return initials | ||
}) |
{ | ||
"name": "initials", | ||
"version": "1.0.3", | ||
"description": "initials for names", | ||
"main": "initials.js", | ||
"scripts": { | ||
"jshint": "jshint -c .jshintrc *.js test/*.js", | ||
"test": "npm run jshint && testmate test/*.js", | ||
"test:node": "testmate --client node test/*.js", | ||
"test:browser": "testmate --client selenium:firefox test/*.js", | ||
"prepublish": "semantic-release pre", | ||
"postpublish": "semantic-release post" | ||
"test": "standard initials.js test/initials-test.js && npm run -s test:node | tap-spec", | ||
"test:node": "node test/initials-test.js", | ||
"coverage": "istanbul cover test/initials-test.js && istanbul-coveralls ", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/gr2m/initials.git" | ||
"url": "https://github.com/gr2m/initials.git" | ||
}, | ||
@@ -25,6 +22,14 @@ "author": "Gregor Martynus <gregor@martynus.net>", | ||
"devDependencies": { | ||
"jshint": "^2.6.0", | ||
"semantic-release": "^3.0.2", | ||
"testmate": "^1.0.1" | ||
} | ||
"bootstrap-editable-table": "github:gr2m/bootstrap-editable-table#gh-pages", | ||
"istanbul": "^0.3.5", | ||
"istanbul-coveralls": "^1.0.1", | ||
"semantic-release": "^4.3.4", | ||
"standard": "^3.11.1", | ||
"tap-spec": "^3.0.0", | ||
"tape": "^4.2.0" | ||
}, | ||
"release": { | ||
"branch": "gh-pages" | ||
}, | ||
"version": "1.0.4" | ||
} |
@@ -93,3 +93,3 @@ Initials. Because JD is shorter than John Doe | ||
In order to run them in a browser or trough saucelabs, | ||
set `WURST_CLIENT` accordingly, for example | ||
set `TEST_CLIENT` accordingly, for example | ||
@@ -96,0 +96,0 @@ ``` |
@@ -1,202 +0,153 @@ | ||
/* global describe, it, expect */ | ||
'use strict'; | ||
var initials = require('../initials'); | ||
'use strict' | ||
var initials = require('../initials') | ||
var test = require('tape') | ||
describe( 'initials( name )', function() { | ||
it('John Doe ☛ JD', function() { | ||
expect( initials('John Doe') ).to.equal('JD'); | ||
}); | ||
it('joe doe ☛ jd', function() { | ||
expect( initials('john doe') ).to.equal('jd'); | ||
}); | ||
it('John Doe <joe@example.com> ☛ JD', function() { | ||
expect( initials('John Doe <joe@example.com>') ).to.equal('JD'); | ||
}); | ||
it('joe@example.com ☛ jo', function() { | ||
expect( initials('joe@example.com') ).to.equal('jo'); | ||
}); | ||
it('John Doe (dj) ☛ dj', function() { | ||
expect( initials('John Doe (dj)') ).to.equal('dj'); | ||
}); | ||
test('initials(name)', function (t) { | ||
t.plan(6) | ||
t.equal(initials('John Doe'), 'JD', 'John Doe ☛ JD') | ||
t.equal(initials('john doe'), 'jd', 'joe doe ☛ jd') | ||
t.equal(initials('John Doe <joe@example.com>'), 'JD', 'John Doe <joe@example.com> ☛ JD') | ||
t.equal(initials('joe@example.com'), 'jo', 'joe@example.com ☛ jo') | ||
t.equal(initials('John Doe (dj)'), 'dj', 'John Doe (dj) ☛ dj') | ||
// https://github.com/gr2m/initials/issues/6 | ||
it('안형준 -> 안형', function() { | ||
expect( initials('안형준') ).to.equal('안형'); | ||
}); | ||
}); | ||
t.equal(initials('안형준'), '안형', '안형준 -> 안형') | ||
}) | ||
describe( 'initials( name, 3 )', function() { | ||
it('John Doe ☛ JDo', function() { | ||
expect( initials('John Doe', 3) ).to.equal('JDo'); | ||
}); | ||
it('John D. ☛ JoD', function() { | ||
expect( initials('John D.', 3) ).to.equal('JoD'); | ||
}); | ||
}); | ||
test('initials(name, 3)', function (t) { | ||
t.plan(2) | ||
describe( 'initials( namesArray )', function() { | ||
it('John Doe, Robert Roe, Larry Loe ☛ JD, RR, LL', function() { | ||
expect( initials(['John Doe', 'Robert Roe', 'Larry Loe'])).to.deep.equal(['JD', 'RR', 'LL']); | ||
}); | ||
it('guarantees unique initials: John Doe, Jane Dane ☛ JDo, JDa', function() { | ||
expect( initials(['John Doe', 'Jane Dane'])).to.deep.equal(['JDo', 'JDa']); | ||
}); | ||
it('guarantees unique initials, respecting preferences: John Doe (JD), Jane Dane ☛ JDo, JDa', function() { | ||
expect( initials(['John Doe (JD)', 'Jane Dane'])).to.deep.equal(['JD', 'JDa']); | ||
}); | ||
it('same initials for same names: John Doe, Jane Dane, John Doe ☛ JDo, JDa, JDo', function() { | ||
expect( initials(['John Doe', 'Jane Dane', 'John Doe'])).to.deep.equal(['JDo', 'JDa', 'JDo']); | ||
}); | ||
it('shortest initials possible: John Smith, Jane Smith ☛ JoS, JaS', function() { | ||
expect( initials(['John Smith', 'Jane Smith'])).to.deep.equal(['JoS', 'JaS']); | ||
}); | ||
t.equal(initials('John Doe', 3), 'JDo', 'John Doe ☛ JDo') | ||
t.equal(initials('John D.', 3), 'JoD', 'John D. ☛ JoD') | ||
}) | ||
it('respects preferred initials: John Doe (JoDo), Jane Dane ☛ JoDo, JD', function() { | ||
expect( initials(['John Doe (JoDo)', 'Jane Dane'])).to.deep.equal(['JoDo', 'JD']); | ||
}); | ||
it('conflicting initials can be enforced: John Doe (JoDo), Jane Dane (JoDo) ☛ JoDo, JoDo', function() { | ||
expect( initials(['John Doe (JoDo)','Jane Dane (JoDo)'])).to.deep.equal( ['JoDo', 'JoDo']); | ||
}); | ||
it('preferred initials are respected in other names: John Doe (JD), Jane Dane ☛ JD, JDa', function() { | ||
expect( initials(['John Doe (JD)', 'Jane Dane'])).to.deep.equal( ['JD', 'JDa']); | ||
}); | ||
test('initials(namesArray)', function (t) { | ||
t.plan(11) | ||
it('emails are ignored in arrays', function() { | ||
expect( initials(['John Doe <joe@example.com>'])).to.deep.equal(['JD']); | ||
}); | ||
it('domains are ignored when a name is an email', function() { | ||
expect( initials(['joe@example.com'])).to.deep.equal(['jo']); | ||
}); | ||
t.deepEqual(initials(['John Doe', 'Robert Roe', 'Larry Loe']), ['JD', 'RR', 'LL'], 'John Doe, Robert Roe, Larry Loe ☛ JD, RR, LL') | ||
t.deepEqual(initials(['John Doe', 'Jane Dane']), ['JDo', 'JDa'], 'guarantees unique initials: John Doe, Jane Dane ☛ JDo, JDa') | ||
t.deepEqual(initials(['John Doe (JD)', 'Jane Dane']), ['JD', 'JDa'], 'guarantees unique initials, respecting preferences: John Doe (JD), Jane Dane ☛ JDo, JDa') | ||
t.deepEqual(initials(['John Doe', 'Jane Dane', 'John Doe']), ['JDo', 'JDa', 'JDo'], 'same initials for same names: John Doe, Jane Dane, John Doe ☛ JDo, JDa, JDo') | ||
t.deepEqual(initials(['John Smith', 'Jane Smith']), ['JoS', 'JaS'], 'shortest initials possible: John Smith, Jane Smith ☛ JoS, JaS') | ||
t.deepEqual(initials(['John Doe (JoDo)', 'Jane Dane']), ['JoDo', 'JD'], 'respects preferred initials: John Doe (JoDo), Jane Dane ☛ JoDo, JD') | ||
t.deepEqual(initials(['John Doe (JoDo)', 'Jane Dane (JoDo)']), ['JoDo', 'JoDo'], 'conflicting initials can be enforced: John Doe (JoDo), Jane Dane (JoDo) ☛ JoDo, JoDo') | ||
t.deepEqual(initials(['John Doe (JD)', 'Jane Dane']), ['JD', 'JDa'], 'preferred initials are respected in other names: John Doe (JD), Jane Dane ☛ JD, JDa') | ||
t.deepEqual(initials(['John Doe <joe@example.com>']), ['JD'], 'emails are ignored in arrays') | ||
t.deepEqual(initials(['joe@example.com']), ['jo'], 'domains are ignored when a name is an email') | ||
it('domains are ignored when a name is an email', function() { | ||
expect( initials(['joe@example.com'])).to.deep.equal(['jo']); | ||
}); | ||
// https://github.com/gr2m/initials/issues/1 | ||
it('j ☛ j', function() { | ||
expect( initials(['j'])).to.deep.equal(['j']); | ||
}); | ||
}); | ||
t.deepEqual(initials(['j']), ['j'], 'j ☛ j') | ||
}) | ||
describe( 'initials( nameOrNames, {existing: initialsForNames} )', function() { | ||
it('respect existing initials', function() { | ||
expect( initials('John Doe', { | ||
test('initials(nameOrNames, {existing: initialsForNames})', function (t) { | ||
t.plan(2) | ||
t.equal(initials('John Doe', { | ||
existing: { | ||
'John Doe': 'JoDo' | ||
} | ||
})).to.deep.equal('JoDo'); | ||
}); | ||
}), 'JoDo', 'respect existing initials' | ||
) | ||
it('respect existing initials', function() { | ||
expect( initials(['John Doe', 'Jane Dane'], { | ||
t.deepEqual(initials(['John Doe', 'Jane Dane'], { | ||
existing: { | ||
'John Doe': 'JD' | ||
} | ||
})).to.deep.equal(['JD', 'JDa']); | ||
}); | ||
}); | ||
}), ['JD', 'JDa'], 'respect existing initials') | ||
}) | ||
describe( 'initials.addTo( name )', function() { | ||
it('John Doe ☛ John Doe (JD)',function() { | ||
expect( initials.addTo('John Doe'), 'John Doe (JD)'); | ||
}); | ||
it('Jack Johnson ☛ Jack Johnson (JJ)',function() { | ||
expect( initials.addTo('(JJ) Jack Johnson'), 'Jack Johnson (JJ)'); | ||
}); | ||
it('JD ☛ JD',function() { | ||
expect( initials.addTo('JD'), 'JD'); | ||
}); | ||
it('JD (JD) ☛ JD (JD)',function() { | ||
expect( initials.addTo('JD (JD)'), 'JD (JD)'); | ||
}); | ||
it('John Doe (JoDo) joe@example.com ☛ John Doe (JoDo) <joe@example.com>',function() { | ||
expect( initials.addTo('John Doe (JoDo) joe@example.com'), 'John Doe (JoDo) <joe@example.com>'); | ||
}); | ||
it('joe@example.com ☛ joe@example.com (jo)',function() { | ||
expect( initials.addTo('joe@example.com'), 'joe@example.com (jo)'); | ||
}); | ||
it('joe (j) ☛ joe (j)',function() { | ||
expect( initials.addTo('joe (j)'), 'joe (j)'); | ||
}); | ||
it('Frönkää Üüd ☛ Frönkää Üüd (FÜ)',function() { | ||
expect( initials.addTo('Frönkää Üüd'), 'Frönkää Üüd (FÜ)'); | ||
}); | ||
it('funky (fu) ☛ funky (fu)',function() { | ||
expect( initials.addTo('funky (fu)'), 'funky (fu)'); | ||
}); | ||
}); | ||
describe( 'initials.addTo( namesArray )', function() { | ||
it('John Doe, Robert Roe, Larry Loe ☛ John Doe (JD), Robert Roe (RR), Larry Loe (LL)',function() { | ||
expect( initials.addTo(['John Doe', 'Robert Roe', 'Larry Loe']), ['John Doe (JD)', 'Robert Roe (RR)', 'Larry Loe (LL)']); | ||
}); | ||
it('John Doe, Jane Dane ☛ John Doe (JDo), Jane Dane (JDa)',function() { | ||
expect( initials.addTo(['John Doe', 'Jane Dane']), ['John Doe (JDo)', 'Jane Dane (JDa)']); | ||
}); | ||
}); | ||
test('initials.addTo(name)', function (t) { | ||
t.plan(10) | ||
describe( 'initials.addTo( nameOrNames, {existing: initialsForNames} )', function() { | ||
it('respect existing initials', function() { | ||
expect( initials.addTo('John Doe', { | ||
t.equal(initials.addTo('John Doe'), 'John Doe (JD)', 'John Doe ☛ John Doe (JD)') | ||
t.equal(initials.addTo('(JJ) Jack Johnson'), 'Jack Johnson (JJ)', 'Jack Johnson ☛ Jack Johnson (JJ)') | ||
t.equal(initials.addTo('JD'), 'JD', 'JD ☛ JD') | ||
t.equal(initials.addTo('JD (JD)'), 'JD (JD)', 'JD (JD) ☛ JD (JD)') | ||
t.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>') | ||
t.equal(initials.addTo('joe@example.com'), 'joe@example.com (jo)', 'joe@example.com ☛ joe@example.com (jo)') | ||
t.equal(initials.addTo('joe (j)'), 'joe (j)', 'joe (j) ☛ joe (j)') | ||
t.equal(initials.addTo('Frönkää Üüd'), 'Frönkää Üüd (FÜ)', 'Frönkää Üüd ☛ Frönkää Üüd (FÜ)') | ||
t.equal(initials.addTo('funky (fu)'), 'funky (fu)', 'funky (fu) ☛ funky (fu)') | ||
// https://github.com/gr2m/initials/issues/7 | ||
t.equal(initials.addTo('test.test@test.org <test.test@test.org>'), 'test.test@test.org (tt)', 'test.test@test.org <test.test@test.org> ☛ test.test@test.org (tt)') | ||
}) | ||
test('initials.addTo(namesArray)', function (t) { | ||
t.plan(2) | ||
t.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)') | ||
t.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.addTo(nameOrNames, {existing: initialsForNames})', function (t) { | ||
t.plan(2) | ||
t.equal( | ||
initials.addTo('John Doe', { | ||
existing: { | ||
'John Doe': 'JoDo' | ||
} | ||
})).to.equal('John Doe (JoDo)'); | ||
}); | ||
}), 'John Doe (JoDo)', 'respect existing initials') | ||
it('respect existing initials', function() { | ||
expect( initials.addTo(['John Doe', 'Jane Dane'], { | ||
t.deepEqual( | ||
initials.addTo(['John Doe', 'Jane Dane'], { | ||
existing: { | ||
'John Doe': 'JD' | ||
} | ||
})).to.deep.equal(['John Doe (JD)', 'Jane Dane (JDa)']); | ||
}); | ||
}); | ||
}), ['John Doe (JD)', 'Jane Dane (JDa)'], 'respect existing initials') | ||
}) | ||
describe( 'initials.parse( name )', function() { | ||
it('John Doe ☛ name: John Doe, initials: JD', function() { | ||
expect( initials.parse('John Doe') ) | ||
.to.deep.equal({name: 'John Doe', initials: 'JD'}); | ||
}); | ||
it('JD ☛ initials: JD', function() { | ||
expect( initials.parse('JD') ) | ||
.to.deep.equal({initials: 'JD'}); | ||
}); | ||
it('joe@example.com ☛ email: joe@example.com, initials: jo', function() { | ||
expect( initials.parse('joe@example.com') ) | ||
.to.deep.equal({email: 'joe@example.com', initials: 'jo'}); | ||
}); | ||
it('joe@example.com ☛ email: joe@example.com, initials: jo', function() { | ||
expect( initials.parse('John Doe <joe@example.com>') ) | ||
.to.deep.equal({name: 'John Doe', initials: 'JD', email: 'joe@example.com'}); | ||
}); | ||
}); | ||
test('initials.parse(name)', function (t) { | ||
t.plan(4) | ||
describe( 'initials.parse( namesArray )', function() { | ||
it('John Doe, Robert Roe, Larry Loe ☛ name: John Doe, initials: JD; name: Robert Roe, initials: RR; name: Larry Loe, initials: LL', function() { | ||
expect( initials.parse(['John Doe', 'Robert Roe', 'Larry Loe']) ) | ||
.to.deep.equal([{name: 'John Doe', initials: 'JD'},{name: 'Robert Roe', initials: 'RR'},{name: 'Larry Loe', initials: 'LL'}]); | ||
}); | ||
}); | ||
t.deepEqual(initials.parse('John Doe'), {name: 'John Doe', initials: 'JD'}, 'John Doe ☛ name: John Doe, initials: JD') | ||
t.deepEqual(initials.parse('JD'), {initials: 'JD'}, 'JD ☛ initials: JD') | ||
t.deepEqual(initials.parse('joe@example.com'), {email: 'joe@example.com', initials: 'jo'}, 'joe@example.com ☛ email: joe@example.com, initials: jo') | ||
t.deepEqual(initials.parse('John Doe <joe@example.com>'), {name: 'John Doe', initials: 'JD', email: 'joe@example.com'}, 'joe@example.com ☛ email: joe@example.com, initials: jo') | ||
}) | ||
describe( 'initials.parse( nameOrNames, {existing: initialsForNames} )', function() { | ||
it('respect existing initials for single name', function() { | ||
expect( initials.parse('John Doe', { | ||
test('initials.parse(namesArray)', function (t) { | ||
t.plan(1) | ||
t.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') | ||
}) | ||
test('initials.parse(nameOrNames, {existing: initialsForNames})', function (t) { | ||
t.plan(2) | ||
t.deepEqual(initials.parse('John Doe', { | ||
existing: { | ||
'John Doe': 'JoDo' | ||
} | ||
})).to.deep.equal({name: 'John Doe', initials: 'JoDo'}); | ||
}); | ||
}), {name: 'John Doe', initials: 'JoDo'}, 'respect existing initials for single name') | ||
it('respect existing initials for multiple names', function() { | ||
expect( initials.parse(['John Doe', 'Jane Dane'], { | ||
t.deepEqual(initials.parse(['John Doe', 'Jane Dane'], { | ||
existing: { | ||
'John Doe': 'JD' | ||
} | ||
})).to.deep.equal([{name: 'John Doe', initials: 'JD'}, {name: 'Jane Dane', initials: 'JDa'}]); | ||
}); | ||
}); | ||
}), [{name: 'John Doe', initials: 'JD'}, {name: 'Jane Dane', initials: 'JDa'}], 'respect existing initials for multiple names') | ||
}) | ||
// // describe('debug', function() { | ||
// // equal( initials('John Doe', 3), 'JDo', 'John Doe ☛ JDo' ); | ||
// // }); | ||
test('initials(), no params', function (t) { | ||
t.plan(4) | ||
t.equal(initials(), '', 'initials() without nameOrNames, no initials') | ||
t.equal(initials.addTo(), '', 'initials.addTo() without nameOrNames, no initials') | ||
t.deepEqual(initials.parse(), {}, 'initials.parse() without nameOrNames, no initials') | ||
t.deepEqual(initials(['', '']), ['', ''], 'initials with multiple persons but no names') | ||
}) | ||
test('initials(), name.length is less 3', function (t) { | ||
t.plan(2) | ||
t.equal(initials('K'), 'K', 'name.length is < 2, so the initials are equal to name') | ||
t.equal(initials('Mo'), 'Mo', 'name.length is < 3, so the initials are equal to name') | ||
}) | ||
// describe('debug', function() { | ||
// equal( initials('John Doe', 3), 'JDo', 'John Doe ☛ JDo' ); | ||
// }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
27055
7
8
481
1