2

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);
            }
        });
    }
});
6
  • Is versions in the bottom code the same as the array called version at the top ? Commented Dec 17, 2013 at 23:40
  • If you just want the first item, why not stopping the forEach loop with a "return;" ? Commented Dec 17, 2013 at 23:45
  • I believe he's saying that he only wants the first item because the first filename is the only one that doesn't contain one of those suffixes in versions. Commented Dec 17, 2013 at 23:48
  • use some() instead of forEach() and return true to stop the iteration. Commented Dec 18, 2013 at 1:22
  • Something's wrong in the logic. Your version.forEach will push the file unless all the vesions occur in the filename. Commented Dec 18, 2013 at 7:04

3 Answers 3

1

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);
}

)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

This is also wrong. Why ? it will push the filename if it contains a version and you match for another vesion.
0

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' ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.