Comparing version 1.0.1 to 1.0.2
196
lib/utils.js
@@ -14,24 +14,24 @@ // Utility Functions | ||
function pad(string, width) { | ||
let padRight; | ||
let padChar; | ||
let result; | ||
string = string + ''; | ||
let padRight; | ||
let padChar; | ||
let result; | ||
string = string + ''; | ||
if(typeof arguments[2] == 'boolean') { | ||
padRight = arguments[2]; | ||
padChar = arguments[3] || ' '; | ||
} else if(typeof arguments[3] == 'boolean') { | ||
padRight = arguments[3]; | ||
padChar = arguments[2]; | ||
} else { | ||
padRight = true; // padRight is true be default | ||
padChar = arguments[2] || ' '; // The padding character is just a space by default | ||
} | ||
if (typeof arguments[2] == 'boolean') { | ||
padRight = arguments[2]; | ||
padChar = arguments[3] || ' '; | ||
} else if (typeof arguments[3] == 'boolean') { | ||
padRight = arguments[3]; | ||
padChar = arguments[2]; | ||
} else { | ||
padRight = true; // padRight is true be default | ||
padChar = arguments[2] || ' '; // The padding character is just a space by default | ||
} | ||
if(string.length >= width) { | ||
return string; | ||
} else { | ||
result = new Array(width - string.length + 1).join(padChar); | ||
return padRight ? string + result : result + string; | ||
} | ||
if (string.length >= width) { | ||
return string; | ||
} else { | ||
result = new Array(width - string.length + 1).join(padChar); | ||
return padRight ? string + result : result + string; | ||
} | ||
} | ||
@@ -41,5 +41,5 @@ | ||
function computeCheckDigit(routing) { | ||
let a = routing.split('').map(Number); | ||
return a.length !== 8 ? routing : routing + (7 * (a[0] + a[3] + a[6]) + 3 * (a[1] + a[4] + a[7]) + 9 * (a[2] + a[5])) % 10; | ||
let a = routing.split('').map(Number); | ||
return a.length !== 8 ? routing : routing + (7 * (a[0] + a[3] + a[6]) + 3 * (a[1] + a[4] + a[7]) + 9 * (a[2] + a[5])) % 10; | ||
} | ||
@@ -50,11 +50,13 @@ | ||
let string = field.number ? parseFloat(field.value).toFixed(2).replace(/\./, '') : field.value; | ||
if(!regex.test(string)) { | ||
throw new nACHError({ | ||
name: 'Invalid Data Type', | ||
message: field.name+'\'s data type is required to be '+field.type+', but its contents don\'t reflect that.' | ||
}); | ||
} | ||
let string = field.number ? parseFloat(field.value).toFixed(2).replace(/\./, '') : field.value; | ||
if ((field.value) || (field.required && !field.value)) { | ||
if (!regex.test(string)) { | ||
throw new nACHError({ | ||
name: 'Invalid Data Type', | ||
message: field.name + '\'s data type is required to be ' + field.type + ', but its contents don\'t reflect that.' | ||
}); | ||
} | ||
} | ||
return true; | ||
return true; | ||
} | ||
@@ -65,41 +67,41 @@ | ||
function generateString(object, cb) { | ||
let counter = 1; | ||
let result = ''; | ||
let objectCount; | ||
let counter = 1; | ||
let result = ''; | ||
let objectCount; | ||
// How does this actually work? It doens't seem like this is enough protection from iterating infinitely. | ||
objectCount = _.size(object); | ||
// How does this actually work? It doens't seem like this is enough protection from iterating infinitely. | ||
objectCount = _.size(object); | ||
while(counter < objectCount) { | ||
_.forEach(object, function(field) { | ||
if(field.position === counter) { | ||
if(field.blank === true || field.type === 'alphanumeric') { | ||
result = result + pad(field.value, field.width); | ||
} else { | ||
let string = field.number ? parseFloat(field.value).toFixed(2).replace(/\./, '') : field.value; | ||
let paddingChar = field.paddingChar || '0'; | ||
result = result + pad(string, field.width, false, paddingChar); | ||
} | ||
counter++; | ||
} | ||
}); | ||
} | ||
cb(result); | ||
while (counter < objectCount) { | ||
_.forEach(object, function (field) { | ||
if (field.position === counter) { | ||
if (field.blank === true || field.type === 'alphanumeric') { | ||
result = result + pad(field.value, field.width); | ||
} else { | ||
let string = field.number ? parseFloat(field.value).toFixed(2).replace(/\./, '') : field.value; | ||
let paddingChar = field.paddingChar || '0'; | ||
result = result + pad(string, field.width, false, paddingChar); | ||
} | ||
counter++; | ||
} | ||
}); | ||
} | ||
cb(result); | ||
} | ||
function unique() { | ||
return counter++; | ||
return counter++; | ||
} | ||
function overrideLowLevel(values, options, self) { | ||
// For each override value, check to see if it exists on the options object & if so, set it | ||
_.forEach(values, function(field) { | ||
if(options[field]) { | ||
self.set(field, options[field]); | ||
} | ||
}); | ||
// For each override value, check to see if it exists on the options object & if so, set it | ||
_.forEach(values, function (field) { | ||
if (options[field]) { | ||
self.set(field, options[field]); | ||
} | ||
}); | ||
} | ||
function getNextMultiple(value, multiple) { | ||
return value % multiple === 0 ? value : value + (multiple - value % multiple); | ||
function getNextMultiple(value, multiple) { | ||
return value % multiple === 0 ? value : value + (multiple - value % multiple); | ||
} | ||
@@ -112,54 +114,54 @@ | ||
// This allows us to create a valid ACH date in the YYMMDD format | ||
let formatDate = function(date) { | ||
let year = pad(date.getFullYear().toString().slice(-2), 2, false,'0'); | ||
let month = pad((date.getMonth() + 1).toString(), 2, false, '0'); | ||
let day = pad(date.getDate().toString(), 2, false, '0'); | ||
let formatDate = function (date) { | ||
let year = pad(date.getFullYear().toString().slice(-2), 2, false, '0'); | ||
let month = pad((date.getMonth() + 1).toString(), 2, false, '0'); | ||
let day = pad(date.getDate().toString(), 2, false, '0'); | ||
return year + month + day; | ||
return year + month + day; | ||
}; | ||
// Create a valid timestamp used by the ACH system in the HHMM format | ||
let formatTime = function(date) { | ||
let hour = date.getHours().toString(); | ||
let minute = date.getMinutes().toString(); | ||
let formatTime = function (date) { | ||
let hour = date.getHours().toString(); | ||
let minute = date.getMinutes().toString(); | ||
return pad(hour, 2 , false , '0') + pad(minute , 2 , false ,'0'); | ||
return pad(hour, 2, false, '0') + pad(minute, 2, false, '0'); | ||
}; | ||
let isBusinessDay = module.exports.isBusinessDay = function(day) { | ||
let d = moment(day).day(); | ||
return (d !== 0 && d !== 6); | ||
let isBusinessDay = module.exports.isBusinessDay = function (day) { | ||
let d = moment(day).day(); | ||
return (d !== 0 && d !== 6); | ||
}; | ||
// This function takes an optional starting date to iterate from based | ||
let computeBusinessDay = module.exports.computeBusinessDay = function(businessDays) { | ||
let day = arguments[1] ? moment(arguments[1]) : moment(); | ||
let counter = 0; | ||
function addDays() { | ||
day.add(1, 'days'); | ||
if(isBusinessDay(day)) { | ||
counter++; | ||
let computeBusinessDay = module.exports.computeBusinessDay = function (businessDays) { | ||
let day = arguments[1] ? moment(arguments[1]) : moment(); | ||
let counter = 0; | ||
function addDays() { | ||
day.add(1, 'days'); | ||
if (isBusinessDay(day)) { | ||
counter++; | ||
} | ||
return (counter === businessDays) ? day.toDate() : addDays(); | ||
} | ||
return (counter === businessDays) ? day.toDate() : addDays(); | ||
} | ||
return addDays(); | ||
return addDays(); | ||
}; | ||
let newLineChar = function() { | ||
return '\r\n'; | ||
let newLineChar = function () { | ||
return '\r\n'; | ||
}; | ||
module.exports.pad = pad; | ||
module.exports.unique = unique; | ||
module.exports.testRegex = testRegex; | ||
module.exports.formatDate = formatDate; | ||
module.exports.formatTime = formatTime; | ||
module.exports.newLineChar = newLineChar; | ||
module.exports.generateString = generateString; | ||
module.exports.getNextMultiple = getNextMultiple; | ||
module.exports.overrideLowLevel = overrideLowLevel; | ||
module.exports.computeCheckDigit = computeCheckDigit; | ||
module.exports.computeBusinessDay = computeBusinessDay; | ||
module.exports.pad = pad; | ||
module.exports.unique = unique; | ||
module.exports.testRegex = testRegex; | ||
module.exports.formatDate = formatDate; | ||
module.exports.formatTime = formatTime; | ||
module.exports.newLineChar = newLineChar; | ||
module.exports.generateString = generateString; | ||
module.exports.getNextMultiple = getNextMultiple; | ||
module.exports.overrideLowLevel = overrideLowLevel; | ||
module.exports.computeCheckDigit = computeCheckDigit; | ||
module.exports.computeBusinessDay = computeBusinessDay; | ||
module.exports.getNextMultipleDiff = getNextMultipleDiff; |
{ | ||
"name": "sp-nacha", | ||
"description": "NACHA file format is a set of instructions that, when uploaded into a bank portal, successfully initiates a batch of ACH payments. NACHA file format is the protocol for structuring those instructions in a way that successfully initiates the payments. NACHA is a highly customizable Node.js module exposing a high & low-level API for generating ACH files for use within the ACH network.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "Seerportal Nacha", |
@@ -58,6 +58,11 @@ let chai = require('chai') | ||
describe('Generate ACH File', async function () { | ||
it('should create an ACH file successfully', function () { | ||
GenerateAchFile([], './test/ach_file').then(result => { | ||
it('should create an ACH file successfully', function (done) { | ||
GenerateAchFile([], './test/ach_file').then((result) => { | ||
expect(result.error).to.equal(false); | ||
expect(result.message).to.equal('Successfully writing file.'); | ||
done(); | ||
}).catch(error => { | ||
expect(true).to.be.false; | ||
expect(error.message).to.equal('Successfully writing file.'); | ||
done(); | ||
}) | ||
@@ -64,0 +69,0 @@ }) |
@@ -29,4 +29,4 @@ const Moment = require('moment') | ||
"amount": "697", | ||
"idNumber": "501116885ASSA", | ||
"individualName": "TEYUNNA L SPEARS", | ||
"idNumber": "", | ||
"individualName": "Demo", | ||
"discretionaryData": "00", | ||
@@ -42,3 +42,3 @@ "transactionCode": "22", | ||
async function GenerateAchFile(queuedTransaction = [], fileFullPath = './') { | ||
return new Promise(resolve => { | ||
return new Promise((resolve, reject) => { | ||
let totalRunsFile = []; | ||
@@ -72,5 +72,5 @@ queuedTransaction = queuedTransaction.length > 0 ? queuedTransaction : data; | ||
batch.addEntry(entryRecord); | ||
successRecords.push({row_id: id, message: 'Success'}) | ||
successRecords.push({row_id: id, message: 'Success', error: false}) | ||
} catch (e) { | ||
return resolve({row_id: id, message: e.message}) | ||
return reject({row_id: id, message: e.message, error: true}) | ||
} | ||
@@ -81,3 +81,3 @@ }) | ||
} catch (e) { | ||
return resolve({row_id: id, message: e.message}) | ||
return reject({row_id: id, message: e.message, error: true}) | ||
} | ||
@@ -94,3 +94,3 @@ }) | ||
}) | ||
return resolve(totalRunsFile) | ||
return reject(totalRunsFile) | ||
} | ||
@@ -127,3 +127,3 @@ if (errorBatchEntryRecords.length === 0) { | ||
} catch (error) { | ||
return resolve({ | ||
return reject({ | ||
row_id: id, | ||
@@ -130,0 +130,0 @@ message: error.message ? error.message : error.stack, |
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
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
71701
1588