Skip to content

Commit

Permalink
Merge pull request #2 from fundter/develop
Browse files Browse the repository at this point in the history
Updated dependencies, refactoring
  • Loading branch information
fundter committed Aug 25, 2019
2 parents 435a71f + 269cb6e commit d742dc0
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 64 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.DS_Store
.nvmrc
.nyc_output
.vscode

coverage
dist
node_modules

.DS_Store
8 changes: 5 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
.DS_Store
.nvmrc
.nycrc.json
.nyc_output
.travis.yml
.vscode

coverage
src
test
.travis.yml
.nycrc.json
tsconfig.json
tslint.json
.DS_Store

*.js.map
dist/bin/cli.d.ts
95 changes: 88 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "airac-cc",
"version": "1.0.4",
"version": "1.0.5",
"description": "Utility for AIRAC cycle calculations",
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,11 +40,11 @@
"nyc": "^14.1.1",
"source-map-support": "^0.5.13",
"ts-node": "^8.3.0",
"tslint": "^5.18.0",
"tslint": "^5.19.0",
"typescript": "^3.5.3"
},
"dependencies": {
"chalk": "^2.4.2",
"yargs": "^13.3.0"
"yargs": "^14.0.0"
}
}
66 changes: 33 additions & 33 deletions src/airac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,6 @@ import { base, matchAiracIdentifier, millisPerCycle, millisPerDay } from "./util

export class Cycle {

get identifier(): string {
const yearPart = (this.year % 100).toString().padStart(2, "0");
const ordinalPart = this.ordinal.toString().padStart(2, "0");
return `${yearPart}${ordinalPart}`;
}

get effectiveStart(): Date {
const millis = base.getTime() + (this.serial * millisPerCycle);
return new Date(millis);
}

get effectiveEnd(): Date {
const millis = this.effectiveStart.getTime() + millisPerCycle - millisPerDay;
return new Date(millis);
}

private get year(): number {
return this.effectiveStart.getFullYear();
}

private get ordinal(): number {
const yearStartMillis = new Date(Date.UTC(this.effectiveStart.getFullYear(), 0, 1));
const yearMillis = this.effectiveStart.getTime() - yearStartMillis.getTime();
return Math.floor(yearMillis / millisPerCycle) + 1;
}

public static fromDate(date: Date): Cycle {
const baseMillis = base.getTime();
const dateMillis = date.getTime();
const cycles = Math.floor((dateMillis - baseMillis) / millisPerCycle);
return new Cycle(cycles);
}

public static fromIdentifier(identifier: string): Cycle {
const match = matchAiracIdentifier(identifier);
if (match == null) {
Expand All @@ -57,9 +24,42 @@ export class Cycle {
return cycle;
}

public static fromDate(date: Date): Cycle {
const baseMillis = base.getTime();
const dateMillis = date.getTime();
const cycles = Math.floor((dateMillis - baseMillis) / millisPerCycle);
return new Cycle(cycles);
}

private serial: number;

private constructor(serial: number) {
this.serial = serial;
}

get identifier(): string {
const yearPart = (this.year % 100).toString().padStart(2, "0");
const ordinalPart = this.ordinal.toString().padStart(2, "0");
return `${yearPart}${ordinalPart}`;
}

get effectiveStart(): Date {
const millis = base.getTime() + (this.serial * millisPerCycle);
return new Date(millis);
}

get effectiveEnd(): Date {
const millis = this.effectiveStart.getTime() + millisPerCycle - millisPerDay;
return new Date(millis);
}

private get year(): number {
return this.effectiveStart.getFullYear();
}

private get ordinal(): number {
const yearStartMillis = new Date(Date.UTC(this.effectiveStart.getFullYear(), 0, 1));
const yearMillis = this.effectiveStart.getTime() - yearStartMillis.getTime();
return Math.floor(yearMillis / millisPerCycle) + 1;
}
}
16 changes: 7 additions & 9 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { InvalidCycleIdentifierError } from "../errors";
import { printCycle } from "../utils";

const isoDatePattern = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/;
const moduleInfo = JSON.parse(fs.readFileSync(`${__dirname}/../../package.json`, "utf-8"));

const args = yargs
.usage("Usage: $0 [-i <airac-identifier>|-d <iso-date>]")
Expand All @@ -26,7 +25,7 @@ const args = yargs
.conflicts("i", "d")
.help("h")
.alias("h", "help")
.version("version", "Show version", `${moduleInfo.name} v${moduleInfo.version}`)
.version()
.alias("v", "version")
.example("$0", "Prints the AIRAC cycle for the current date.")
.example("$0 -i <airac-identifier>", "Prints the AIRAC cycle identified by the given AIRAC identifier")
Expand All @@ -35,9 +34,9 @@ const args = yargs
.strict()
.argv;

if (args.identifier) {
if (args.i) {
try {
const cycle = Cycle.fromIdentifier(args.identifier as string);
const cycle = Cycle.fromIdentifier(args.i);
printCycle(cycle);
} catch (error) {
if (error instanceof InvalidCycleIdentifierError) {
Expand All @@ -47,13 +46,12 @@ if (args.identifier) {
throw error;
}
}
} else if (args.date) {
const date = args.date as string;
if (!date.match(isoDatePattern)) {
console.error(`Not a valid ISO date string: '${date}'`);
} else if (args.d) {
if (!args.d.match(isoDatePattern)) {
console.error(`Not a valid ISO date string: '${args.d}'`);
process.exit(1);
}
const cycle = Cycle.fromDate(new Date(date));
const cycle = Cycle.fromDate(new Date(args.d));
printCycle(cycle);
} else {
const cycle = Cycle.fromDate(new Date());
Expand Down
9 changes: 2 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
import { Cycle } from "./airac";
import { InvalidCycleIdentifierError } from "./errors";

export {
Cycle,
InvalidCycleIdentifierError,
};
export { Cycle } from "./airac";
export { InvalidCycleIdentifierError } from "./errors";

0 comments on commit d742dc0

Please sign in to comment.