Search code examples
c#minify

Trying to create simple js minifier, but it doesn't remove anything, even though I delete the white space


I am trying to create a simple minifier because I am unsatisfied with the tools online. I have made a console application, but the problem is that nothing is removed, even though I split the text and remove /n and /t characters.

I've tried different methods of removing the whitespace.

static string restrictedSymbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,0123456789";
...
static void Compress(string command)
        {
            string[] commandParts = command.Split(' ');

            string text = String.Empty;

            try
            {
                using (StreamReader sr = new StreamReader(commandParts[1]))
                {
                    text = sr.ReadToEnd();
                    text.Replace("\n", "");
                    text.Replace("\t", "");
                    string formattedText = text;

                    string[] splitText = text.Split(' ');
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < splitText.Length - 1; i++)
                    {
                        splitText[i].TrimStart();
                        StringBuilder tSB = new StringBuilder(splitText[i]);
                        if (splitText[i].Length > 1 && splitText[i + 1].Length > 1)
                        {
                            int textLength = splitText[i].Length - 1;

                            if (restrictedSymbols.Contains(splitText[i + 1][0]) && restrictedSymbols.Contains(splitText[i][textLength]))
                            {
                                tSB.Append(" ");
                            }
                        }

                        sb.Append(tSB.ToString());
                    }

                    sb.Append(splitText[splitText.Length - 1]);

                    text = sb.ToString();
                    Console.WriteLine(text);
                }    
            } catch (IOException e)
            {
                Console.WriteLine(e.ToString());
            }

            if (text != String.Empty)
            {
                try
                {
                    using (StreamWriter stream = File.CreateText(commandParts[2] + commandParts[3]))
                    {
                        stream.Write(text);
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.ToString());
                }
            }

            Console.WriteLine("Process Complete...");
            GetCommand();
        }

It should print output a minified file, but it just outputs the same exact file that I put in.


Solution

  • Ignoring any other problem, Replace by it self does nothing

    Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.

    So basically you are ignoring any changes by not keeping the return value

    At minimum you will need to do something like this

    text = text.Replace("\n", "");