new StringInterpolator()
- Source:
Methods
canInterpolate(string) → {boolean}
Returns true if the string contains a placeholder that can be interpolated
- Source:
Parameters:
Name | Type | Description |
---|---|---|
string |
String
|
the string to be interpolated |
Returns:
- Type:
-
boolean
Example
const string = 'I got a new phone! It's a {{ phone.manufacture.name }}!'
const canInterpolate = interpolator.canInterpolate(string)
// canInterpolate => true
interpolatables(string) → {Object}
Returns an object containing placeholders and static strings
- Source:
Parameters:
Name | Type | Description |
---|---|---|
string |
String
|
the string to be interpolated |
Returns:
- Type:
-
Object
Example
const string = 'I got a new phone! It's a {{ phone.manufacture.name }}!'
const canInterpolate = interpolator.interpolatables(string)
// canInterpolate => true
interpolate(string, context) → {String}
Interpolates the given string by searching for placeholders/handlebars and resolving the values using
the given context
- Source:
Parameters:
Name | Type | Description |
---|---|---|
string |
String
|
the string to be interpolated |
context |
Object
|
the context the interpolated values are resolved against |
Returns:
- Type:
-
String
the interpolated String
Example
const string = 'I got a new phone! It's a {{ phone.manufacture.name }}!'
const context = {
phone: {
manufacturer: {
name: 'Pixus'
}
}
}
const propertyPath = 'phone.manufacturer.name'
const value = interpolator.interpolate(propertyPath, context)
// value => 'I got a new phone! It's a Pixus!'
valueFor(propertyPath, context) → {Any}
Extracts a property out of a given context. Properties can be looked up deeply by specifying the correct
names of the objects all along the path to the value we want to get. Names have to be separated by dots.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
propertyPath |
String
|
the path to lookup the value we want |
context |
Object
|
the context the value is resolved against |
Returns:
- Type:
-
Any
Example
const context = {
phone: {
manufacturer: {
name: 'Sammy'
}
}
}
const propertyPath = 'phone.manufacturer.name'
const value = interpolator.valueFor(propertyPath, context)
// value => 'Sammy'