[SERVER-27144] Number.prototype.zeroPad() returns erroneous output if target width is not greater than stringified number's length Created: 21/Nov/16  Updated: 06/Dec/22  Resolved: 03/Dec/21

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Jonathan Abrahams Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 0
Labels: move-stm
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Server Tooling & Methods
Operating System: ALL
Steps To Reproduce:

> x=40
40
> x.zeroPad(4)
0040
> x.zeroPad(2)
{
	"0" : "4",
	"1" : "0",
	"ltrim" : function trimLeft() {
    [native code]
},
	"rtrim" : function trimRight() {
    [native code]
},
	"pad" : function (length, right, chr) {
    if (typeof chr == 'undefined')
        chr = ' ';
    var str = this;
    for (var i = length - str.length; i > 0; i--) {
        if (right) {
            str = str + chr;
        } else {
            str = chr + str;
        }
    }
    return str;
}
}

Participants:

 Description   

Padding on a number should pad up to the width specified or return the stringified number if no padding is required. Doing so would match the behavior of String.prototype.padStart().

The length of the resulting string once the current string has been padded. If this parameter is smaller than the current string's length, the current string will be returned as it is.



 Comments   
Comment by Brooke Miller [ 03/Dec/21 ]

We've deprecated the mongo shell in favor of the new mongosh. Unfortunately, we aren't able to pursue improvements to the deprecated shell except in extreme cases, such as critical security fixes. Please start making use of mongosh and let us know if it works for you in this case.

Comment by Max Hirschhorn [ 21/Nov/16 ]

I'm moving this ticket over to the the platforms team because it has to deal with the behavior of the mongo shell and I don't think this behavior impacts existing tests that use Number.prototype.zeroPad().

  • jstests/aggregation/bugs/server11118.js
  • jstests/core/insert_one.js
  • jstests/noPassthrough/ttl_capped.js
  • jstests/sharding/printShardingStatus.js

I'm clearing the backport request for v3.4 because this has been the behavior as far back as MongoDB 2.6 and I don't think it necessarily urgent to fix.

I've also clarified the title to reflect that an exception isn't being thrown. I suspect that this may simply be an issue with how the mongo shell displays values that are instances of String, and not typeof "string".

> typeof (40).zeroPad(2)
object
> (40).zeroPad(2) instanceof String
true

Generated at Thu Feb 08 04:14:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.