GithubHelp home page GithubHelp logo

bubdm / sharp2js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from castle-it/sharp2js

0.0 0.0 0.0 2.8 MB

Simple library to convert your C# POCOs/DTOs to javascript objects

License: Apache License 2.0

C# 97.03% JavaScript 2.97%

sharp2js's Introduction

sharp2Js

Build status Coverage Status GitHub version NuGet version

sharp2Js is a small library that can create javascript objects that mirror your C# POCO classes and can be easily used to generate js files using T4 templates.

Features (v1.5.0)

  • Optionally generates merge map function to make applying model changes easy
  • Optionally generates a deep comparison $equals function to compare equality easily
  • Handle custom types, primitives, arrays, recursive types, structs, enums, and collections (IList, ICollection, IDictionary)
  • Allows for an override constructor if you need to wrap the created objects (e.g. for Angular)
  • Outputs to string for easy addition to T4 template output or otherwise
  • Supports optional camel casing
  • Supports optional automatic removal of phrases like Dto, etc
  • Supports the [DefaultValue] attribute for primitive types
  • Supports [IgnoreDataMember] and [DataMember] attributes
  • Supports custom function injection into generated Js objects

Examples/How-to

Installation

sharp2Js can be installed via the nuget UI (as sharp2Js), or via the nuget package manager console:

PM> Install-Package sharp2Js

Sample T4 Example

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".js" #>
<#@ assembly name="$(ProjectDir)bin\$(ConfigurationName)\Castle.Sharp2Js.dll" #>
<#@ output extension=".js" #>
<# var str = Castle.Sharp2Js.JsGenerator.Generate(new [] { typeof(Castle.Sharp2Js.SampleData.AddressInformation) }); #>
models = {};

<#=str#>

C# POCOs Example

public class AddressInformation
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public int ZipCode { get; set; }
        public OwnerInformation Owner { get; set; }
        public List<Feature> Features { get; set; }
        public List<string> Tags { get; set; }
    }

    public class OwnerInformation
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

    public class Feature
    {
        public string Name { get; set; }
        public double Value { get; set; }
    }

Javascript Object Output

models = {};

models.AddressInformation = function (cons, overrideObj) {
	if (!overrideObj) { overrideObj = { }; }
	if (!cons) { cons = { }; }
	var i, length;

	this.Name = cons.Name;
	this.Address = cons.Address;
	this.ZipCode = cons.ZipCode;
	if (!overrideObj.OwnerInformation) {
		this.Owner = new models.OwnerInformation(cons.Owner);
	} else {
		this.Owner = new overrideObj.OwnerInformation(cons.Owner, overrideObj);
	}
	this.Features = new Array(cons.Features == null ? 0 : cons.Features.length );
	if(cons.Features != null) {
		for (i = 0, length = cons.Features.length; i < length; i++) {
			if (!overrideObj.Feature) {
				this.Features[i] = new models.Feature(cons.Features[i]);
			} else {
				this.Features[i] = new overrideObj.Feature(cons.Features[i], overrideObj);
			}
		}
	}
	this.Tags = new Array(cons.Tags == null ? 0 : cons.Tags.length );
	if(cons.Tags != null) {
		for (i = 0, length = cons.Tags.length; i < length; i++) {
			this.Tags[i] = cons.Tags[i];
		}
	}
}


models.OwnerInformation = function (cons) {
	if (!cons) { cons = { }; }

	this.FirstName = cons.FirstName;
	this.LastName = cons.LastName;
	this.Age = cons.Age;
}


models.Feature = function (cons) {
	if (!cons) { cons = { }; }

	this.Name = cons.Name;
	this.Value = cons.Value;
}

Change Log

v1.5.0

June 20, 2020
  • Support for .Net Standard 2.0

v1.4.1

June 7, 2016
  • Addresses OutOfMemoryException caused by some object graphs.

v1.4.0

January 1, 2016
  • Adds support for an $equals method that can be used to perform deep comparisons on objects
  • Fixes an issue that caused bool DefaultValue values to be rendered improperly
  • Switches to Appveyor CI
  • Adds coveralls/OpenCover support

v1.3.0

November 27, 2015
  • Adds support for Enum types
  • Adds support for custom function parsers if you want to add your own custom functions to the output
  • Adds support for all IList, ICollection, and IDictionary types
  • Light internal refactoring for maintainability

v1.2.0

November 16, 2015
  • Fixes the camel-casing implementation
  • Fixes an issue with improperly initialized object types
  • Adds support for the DefaultValueAttribute on primitive types
  • Test coverage upped to 100%

v1.1.1

November 5, 2015
  • Adds Slack integration to CI
  • Switches to NUnit for unit tests
  • Adds support for DataMember and IgnoreDataMember attributes

v1.1.0

October 24, 2015
  • Adds support for array and recursive types
  • Includes NuGet package
  • Adds $merge function to perform a deep merge on objects

v1.0.0

October 20, 2015
  • Initial Commit/Version

Contributions

Any improvements are welcome. sharp2Js has served our limited purposes so far, but we would love to see it grow.

sharp2js's People

Contributors

tghamm avatar zalos 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.