-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Number & Currency: Add format to parts support
- Loading branch information
Showing
35 changed files
with
3,395 additions
and
497 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
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
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
define([ | ||
"./parts/push" | ||
], function( partsPush ) { | ||
|
||
/** | ||
* formatMessage( message, data ) | ||
* | ||
* @message [String] A message with optional {vars} to be replaced. | ||
* | ||
* @data [Array or JSON] Object with replacing-variables content. | ||
* | ||
* Return the formatted message. For example: | ||
* | ||
* - formatMessage( "{0} second", [ 1 ] ); | ||
* > [{type: "variable", value: "1", name: "0"}, {type: "literal", value: " second"}] | ||
* | ||
* - formatMessage( "{0}/{1}", ["m", "s"] ); | ||
* > [ | ||
* { type: "variable", value: "m", name: "0" }, | ||
* { type: "literal", value: " /" }, | ||
* { type: "variable", value: "s", name: "1" } | ||
* ] | ||
*/ | ||
return function( message, data ) { | ||
|
||
var lastOffset = 0, | ||
parts = []; | ||
|
||
// Create parts. | ||
message.replace( /{[0-9a-zA-Z-_. ]+}/g, function( nameIncludingBrackets, offset ) { | ||
var name = nameIncludingBrackets.slice( 1, -1 ); | ||
partsPush( parts, "literal", message.slice( lastOffset, offset )); | ||
partsPush( parts, "variable", data[ name ] ); | ||
parts[ parts.length - 1 ].name = name; | ||
lastOffset += offset + nameIncludingBrackets.length; | ||
}); | ||
|
||
// Skip empty ones such as `{ type: 'literal', value: '' }`. | ||
return parts.filter(function( part ) { | ||
return part.value !== ""; | ||
}); | ||
}; | ||
|
||
}); |
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,12 @@ | ||
define(function() { | ||
|
||
/** | ||
* Returns joined parts values. | ||
*/ | ||
return function( parts ) { | ||
return parts.map( function( part ) { | ||
return part.value; | ||
}).join( "" ); | ||
}; | ||
|
||
}); |
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,17 @@ | ||
define(function() { | ||
|
||
/** | ||
* Pushes part to parts array, concat two consecutive parts of the same type. | ||
*/ | ||
return function( parts, type, value ) { | ||
|
||
// Concat two consecutive parts of same type | ||
if ( parts.length && parts[ parts.length - 1 ].type === type ) { | ||
parts[ parts.length - 1 ].value += value; | ||
return; | ||
} | ||
|
||
parts.push( { type: type, value: value } ); | ||
}; | ||
|
||
}); |
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
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
Oops, something went wrong.