Reputation: 2519
I have an array of versions:
var versions = ['xs', 'sm', 'md', 'lg', 'thumbnail'];
I want to get all the image stored in this array that do not contain any of these suffixes. Here is an example of the array of images:
{
"files": [
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_xs.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_sm.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_md.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_lg.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_thumbnail.png"
]
}
I only want the first item, however, I am getting all of them with my current code:
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
// perform directory code here
} else {
versions.forEach(function(version) {
if (file.indexOf(version) != -1) {
results.push(file);
}
});
}
});
Upvotes: 2
Views: 1828
Reputation: 48003
Here is how I would do it:
var versions = ['xs', 'sm', 'md', 'lg', 'thumbnail'];
results=[];
files=[
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_xs.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_sm.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_md.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_lg.png",
"/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1_thumbnail.png"
]
files.forEach(function (file){
//start checking
findany=false;
versions.forEach(function(version) {
if (file.indexOf(version) != -1) findany=true;
});
if(!findany) results.push(file);
//end checking
})
Output
> results
[ '/public/uploads/contentitems/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0/063012A5-60BC-4A4C-AEC2-56B0D5D99EF0_1.png' ]
Upvotes: 0
Reputation: 19294
Here's how i would do it :
1) take the suffix of the file. The definition i took was 'a non-numeric string between a _ and the . that precedes the 3 letters of the file format.' You might want another definition or do some checks.
But i think it' a good start to have at hand the suffix to avoid surprises if other part of file name contains a matching string.
// provides lower case suffix of the file, or null if none found.
function getSuffix (s) {
var res = /_([a-zA-Z]+)\.\w{3}$/g.exec(s) ;
if (!res) return null;
return res[1].toLowerCase();
}
2) write the available formats like this :
var version = { xs : true, sm : true, md : true, lg : true, thumbnail : true } ;
3) then checking is done with :
var suffix = getSuffix(fileName) ;
if (version[suffix]) /* do something */
( Notice that this way you can easily associate a function to a format :
var versionProcessors = { xs : function(fileBuffer) { /* process a xs buffer */ },
sm : function(fileBuffer) ...,
. .. } ;
// ... later ....
var suffix = getSuffix(fileName) ;
var processor = versionProcessors[suffix];
if (processor) {
getFileBuffer(fileName, processor);
}
)
Upvotes: 1
Reputation: 88
I believe you want this:
if (file.indexOf(version) == -1) {
results.push(file);
}
If the filename contains the suffix, file.indexOf(version)
will return an index > -1, so you don't want to add it to the results. However, if the filename doesn't contain the suffix, that line will return -1.
Upvotes: 0