Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove . and , in the beginning/end of every element from the array list in Angular 6

Tags:

I have a reactive form in Angular which gets file format as text input

<input type="text" name="formats" formControlName= "formats">

Eg: .txt, .zip, .tar.gz

I needed to convert those inputs as an array list. With the help of @danday74, I can able to do that. here is the code:

const input = ".txt, .zip, .tar.gz"

const parts = input.split(' ')

const output = parts.map(x => x.replace(',', '').replace('.', ''))

console.log(output)

The output generated by the code is ["txt","zip","tar.gz"], which is what I expected.

However, my concern is, if a user enters something like this .. ., .tar.gf.ds ,.tar or tar tar.gz zip the output will be [".","","tar.gf.ds","tar"] and ["tar","targz","zip"] respectively

My question is how do I implement this in such a way that a user can enter the file format without any specific structure (for Eg: .txt, .zip, .tar.gz, .txt .zip .tar.gz, txt, zip, tar.gz, txt zip tar.gz ) and I should be able to generate the output like this ["txt","zip","tar.gz"]. like I should be able to neglect the input if it is just .. or ., and only consider the input with string.

like image 810
Navaneeth Selvaraj Avatar asked Aug 09 '18 02:08

Navaneeth Selvaraj


2 Answers

If you're only concerned about leading . and ,, you could use a regex as follows:

const input = '.txt, .zip, .tar.gz, ,.tar, txt, zip, tar.gz, .., .,'

const output = input.split(', ')
  .map(ext => ext.replace(/^\W+/, '')) // remove any character that's not a word character from the beginning of each string
  .filter(Boolean); // filter just to remove empty strings

console.log(output);

If you also need to remove trailing characters you could modify the regex to remove them from the end as well:

const input = '.txt, .zip, .tar.gz, ,.tar, txt, zip, tar.gz, .txt., .tar.gz., ,.tar,., .., .,'

const output = input.split(' ') // split on space character as trailing commas will be handled in the regex
  .map(ext => ext.replace(/^\W+|\W+$/g, ''))
  .filter(Boolean);

console.log(output);

Let me know if there are any other considerations.

like image 108
Scott Rudiger Avatar answered Sep 28 '22 18:09

Scott Rudiger


Here is a general way how you can do it!

If there are two .. anywhere in the string! Do not add it.

Some use cases might have been left, comment if there are any

var strings = [
  ".txt, .zip, .tar.gz",
  "..txt .zip .tar.gz a.b.should.be.in", 
  "txt, zip, tar.gz",
  ".., ..., ...., .this.should.be.in, .not..this"
]

var extensions = []

strings
  .forEach((item) => {
    let items = item.split(',');
    if (items.length <= 1) {
      items = item.split(' ');
    }
    items.forEach(ext => {
      const trimedExt = ext.trim()
      if (
        trimedExt.indexOf('..') === -1
      ) {
        if (trimedExt[0] === '.') {
          extensions.push(trimedExt.substr(1))
        } else {
          extensions.push(trimedExt)
        }        
      }
    });
  })
  
 console.log(extensions)
like image 31
Prasanna Avatar answered Sep 28 '22 18:09

Prasanna