public static ToCamelCase ( string input, bool camelCase ) : string | ||
input | string | The string. |
camelCase | bool | if set to |
리턴 | string |
/// <summary> /// Builds an array property. /// </summary> /// <param name="sb">The string builder.</param> /// <param name="propEntry">The property entry.</param> /// <param name="options">The options.</param> private static void BuildArrayProperty(StringBuilder sb, PropertyBag propEntry, JsGeneratorOptions options) { sb.AppendLine(string.Format("\tthis.{0} = new Array(cons.{0} == null ? 0 : cons.{1}.length );", Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase), Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase))); sb.AppendLine($"\tif(cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} != null) {{"); sb.AppendLine( $"\t\tfor (i = 0, length = cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.length; i < length; i++) {{"); var collectionType = propEntry.CollectionInnerTypes.First(); if (!collectionType.IsPrimitiveType) { sb.AppendLine( $"\t\t\tif (!overrideObj.{Helpers.GetName(collectionType.Type.Name, options.ClassNameConstantsToRemove)}) {{"); sb.AppendLine( $"\t\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i] = new {options.OutputNamespace}.{Helpers.GetName(collectionType.Type.Name, options.ClassNameConstantsToRemove)}(cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i]);"); sb.AppendLine("\t\t\t} else {"); sb.AppendLine( $"\t\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i] = new overrideObj.{Helpers.GetName(collectionType.Type.Name, options.ClassNameConstantsToRemove)}(cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i], overrideObj);"); sb.AppendLine("\t\t\t}"); } else { sb.AppendLine( $"\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i] = cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i];"); } sb.AppendLine("\t\t}"); sb.AppendLine("\t}"); }
/// <summary> /// Builds a primitive property. /// </summary> /// <param name="propEntry">The property entry.</param> /// <param name="sb">The string builder.</param> /// <param name="options">The options.</param> private static void BuildPrimitiveProperty(PropertyBag propEntry, StringBuilder sb, JsGeneratorOptions options) { if (propEntry.TypeDefinition.IsEnum) { sb.AppendLine( propEntry.PropertyType == typeof(string) ? $"\t{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}: '{propEntry.DefaultValue}'," : $"\t{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}: {propEntry.DefaultValue},"); } else if (propEntry.HasDefaultValue) { var writtenValue = propEntry.DefaultValue is bool ?propEntry.DefaultValue.ToString().ToLower() : propEntry.DefaultValue; sb.AppendLine( $"\tif (!cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine( propEntry.PropertyType == typeof(string) ? $"\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = '{writtenValue}';" : $"\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = {writtenValue};"); sb.AppendLine("\t} else {"); sb.AppendLine( $"\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)};"); sb.AppendLine("\t}"); } else { sb.AppendLine( $"\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)};"); } }
/// <summary> /// Builds an object/reference property. /// </summary> /// <param name="sb">The string builder.</param> /// <param name="propEntry">The property entry.</param> /// <param name="options">The options.</param> private static void BuildObjectProperty(StringBuilder sb, PropertyBag propEntry, JsGeneratorOptions options) { sb.AppendLine($"\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = null;"); sb.AppendLine($"\tif (cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine( $"\t\tif (!overrideObj.{Helpers.GetName(propEntry.PropertyType.Name, options.ClassNameConstantsToRemove)}) {{"); sb.AppendLine( $"\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = new {options.OutputNamespace}.{Helpers.GetName(propEntry.PropertyType.Name, options.ClassNameConstantsToRemove)}(cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)});"); sb.AppendLine("\t\t} else {"); sb.AppendLine( $"\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = new overrideObj.{Helpers.GetName(propEntry.PropertyType.Name, options.ClassNameConstantsToRemove)}(cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}, overrideObj);"); sb.AppendLine("\t\t}"); sb.AppendLine("\t}"); }
/// <summary> /// Builds a dictionary property. /// </summary> /// <param name="sb">The string builder.</param> /// <param name="propEntry">The property entry.</param> /// <param name="options">The options.</param> private static void BuildDictionaryProperty(StringBuilder sb, PropertyBag propEntry, JsGeneratorOptions options) { sb.AppendLine($"\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = {{}};"); sb.AppendLine($"\tif(cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} != null) {{"); sb.AppendLine( $"\t\tfor (var key in cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine( $"\t\t\tif (cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.hasOwnProperty(key)) {{"); var keyType = propEntry.CollectionInnerTypes.First(p => p.IsDictionaryKey); if (!AllowedDictionaryKeyTypes.Contains(keyType.Type)) { throw new Exception( $"Dictionaries must have strings, enums, or integers as keys, error found in type: {propEntry.TypeName}"); } var valueType = propEntry.CollectionInnerTypes.First(p => !p.IsDictionaryKey); if (!valueType.IsPrimitiveType) { sb.AppendLine( $"\t\t\t\tif (!overrideObj.{Helpers.GetName(valueType.Type.Name, options.ClassNameConstantsToRemove)}) {{"); sb.AppendLine( $"\t\t\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key] = new {options.OutputNamespace}.{Helpers.GetName(valueType.Type.Name, options.ClassNameConstantsToRemove)}(cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key]);"); sb.AppendLine("\t\t\t\t} else {"); sb.AppendLine( $"\t\t\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key] = new overrideObj.{Helpers.GetName(valueType.Type.Name, options.ClassNameConstantsToRemove)}(cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key], overrideObj);"); sb.AppendLine("\t\t\t\t}"); } else { sb.AppendLine( $"\t\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key] = cons.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key];"); } sb.AppendLine("\t\t\t}"); sb.AppendLine("\t\t}"); sb.AppendLine("\t}"); }
/// <summary> /// Builds the merge function for a type. /// </summary> /// <param name="sb">The string builder.</param> /// <param name="propList">The property list.</param> /// <param name="options">The options.</param> private static void BuildMergeFunctionForClass(StringBuilder sb, IEnumerable <PropertyBag> propList, JsGeneratorOptions options) { //Generate a merge function to merge two objects sb.AppendLine("\tthis.$merge = function (mergeObj) {"); sb.AppendLine("\t\tif (!mergeObj) { mergeObj = { }; }"); foreach (var propEntry in propList) { switch (propEntry.TransformablePropertyType) { case PropertyBag.TransformablePropertyTypeEnum.CollectionType: sb.AppendLine( $"\t\tif (!mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = [];"); sb.AppendLine("\t\t}"); sb.AppendLine( $"\t\tif (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} != null) {{"); sb.AppendLine(string.Format("\t\t\tthis.{0}.splice(0, this.{0}.length);", Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase))); sb.AppendLine("\t\t}"); sb.AppendLine( $"\t\tif (mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine( $"\t\t\tif (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} === null) {{"); sb.AppendLine($"\t\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = [];"); sb.AppendLine("\t\t\t}"); sb.AppendLine( $"\t\t\tfor (i = 0; i < mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.length; i++) {{"); sb.AppendLine(string.Format("\t\t\t\tthis.{0}.push(mergeObj.{0}[i]);", Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase))); sb.AppendLine("\t\t\t}"); sb.AppendLine("\t\t}"); break; case PropertyBag.TransformablePropertyTypeEnum.DictionaryType: sb.AppendLine( $"\t\tif (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} != null) {{"); sb.AppendLine( $"\t\t\tfor (var key in this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine( $"\t\t\t\tif (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.hasOwnProperty(key)) {{"); sb.AppendLine( $"\t\t\t\t\tdelete this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key];"); sb.AppendLine("\t\t\t\t}"); sb.AppendLine("\t\t\t}"); sb.AppendLine("\t\t}"); sb.AppendLine( $"\t\tif (mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine( $"\t\t\tfor (var key in mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine( $"\t\t\t\tif (mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.hasOwnProperty(key)) {{"); sb.AppendLine( $"\t\t\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key] = mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key];"); sb.AppendLine("\t\t\t\t}"); sb.AppendLine("\t\t\t}"); sb.AppendLine("\t\t}"); break; case PropertyBag.TransformablePropertyTypeEnum.ReferenceType: sb.AppendLine( $"\t\tif (mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} == null) {{"); sb.AppendLine($"\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = null;"); sb.AppendLine( $"\t\t}} else if (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} != null) {{"); sb.AppendLine( $"\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.$merge(mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)});"); sb.AppendLine("\t\t} else {"); sb.AppendLine( $"\t\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)};"); sb.AppendLine("\t\t}"); break; case PropertyBag.TransformablePropertyTypeEnum.Primitive: sb.AppendLine( $"\t\tthis.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} = mergeObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)};"); break; } } sb.AppendLine("\t}"); }
/// <summary> /// Builds the equals function for a type. /// </summary> /// <param name="sb">The string builder.</param> /// <param name="propList">The property list.</param> /// <param name="options">The options.</param> private static void BuildEqualsFunctionForClass(StringBuilder sb, IEnumerable <PropertyBag> propList, JsGeneratorOptions options) { //Generate an equals function for two objects sb.AppendLine("\tthis.$equals = function (compareObj) {"); sb.AppendLine("\t\tif (!compareObj) { return false; }"); foreach (var propEntry in propList) { switch (propEntry.TransformablePropertyType) { case PropertyBag.TransformablePropertyTypeEnum.CollectionType: sb.AppendLine( $"\t\tif (compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} !== this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\tif (!compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\t\treturn false;"); sb.AppendLine($"\t\t\t}}"); sb.AppendLine($"\t\t\tif (!this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\t\treturn false;"); sb.AppendLine($"\t\t\t}}"); sb.AppendLine($"\t\t\tif (compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.length != this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.length) {{"); sb.AppendLine($"\t\t\t\treturn false;"); sb.AppendLine($"\t\t\t}}"); sb.AppendLine( $"\t\t\tfor (i = 0; i < this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.length; i++) {{"); var collectionType = propEntry.CollectionInnerTypes.First(); if (!collectionType.IsPrimitiveType) { sb.AppendLine( $"\t\t\t\tif (!this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i].$equals(compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i])) {{ return false; }};"); } else { sb.AppendLine( $"\t\t\t\tif (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i] !== compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[i]) {{ return false; }};"); } sb.AppendLine($"\t\t\t}}"); sb.AppendLine($"\t\t}}"); break; case PropertyBag.TransformablePropertyTypeEnum.DictionaryType: sb.AppendLine( $"\t\tif (compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} !== this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\tif (!compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\t\treturn false;"); sb.AppendLine($"\t\t\t}}"); sb.AppendLine($"\t\t\tif (!this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\t\treturn false;"); sb.AppendLine($"\t\t\t}}"); /*sb.AppendLine( * $"\t\tif (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} != null) {{");*/ sb.AppendLine( $"\t\t\tfor (var key in this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine( $"\t\t\t\tif (!compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.hasOwnProperty(key)) {{"); sb.AppendLine( $"\t\t\t\t\treturn false;"); sb.AppendLine("\t\t\t\t}"); var valueType = propEntry.CollectionInnerTypes.First(p => !p.IsDictionaryKey); if (!valueType.IsPrimitiveType) { sb.AppendLine( $"\t\t\t\tif (!this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key].$equals(compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key])) {{ return false; }};"); } else { sb.AppendLine( $"\t\t\t\tif (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key] !== compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}[key]) {{ return false; }};"); } sb.AppendLine("\t\t\t}"); sb.AppendLine("\t\t}"); break; case PropertyBag.TransformablePropertyTypeEnum.ReferenceType: sb.AppendLine( $"\t\tif (compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} !== this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\tif (!compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\t\treturn false;"); sb.AppendLine($"\t\t\t}}"); sb.AppendLine($"\t\t\tif (!this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{"); sb.AppendLine($"\t\t\t\treturn false;"); sb.AppendLine($"\t\t\t}}"); sb.AppendLine( $"\t\t\tif (!this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}.$equals(compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)})) {{ return false; }};"); sb.AppendLine("\t\t}"); break; case PropertyBag.TransformablePropertyTypeEnum.Primitive: sb.AppendLine( $"\t\tif (this.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)} !== compareObj.{Helpers.ToCamelCase(propEntry.PropertyName, options.CamelCase)}) {{ return false; }};"); break; } } sb.AppendLine("\treturn true;"); sb.AppendLine("\t}"); }