Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jhermsmeier committed Sep 12, 2015
0 parents commit c8687b2
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# See http://editorconfig.org
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = false
indent_style = space
indent_size = 2
charset = utf-8
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.*
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- "4"
- "2"
- "0.12"
- "0.10"
branches:
only:
- master
sudo: false
script: npm test
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)
Copyright (c) 2015 Jonas Hermsmeier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.

42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Flight Designator
[![npm](http://img.shields.io/npm/v/flight-designator.svg?style=flat-square)](https://npmjs.com/flight-designator)
[![npm downloads](http://img.shields.io/npm/dm/flight-designator.svg?style=flat-square)](https://npmjs.com/flight-designator)
[![build status](http://img.shields.io/travis/jhermsmeier/node-flight-designator.svg?style=flat-square)](https://travis-ci.org/jhermsmeier/node-flight-designator)

## Install via [npm](https://npmjs.com)

```sh
$ npm install flight-designator
```

## Usage

```js
var FlightDesignator = require( 'flight-designator' )
```

```js
FlightDesignator.parse( 'U24511A' )
> {
airlineCode: 'U2',
flightNumber: '4511',
operationalSuffix: 'A'
}
```

```js
new FlightDesignator( 'KLM', '645' )
> {
airlineCode: 'KLM',
flightNumber: '645',
operationalSuffix: ''
}
```

```js
var flight = new FlightDesignator( 'LH', '2054', 'X' )
flight.toString()
> 'LH2054X'
flight.toString( true )
> 'LH 2054 X'
```
70 changes: 70 additions & 0 deletions lib/flight-designator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* FlightDesignator
* @constructor
* @param {String} airline
* @param {String} number
* @param {String} suffix
* @return {FlightDesignator}
*/
function FlightDesignator( airline, number, suffix ) {

if( !(this instanceof FlightDesignator) )
return new FlightDesignator( airline, number, suffix )

this.airlineCode = airline || ''
this.flightNumber = number || ''
this.operationalSuffix = suffix || ''

}

/**
* Flight designator pattern (IATA/ICAO)
* @type {RegExp}
*/
FlightDesignator.pattern = /^([A-Z0-9]{1,2}[A-Z]?)\s*(0*[1-9][0-9]{0,3})([A-Z]?)$/i

/**
* Parses a flight designator
* @param {String} value
* @return {FlightDesignator}
*/
FlightDesignator.parse = function( value ) {
return new FlightDesignator().parse( value )
}

/**
* FlightDesignator prototype
* @type {Object}
*/
FlightDesignator.prototype = {

constructor: FlightDesignator,

parse: function( value ) {

var parts = ( value + '' )
.match( FlightDesignator.pattern )

if( parts == null )
throw new Error( 'Invalid flight designator "' + value + '"' )

this.operationalSuffix = parts.pop()
this.flightNumber = parts.pop()
this.airlineCode = parts.pop()

return this

},

toString: function( spaces ) {
return [
this.airlineCode,
this.flightNumber,
this.operationalSuffix
].join( spaces ? ' ' : '' )
},

}

// Exports
module.exports = FlightDesignator
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "flight-designator",
"version": "1.0.0",
"description": "Parse and construct flight designators",
"license": "MIT",
"keywords": [
"flight",
"designator",
"code",
"airline",
"number",
"iata",
"icao"
],
"main": "lib/flight-designator",
"author": "Jonas Hermsmeier <[email protected]> (https://jhermsmeier.de/)",
"dependencies": {},
"devDependencies": {
"mocha": "~2.3.2"
},
"scripts": {
"test": "mocha"
},
"homepage": "https://github.com/jhermsmeier/node-flight-designator",
"repository": {
"type": "git",
"url": "git+https://github.com/jhermsmeier/node-flight-designator.git"
},
"bugs": {
"url": "https://github.com/jhermsmeier/node-flight-designator/issues"
},
"directories": {
"test": "test"
}
}
62 changes: 62 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var Flight = require( '..' )
var assert = require( 'assert' )

describe( 'FlightDesignator', function() {
describe( 'parse', function() {

it( 'U24511', function() {
var result = Flight.parse( 'U24511' )
assert.deepEqual( result, {
airlineCode: 'U2',
flightNumber: '4511',
operationalSuffix: '',
})
})

it( 'LH2054', function() {
var result = Flight.parse( 'LH2054' )
assert.deepEqual( result, {
airlineCode: 'LH',
flightNumber: '2054',
operationalSuffix: '',
})
})

it( 'AS33', function() {
var result = Flight.parse( 'AS33' )
assert.deepEqual( result, {
airlineCode: 'AS',
flightNumber: '33',
operationalSuffix: '',
})
})

it( 'KLM645', function() {
var result = Flight.parse( 'KLM645' )
assert.deepEqual( result, {
airlineCode: 'KLM',
flightNumber: '645',
operationalSuffix: '',
})
})

it( 'YYY9999A', function() {
var result = Flight.parse( 'YYY9999A' )
assert.deepEqual( result, {
airlineCode: 'YYY',
flightNumber: '9999',
operationalSuffix: 'A'
})
})

it( 'AS33C', function() {
var result = Flight.parse( 'AS33C' )
assert.deepEqual( result, {
airlineCode: 'AS',
flightNumber: '33',
operationalSuffix: 'C',
})
})

})
})

0 comments on commit c8687b2

Please sign in to comment.