emailjs-imap-handler
Advanced tools
Comparing version 2.0.1 to 2.1.0
{ | ||
"name": "emailjs-imap-handler", | ||
"main": "src/emailjs-imap-handler.js", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"homepage": "https://github.com/emailjs/emailjs-imap-handler", | ||
@@ -6,0 +6,0 @@ "author": "Andris Reinman <andris@kreata.ee>", |
@@ -15,3 +15,3 @@ # IMAP Handler | ||
imapHandler.parser(imapCommand); | ||
imapHandler.parser(imapCommand[, options]); | ||
@@ -21,3 +21,8 @@ Where | ||
* **imapCommand** is an Uint8Array without the final line break | ||
* **options** (optional) contains options that affect the returned value | ||
Where available options are | ||
* **valueAsString** LITERAL and STRING values are returned as strings rather than Uint8Array objects. Defaults to true. | ||
The function returns an object in the following form: | ||
@@ -24,0 +29,0 @@ |
@@ -41,18 +41,11 @@ // Copyright (c) 2013 Andris Reinman | ||
function fromCharCode(uint8Array, skip) { | ||
skip = skip || []; | ||
var max = 10240; | ||
var begin = 0; | ||
function fromCharCode(uint8Array) { | ||
var batchSize = 10240; | ||
var strings = []; | ||
for (var i = 0; i < uint8Array.length; i++) { | ||
if (skip.indexOf(i) >= 0) { | ||
strings.push(String.fromCharCode.apply(null, uint8Array.subarray(begin, i))); | ||
begin = i + 1; | ||
} else if (i - begin >= max) { | ||
strings.push(String.fromCharCode.apply(null, uint8Array.subarray(begin, i))); | ||
begin = i; | ||
} | ||
for (var i = 0; i < uint8Array.length; i += batchSize) { | ||
var begin = i; | ||
var end = Math.min(i + batchSize, uint8Array.length); | ||
strings.push(String.fromCharCode.apply(null, uint8Array.subarray(begin, end))); | ||
} | ||
strings.push(String.fromCharCode.apply(null, uint8Array.subarray(begin, i))); | ||
@@ -62,3 +55,3 @@ return strings.join(''); | ||
function fromCharCodeTrimmed(uint8Array, skip) { | ||
function fromCharCodeTrimmed(uint8Array) { | ||
var begin = 0; | ||
@@ -79,3 +72,3 @@ var end = uint8Array.length; | ||
return fromCharCode(uint8Array, skip); | ||
return fromCharCode(uint8Array); | ||
} | ||
@@ -200,3 +193,3 @@ | ||
Node.prototype.getValue = function() { | ||
var value = fromCharCode(this.uint8Array.subarray(this.valueStart, this.valueEnd), this.valueSkip); | ||
var value = fromCharCode(this.getValueArray()); | ||
return this.valueToUpperCase ? value.toUpperCase() : value; | ||
@@ -209,2 +202,28 @@ }; | ||
Node.prototype.getValueArray = function() { | ||
var valueArray = this.uint8Array.subarray(this.valueStart, this.valueEnd); | ||
if (this.valueSkip.length === 0) { | ||
return valueArray; | ||
} | ||
var filteredArray = new Uint8Array(valueArray.length - this.valueSkip.length); | ||
var begin = 0; | ||
var offset = 0; | ||
var skip = this.valueSkip.slice(); | ||
skip.push(valueArray.length); | ||
skip.forEach(function(end) { | ||
if (end > begin) { | ||
var subArray = valueArray.subarray(begin, end); | ||
filteredArray.set(subArray, offset); | ||
offset += subArray.length; | ||
} | ||
begin = end + 1; | ||
}); | ||
return filteredArray; | ||
}; | ||
Node.prototype.equals = function(value, caseSensitive) { | ||
@@ -320,2 +339,6 @@ if (this.getValueLength() !== value.length) { | ||
if (this.options.valueAsString === undefined) { | ||
this.options.valueAsString = true; | ||
} | ||
this.processString(); | ||
@@ -345,2 +368,8 @@ } | ||
case 'STRING': | ||
elm = { | ||
type: node.type.toUpperCase(), | ||
value: this.options.valueAsString ? node.getValue() : node.getValueArray() | ||
}; | ||
branch.push(elm); | ||
break; | ||
case 'SEQUENCE': | ||
@@ -347,0 +376,0 @@ elm = { |
@@ -403,2 +403,15 @@ (function(root, factory) { | ||
it('should return literal as Uint8Array', function() { | ||
var options = { valueAsString: false }; | ||
var results = imapHandler.parser(toUint8ArrayList('TAG1 CMD {4}\r\nabcd'), options); | ||
var literal = results.attributes[0]; | ||
expect(literal.type).to.equal('LITERAL'); | ||
expect(literal.value instanceof Uint8Array).to.equal(true); | ||
expect(literal.value.length).to.equal(4); | ||
expect(literal.value[0]).to.equal(97); | ||
expect(literal.value[1]).to.equal(98); | ||
expect(literal.value[2]).to.equal(99); | ||
expect(literal.value[3]).to.equal(100); | ||
}); | ||
}); | ||
@@ -405,0 +418,0 @@ |
211670
4285
170
14