GithubHelp home page GithubHelp logo

maximburyak / lambda2js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aviv86/lambda2js

0.0 2.0 0.0 452 KB

Converts a C# expression tree (from Linq namespace) to a syntatically correct javascript code.

License: MIT License

C# 98.25% Visual Basic 0.16% Smalltalk 1.60%

lambda2js's Introduction

Lambda2Js

This is an ExpressionTree (lambda) to Javascript converter.

It is portable, so that you can use it in most environments.

It's purpose is to convert a C# expression tree (from Linq namespace) to a syntatically correct javascript code.

It can be extended to customize the mapping of expressions:

  • support custom static methods, instead of emiting code that would otherwise depend on external javascript
  • support custom types, converting method calls and properties accordingly

It is well tesded, and won't break. More than 120 tests passing.

This project uses Semantic versioning.

Installing NuGet package:

PM> Install-Package Lambda2Js

Samples

Converting lambda with boolean and numeric operations:

Expression<Func<MyClass, object>> expr = x => x.PhonesByName["Miguel"].DDD == 32 || x.Phones.Length != 1;
var js = expr.CompileToJavascript();
// js = PhonesByName["Miguel"].DDD===32||Phones.length!==1

Converting lambda with LINQ expression, containing a inner lambda:

Expression<Func<MyClass, object>> expr = x => x.Phones.FirstOrDefault(p => p.DDD > 10);
var js = expr.CompileToJavascript();
// js = System.Linq.Enumerable.FirstOrDefault(Phones,function(p){return p.DDD>10;})

Converting lambda with Linq Select method:

Expression<Func<string[], IEnumerable<char>>> expr = array => array.Select(x => x[0]);
var js = expr.CompileToJavascript(
    new JavascriptCompilationOptions(
        JsCompilationFlags.BodyOnly | JsCompilationFlags.ScopeParameter,
        new[] { new LinqMethods(), }));
// js = array.map(function(x){return x[0];})

Clone using ToArray and targeting ES6:

Expression<Func<string[], IEnumerable<string>>> expr = array => array.ToArray();
var js = expr.Body.CompileToJavascript(
    ScriptVersion.Es60,
    new JavascriptCompilationOptions(new LinqMethods()));
// js = [...array]

Developing custom plugins

You can develop and use some prebuilt plugins. See the readme.md in the Plugins folder.

Building and testing

ATENTION! Please, run the ProjectsGenerator before doing any of these:

  • build the signed assembly
  • running tests for specific frameworks

Due to current Visual Studio limitations, I had to create a project generator to create some of the csproj files:

  • Lambda2Js.Signed.csproj is generated using the Lambda2Js.csproj as it's template. It will copy the package version to the FileVersion and to the AssemblyVersion fields to keep them consistent. Also, it adds the ".Signed" string where appropriate in file names and in project name, and finally it includes a reference to the snk file.

  • Lambda2Js.Tests.$(TargetFramework).csproj are generated from Lambda2Js.Tests.csproj. That is needed because this project is a multitargeted test project, and Visual Studio cannot see the tests inside after compiling it... so what I dis was: create copy projects that have only one target framework for each of the possible targets.

Testing

To test support on the .Net 4.0, you need to run the Test.Net-v4.0.csproj because this framework version does not support the native test attributes to do automatic testing.

To test other framework versions, please, take a look at the TargetedTests solution folder. In that folder you will find all the tests. Unload all of them, but keep the framework version you want to test, then build it. Visual Studio will see the newly built tests and will list them. If the other frameworks test projects are not unloaded, Visual Studio may or may not list them.

lambda2js's People

Contributors

andrerpena avatar aviv86 avatar bgurmendi avatar bryantlikes avatar devanarch avatar lahma avatar masbicudo avatar ppekrol avatar ysmoradi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.