Skip to content

Commit

Permalink
Update lib: Impl zero-padding option for .format()
Browse files Browse the repository at this point in the history
  • Loading branch information
jhermsmeier committed Dec 1, 2016
1 parent 72008bf commit 4782df1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
34 changes: 25 additions & 9 deletions lib/flight-designator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ function FlightDesignator( airline, number, suffix ) {

}

function zeroPad( str ) {
str = str + ''
while( str.length < 4 )
str = '0' + str
return str
}

/**
* Flight designator pattern (IATA/ICAO)
* NOTE: This pattern DOES NOT validate the flight number
Expand Down Expand Up @@ -91,11 +98,12 @@ FlightDesignator.parse = function( value ) {
/**
* Parses and formats a flight designator
* @param {String} value
* @param Boolean} spaces
* @param {Boolean} spaces
* @param {Boolean} pad
* @return {String}
*/
FlightDesignator.format = function( value, spaces ) {
return new FlightDesignator().parse( value ).toString( spaces )
FlightDesignator.format = function( value, spaces, pad ) {
return new FlightDesignator().parse( value ).toString( spaces, pad )
}

/**
Expand Down Expand Up @@ -139,15 +147,23 @@ FlightDesignator.prototype = {

/**
* Format the flight designator
* @param {Boolean} spaces - whether or not to separate with spaces
* @param {Boolean} spaces - whether to separate with spaces
* @param {Boolean} pad - whether to zero-pad the flight number
* @return {String}
*/
toString: function( spaces ) {
return [
toString: function( spaces, pad ) {

var parts = [
this.airlineCode.toUpperCase(),
this.flightNumber,
this.operationalSuffix.toUpperCase()
].join( spaces ? ' ' : '' )
( pad ? zeroPad( this.flightNumber ) : this.flightNumber ),
]

if( this.operationalSuffix ) {
parts.push( this.operationalSuffix.toUpperCase() )
}

return parts.join( spaces ? ' ' : '' )

},

}
Expand Down
20 changes: 20 additions & 0 deletions test/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,25 @@ describe( 'FlightDesignator', function() {
assert.equal( Flight.format('lh0018'), 'LH18' )
})

it( 'lh0018 w/ spaces', function() {
assert.equal( Flight.format('lh0018', true), 'LH 18' )
})

it( 'lh0018 w/ padding', function() {
assert.equal( Flight.format('lh0018', false, true), 'LH0018' )
})

it( 'lh0018 w/ spaces & padding', function() {
assert.equal( Flight.format('lh0018', true, true), 'LH 0018' )
})

it( 'lh0018C w/ padding', function() {
assert.equal( Flight.format('lh0018c', false, true), 'LH0018C' )
})

it( 'lh0018C w/ spaces & padding', function() {
assert.equal( Flight.format('lh0018c', true, true), 'LH 0018 C' )
})

})
})

0 comments on commit 4782df1

Please sign in to comment.