Skip to content

Commit 1f570b4

Browse files
committed
Phaser.Utils.pad now calls toString on the input given, which means you can pass in common data typs such as numbers and have them padded and returned as strings.
1 parent 416c545 commit 1f570b4

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
331331
* TilemapLayer.renderRegion has had an assignment to the obsolete `tileColor` property removed (thanks @cryptographer #2583)
332332
* Group.getFurthestFrom and Group.getClosestTo has a new optional argument: `callback`. This allows you to apply your own additional filtering to the distance checks, ultimately influencing the selected child (thanks @LoneStranger #2577)
333333
* Text.setText has a new optional argument `immediate` which will re-create the texture immediately upon call, rather than wait for the next render pass to do so (thanks @Scraft #2594)
334+
* Phaser.Utils.pad now calls `toString` on the input given, which means you can pass in common data typs such as numbers and have them padded and returned as strings.
334335

335336
### Bug Fixes
336337

src/utils/Utils.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,40 @@ Phaser.Utils = {
159159
},
160160

161161
/**
162-
* JavaScript string pad http://www.webtoolkit.info/.
162+
* Takes the given string and pads it out, to the length required, using the character
163+
* specified. For example if you need a string to be 6 characters long, you can call:
164+
*
165+
* `pad('bob', 6, '-', 2)`
166+
*
167+
* This would return: `bob---` as it has padded it out to 6 characters, using the `-` on the right.
168+
*
169+
* You can also use it to pad numbers (they are always returned as strings):
170+
*
171+
* `pad(512, 6, '0', 1)`
172+
*
173+
* Would return: `000512` with the string padded to the left.
174+
*
175+
* If you don't specify a direction it'll pad to both sides:
176+
*
177+
* `pad('c64', 7, '*')`
178+
*
179+
* Would return: `**c64**`
163180
*
164181
* @method Phaser.Utils.pad
165-
* @param {string} str - The target string.
182+
* @param {string} str - The target string. `toString()` will be called on the string, which means you can also pass in common data types like numbers.
166183
* @param {integer} [len=0] - The number of characters to be added.
167184
* @param {string} [pad=" "] - The string to pad it out with (defaults to a space).
168-
* @param {integer} [dir=3] The direction dir = 1 (left), 2 (right), 3 (both).
169-
* @return {string} The padded string
185+
* @param {integer} [dir=3] - The direction dir = 1 (left), 2 (right), 3 (both).
186+
* @return {string} The padded string.
170187
*/
171188
pad: function (str, len, pad, dir) {
172189

173190
if (len === undefined) { var len = 0; }
174191
if (pad === undefined) { var pad = ' '; }
175192
if (dir === undefined) { var dir = 3; }
176193

194+
str = str.toString();
195+
177196
var padlen = 0;
178197

179198
if (len + 1 >= str.length)

0 commit comments

Comments
 (0)