From 5f4e1c35763eee91b57900b906dfe64079be9766 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 22 Oct 2017 11:01:36 +1300 Subject: [PATCH] Working towards fixing date formatting #520 --- .../TypeScript/DateCodeGenerationTests.cs | 2 +- .../DataConversionGenerator.cs | 2 +- .../Models/FileTemplateModel.cs | 3 +++ .../Templates/ConvertToJavaScriptTemplate.tt | 6 +++--- .../Templates/FileTemplate.tt | 6 ++++++ 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.Tests/TypeScript/DateCodeGenerationTests.cs b/src/NJsonSchema.CodeGeneration.Tests/TypeScript/DateCodeGenerationTests.cs index 0024243f7..ca02461b2 100644 --- a/src/NJsonSchema.CodeGeneration.Tests/TypeScript/DateCodeGenerationTests.cs +++ b/src/NJsonSchema.CodeGeneration.Tests/TypeScript/DateCodeGenerationTests.cs @@ -53,7 +53,7 @@ public async Task When_date_handling_is_moment_then_moment_property_are_generate //// Assert Assert.IsTrue(code.Contains("myDate: moment.Moment")); Assert.IsTrue(code.Contains("this.myDate = data[\"myDate\"] ? moment(data[\"myDate\"].toString()) : undefined;")); - Assert.IsTrue(code.Contains("data[\"myDate\"] = this.myDate ? this.myDate.toISOString().slice(0, 10) : undefined;")); + Assert.IsTrue(code.Contains("data[\"myDate\"] = this.myDate ? this.myDate.format('YYYY-MM-DD') : undefined;")); } [TestMethod] diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/DataConversionGenerator.cs b/src/NJsonSchema.CodeGeneration.TypeScript/DataConversionGenerator.cs index 74fd57533..5a8be629e 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/DataConversionGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/DataConversionGenerator.cs @@ -78,7 +78,7 @@ private static object CreateModel(DataConversionParameters parameters) //StringToDateCode is used for date and date-time formats StringToDateCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date ? "new Date" : "moment", - DateToStringCode = "toISOString().slice(0, 10)", + DateToStringCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date ? "formatDate(THIS)" : "THIS.format('YYYY-MM-DD')", DateTimeToStringCode = "toISOString()", HandleReferences = parameters.Settings.HandleReferences diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/Models/FileTemplateModel.cs b/src/NJsonSchema.CodeGeneration.TypeScript/Models/FileTemplateModel.cs index dc7bf7d70..31568635c 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/Models/FileTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/Models/FileTemplateModel.cs @@ -41,6 +41,9 @@ public FileTemplateModel(TypeScriptGeneratorSettings settings) /// Gets a value indicating whether to handle JSON references. public bool HandleReferences => _settings.HandleReferences; + /// Gets a value indicating whether Date(Time) objects are handled using JS Dates + public bool UsesJsDate => _settings.DateTimeType == TypeScriptDateTimeType.Date; + /// Gets the reference handling code. public string ReferenceHandlingCode => TypeScriptReferenceHandlingCodeGenerator.Generate(); // TODO: Remove after T4 has been removed } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToJavaScriptTemplate.tt b/src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToJavaScriptTemplate.tt index c7ee5876a..f4f785b80 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToJavaScriptTemplate.tt +++ b/src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToJavaScriptTemplate.tt @@ -10,7 +10,7 @@ if (<#=Model.Value#> && <#=Model.Value#>.constructor === Array) { <#=Model.Variable#>.push(item.toJSON()); <#}else{#> <#if(Model.IsArrayItemDate){#> - <#=Model.Variable#>.push(item.<#=Model.DateToStringCode#>); + <#=Model.Variable#>.push(<#=Model.DateToStringCode.Replace("THIS", "item")#>); <#}else if(Model.IsArrayItemDateTime){#> <#=Model.Variable#>.push(item.<#=Model.DateTimeToStringCode#>); <#}else{#> @@ -27,7 +27,7 @@ if (<#=Model.Value#>) { <#=Model.Variable#>[key] = <#=Model.Value#>[key] ? <#=Model.Value#>[key].toJSON() : <#=Model.NullValue#>; <#}else{#> <#if(Model.IsDictionaryValueDate){#> - <#=Model.Variable#>[key] = <#=Model.Value#>[key] ? <#=Model.Value#>[key].<#=Model.DateToStringCode#> : <#=Model.NullValue#>; + <#=Model.Variable#>[key] = <#=Model.Value#>[key] ? <#=Model.DateToStringCode.Replace("THIS", Model.Value + "[key]")#> : <#=Model.NullValue#>; <#} else if(Model.IsDictionaryValueDateTime){#> <#=Model.Variable#>[key] = <#=Model.Value#>[key] ? <#=Model.Value#>[key].<#=Model.DateTimeToStringCode#> : <#=Model.NullValue#>; <#}else{#> @@ -41,7 +41,7 @@ if (<#=Model.Value#>) { } <#}else{#> <# if(Model.IsDate){#> -<#=Model.Variable#> = <#=Model.Value#> ? <#=Model.Value#>.<#=Model.DateToStringCode#> : <#if(Model.HasDefaultValue){#><#=Model.DefaultValue#><#}else{#><#=Model.NullValue#><#}#>; +<#=Model.Variable#> = <#=Model.Value#> ? <#=Model.DateToStringCode.Replace("THIS", Model.Value)#> : <#if(Model.HasDefaultValue){#><#=Model.DefaultValue#><#}else{#><#=Model.NullValue#><#}#>; <# } else if(Model.IsDateTime){#> <#=Model.Variable#> = <#=Model.Value#> ? <#=Model.Value#>.<#=Model.DateTimeToStringCode#> : <#if(Model.HasDefaultValue){#><#=Model.DefaultValue#><#}else{#><#=Model.NullValue#><#}#>; <# }else{#> diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/Templates/FileTemplate.tt b/src/NJsonSchema.CodeGeneration.TypeScript/Templates/FileTemplate.tt index 279e3e0b5..c3fc7b5a6 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/Templates/FileTemplate.tt +++ b/src/NJsonSchema.CodeGeneration.TypeScript/Templates/FileTemplate.tt @@ -7,6 +7,12 @@ <#=Model.ExtensionCode.ImportCode#> +<#if(Model.UsesJsDate){#> +function formatDate(d: Date) { + return d.getFullYear() + '-' + (d.getMonth() < 9 ? ('0' + (d.getMonth()+1)) : (d.getMonth()+1)) + '-' + (d.getDate() < 10 ? ('0' + d.getDate()) : d.getDate()); +} +<#}#> + <#if(Model.HasModuleName){#> module <#=Model.ModuleName#> { <#}