2

I have a javascript file in which I have defined a custom function which take some input object and perform some action.
This file is called from many files.
Now I want to priint all the params name used in this function.

May be I need to read whole file as a string and print the desired output

 function customFunction(param){
       if(param.isOK == yes){}
       if(param.hasMenu == no){}
       ..
       ..
       ..
       if(param.cancelText == "cancel"){}

    }

I want to write a program which reads this file and output all the params name. e.g.

isOK | hasMenu | ...... | CancelText ..

Vishu238
  • 673
  • 4
  • 17

2 Answers2

2

You can use Regular Expression.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace _32666940
{

    class Program
    {
        public static String str = "function customFunction(param){ " +
            "if(param.isOK == yes){}" +
            "if(param.hasMenu == no){}" +
            "if(param.cancelText == \"cancel\"){}" +    // I had to add \ before "
            "if(param[  'isOK'  ] == yes){}" +
            "if(param[\"isOK\"] == yes){}" +
            "}";
        static void Main()
        {
            //@"param\.[a-zA-Z_]+[a-zA-Z0-9_]*"
            Regex regex = new Regex(@"param\.[a-zA-Z_]+[a-zA-Z0-9_]*");
            Match match = regex.Match(str);
            while (match.Success)
            {
                Console.WriteLine(match.Value);
                match = match.NextMatch();
            }

            regex = new Regex(@"\bparam\[\s*(['""])(.+?)\1\s*]");
            match = regex.Match(str);
            while (match.Success)
            {
                Console.WriteLine(match.Value);
                match = match.NextMatch();
            }

            Console.ReadKey();

        }
    }
}

Edit: updated the code to match param['some-key']

Edit updated the code to match param["some-key"]

Rafaf Tahsin
  • 7,652
  • 4
  • 28
  • 45
  • 1
    This will not get properties which are accessed using array notation: e.g. param['this-is-another-property'] – chriskelly Sep 19 '15 at 11:52
  • 1
    This solve my one problem. Can you please provide me the regex for situation like param['this-is-another-property'] – Vishu238 Sep 19 '15 at 12:51
  • I've updated the code again. The code is now working in all cases. You can also have a glance here ... http://stackoverflow.com/questions/32676695/regular-expression-to-detect-double-quoted-javascript-object-properties-with-bra/32676719?noredirect=1#32676719 – Rafaf Tahsin Sep 20 '15 at 07:53
0

If param is object, then you can use jQuery each() method. $.each() is an object iterator utility.

You can loop through this object using each() method as following

$.each( param, function( key, value ) {
     console.log(value);
    //Add your logic as per requirement
});

It will print all the values(in console) available in the param object.

Ref: jQuery each()

AbdulRahman Ansari
  • 3,007
  • 1
  • 21
  • 29
  • In this way I need to pass all the param's during function call.. This is the main issue .. I have to calculate the number of parameter that I am supporting in this function .. It is around 1000 . I can not pass all the params at a time.. Different combinations of param are passed from different file – Vishu238 Sep 19 '15 at 10:39