Description
The StringValueMap class allows you to create a cross-language implementation of a map (dictionary) where all keys and values are strings.
Important points
- The class provides several methods to convert the stored values to different types, such as array, boolean or datetime.
- This class is widely used in Pip.Services as a basis for variety of classes, such as ConfigParams, ConnectionParams, CredentialParams (in the Pip.Services components package) and others.
Constructors
Creates a new instance of the map and assigns its value.
publicconstructor(map: any = null)
- map: any - (optional) values to initialize this map.
Instance methods
append
Appends new elements to this map.
publicappend(map: any): void
- map: any - map with elements to be added.
clear
Clears this map by removing all its elements.
publicclear(): void
clone
Creates a binary clone of this object.
publicclone(): any
- returns: any - clone of this object.
get
Gets a map element specified by its key.
publicget(key: string): string
- key: string - key of the element to get.
- returns: string - value of the map element.
getAsArray
Converts a map’s element into an AnyValueArray object or returns an empty AnyValueArray object if the conversion is not possible.
See AnyValueArray, AnyValueArray.fromValue
publicgetAsArray(key: string): AnyValueArray
- key: string -key of element to get.
- returns: AnyValueArray - AnyValueArray value of the element or empty AnyValueArray if the conversion is not supported.
getAsArrayWithDefault
Converts a map’s element into an AnyValueArray object or returns a given default value if the conversion is not possible.
See AnyValueArray
publicgetAsArrayWithDefault(key: string, defaultValue: AnyValueArray): AnyValueArray
- key: string - key of the element to get.
- defaultValue: AnyValueArray - default value
- returns: AnyValueArray - AnyValueArray value of the element or given default value if the conversion is not supported.
getAsBoolean
Converts a map’s element into a boolean or returns false if the conversion is not possible.
publicgetAsBoolean(key: string): boolean
- key: string - key of the element to get.
- returns: boolean - boolean value of the element or false if the conversion is not supported.
getAsBooleanWithDefault
Converts a map’s element into a boolean or returns a given default value if the conversion is not possible.
See BooleanConverter.toBooleanWithDefault
publicgetAsBooleanWithDefault(key: string, defaultValue: boolean): boolean
- key: string - key of element to get.
- defaultValue: boolean - default value
- returns: boolean - boolean value of the element or given default value if the conversion is not supported.
getAsDateTime
Converts a map’s element into a Date or returns the current date if the conversion is not possible.
See BooleanConverter.toBooleanWithDefault
publicgetAsDateTime(key: string): Date
- key: string - key of element to get.
- returns: Date - Date value of the element or the current date if the conversion is not supported.
getAsDateTimeWithDefault
Converts a map’s element into a Date or returns a given default value if the conversion is not possible.
See DateTimeConverter.toDateTimeWithDefault
publicgetAsDateTimeWithDefault(key: string, defaultValue: Date): Date
- key: string - key of the element to get.
- defaultValue: Date - default value
- returns: Date - Date value of the element or given default value if the conversion is not supported.
getAsDouble
Converts a map’s element into a double or returns 0 if the conversion is not possible.
publicgetAsDouble(key: string): number
- key: string - key of the element to get.
- returns: number - double value of the element or 0 if the conversion is not supported.
getAsDoubleWithDefault
Converts a map’s element into a double or returns a given default value if the conversion is not possible.
See DoubleConverter.to_double_with_default
publicgetAsDoubleWithDefault(key: string, defaultValue: number): number
- key: string - key of the element to get.
- defaultValue: number - default value
- returns: number - double value of the element or given default value if the conversion is not supported.
getAsFloat
Converts a map’s element into a float or returns 0 if the conversion is not possible.
publicgetAsFloat(key: string): number
- key: string - key the of element to get.
- returns: number - float value of the element or 0 if the conversion is not supported.
getAsFloatWithDefault
Converts a map’s element into a float or returns a given default value if the conversion is not possible.
See FloatConverter.toFloatWithDefault
publicgetAsFloatWithDefault(key: string, defaultValue: number): number
- key: string - key of the element to get.
- defaultValue: number - default value
- returns: number - float value of the element or given default value if the conversion is not supported.
getAsInteger
Converts a map’s element into an integer or returns 0 if the conversion is not possible.
publicgetAsInteger(key: string): number
- key: string - key of the element to get.
- returns: number - integer value of the element or 0 if the conversion is not supported.
getAsIntegerWithDefault
Converts a map’s element into an integer or returns a given default value if the conversion is not possible.
See IntegerConverter.toIntegerWithDefault
publicgetAsIntegerWithDefault(key: string, defaultValue: number): number
- key: string - key of the element to get.
- defaultValue: number - default value
- returns: number - integer value of the element or given default value if the conversion is not supported.
getAsLong
Converts a map’s element into a long or returns 0 if the conversion is not possible.
publicgetAsLong(key: string): number
- key: string - key of the element to get.
- returns: number - long value of the element or 0 if the conversion is not supported.
getAsLongWithDefault
Converts a map’s element into a long or returns a given default value if the conversion is not possible.
See LongConverter.toLongWithDefault
publicgetAsLongWithDefault(key: string, defaultValue: number): number
- key: string - key of the element to get.
- defaultValue: number - default value
- returns: number - long value of the element or default value if the conversion is not supported.
getAsMap
Converts a map’s element into an AnyValueMap object or returns an empty AnyValueMap object if the conversion is not possible.
publicgetAsMap(key: string): AnyValueMap
- key: string - key of the element to get.
- returns: AnyValueMap - long value of the element or 0 if the conversion is not supported.
getAsMapWithDefault
Converts a map’s element into an AnyValueMap object or returns a given default value if the conversion is not possible.
publicgetAsMapWithDefault(key: string, defaultValue: AnyValueMap): AnyValueMap
- key: string - key of the element to get.
- defaultValue: AnyValueMap - default value
- returns: AnyValueMap - AnyValueMap value of the element or given default value if the conversion is not supported.
getAsNullableArray
Converts a map’s element into an AnyValueArray or returns null if the conversion is not possible.
See AnyValueMap, AnyValueMap.fromValue
publicgetAsNullableArray(key: string): AnyValueMap
- key: string - key of the element to get.
- returns: AnyValueMap - AnyValueMap value of the element or given default value if the conversion is not supported.
getAsNullableBoolean
Converts a map’s element into a boolean or returns null if the conversion is not possible.
See BooleanConverter.toNullableBoolean
publicgetAsNullableBoolean(key: string): boolean
- key: string - key of the element to get.
- returns: boolean - boolean value of the element or null if the conversion is not supported.
getAsNullableDateTime
Converts a map’s element into a Date or returns null if the conversion is not possible.
See DateTimeConverter.toNullableDateTime
publicgetAsNullableDateTime(key: string): Date
- key: string - key of the element to get.
- returns: Date - Date value of the element or null if the conversion is not supported.
getAsNullableDouble
Converts a map’s element into a double or returns null if the conversion is not possible.
See DoubleConverter.toNullableDouble
publicgetAsNullableDouble(key: string): number
- key: string - key of the element to get.
- returns: number - double value of the element or null if the conversion is not supported.
getAsNullableFloat
Converts a map’s element into a float or returns null if the conversion is not possible.
See FloatConverter.toNullableFloat
publicgetAsNullableFloat(key: string): number
- key: string - key of the element to get.
- returns: number - float value of the element or null if the conversion is not supported.
getAsNullableInteger
Converts a map’s element into an integer or returns null if the conversion is not possible.
See IntegerConverter.toNullableInteger
publicgetAsNullableInteger(key: string): number
- key: string - key of the element to get.
- returns: number - integer value of the element or null if the conversion is not supported.
getAsNullableLong
Converts a map’s element into a long or returns null if the conversion is not possible.
See LongConverter.toNullableLong
publicgetAsNullableLong(key: string): number
- key: string - key of the element to get.
- returns: number - long value of the element or null if the conversion is not supported.
getAsNullableMap
Converts a map’s element into an AnyValueMap object or returns null if the conversion is not possible.
publicgetAsNullableMap(key: string): AnyValueMap
- key: string - key of the element to get.
- returns: AnyValueMap - AnyValueMap value of the element or null if the conversion is not supported.
getAsNullableString
Converts a map’s element into a string or returns null if the conversion is not possible.
See StringConverter.toNullableString
publicgetAsNullableString(key: string): string
- key: string - key of the element to get.
- returns: string - string value of the element or null if the conversion is not supported.
getAsNullableType
Converts a map’s element into a value defined by a specified typecode.
If conversion is not possible, it returns null.
See TypeConverter.toNullableType
publicgetAsNullableType<T>(type: TypeCode, key: string): T
- type: TypeCode - TypeCode that defines the type of the result.
- key: string - key of the element to get.
- returns: T - element value defined by the typecode or null if the conversion is not supported.
getAsObject
Gets the value stored in map element without any conversions. When the element’s key is not defined, it returns the entire map value.
publicgetAsObject(key: string = undefined): any
- key: string - (optional) key of the element to get.
- returns: any - element value or value of the map when the index is not defined.
getAsString
Converts map’s element into a string or returns "" if the conversion is not possible.
publicgetAsString(key: string): string
- key: string - (optional) key of the element to get.
- returns: string - string value of the element or "" if the conversion is not supported.
getAsStringWithDefault
Converts map’s element into a string or returns "" if the conversion is not possible.
publicgetAsStringWithDefault(key: string, defaultValue: string): string
- key: string - key of the element to get.
- defaultValue: string - default value
- returns: string - string value of the element or default value if the conversion is not supported.
getAsType
Converts a map’s element into a value defined by a specified typecode. If conversion is not possible it returns the default value for the specified type.
publicgetAsType<T>(type: TypeCode, key: string): T
- type: TypeCode - TypeCode that defines the type of the result.
- key: string - key of the element to get.
- returns: T - element value defined by the typecode or default if the conversion is not supported.
getAsTypeWithDefault
Converts a map’s element into a value defined by a specified typecode.
If the conversion is not possible, it returns the default value for the specified type.
See TypeConverter.toTypeWithDefault
publicgetAsTypeWithDefault<T>(type: TypeCode, key: string, defaultValue: T): T
- type: TypeCode - TypeCode that defines the type of the result
- key: string - key of the element to get.
- defaultValue: T - default value
- returns: T - element’s value defined by the typecode or default value if the conversion is not supported.
getAsValue
Converts a map’s element into an AnyValue or returns an empty AnyValue if the conversion is not possible.
See AnyValue, AnyValue.constructors
publicgetAsValue(key: string): AnyValue
- key: string - key of the element to get.
- returns: AnyValue - AnyValue value of the element or an empty AnyValue object if the conversion is not supported.
getKeys
Gets keys of all elements stored in this map.
publicgetKeys(): string[]
- returns: string[] - list with all map keys.
length
Gets the number of elements stored in this map.
publiclength(): number
- returns: number - number of elements in this map.
put
Puts a new value into a map’s element specified by its key.
publicput(key: string, value: any): void
- key: string - key of the element to put.
- value: any - new value for the map’s element.
remove
Removes a map’s element specified by its key.
publicremove(key: string): void
- key: string - key of the element to remove.
setAsObject
Sets a new value to a map’s element specified by its index.
When the index is not defined, it resets the entire map value.
This method has double purpose, because method overrides are not supported in JavaScript.
See MapConverter.toMap
publicsetAsObject(key: any, value: any = undefined): void
- key: string - (optional) key of the element to set
- value: any - new element or map value.
toString
Gets a string representation of the object. The result is a semicolon-separated list of key-value pairs as “key1=value1;key2=value2;key=value3”
publictoString(): string
- returns: string - string representation of the object.
Static methods
fromMaps
Creates a new AnyValueMap by merging two or more maps. Maps defined later in the list override values from previously defined maps.
public staticfromMaps(…maps: any[]): StringValueMap
- maps: any[] - array of maps to be merged
- returns: StringValueMap - newly created StringValueMap.
fromString
Parses semicolon-separated key-value pairs and returns them as a StringValueMap.
public staticfromString(line: string): StringValueMap
- line: string - semicolon-separated key-value list to initialize StringValueMap.
- returns: StringValueMap - newly created StringValueMap.
fromTuples
Creates a new StringValueMap object from a list of key-value pairs called tuples.
public staticfromTuples(…tuples: any[]): StringValueMap
- tuples: any[] - list of values where odd elements are keys and the following even elements are values.
- returns: StringValueMap - newly created StringValueMap.
fromTuplesArray
Creates a new StringValueMap object from a list of key-value pairs called tuples. The method is similar to fromTuples but tuples are passed as array instead of parameters.
public staticfromTuplesArray(tuples: any[]): StringValueMap
- tuples: any[] - list of values where odd elements are keys and the following even elements are values.
- returns: StringValueMap - newly created StringValueMap.
fromValue
Converts a specified value into StringValueMap.
public staticfromValue(value: any): StringValueMap
- value: any - value to be converted
- returns: StringValueMap - newly created StringValueMap.
Examples
let value1 = StringValueMap.fromString("key1=1;key2=123.456;key3=2018-01-01");
value1.getAsBoolean("key1"); // Result: true
value1.getAsInteger("key2"); // Result: 123
value1.getAsFloat("key2"); // Result: 123.456
value1.getAsDateTime("key3"); // Result: new Date(2018,0,1)
See also
-
StringConverter
-
TypeConverter
-
StringConverter
-
BooleanConverter
-
IntegerConverter
-
LongConverter
-
DoubleConverter
-
FloatConverter
-
DateTimeConverter
-
ICloneable