Socket
Socket
Sign inDemoInstall

addressparser

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

addressparser - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

106

index.js

@@ -0,8 +1,16 @@

"use strict";
/**
* Defines an object as a namespace for the parsing function
*/
var addressparser = {};
// expose to the world
module.exports = parser;
module.exports = function(address){
return addressparser.parse(address);
};
/**
* Parses structured e-mail addresses from an address field
*
*
* Example:

@@ -19,7 +27,6 @@ *

*/
function parser(str){
var tokenizer = new Tokenizer(str),
addressparser.parse = function(str){
var tokenizer = new addressparser.Tokenizer(str),
tokens = tokenizer.tokenize();
var addresses = [],

@@ -31,3 +38,5 @@ address = [],

if(token.type == "operator" && (token.value =="," || token.value ==";")){
addresses.push(address);
if(address.length){
addresses.push(address);
}
address = [];

@@ -44,3 +53,3 @@ }else{

addresses.forEach(function(address){
address = handleAddress(address);
address = addressparser._handleAddress(address);
if(address.length){

@@ -52,3 +61,3 @@ parsedAddresses = parsedAddresses.concat(address);

return parsedAddresses;
}
};

@@ -61,3 +70,3 @@ /**

*/
function handleAddress(tokens){
addressparser._handleAddress = function(tokens){
var token,

@@ -69,6 +78,6 @@ isGroup = false,

data = {
address: [],
comment: [],
group: [],
text: []
address: [],
comment: [],
group: [],
text: []
},

@@ -80,3 +89,3 @@ i, len;

token = tokens[i];
if(token.type == "operator"){

@@ -110,13 +119,9 @@ switch(token.value){

if(data.group.length){
if(data.text.length){
data.text = data.text.join(" ");
}
addresses = addresses.concat(parser(data.group.join(",")).map(function(address){
address.name = data.text || address.name;
return address;
}));
if(isGroup){
// http://tools.ietf.org/html/rfc2822#appendix-A.1.3
data.text = data.text.join(" ");
addresses.push({
name: data.text || address.name,
group: data.group.length ? addressparser.parse(data.group.join(",")) : []
});
}else{

@@ -132,17 +137,19 @@ // If no address was found, try to detect one from regular text

var _regexHandler = function(address){
if(!data.address.length){
data.address = [address.trim()];
return " ";
}else{
return address;
}
};
// still no address
if(!data.address.length){
for(i = data.text.length - 1; i>=0; i--){
data.text[i] = data.text[i].replace(/\s*\b[^@\s]+@[^@\s]+\b\s*/, function(address){
if(!data.address.length){
data.address = [address.trim()];
return " ";
}else{
return address;
}
}).trim();
data.text[i] = data.text[i].replace(/\s*\b[^@\s]+@[^@\s]+\b\s*/, _regexHandler).trim();
if(data.address.length){
break;
}
}
}
}

@@ -155,3 +162,3 @@ }

data.comment = [];
}
}

@@ -171,8 +178,8 @@ // Keep only the first address occurence, push others to regular text

address = {
address: data.address || data.text || "",
name: data.text || data.address || ""
address: data.address || data.text || "",
name: data.text || data.address || ""
};
if(address.address == address.name){
if((address.address || "").match(/@/)){
if((address.address || "").match(/@/)){
address.name = "";

@@ -182,3 +189,3 @@ }else{

}
}

@@ -191,7 +198,6 @@

return addresses;
}
};
/**
* Creates a TOkenizer object for tokenizing address field strings
* Creates a Tokenizer object for tokenizing address field strings
*

@@ -201,5 +207,5 @@ * @constructor

*/
function Tokenizer(str){
addressparser.Tokenizer = function(str){
this.str = (str || "").toString();
this.str = (str || "").toString();
this.operatorCurrent = "";

@@ -212,3 +218,3 @@ this.operatorExpecting = "";

}
};

@@ -218,3 +224,3 @@ /**

*/
Tokenizer.prototype.operators = {
addressparser.Tokenizer.prototype.operators = {
"\"": "\"",

@@ -232,3 +238,3 @@ "(": ")",

*/
Tokenizer.prototype.tokenize = function(){
addressparser.Tokenizer.prototype.tokenize = function(){
var chr, list = [];

@@ -255,4 +261,4 @@ for(var i=0, len = this.str.length; i<len; i++){

*/
Tokenizer.prototype.checkChar = function(chr){
if((chr in this.operators || chr == "\\") && this.escaped){
addressparser.Tokenizer.prototype.checkChar = function(chr){
if((chr in this.operators || chr == "\\") && this.escaped){
this.escaped = false;

@@ -300,2 +306,2 @@ }else if(this.operatorExpecting && chr == this.operatorExpecting){

this.escaped = false;
};
};
{
"name": "addressparser",
"version": "0.1.3",
"version": "0.2.0",
"description": "Parse e-mail addresses",

@@ -9,3 +9,6 @@ "main": "index.js",

},
"repository": "",
"repository": {
"type": "git",
"url": "https://github.com/andris9/addressparser.git"
},
"author": "Andris Reinman",

@@ -12,0 +15,0 @@ "license": "MIT",

@@ -22,12 +22,25 @@ # addressparser

Even complex address fields are supported
And when using groups
addressparser('"Bach, Sebastian" <sebu@example.com>, mozart@example.com (Mozzie)');
// [{name: "Bach, Sebastian", address: "sebu@example.com"},
// {name: "Mozzie", address: "mozart@example.com"}]
addressparser('Composers:"Bach, Sebastian" <sebu@example.com>, mozart@example.com (Mozzie);');
addressparser("Music Group: sebu@example.com, mozart@example.com;");
// [{name: "Music Group", address: "sebu@example.com"},
// {name: "Music Group", address: "mozart@example.com"}]
the result is
[
{
name: "Composers",
group: [
{
address: "sebu@example.com",
name: "Bach, Sebastian"
},
{
address: "mozart@example.com",
name: "Mozzie"
}
]
}
]
## Notes

@@ -34,0 +47,0 @@

@@ -37,3 +37,4 @@ var testCase = require('nodeunit').testCase,

var input = "Undisclosed:;",
expected = [];
expected = [{name: 'Undisclosed', group: []}];
test.deepEqual(addressparser(input), expected);

@@ -44,6 +45,12 @@ test.done();

var input = "Disclosed:andris@tr.ee, andris@example.com;",
expected = [{name: "Disclosed", address:"andris@tr.ee"}, {name: "Disclosed", address:"andris@example.com"}];
expected = [{name: 'Disclosed', group:[{address: 'andris@tr.ee', name: ''},{address: 'andris@example.com', name: ''}]}];
test.deepEqual(addressparser(input), expected);
test.done();
},
"Mixed group": function(test){
var input = "Test User <test.user@mail.ee>, Disclosed:andris@tr.ee, andris@example.com;,,,, Undisclosed:;",
expected = [{"address":"test.user@mail.ee","name":"Test User"}, {"name":"Disclosed","group":[{"address":"andris@tr.ee","name":""}, {"address":"andris@example.com","name":""}]}, {"name":"Undisclosed","group":[]}];
test.deepEqual(addressparser(input), expected);
test.done();
},
"Name from comment": function(test){

@@ -50,0 +57,0 @@ var input = "andris@tr.ee (andris)",

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