@twec/node-suite
Version:
Generic functionality for connecting to NetSuite Web Services from Node
47 lines (42 loc) • 1.51 kB
JavaScript
// Some utility functions for dealing with folders
function doFolderRecordsMatch(folderRecord1, folderRecord2) {
if (!folderRecord1 || !folderRecord2) { // no records
return false;
}
if (folderRecord1.name !== folderRecord2.name) { // names don't match
return false;
}
if (folderRecord1.parent && folderRecord2.parent) { // both have parents
return folderRecord1.parent.name === folderRecord2.parent.name;
}
if (!folderRecord1.parent && !folderRecord2.parent) { // neither one have parents
return true;
}
return false; // one parent and not the other
}
function getSimpleFolderRecordFromPath(folderPathArray) {
const len = folderPathArray.length || 0;
if (len < 1) {
return null;
}
const record = {
name: folderPathArray[len - 1],
};
if (len > 1) {
record.parent = { name: folderPathArray[len - 2] };
}
return record;
}
function findFolderRecord(folderPathArray, folderRecords) {
// Same intention as https://gitlab.com/twec/digital/netsuite-upload-utils/blob/master/src/file-cabinet-client.ts#L220
// Note: This only matches on one folder deep (folder name and parent's folder name)
// This is likely an OK limitation due to the rarity of a folder+sub-folder having duplicate
// names.
const folderPathRecord = getSimpleFolderRecordFromPath(folderPathArray);
return folderRecords.find(r => doFolderRecordsMatch(r, folderPathRecord));
}
module.exports = {
doFolderRecordsMatch,
getSimpleFolderRecordFromPath,
findFolderRecord,
};