-
-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring + documentation, renammed
setDefault
to setDefaultLang
- Loading branch information
Showing
5 changed files
with
186 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './src/translate.pipe'; | ||
export * from './src/translate.service'; | ||
export * from './src/translate.service'; | ||
export * from './src/translate.parser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export class Parser { | ||
templateMatcher: RegExp = /{{([^{}]*)}}/g; | ||
|
||
/** | ||
* Flattens an object | ||
* { key1: { keyA: 'valueI' }} ==> { 'key1.keyA': 'valueI' } | ||
* @param target | ||
* @returns {any} | ||
*/ | ||
private flattenObject(target: Object): Object { | ||
var delimiter = '.'; | ||
var maxDepth: number; | ||
var currentDepth = 1; | ||
var output: any = {}; | ||
|
||
function step(object: any, prev?: string) { | ||
Object.keys(object).forEach(function (key) { | ||
var value = object[key]; | ||
var newKey = prev ? prev + delimiter + key : key; | ||
|
||
maxDepth = currentDepth + 1; | ||
|
||
if(!Array.isArray(value) && typeof value === 'object' && Object.keys(value).length && currentDepth < maxDepth) { | ||
++currentDepth; | ||
return step(value, newKey) | ||
} | ||
|
||
output[newKey] = value | ||
}) | ||
} | ||
|
||
step(target); | ||
|
||
return output; | ||
} | ||
|
||
/** | ||
* Interpolates a string to replace parameters | ||
* "This is a {{ key }}" ==> "This is a value", with params = { key: "value" } | ||
* @param expr | ||
* @param params | ||
* @returns {string} | ||
*/ | ||
interpolate(expr: string, params?: any): string { | ||
if(!params) { | ||
return expr; | ||
} else { | ||
params = this.flattenObject(params); | ||
} | ||
|
||
return expr.replace(this.templateMatcher, function (substring: string, b: string): string { | ||
var r = params[b]; | ||
return typeof r !== 'undefined' ? r : substring; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters