Reconstruct and execute javascript functions dynamically

In some scenarios , we have to reconstruct and execute javascript functions dynamically.

//function name to be reconstruct
 function add(a,b) {
 console.log("Result is : " ,a + b);
 }

//regular expression for converting the source code 
 let regExp = /\((.*)\).*\{([\s\S]*)\}/m;
 let sourceCodeParts = regExp.exec(add.toString());
 var functions = {};
 functions["add"] = new Function(sourceCodeParts[1], sourceCodeParts[2]);

//execute the function
functions["add"].apply(null, [8, 2]);
 

 

Output : Result is : 10

Create extension for Visual Studio Team Services

Visual Studio Team Services

Package management enables scenarios where your team needs to share packaged software components within the team or with other teams.Here I am going to create a new Team Services Package by using npm. If you don’t have npm , please install Node.js for windows.

https://nodejs.org/en/

In order to package the extension, you should run a tfx-cli(TFS Cross Platform Command Line Interface ) command using npm .

  • Go to Node.js command prompt and type the command npm i -g tfx-cli and press enter

one

  • Create a new folder somewhere on your local disk for example C:\MyCave\TeamServices\helloworld
  •  Navigate to the above directory from Node.js command prompt

2

  • Once you navigate to the directory use the ‘npm install’ command to retrieve the SDK :npm install vss-web-extension-sdk  and press enter. It will download some files

3

  • Open the helloworld folder from Visual Studio as website for edit
  • Create a new HTML page for your hub. the method init() and notifyLoadSucceeded() will initialize the sdk.

4

  • Add an icon to the img folder that identifies your extension
  • create extension manifest that describes your extension. for this create a JSON file (vss-extension.json ,for example) in the home directory

You can download JSON file from here –  https://plnkr.co/edit/l9eKrr?p=catalogue

6

  • Edit the JSON file – package name, path, version etc
  • Navigate to root directory where vss-extension.json placed and run the command tfx extension create –manifest-globs vss-extension.json for creating the package.

7

  • When you open your directory you could see the package file

8

Enjoy programming…

Avoid Multiple enumeration of IEnumerable

When we working with Linq queries it’s important to know that  query doesn’t get executed when we define it. Rather it’s get executed we actually use it or enumerated it.

Possible Multiple Enumeration

//Avoid Reevaluating of Linq Queries
Console.WriteLine("First Enumeration");
var nums = new List<int> {1,2,3,4};
var result = from n in nums select new {Number=n,ExecutionTime=DateTime.Now.ToString("mm:ss:fff")};
foreach (var item in result)
{
Console.WriteLine(item);
}
foreach (var item in result)
{
Console.WriteLine(item);
}

Console.WriteLine(“Second Enumeration”);

Avoid Multiple enumeration Of IEnumerable

Console.WriteLine(“Second Enumeration”);
//You can avoid the multiple enumeration possible by converting the results to List,Dictionary,Array..
var goodresult = (from n in nums select new { Number = n, ExecutionTime = DateTime.Now.ToString("mm:ss:fff") }).ToList();
foreach (var item in goodresult)
{
Console.WriteLine(item);
}
foreach (var item in goodresult)
{
Console.WriteLine(item);
}

Enjoy..Coding…

Tips in ASP.NET MVC

Enabling and Disabling Client-Side Validation Globally

From Config
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

From Code
protected void Application_Start()
{
//Enable or Disable Client Side Validation at Application Level
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}

Disable validation for a single field in client side

@Html.TexBoxFor(model => model.SomeValue,                  
new Dictionary<string, object> { { "data-val", false }}) 

Enabling and Disabling Client-Side Validation for Specific View

@model MvcApp.Models.SampleModel
@{
ViewBag.Title = "Home";
HtmlHelper.ClientValidationEnabled = false;
}

Enjoy reading